Skip to content
Snippets Groups Projects
Commit 7fee511a authored by David Trattnig's avatar David Trattnig
Browse files

Improve clock model construction. engine-clock#2

parent 1179f4d1
No related branches found
No related tags found
No related merge requests found
...@@ -93,9 +93,9 @@ class PlayLog(db.Model): ...@@ -93,9 +93,9 @@ class PlayLog(db.Model):
@staticmethod @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() db.session.commit()
now = datetime.datetime.now() now = datetime.datetime.now()
...@@ -104,6 +104,20 @@ class PlayLog(db.Model): ...@@ -104,6 +104,20 @@ class PlayLog(db.Model):
order_by(PlayLog.track_start.desc()).\ order_by(PlayLog.track_start.desc()).\
filter(PlayLog.track_start <= str(now)).first() 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: if track:
# Preferably only get playlogs which are known for still being on air # Preferably only get playlogs which are known for still being on air
if track.track_start + datetime.timedelta(0, track.track_duration) > now: if track.track_start + datetime.timedelta(0, track.track_duration) > now:
...@@ -144,7 +158,7 @@ class PlayLog(db.Model): ...@@ -144,7 +158,7 @@ class PlayLog(db.Model):
else: else:
result = db.session.query(PlayLog).\ result = db.session.query(PlayLog).\
order_by(PlayLog.track_start.desc()).\ order_by(PlayLog.track_start.desc()).\
filter(PlayLog.timeslot_id == timeslot_id).\ filter(PlayLog.timeslot_id == timeslot_id)
playlogs = result.all() playlogs = result.all()
return playlogs return playlogs
...@@ -437,47 +451,64 @@ class ClockInfo(db.Model): ...@@ -437,47 +451,64 @@ class ClockInfo(db.Model):
data = db.session.query(ClockInfo).filter(ClockInfo.log_source == source_number).first() data = db.session.query(ClockInfo).filter(ClockInfo.log_source == source_number).first()
current_track = PlayLog.select_current() current_track = PlayLog.select_current()
current_playlist_id = -1 current_playlist_id = -1
updated_playlist = None playlogs = None
if current_track: # Construct the clock `info` object
updated_playlist = PlayLog.select_for_timeslot(current_track.timeslot_id)
updated_playlist.sort(key=lambda track: track.track_start, reverse=False)
if data: if data:
info["log_source"] = data.log_source info["log_source"] = data.log_source
info["log_time"] = data.log_time info["log_time"] = data.log_time
# Get the track currently playing
if current_track: if current_track:
info["current_track"] = track_schema.dump(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: if data.current_playlist:
info["planned_playlist"] = json.loads(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"]: # Get the current timeslot
if next_entry.get("start_date") and next_entry.get("start_date") > datetime.datetime.now():
updated_playlist["entries"].append(next_entry)
if data.current_timeslot: if data.current_timeslot:
info["current_timeslot"] = json.loads(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: if data.next_timeslot:
info["next_timeslot"] = json.loads(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 return info
def save(self): def save(self):
db.session.add(self) db.session.add(self)
db.session.commit() db.session.commit()
def update(self): def update(self):
db.session.merge(self) db.session.merge(self)
db.session.commit() db.session.commit()
class ClockInfoSchema(ma.SQLAlchemySchema): class ClockInfoSchema(ma.SQLAlchemySchema):
""" """
Schema for trackservice entries. Schema for trackservice entries.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment