diff --git a/src/models.py b/src/models.py index 8fb6f7561af898d10c0fe73c3e6bd15a6267109e..1efaa188216ae84322d038edbed8c76d41cc491b 100644 --- a/src/models.py +++ b/src/models.py @@ -343,13 +343,20 @@ class ClockInfo(db.Model): # Columns log_time = Column(DateTime) current_track = None # Populated live from within `get_info(..)` - current_playlist = Column(String(2048)) # Stringified "#/components/schemas/Playlist" OpenAPI JSON object + current_playlist = Column(String(4096)) # Stringified "#/components/schemas/Playlist" OpenAPI JSON object current_schedule = Column(String(2048)) # Stringified "#/components/schemas/Schedule" OpenAPI JSON object next_schedule = Column(String(2048)) # Stringified "#/components/schemas/Schedule" OpenAPI JSON object - def __init__(self, source_number, current_playlist, current_schedule, next_schedule): + + def __init__(self): """ - Initializes an health entry. + Initializes an clock info entry. + """ + + + def set_info(self, source_number, current_playlist, current_schedule, next_schedule): + """ + Sets the values for a clock info entry. """ self.log_time = datetime.datetime.now() self.log_source = source_number @@ -361,6 +368,14 @@ class ClockInfo(db.Model): self.next_schedule = json.dumps(next_schedule.to_dict(), default=str) + @staticmethod + def get(source_number): + """ + Retrieves the clock info for the given source number. + """ + return db.session.query(ClockInfo).filter(ClockInfo.log_source == source_number).first() + + @staticmethod def get_info(source_number): """ @@ -371,9 +386,7 @@ class ClockInfo(db.Model): current_track = PlayLog.select_current() track_schema = TrackSchema() - if not data: - self.log.warn("No clock info data available!") - else: + if data: info["log_source"] = data.log_source info["log_time"] = data.log_time @@ -393,7 +406,7 @@ class ClockInfo(db.Model): db.session.add(self) db.session.commit() - def merge(self): + def update(self): db.session.merge(self) db.session.commit() diff --git a/src/service.py b/src/service.py index 9b28ef8aa882b5b0e60ab9b44a95d61002d09786..1ed2ad0cc7b3f1b9b08f667bf420f64d4e14c8c4 100644 --- a/src/service.py +++ b/src/service.py @@ -201,8 +201,19 @@ class ApiService(): if data.engine_source <= 0: return - clock_info = ClockInfo(data.engine_source, data.current_playlist, data.current_schedule, data.next_schedule) - clock_info.merge() + is_existing = False + clock_info = ClockInfo.get(self.get_active_source()) + if clock_info: + is_existing = True + else: + clock_info = ClockInfo() + + clock_info.set_info(data.engine_source, data.current_playlist, data.current_schedule, data.next_schedule) + + if is_existing: + clock_info.update() + else: + clock_info.save() if self.config.get("enable_federation") == "false": return