From f50bd04b5db5f34c2fa8c3b6c6d5e7a8177aca0a Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Wed, 2 Sep 2020 11:51:15 +0200 Subject: [PATCH] Update handling, inc col size for playlist. #1 --- src/models.py | 27 ++++++++++++++++++++------- src/service.py | 15 +++++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/models.py b/src/models.py index 8fb6f75..1efaa18 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 9b28ef8..1ed2ad0 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 -- GitLab