|
| 1 | +import nebula |
| 2 | +from nebula.helpers.create_new_event import create_new_event |
| 3 | +from server.dependencies import CurrentUser, RequestInitiator |
| 4 | +from server.request import APIRequest |
| 5 | + |
| 6 | +from .models import ( |
| 7 | + ApplyTemplateRequestModel, |
| 8 | + ListTemplatesResponseModel, |
| 9 | + TemplateItemModel, |
| 10 | +) |
| 11 | +from .template_importer import TemplateImporter |
| 12 | +from .utils import list_templates, load_template |
| 13 | + |
| 14 | + |
| 15 | +class ListTemplatesRequest(APIRequest): |
| 16 | + """Retrieve or update the schedule for a channel |
| 17 | +
|
| 18 | + This endpoint handles chanel macro-scheduling, |
| 19 | + including the creation, modification, and deletion of playout events. |
| 20 | +
|
| 21 | + Schedule is represented as a list of events, typically for one week. |
| 22 | + """ |
| 23 | + |
| 24 | + name = "list-scheduling-templates" |
| 25 | + title = "List scheduling templates" |
| 26 | + response_model = ListTemplatesResponseModel |
| 27 | + |
| 28 | + async def handle( |
| 29 | + self, |
| 30 | + # user: CurrentUser, |
| 31 | + initiator: RequestInitiator, |
| 32 | + ) -> ListTemplatesResponseModel: |
| 33 | + template_names = list_templates() |
| 34 | + |
| 35 | + return ListTemplatesResponseModel( |
| 36 | + templates=[ |
| 37 | + TemplateItemModel(name=name, title=name.capitalize()) |
| 38 | + for name in template_names |
| 39 | + ] |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +class ApplyTemplateRequest(APIRequest): |
| 44 | + """Apply a template to a channel""" |
| 45 | + |
| 46 | + name = "apply-scheduling-template" |
| 47 | + title = "Apply scheduling template" |
| 48 | + |
| 49 | + async def handle( |
| 50 | + self, |
| 51 | + user: CurrentUser, |
| 52 | + request: ApplyTemplateRequestModel, |
| 53 | + initiator: RequestInitiator, |
| 54 | + ) -> None: |
| 55 | + if not (channel := nebula.settings.get_playout_channel(request.id_channel)): |
| 56 | + raise nebula.BadRequestException(f"No such channel {request.id_channel}") |
| 57 | + |
| 58 | + template = load_template(request.template_name) |
| 59 | + hh, mm = channel.day_start |
| 60 | + |
| 61 | + importer = TemplateImporter(template.get("schedule", {}), hh, mm) |
| 62 | + edata = importer.build_for_week(request.date) |
| 63 | + |
| 64 | + if not edata: |
| 65 | + nebula.log.warn("No events found in template") |
| 66 | + return |
| 67 | + |
| 68 | + first_ts = min(edata.keys()) |
| 69 | + last_ts = max(edata.keys()) |
| 70 | + |
| 71 | + pool = await nebula.db.pool() |
| 72 | + async with pool.acquire() as conn, conn.transaction(): |
| 73 | + if request.clear: |
| 74 | + # Clear mode |
| 75 | + query = """ |
| 76 | + DELETE FROM events |
| 77 | + WHERE start >= $1 AND start <= $2 AND id_channel = $3 |
| 78 | + """ |
| 79 | + await conn.execute(query, first_ts, last_ts, request.id_channel) |
| 80 | + |
| 81 | + else: |
| 82 | + # Merge mode |
| 83 | + query = """ |
| 84 | + SELECT start FROM events |
| 85 | + WHERE start >= $1 AND start <= $2 AND id_channel = $3 |
| 86 | + """ |
| 87 | + existing_times = [] |
| 88 | + async for row in nebula.db.iterate( |
| 89 | + query, first_ts, last_ts, request.id_channel |
| 90 | + ): |
| 91 | + existing_times.append(row["start"]) |
| 92 | + |
| 93 | + MINIMUM_GAP_SECONDS = 5 * 60 |
| 94 | + for new_ts in list(edata.keys()): |
| 95 | + if any( |
| 96 | + abs(new_ts - existing_ts) < MINIMUM_GAP_SECONDS |
| 97 | + for existing_ts in existing_times |
| 98 | + ): |
| 99 | + nebula.log.warn( |
| 100 | + f"Skipping event at {new_ts}: too close to existing event" |
| 101 | + ) |
| 102 | + edata.pop(new_ts) |
| 103 | + |
| 104 | + for _, event_data in edata.items(): |
| 105 | + await create_new_event(channel, event_data, user, conn) |
0 commit comments