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

Reporting API.

parent ef293337
No related branches found
No related tags found
No related merge requests found
...@@ -25,10 +25,12 @@ __author__ = 'David Trattnig <david.trattnig@subsquare.at>' ...@@ -25,10 +25,12 @@ __author__ = 'David Trattnig <david.trattnig@subsquare.at>'
import logging import logging
import os, os.path
from datetime import datetime, date from datetime import datetime, date, timedelta
from flask import Flask, Response from flask import Flask, Response
from flask_caching import Cache
from flask_cors import CORS from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow from flask_marshmallow import Marshmallow
...@@ -42,8 +44,7 @@ from libraries.base.logger import AuraLogger ...@@ -42,8 +44,7 @@ from libraries.base.logger import AuraLogger
from libraries.base.config import AuraConfig from libraries.base.config import AuraConfig
from libraries.database.broadcasts import AuraDatabaseModel, Schedule, Playlist, PlaylistEntry, PlaylistEntryMetaData, TrackService 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: ...@@ -97,12 +98,15 @@ class EngineApi:
# Schema instances # Schema instances
EngineApi.trackservice_schema = TrackServiceSchema(many=True) EngineApi.trackservice_schema = TrackServiceSchema(many=True)
EngineApi.track_schema = TrackServiceSchema() EngineApi.track_schema = TrackServiceSchema()
EngineApi.report_schema = ReportSchema(many=True)
# Define API routes # Define API routes
self.api.add_resource(TrackServiceResource, config.api_prefix + "/trackservice/") 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(TrackResource, config.api_prefix + "/trackservice/<int:track_id>")
self.api.add_resource(CurrentTrackResource, config.api_prefix + "/trackservice/current") 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(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!") self.logger.info("Engine API routes successfully set!")
# Static resources # Static resources
...@@ -156,47 +160,51 @@ class TrackServiceSchema(ma.Schema): ...@@ -156,47 +160,51 @@ class TrackServiceSchema(ma.Schema):
"schedule.schedule_id", "schedule.schedule_id",
"schedule.schedule_start", "schedule.schedule_start",
"schedule.schedule_end", "schedule.schedule_end",
"schedule.show_id",
"schedule.show_name",
"schedule.show_hosts",
"schedule.languages", "schedule.languages",
"schedule.type", "schedule.type",
"schedule.category", "schedule.category",
"schedule.topic", "schedule.topic",
"schedule.musicfocus", "schedule.musicfocus",
"schedule.is_repetition", "schedule.is_repetition",
"schedule.show_id",
"schedule.show_name",
"schedule.show_hosts",
"track", "track",
"track_start" "track_start"
) )
# class ReportSchema(ma.Schema): class ReportSchema(ma.Schema):
# class Meta: class Meta:
# fields = ( fields = (
# "id", "id",
# "schedule_start", "schedule.schedule_id",
# "schedule_end", "schedule.schedule_start",
# "is_repetition", "schedule.schedule_end",
# "title", # ? "schedule.languages",
# "playlist_description", # ? "schedule.type",
# "show_name", "schedule.category",
# "show_hosts", "schedule.topic",
# "show_slug", "schedule.musicfocus",
# "show_funding_category", "schedule.is_repetition",
# "show_languages",
# "show_type", "schedule.show_id",
# "show_categories", "schedule.show_name",
# "show_topics", "schedule.show_hosts",
# "show_musicfocus", "schedule.show_type",
# "show_description", "schedule.show_funding_category",
# "show_short_description"
# "show_email", "track",
# "show_image", "track_start",
# "show_thumbnails",
# "show_logo", "playlist_id",
# "show_website", "fallback_type",
# "show_type" "schedule_fallback_id",
# ) "show_fallback_id",
"station_fallback_id"
)
...@@ -253,6 +261,23 @@ class TracksByDayResource(Resource): ...@@ -253,6 +261,23 @@ class TracksByDayResource(Resource):
return EngineApi.trackservice_schema.dump(tracks) 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)
......
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