Skip to content

Commit

Permalink
refactor: add calculated_duration_s (post-process)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohrstrom committed Oct 30, 2024
1 parent 2c0355f commit 6e42cbe
Show file tree
Hide file tree
Showing 14 changed files with 488 additions and 205 deletions.
10 changes: 8 additions & 2 deletions functions/on-air-updater/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def get_metadata(timeshift=0):
if not len(data):
raise ApiError("empty result")

now = datetime.now(tz=datetime.now().astimezone().tzinfo) + timedelta(seconds=timeshift)
now = datetime.now(tz=datetime.now().astimezone().tzinfo) - timedelta(seconds=timeshift)
print(f"now shifted: {now:%H:%M:%S}")

try:
result = next(
Expand All @@ -57,10 +58,15 @@ def get_metadata(timeshift=0):

try:
dt = datetime.fromisoformat(result["timeEnd"])
next_start_in = (dt - datetime.now(tz=dt.tzinfo) + timedelta(seconds=timeshift)).total_seconds()
next_start_in = (dt - datetime.now(tz=dt.tzinfo)).total_seconds() + timeshift
except ValueError as e:
raise ApiError(str(e)) from e

ts = datetime.fromisoformat(result["timeStart"])
te = datetime.fromisoformat(result["timeEnd"])
title = result["media"]["name"]
print(f"runs: {ts:%H:%M:%S} - {te:%H:%M:%S} # {title}")

return next_start_in, result


Expand Down
10 changes: 4 additions & 6 deletions functions/on-air-updater/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ def run(request):

logging.debug(result)

next_run_in = next_run_in - TIMESHIFT

except api_client.ApiError as e:
logging.warning({
"error": "ApiError",
Expand All @@ -110,8 +108,8 @@ def run(request):
print(f"ApiError: {e}")
next_run_in = 5

logging.debug({
"next_run_in": next_run_in,
})
print(f"next run in: {next_run_in}")

time.sleep(2)

time.sleep(next_run_in + 1)
# time.sleep(next_run_in + 1)
4 changes: 3 additions & 1 deletion functions/on-air-updater/pub-sub-subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


PROJECT_ID = "open-broadcast"
SUBSCRIPTION_ID = "on-air-updated"
SUBSCRIPTION_ID = "on-air-update-in-60s"

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(PROJECT_ID, SUBSCRIPTION_ID)
Expand All @@ -17,6 +17,8 @@ def callback(message: pubsub_v1.subscriber.message.Message):
message.ack()




streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"listening for messages: {subscription_path}")

Expand Down
4 changes: 3 additions & 1 deletion functions/quantumcast-updater/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def update_metadata(item):
if path := releases[0].get("image", {}).get("path"):
cover_url = f"https://openbroadcast.ch/images/crop/800x800/{path}"


payload = {
"id": item["key"],
"time": item["timeStart"],
#
"song": media["name"],
"artist": media["artistDisplay"],
# "album": media["releaseDisplay"],
"duration": round(duration),
#
"separator": " - ",
Expand All @@ -45,6 +45,8 @@ def update_metadata(item):
if cover_url:
payload["cover"] = cover_url

print(json.dumps(payload, indent=2))

try:
r = requests.post(
API_URL,
Expand Down
5 changes: 3 additions & 2 deletions obr_core/broadcast/api/views/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page

from broadcast.api import serializers
from broadcast.models import Emission
from common.cache.decorators import ignorable_cache_page
from drf_spectacular.utils import extend_schema
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
Expand Down Expand Up @@ -40,7 +40,8 @@ def get_queryset(self):
),
},
)
@method_decorator(cache_page(5 * 60))
# @method_decorator(cache_page(5 * 60))
@method_decorator(ignorable_cache_page(5 * 60))
def get(self, request):
seconds_ahead = int(request.GET.get("secondsAhead", 0))
seconds_back = int(request.GET.get("secondsBack", 0))
Expand Down
Empty file.
19 changes: 19 additions & 0 deletions obr_core/common/cache/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from functools import wraps

from django.views.decorators.cache import cache_page

IGNORE_CACHE_HEADER = "X-No-Cache"


def ignorable_cache_page(timeout):
def decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
if IGNORE_CACHE_HEADER in request.headers:
return view_func(request, *args, **kwargs)
else:
return cache_page(timeout)(view_func)(request, *args, **kwargs)

return _wrapped_view

return decorator
113 changes: 0 additions & 113 deletions obr_core/stats/__events.py

This file was deleted.

41 changes: 28 additions & 13 deletions obr_core/stats/admin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ class PlayerEventAdmin(
"state",
"time_display",
"time_end_display",
#
"calculated_duration_s",
#
"max_duration_display",
"annotated_duration_display",
"duration_display",
"user_display",
# "annotated_duration_display",
"obj_key",
"source",
# "user_identity",
# "device_key",
"user_display",
"device_key",
]
list_filter = [
"source",
Expand All @@ -37,9 +38,25 @@ class PlayerEventAdmin(
"user_identity",
"device_key",
]
exclude = [
"time",
"time_end",
]
readonly_fields = [
"time_display",
"time_end_display",
"state",
"source",
"obj_key",
"user_identity",
"device_key",
"max_duration",
#
"calculated_duration_s",
"duration_display",
]

def get_queryset(self, request):
return super().get_queryset(request).annotate_times_and_durations()
# def get_queryset(self, request):

@admin.display(description="Time", ordering="time")
def time_display(self, obj):
Expand All @@ -55,17 +72,15 @@ def time_end_display(self, obj):

@admin.display(description="Duration (est)")
def annotated_duration_display(self, obj):
if obj.state == PlayerEvent.State.STOPPED:
return None
if not obj.annotated_duration:
return None
return round(obj.annotated_duration.total_seconds())
return "N/A"
# if obj.state == PlayerEvent.State.STOPPED:
# if not obj.annotated_duration:

@admin.display(description="Duration")
def duration_display(self, obj):
if not obj.time_end:
return None
return round((obj.time_end - obj.time).total_seconds())
return round((obj.time_end - obj.time).total_seconds(), 3)

@admin.display(description="Duration (max)")
def max_duration_display(self, obj):
Expand Down
30 changes: 26 additions & 4 deletions obr_core/stats/events.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta

from django.db.models import F
from django.db.models.functions import Now
from django.db.models import DecimalField, ExpressionWrapper, F
from django.db.models.functions import Cast, Extract, Now
from django.utils import timezone

from stats.models import PlayerEvent
Expand Down Expand Up @@ -77,7 +77,7 @@ def set_events_time_end_by_max_duration(database="default"):
return num_updated


def fix_durations(database="default"):
def fix_events_duration(database="default"):
qs = (
PlayerEvent.objects.using(database)
.annotate_times_and_durations()
Expand Down Expand Up @@ -106,10 +106,32 @@ def fix_durations(database="default"):
return len(updated_events)


def set_events_calculated_duration_s(database="default"):
qs = PlayerEvent.objects.using(database).filter(
state=PlayerEvent.State.PLAYING,
time__gte=timezone.now() - timedelta(seconds=MAX_AGE),
time_end__isnull=False,
calculated_duration_s=0,
)

num_updated = qs.update(
calculated_duration_s=ExpressionWrapper(
Cast(
Extract(F("time_end") - F("time"), "epoch"),
output_field=DecimalField(max_digits=10, decimal_places=3),
),
output_field=DecimalField(max_digits=10, decimal_places=3),
),
)

return num_updated


def post_process_player_events(database="default"):
num_updated = set_events_time_end_by_next_event(database=database)
num_updated += set_events_time_end_by_max_duration(database=database)

num_updated += fix_durations(database=database)
num_updated += fix_events_duration(database=database)
num_updated += set_events_calculated_duration_s(database=database)

return num_updated
Loading

0 comments on commit 6e42cbe

Please sign in to comment.