From 6d3fe0fee0408dddaf10e27ee0f46f2e759cf167 Mon Sep 17 00:00:00 2001 From: David Trattnig <david@subsquare.at> Date: Fri, 12 May 2023 12:45:44 +0200 Subject: [PATCH] refact: API props in camelCase notation --- src/aura_engine_api/models.py | 165 +++++++++++++++++++++------------ src/aura_engine_api/service.py | 21 ++--- 2 files changed, 117 insertions(+), 69 deletions(-) diff --git a/src/aura_engine_api/models.py b/src/aura_engine_api/models.py index 6d534e8..29a3eaa 100644 --- a/src/aura_engine_api/models.py +++ b/src/aura_engine_api/models.py @@ -22,7 +22,7 @@ import json from flask_marshmallow import Marshmallow from flask_sqlalchemy import SQLAlchemy -from marshmallow import post_dump +from marshmallow import Schema, fields, post_dump from sqlalchemy import Boolean, Column, DateTime, Integer, String from sqlalchemy.event import listen @@ -58,7 +58,7 @@ class PlayLog(db.Model): def __init__(self, data): """ - Initializes a trackservice entry + Initialize a trackservice entry """ self.track_start = data.track_start self.track_artist = data.track_artist @@ -83,7 +83,7 @@ class PlayLog(db.Model): @staticmethod def get(start_time): """ - Selects the playlog identified by start time. + Select the playlog identified by start time. """ db.session.commit() track = ( @@ -97,7 +97,7 @@ class PlayLog(db.Model): @staticmethod def select_recent(): """ - Selects the most recent played track. Equals to the current track if it's still playing. + Select the most recent played track. Equals to the current track if it's still playing. """ db.session.commit() now = datetime.datetime.now() @@ -114,7 +114,7 @@ class PlayLog(db.Model): @staticmethod def select_current(): """ - Selects the currently playing track. + Select the currently playing track. """ db.session.commit() now = datetime.datetime.now() @@ -135,7 +135,7 @@ class PlayLog(db.Model): @staticmethod def select_for_timeslot(timeslot_id): """ - Selects all playlogs which appear to be in the current timeslot. + Select all playlogs which appear to be in the current timeslot. If playlogs without a valid timeslot are queried (-1), then only the last 50 total playlogs are respected. """ @@ -171,7 +171,7 @@ class PlayLog(db.Model): @staticmethod def paginate(page, page_size, from_time=None, to_time=None, skip_synced=False): """ - Returns a list of entries for a given page and an start time (optional). + Return a list of entries for a given page and an start time (optional). """ if not from_time: from_time = datetime.datetime.utcnow().date() @@ -205,7 +205,7 @@ class PlayLog(db.Model): @staticmethod def select_last_hours(n): """ - Selects the tracks playing in the past (`n`) hours. + Select the tracks playing in the past (`n`) hours. """ db.session.commit() last_hours = datetime.datetime.today() - datetime.timedelta(hours=n) @@ -235,7 +235,7 @@ class PlayLog(db.Model): @staticmethod def select_by_range(from_day, to_day): """ - Selects the track-service items for a day range. + Select the track-service items for a day range. """ db.session.commit() tracks = ( @@ -269,29 +269,6 @@ class PlayLogSchema(ma.SQLAlchemyAutoSchema): return {key: value for key, value in data.items() if value not in self.SKIP_VALUES} -class TrackSchema(ma.SQLAlchemySchema): - """ - Schema for trackservice entries. - """ - - class Meta: - model = PlayLog - sqla_session = db.session - fields = ( - "track_start", - "track_artist", - "track_album", - "track_title", - "track_duration", - "track_type", - "track_num", - "playlist_id", - "timeslot_id", - "show_id", - "show_name", - ) - - class ActivityLog(db.Model): """ Table holding a log of play-out source activity and their sync states. @@ -310,7 +287,7 @@ class ActivityLog(db.Model): def __init__(self, source_number): """ - Initializes an activity entry + Initialize an activity entry """ self.log_time = datetime.datetime.now() self.source_number = source_number @@ -319,7 +296,7 @@ class ActivityLog(db.Model): @staticmethod def is_empty(): """ - Checks if the tables is empty. + Check if the tables is empty. """ db.session.commit() return not db.session.query(ActivityLog).one_or_none() @@ -327,7 +304,7 @@ class ActivityLog(db.Model): @staticmethod def get_active_source(): """ - Retrieves the currently active source. + Retrieve the currently active source. """ db.session.commit() source = db.session.query(ActivityLog).order_by(ActivityLog.log_time.desc()).first() @@ -356,7 +333,7 @@ class HealthHistory(db.Model): def __init__(self, source_number, log_time, is_healthy, health_info): """ - Initializes an health entry. + Initialize an health entry. """ self.log_time = log_time self.log_source = source_number @@ -367,7 +344,7 @@ class HealthHistory(db.Model): @staticmethod def get_latest_entry(source_number): """ - Retrieves the most recent health history entry for the given source number. + Retrieve the most recent health history entry for the given source number. """ return ( db.session.query(HealthHistory) @@ -453,16 +430,15 @@ class ClockInfo(db.Model): @staticmethod def get(source_number): """ - Retrieves the clock info for the given source number. + Retrieve 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): """ - Retrieves the clock info for the given source number. + Retrieve the clock info for the given source number. """ - track_schema = TrackSchema() info = dict() data = db.session.query(ClockInfo).filter(ClockInfo.log_source == source_number).first() current_track = PlayLog.select_current() @@ -475,7 +451,7 @@ class ClockInfo(db.Model): # Get the track currently playing if current_track: - info["current_track"] = track_schema.dump(current_track) + info["current_track"] = current_track # Append the missing planned playlist items to the ones played if data.planned_playlist: @@ -492,12 +468,10 @@ class ClockInfo(db.Model): # 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_playlogs"] = playlog_schema.dump(playlogs) + info["current_playlogs"] = playlogs if info["current_playlogs"] is None: info["current_playlogs"] = {} @@ -524,20 +498,97 @@ class ClockInfo(db.Model): db.session.commit() -class ClockInfoSchema(ma.SQLAlchemySchema): +# +# Schemas for JSON responses +# + + +def camelcase(s): + parts = iter(s.split("_")) + return next(parts) + "".join(i.title() for i in parts) + + +class CamelCaseSchema(Schema): + """ + Schema that uses camel-case for its external representation + and snake-case for its internal representation. + """ + + def on_bind_field(self, field_name, field_obj): + field_obj.data_key = camelcase(field_obj.data_key or field_name) + + +class TrackSchema(CamelCaseSchema): """ Schema for trackservice entries. """ - class Meta: - model = ClockInfo - sqla_session = db.session - fields = ( - "log_source", - "log_time", - "current_track", - "planned_playlist", - "current_playlogs", - "current_timeslot", - "upcoming_timeslots", - ) + track_start = fields.Str(required=True) + track_artist = fields.Str(required=True) + track_album = fields.Str(required=True) + track_title = fields.Str(required=True) + track_duration = fields.Str(required=True) + track_type = fields.Str(required=True) + track_num = fields.Str(required=True) + playlist_id = fields.Str(required=True) + timeslot_id = fields.Str(required=True) + show_id = fields.Str(required=True) + show_name = fields.Str(required=True) + + +class TimeslotSchema(CamelCaseSchema): + """ + Schema for timeslots. + """ + + show_name = fields.Str(required=True) + show_id = fields.Str(required=True) + timeslot_id = fields.Str(required=True) + timeslot_start = fields.Str(required=True) + timeslot_end = fields.Str(required=True) + playlist_id = fields.Str(required=True) + playlist_type = fields.Str(required=True) + + +class PlaylogSchema(CamelCaseSchema): + """ + Schema for playlogs. + """ + + track_start = fields.Str(required=True) + track_artist = fields.Str(required=True) + track_album = fields.Str(required=True) + track_title = fields.Str(required=True) + track_duration = fields.Str(required=True) + track_type = fields.Str(required=True) + track_num = fields.Str(required=True) + playlist_id = fields.Str(required=True) + timeslot_id = fields.Str(required=True) + show_id = fields.Str(required=True) + show_name = fields.Str(required=True) + log_source = fields.Str(required=True) + custom_json = fields.Str(required=True) + is_synced = fields.Str(required=True) + + +class PlaylistSchema(CamelCaseSchema): + """ + Schema for playlogs. + """ + + playlist_id = fields.Str(required=True) + entries = fields.List(fields.Nested(TrackSchema)) + + +class ClockInfoSchema(CamelCaseSchema): + """ + Schema for trackservice entries. + """ + + log_source = fields.Str(required=True) + log_time = fields.Str(required=True) + current_track = fields.Nested(TrackSchema) + planned_playlist = fields.Nested(PlaylistSchema) + current_playlogs = fields.List(fields.Nested(PlaylogSchema)) + current_timeslot = fields.Nested(TimeslotSchema) + upcoming_timeslots = fields.Nested(TimeslotSchema) diff --git a/src/aura_engine_api/service.py b/src/aura_engine_api/service.py index 6b06a39..4884b47 100644 --- a/src/aura_engine_api/service.py +++ b/src/aura_engine_api/service.py @@ -101,7 +101,7 @@ class ApiService: def current_track(self): """ - Retrieves the currently playing track. + Retrieve the currently playing track. Returns: (JSON) @@ -112,7 +112,7 @@ class ApiService: def list_tracks(self, page=None, size=None, from_time=None, to_time=None): """ - Lists track-service entries with pagination. + List track-service entries with pagination. Args: page (Integer): The number of the page to return @@ -155,7 +155,7 @@ class ApiService: def store_playlog(self, data, plain_json): """ - Stores the passed playlog entry. + Store the passed playlog entry. Args: data (Object): The data to store in the playlog @@ -168,7 +168,6 @@ class ApiService: if self.node_type == NodeType.MAIN or ( self.node_type == NodeType.SYNC and data.log_source == self.active_source ): - try: playlog = PlayLog(data) playlog.save() @@ -207,7 +206,7 @@ class ApiService: def get_clock_info(self): """ - Retrieves the dataset required to render the studio clock. + Retrieve the dataset required to render the studio clock. """ info = ClockInfo.get_info(self.get_active_source()) @@ -222,7 +221,7 @@ class ApiService: def set_clock_info(self, data, plain_json): """ - Sets the clock info for the given source (engine1, engine2, other). + Set the clock info for the given source (engine1, engine2, other). """ if data.engine_source <= 0: return @@ -292,7 +291,7 @@ class ApiService: def set_active_source(self, source_number): """ - Sets the active source (engine1, engine2, other) identified by its source number. + Set the active source (engine1, engine2, other) identified by its source number. Args: source_number (Integer): Number of the engine @@ -305,7 +304,7 @@ class ApiService: def get_active_source(self): """ - Retrieves number of the currently active source (engine1, engine2, other) + Retrieve number of the currently active source (engine1, engine2, other) Returns: (Integer) @@ -314,7 +313,7 @@ class ApiService: def get_source_health(self, source_number): """ - Retrieves the most recent health info of the requested source + Retrieve the most recent health info of the requested source Args: source_number (Integer): Number of the play-out source @@ -329,7 +328,7 @@ class ApiService: def log_source_health(self, source_number, data, plain_json): """ - Logs an health entry for the given source + Log an health entry for the given source Args: source_number (Integer): Number of the play-out source @@ -344,7 +343,6 @@ class ApiService: if self.node_type == NodeType.MAIN or ( self.node_type == NodeType.SYNC and source_number == self.active_source ): - healthlog = HealthHistory(source_number, data.log_time, data.is_healthy, data.details) healthlog.save() self.logger.debug("Stored health info for '%s'" % str(source_number)) @@ -354,7 +352,6 @@ class ApiService: # Main Node: Push to Sync Node, if enabled if self.node_type == NodeType.MAIN and self.sync_host and self.api_healthlog: - # health_schema = HealthHistorySchema() # json_healthlog = health_schema.dump(healthlog) api_url = self.sync_host + self.api_healthlog + "/" + str(source_number) -- GitLab