From 7fee511a1e86d1a8ef79a1789c3b7d670407b65d Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Tue, 24 Nov 2020 15:44:58 +0100 Subject: [PATCH] Improve clock model construction. engine-clock#2 --- src/models.py | 71 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/src/models.py b/src/models.py index c5f0b1c..3a77f53 100644 --- a/src/models.py +++ b/src/models.py @@ -93,9 +93,9 @@ class PlayLog(db.Model): @staticmethod - def select_current(): + def select_recent(): """ - Selects the currently playing track. + Selects the most recent played track. Equals to the current track if it's still playing. """ db.session.commit() now = datetime.datetime.now() @@ -104,6 +104,20 @@ class PlayLog(db.Model): order_by(PlayLog.track_start.desc()).\ filter(PlayLog.track_start <= str(now)).first() + return track + + + + @staticmethod + def select_current(): + """ + Selects the currently playing track. + """ + db.session.commit() + now = datetime.datetime.now() + + track = PlayLog.select_recent() + if track: # Preferably only get playlogs which are known for still being on air if track.track_start + datetime.timedelta(0, track.track_duration) > now: @@ -144,7 +158,7 @@ class PlayLog(db.Model): else: result = db.session.query(PlayLog).\ order_by(PlayLog.track_start.desc()).\ - filter(PlayLog.timeslot_id == timeslot_id).\ + filter(PlayLog.timeslot_id == timeslot_id) playlogs = result.all() return playlogs @@ -437,47 +451,64 @@ class ClockInfo(db.Model): data = db.session.query(ClockInfo).filter(ClockInfo.log_source == source_number).first() current_track = PlayLog.select_current() current_playlist_id = -1 - updated_playlist = None - - if current_track: - updated_playlist = PlayLog.select_for_timeslot(current_track.timeslot_id) - updated_playlist.sort(key=lambda track: track.track_start, reverse=False) - + playlogs = None + + # Construct the clock `info` object if data: info["log_source"] = data.log_source info["log_time"] = data.log_time + # Get the track currently playing if current_track: info["current_track"] = track_schema.dump(current_track) + + # Append the missing planned playlist items to the ones played # FIXME do it client-site if data.current_playlist: - info["planned_playlist"] = json.loads(data.current_playlist) - current_playlist_id = info["planned_playlist"]["playlist_id"] - for next_entry in info["planned_playlist"]["entries"]: - if next_entry.get("start_date") and next_entry.get("start_date") > datetime.datetime.now(): - updated_playlist["entries"].append(next_entry) - + info["planned_playlist"] = json.loads(data.current_playlist) + + # Get the current timeslot if data.current_timeslot: info["current_timeslot"] = json.loads(data.current_timeslot) + + # Get the most recently played track (because right now nothing might be playing) + most_recent_track = PlayLog.select_recent() + + # Is the most recent track part of the current timeslot? + if most_recent_track.timeslot_id == info["current_timeslot"]["timeslot_id"]: + + # Get the actual playlogs of the current timeslot, until now + playlog_schema = PlayLogSchema(many=True) + playlogs = PlayLog.select_for_timeslot(most_recent_track.timeslot_id) + playlogs.sort(key=lambda track: track.track_start, reverse=False) + info["current_playlist"] = { + # "playlist_id": current_playlist_id, + "entries": playlog_schema.dump(playlogs) + } + + # Invalid timeslots (e.g. in fallback scenarios) get a virtual start date of the first fallback track + if info["current_timeslot"]["timeslot_id"] == -1: + if playlogs and playlogs[0]: + info["current_timeslot"]["timeslot_start"] = playlogs[0].track_start + + # Get the next timeslot if data.next_timeslot: info["next_timeslot"] = json.loads(data.next_timeslot) - playlog_schema = PlayLogSchema(many=True) - info["current_playlist"] = { - "playlist_id": current_playlist_id, - "entries": playlog_schema.dump(updated_playlist) - } return info + def save(self): db.session.add(self) db.session.commit() + def update(self): db.session.merge(self) db.session.commit() + class ClockInfoSchema(ma.SQLAlchemySchema): """ Schema for trackservice entries. -- GitLab