From 222fa2694b11ac2f56b505e60aed89d2bc6c9064 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Mon, 9 Mar 2020 09:07:31 +0100
Subject: [PATCH] Reporting API.

---
 api.py | 91 +++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 58 insertions(+), 33 deletions(-)

diff --git a/api.py b/api.py
index b1c978ec..59c5f403 100644
--- a/api.py
+++ b/api.py
@@ -25,10 +25,12 @@ __author__ = 'David Trattnig <david.trattnig@subsquare.at>'
 
 
 import logging
+import os, os.path
 
-from datetime import datetime, date
+from datetime                       import datetime, date, timedelta
 
 from flask                          import Flask, Response
+from flask_caching                  import Cache
 from flask_cors                     import CORS
 from flask_sqlalchemy               import SQLAlchemy
 from flask_marshmallow              import Marshmallow
@@ -42,8 +44,7 @@ from libraries.base.logger          import AuraLogger
 from libraries.base.config          import AuraConfig
 from libraries.database.broadcasts  import AuraDatabaseModel, Schedule, Playlist, PlaylistEntry, PlaylistEntryMetaData, TrackService
 
-from flask_caching import Cache
-import os, os.path
+
 
 
 #
@@ -97,12 +98,15 @@ class EngineApi:
         # Schema instances
         EngineApi.trackservice_schema = TrackServiceSchema(many=True)
         EngineApi.track_schema = TrackServiceSchema()
+        EngineApi.report_schema = ReportSchema(many=True)
 
         # Define API routes
         self.api.add_resource(TrackServiceResource,  config.api_prefix + "/trackservice/")
         self.api.add_resource(TrackResource,         config.api_prefix + "/trackservice/<int:track_id>")
         self.api.add_resource(CurrentTrackResource,  config.api_prefix + "/trackservice/current")
         self.api.add_resource(TracksByDayResource,   config.api_prefix + "/trackservice/date/<string:date_string>")
+        self.api.add_resource(ReportResource,        config.api_prefix + "/report/<string:year_month>")
+
         self.logger.info("Engine API routes successfully set!")
 
         # Static resources
@@ -156,47 +160,51 @@ class TrackServiceSchema(ma.Schema):
             "schedule.schedule_id",
             "schedule.schedule_start",
             "schedule.schedule_end",
-            "schedule.show_id",
-            "schedule.show_name",
-            "schedule.show_hosts",
             "schedule.languages",
             "schedule.type",
             "schedule.category",
             "schedule.topic",
             "schedule.musicfocus",
             "schedule.is_repetition",
+
+            "schedule.show_id",
+            "schedule.show_name",
+            "schedule.show_hosts",
+
             "track",
             "track_start"
             )
 
 
-# class ReportSchema(ma.Schema):
-#     class Meta:
-#         fields = (
-#             "id", 
-#             "schedule_start", 
-#             "schedule_end", 
-#             "is_repetition", 
-#             "title", # ?
-#             "playlist_description", # ?
-#             "show_name", 
-#             "show_hosts",
-#             "show_slug",
-#             "show_funding_category",
-#             "show_languages", 
-#             "show_type", 
-#             "show_categories", 
-#             "show_topics", 
-#             "show_musicfocus",
-#             "show_description",
-#             "show_short_description"
-#             "show_email",
-#             "show_image",
-#             "show_thumbnails",
-#             "show_logo",
-#             "show_website",
-#             "show_type"
-#         )
+class ReportSchema(ma.Schema):
+    class Meta:
+        fields = (
+            "id", 
+            "schedule.schedule_id",
+            "schedule.schedule_start",
+            "schedule.schedule_end",
+            "schedule.languages",
+            "schedule.type",
+            "schedule.category",
+            "schedule.topic",
+            "schedule.musicfocus",
+            "schedule.is_repetition",
+
+            "schedule.show_id",
+            "schedule.show_name",
+            "schedule.show_hosts",
+            "schedule.show_type",
+            "schedule.show_funding_category",
+
+            "track",
+            "track_start",
+
+            "playlist_id",
+            "fallback_type",
+            "schedule_fallback_id",
+            "show_fallback_id",
+            "station_fallback_id"
+            )
 
 
 
@@ -253,6 +261,23 @@ class TracksByDayResource(Resource):
         return EngineApi.trackservice_schema.dump(tracks)
 
 
+class ReportResource(Resource):
+    logger = None
+    
+    def __init__(self):
+        self.logger = logging.getLogger("engine-api")
+
+    def get(self, year_month):
+        year = int(year_month.split("-")[0])
+        month = int(year_month.split("-")[1])
+        
+        first_day = datetime(year, month, 1)    
+        next_month = first_day.replace(day=28) + timedelta(days=4)
+        next_month - timedelta(days=next_month.day)
+
+        self.logger.debug("Query report for month: %s - %s" % (str(first_day), str(next_month)))
+        report = TrackService.select_by_range(first_day, next_month)
+        return EngineApi.report_schema.dump(report)
 
 
 
-- 
GitLab