From 9ca5d81b606c783fbee4b482baf7a5334901fa23 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 5 Jun 2021 12:25:12 -0700 Subject: [PATCH] Update graphs SQL queries to match homepage --- plexpy/graphs.py | 838 +++++++++++++++++++++++------------------------ 1 file changed, 415 insertions(+), 423 deletions(-) diff --git a/plexpy/graphs.py b/plexpy/graphs.py index c2115302..496a44e5 100644 --- a/plexpy/graphs.py +++ b/plexpy/graphs.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- # This file is part of Tautulli. # @@ -20,18 +20,21 @@ from future.builtins import str from future.builtins import range from future.builtins import object +import arrow import datetime import plexpy if plexpy.PYTHON2: import common import database + import helpers import logger import libraries import session else: from plexpy import common from plexpy import database + from plexpy import helpers from plexpy import logger from plexpy import libraries from plexpy import session @@ -45,8 +48,8 @@ class Graphs(object): def get_total_plays_per_day(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -61,36 +64,42 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT date(started, "unixepoch", "localtime") AS date_played, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ - 'SUM(live) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'GROUP BY date(started, "unixepoch", "localtime"), %s) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") %s' \ - 'GROUP BY date_played ' \ - 'ORDER BY started ASC' % (group_by, time_range, user_cond) + query = 'SELECT sh.date_played, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ + 'SUM(shm.live) AS live_count ' \ + 'FROM (SELECT *,' \ + ' date(started, "unixepoch", "localtime") AS date_played ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY date_played, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.date_played ' \ + 'ORDER BY sh.started' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT date(started, "unixepoch", "localtime") AS date_played, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count, ' \ - 'SUM(CASE WHEN live = 1 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") %s' \ - 'GROUP BY date_played ' \ - 'ORDER BY started ASC' % (time_range, user_cond) + query = 'SELECT sh.date_played, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS music_count, ' \ + 'SUM(CASE WHEN shm.live = 1 ' \ + ' THEN sh.d ELSE 0 END) AS live_count ' \ + 'FROM (SELECT *,' \ + ' date(started, "unixepoch", "localtime") AS date_played,' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY date_played, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.date_played ' \ + 'ORDER BY sh.started' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -159,8 +168,8 @@ class Graphs(object): def get_total_plays_per_dayofweek(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -175,52 +184,58 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT strftime("%%w", datetime(started, "unixepoch", "localtime")) AS daynumber, ' \ - '(CASE CAST(strftime("%%w", datetime(started, "unixepoch", "localtime")) AS INTEGER) ' \ - 'WHEN 0 THEN "Sunday" ' \ - 'WHEN 1 THEN "Monday" ' \ - 'WHEN 2 THEN "Tuesday" ' \ - 'WHEN 3 THEN "Wednesday" ' \ - 'WHEN 4 THEN "Thursday" ' \ - 'WHEN 5 THEN "Friday" ' \ - 'ELSE "Saturday" END) AS dayofweek, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ - 'SUM(live) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'GROUP BY strftime("%%w", datetime(started, "unixepoch", "localtime")), %s) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") %s' \ + query = 'SELECT sh.daynumber, ' \ + '(CASE sh.daynumber ' \ + ' WHEN 0 THEN "Sunday" ' \ + ' WHEN 1 THEN "Monday" ' \ + ' WHEN 2 THEN "Tuesday" ' \ + ' WHEN 3 THEN "Wednesday" ' \ + ' WHEN 4 THEN "Thursday" ' \ + ' WHEN 5 THEN "Friday" ' \ + ' ELSE "Saturday" END) AS dayofweek, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ + 'SUM(shm.live) AS live_count ' \ + 'FROM (SELECT *, ' \ + ' CAST(strftime("%%w", date(started, "unixepoch", "localtime")) AS INTEGER) AS daynumber' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY daynumber, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ 'GROUP BY dayofweek ' \ - 'ORDER BY daynumber' % (group_by, time_range, user_cond) + 'ORDER BY sh.daynumber' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT strftime("%%w", datetime(started, "unixepoch", "localtime")) AS daynumber, ' \ - '(CASE CAST(strftime("%%w", datetime(started, "unixepoch", "localtime")) AS INTEGER) ' \ - 'WHEN 0 THEN "Sunday" ' \ - 'WHEN 1 THEN "Monday" ' \ - 'WHEN 2 THEN "Tuesday" ' \ - 'WHEN 3 THEN "Wednesday" ' \ - 'WHEN 4 THEN "Thursday" ' \ - 'WHEN 5 THEN "Friday" ' \ - 'ELSE "Saturday" END) AS dayofweek, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count, ' \ - 'SUM(CASE WHEN live = 1 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") %s' \ + query = 'SELECT sh.daynumber, ' \ + '(CASE sh.daynumber ' \ + ' WHEN 0 THEN "Sunday" ' \ + ' WHEN 1 THEN "Monday" ' \ + ' WHEN 2 THEN "Tuesday" ' \ + ' WHEN 3 THEN "Wednesday" ' \ + ' WHEN 4 THEN "Thursday" ' \ + ' WHEN 5 THEN "Friday" ' \ + ' ELSE "Saturday" END) AS dayofweek, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS music_count, ' \ + 'SUM(CASE WHEN shm.live = 1 ' \ + ' THEN sh.d ELSE 0 END) AS live_count ' \ + 'FROM (SELECT *, ' \ + ' CAST(strftime("%%w", date(started, "unixepoch", "localtime")) AS INTEGER) AS daynumber, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY daynumber, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ 'GROUP BY dayofweek ' \ - 'ORDER BY daynumber' % (time_range, user_cond) + 'ORDER BY sh.daynumber' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -290,8 +305,8 @@ class Graphs(object): def get_total_plays_per_hourofday(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -306,36 +321,42 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT strftime("%%H", datetime(started, "unixepoch", "localtime")) AS hourofday, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ - 'SUM(live) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'GROUP BY strftime("%%H", datetime(started, "unixepoch", "localtime")) , %s) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") %s' \ - 'GROUP BY hourofday ' \ - 'ORDER BY hourofday' % (group_by, time_range, user_cond) + query = 'SELECT sh.hourofday, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ + 'SUM(shm.live) AS live_count ' \ + 'FROM (SELECT *, ' \ + ' strftime("%%H", datetime(started, "unixepoch", "localtime")) AS hourofday' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY hourofday, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.hourofday ' \ + 'ORDER BY sh.hourofday' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT strftime("%%H", datetime(started, "unixepoch", "localtime")) AS hourofday, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count, ' \ - 'SUM(CASE WHEN live = 1 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") %s' \ - 'GROUP BY hourofday ' \ - 'ORDER BY hourofday' % (time_range, user_cond) + query = 'SELECT sh.hourofday, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS music_count, ' \ + 'SUM(CASE WHEN shm.live = 1 ' \ + ' THEN sh.d ELSE 0 END) AS live_count ' \ + 'FROM (SELECT *, ' \ + ' strftime("%%H", datetime(started, "unixepoch", "localtime")) AS hourofday, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY hourofday, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.hourofday ' \ + 'ORDER BY sh.hourofday' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -401,11 +422,11 @@ class Graphs(object): return output def get_total_plays_per_month(self, time_range='12', y_axis='plays', user_id=None, grouping=None): - if not time_range.isdigit(): - time_range = '12' - monitor_db = database.MonitorDatabase() + time_range = helpers.cast_to_int(time_range) or 12 + timestamp = arrow.get(helpers.timestamp()).shift(months=-time_range).floor('month').timestamp + user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): user_cond = 'AND session_history.user_id = %s ' % session.get_session_user_id() @@ -419,36 +440,42 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) AS datestring, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ - 'SUM(live) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")), %s) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s months", "localtime") %s' \ - 'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) ' \ - 'ORDER BY datestring DESC LIMIT %s' % (group_by, time_range, user_cond, time_range) + query = 'SELECT sh.datestring, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ + 'SUM(shm.live) AS live_count ' \ + 'FROM (SELECT *, ' \ + ' strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) AS datestring' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY datestring, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.datestring ' \ + 'ORDER BY sh.datestring' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) AS datestring, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count, ' \ - 'SUM(CASE WHEN live = 1 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS live_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id) ' \ - 'AS session_history ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s months", "localtime") %s' \ - 'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) ' \ - 'ORDER BY datestring DESC LIMIT %s' % (time_range, user_cond, time_range) + query = 'SELECT sh.datestring, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS music_count, ' \ + 'SUM(CASE WHEN shm.live = 1 ' \ + ' THEN sh.d ELSE 0 END) AS live_count ' \ + 'FROM (SELECT *, ' \ + ' strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) AS datestring, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY datestring, %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.datestring ' \ + 'ORDER BY sh.datestring' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -524,8 +551,8 @@ class Graphs(object): def get_total_plays_by_top_10_platforms(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -540,41 +567,44 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT platform, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ - 'SUM(live) AS live_count, ' \ - 'COUNT(id) AS total_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'GROUP BY %s) ' \ - 'AS session_history ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime")) %s' \ - 'GROUP BY platform ' \ - 'ORDER BY total_count DESC ' \ - 'LIMIT 10' % (group_by, time_range, user_cond) + query = 'SELECT sh.platform, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ + 'SUM(shm.live) AS live_count, ' \ + 'COUNT(sh.id) AS total_count ' \ + 'FROM (SELECT * ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.platform ' \ + 'ORDER BY total_count DESC, sh.platform ASC ' \ + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT platform, ' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count, ' \ - 'SUM(CASE WHEN live = 1 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS live_count, ' \ - 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS total_duration ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id) ' \ - 'AS session_history ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime")) %s' \ - 'GROUP BY platform ' \ + query = 'SELECT sh.platform, ' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS music_count, ' \ + 'SUM(CASE WHEN shm.live = 1 ' \ + ' THEN sh.d ELSE 0 END) AS live_count, ' \ + 'SUM(sh.d) AS total_duration ' \ + 'FROM (SELECT *, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'GROUP BY sh.platform ' \ 'ORDER BY total_duration DESC ' \ - 'LIMIT 10' % (time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -620,8 +650,8 @@ class Graphs(object): def get_total_plays_by_top_10_users(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -636,49 +666,50 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT ' \ - 'users.user_id, users.username, ' \ - '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \ - ' THEN users.username ELSE users.friendly_name END) AS friendly_name,' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ - 'SUM(live) AS live_count, ' \ - 'COUNT(session_history.id) AS total_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'GROUP BY %s) ' \ - 'AS session_history ' \ - 'JOIN users ON session_history.user_id = users.user_id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime")) %s' \ - 'GROUP BY session_history.user_id ' \ + query = 'SELECT u.user_id, u.username, ' \ + '(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = "" ' \ + ' THEN u.username ELSE u.friendly_name END) AS friendly_name,' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 THEN 1 ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 THEN 1 ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 THEN 1 ELSE 0 END) AS music_count, ' \ + 'SUM(shm.live) AS live_count, ' \ + 'COUNT(sh.id) AS total_count ' \ + 'FROM (SELECT * ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'JOIN users AS u ON u.user_id = sh.user_id ' \ + 'GROUP BY sh.user_id ' \ 'ORDER BY total_count DESC ' \ - 'LIMIT 10' % (group_by, time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT ' \ - 'users.user_id, users.username, ' \ - '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \ - ' THEN users.username ELSE users.friendly_name END) AS friendly_name,' \ - 'SUM(CASE WHEN media_type = "episode" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tv_count, ' \ - 'SUM(CASE WHEN media_type = "movie" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS movie_count, ' \ - 'SUM(CASE WHEN media_type = "track" AND live = 0 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count, ' \ - 'SUM(CASE WHEN live = 1 AND stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS live_count, ' \ - 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS total_duration ' \ - 'FROM (SELECT * FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id) ' \ - 'AS session_history ' \ - 'JOIN users ON session_history.user_id = users.user_id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime")) %s' \ - 'GROUP BY session_history.user_id ' \ + query = 'SELECT u.user_id, u.username, ' \ + '(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = "" ' \ + ' THEN u.username ELSE u.friendly_name END) AS friendly_name,' \ + 'SUM(CASE WHEN sh.media_type = "episode" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS tv_count, ' \ + 'SUM(CASE WHEN sh.media_type = "movie" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS movie_count, ' \ + 'SUM(CASE WHEN sh.media_type = "track" AND shm.live = 0 ' \ + ' THEN sh.d ELSE 0 END) AS music_count, ' \ + 'SUM(CASE WHEN shm.live = 1 ' \ + ' THEN sh.d ELSE 0 END) AS live_count, ' \ + 'SUM(sh.d) AS total_duration ' \ + 'FROM (SELECT *, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ + 'JOIN users AS u ON u.user_id = sh.user_id ' \ + 'GROUP BY sh.user_id ' \ 'ORDER BY total_duration DESC ' \ - 'LIMIT 10' % (time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -729,8 +760,8 @@ class Graphs(object): def get_total_plays_per_stream_type(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -745,45 +776,36 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT date(session_history.started, "unixepoch", "localtime") AS date_played, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'THEN 1 ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'THEN 1 ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'THEN 1 ELSE 0 END) AS tc_count ' \ - 'FROM (SELECT * FROM session_history ' \ - 'GROUP BY date(session_history.started, "unixepoch", "localtime"), %s) ' \ - 'AS session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime")) AND ' \ - '(session_history.media_type = "episode" OR ' \ - 'session_history.media_type = "movie" OR ' \ - 'session_history.media_type = "track") %s' \ - 'GROUP BY date_played ' \ - 'ORDER BY started ASC' % (group_by, time_range, user_cond) + query = 'SELECT sh.date_played, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN 1 ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN 1 ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN 1 ELSE 0 END) AS tc_count ' \ + 'FROM (SELECT *, ' \ + ' date(started, "unixepoch", "localtime") AS date_played ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY date_played, %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ + 'GROUP BY sh.date_played ' \ + 'ORDER BY sh.started' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT date(session_history.started, "unixepoch", "localtime") AS date_played, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tc_count ' \ - 'FROM session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime") AND ' \ - '(session_history.media_type = "episode" OR session_history.media_type = "movie" OR ' \ - 'session_history.media_type = "track") %s' \ - 'GROUP BY date_played ' \ - 'ORDER BY started ASC' % (time_range, user_cond) + query = 'SELECT sh.date_played, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN sh.d ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN sh.d ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN sh.d ELSE 0 END) AS tc_count ' \ + 'FROM (SELECT *, ' \ + ' date(started, "unixepoch", "localtime") AS date_played,' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s' \ + ' GROUP BY date_played, %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ + 'GROUP BY sh.date_played ' \ + 'ORDER BY sh.started' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -835,8 +857,8 @@ class Graphs(object): def get_total_plays_by_source_resolution(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -851,45 +873,40 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT session_history_media_info.video_full_resolution AS resolution, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'THEN 1 ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'THEN 1 ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'THEN 1 ELSE 0 END) AS tc_count, ' \ - 'COUNT(session_history.id) AS total_count ' \ - 'FROM (SELECT * FROM session_history GROUP BY %s) AS session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime")) AND ' \ - '(session_history.media_type = "episode" OR session_history.media_type = "movie") %s' \ + query = 'SELECT shmi.video_full_resolution AS resolution, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN 1 ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN 1 ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN 1 ELSE 0 END) AS tc_count, ' \ + 'COUNT(sh.id) AS total_count ' \ + 'FROM (SELECT * ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s ' \ + ' AND session_history.media_type IN ("movie", "episode") %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ 'GROUP BY resolution ' \ 'ORDER BY total_count DESC ' \ - 'LIMIT 10' % (group_by, time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT session_history_media_info.video_full_resolution AS resolution,' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tc_count, ' \ - 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS total_duration ' \ - 'FROM session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime")) AND ' \ - '(session_history.media_type = "episode" OR session_history.media_type = "movie") %s' \ + query = 'SELECT shmi.video_full_resolution AS resolution,' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN sh.d ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN sh.d ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN sh.d ELSE 0 END) AS tc_count, ' \ + 'SUM(sh.d) AS total_duration ' \ + 'FROM (SELECT *, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s ' \ + ' AND session_history.media_type IN ("movie", "episode") %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ 'GROUP BY resolution ' \ 'ORDER BY total_duration DESC ' \ - 'LIMIT 10' % (time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -921,8 +938,8 @@ class Graphs(object): def get_total_plays_by_stream_resolution(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -938,68 +955,65 @@ class Graphs(object): try: if y_axis == 'plays': query = 'SELECT ' \ - '(CASE WHEN session_history_media_info.stream_video_full_resolution IS NULL THEN ' \ - '(CASE WHEN session_history_media_info.video_decision = "transcode" THEN ' \ - '(CASE ' \ - 'WHEN session_history_media_info.transcode_height <= 360 THEN "SD" ' \ - 'WHEN session_history_media_info.transcode_height <= 480 THEN "480" ' \ - 'WHEN session_history_media_info.transcode_height <= 576 THEN "576" ' \ - 'WHEN session_history_media_info.transcode_height <= 720 THEN "720" ' \ - 'WHEN session_history_media_info.transcode_height <= 1080 THEN "1080" ' \ - 'WHEN session_history_media_info.transcode_height <= 1440 THEN "QHD" ' \ - 'WHEN session_history_media_info.transcode_height <= 2160 THEN "4k" ' \ - 'ELSE "unknown" END) ELSE session_history_media_info.video_full_resolution END) ' \ - 'ELSE session_history_media_info.stream_video_full_resolution END) AS resolution, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'THEN 1 ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'THEN 1 ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" '\ - 'THEN 1 ELSE 0 END) AS tc_count, ' \ - 'COUNT(session_history.id) AS total_count ' \ - 'FROM (SELECT * FROM session_history GROUP BY %s) AS session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime")) AND ' \ - '(session_history.media_type = "episode" OR session_history.media_type = "movie") %s' \ + '(CASE WHEN shmi.stream_video_full_resolution IS NULL THEN ' \ + ' (CASE WHEN shmi.video_decision = "transcode" THEN ' \ + ' (CASE ' \ + ' WHEN shmi.transcode_height <= 360 THEN "SD" ' \ + ' WHEN shmi.transcode_height <= 480 THEN "480" ' \ + ' WHEN shmi.transcode_height <= 576 THEN "576" ' \ + ' WHEN shmi.transcode_height <= 720 THEN "720" ' \ + ' WHEN shmi.transcode_height <= 1080 THEN "1080" ' \ + ' WHEN shmi.transcode_height <= 1440 THEN "QHD" ' \ + ' WHEN shmi.transcode_height <= 2160 THEN "4k" ' \ + ' ELSE "unknown" END)' \ + ' ELSE shmi.video_full_resolution END) ' \ + ' ELSE shmi.stream_video_full_resolution END) AS resolution, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN 1 ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN 1 ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN 1 ELSE 0 END) AS tc_count, ' \ + 'COUNT(sh.id) AS total_count ' \ + 'FROM (SELECT * ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s ' \ + ' AND session_history.media_type IN ("movie", "episode") %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ 'GROUP BY resolution ' \ 'ORDER BY total_count DESC ' \ - 'LIMIT 10' % (group_by, time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: query = 'SELECT ' \ - '(CASE WHEN session_history_media_info.stream_video_full_resolution IS NULL THEN ' \ - '(CASE WHEN session_history_media_info.video_decision = "transcode" THEN ' \ - '(CASE ' \ - 'WHEN session_history_media_info.transcode_height <= 360 THEN "SD" ' \ - 'WHEN session_history_media_info.transcode_height <= 480 THEN "480" ' \ - 'WHEN session_history_media_info.transcode_height <= 576 THEN "576" ' \ - 'WHEN session_history_media_info.transcode_height <= 720 THEN "720" ' \ - 'WHEN session_history_media_info.transcode_height <= 1080 THEN "1080" ' \ - 'WHEN session_history_media_info.transcode_height <= 1440 THEN "QHD" ' \ - 'WHEN session_history_media_info.transcode_height <= 2160 THEN "4k" ' \ - 'ELSE "unknown" END) ELSE session_history_media_info.video_full_resolution END) ' \ - 'ELSE session_history_media_info.stream_video_full_resolution END) AS resolution, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tc_count, ' \ - 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS total_duration ' \ - 'FROM session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE (datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime")) AND ' \ - '(session_history.media_type = "episode" OR session_history.media_type = "movie") %s' \ + '(CASE WHEN shmi.stream_video_full_resolution IS NULL THEN ' \ + ' (CASE WHEN shmi.video_decision = "transcode" THEN ' \ + ' (CASE ' \ + ' WHEN shmi.transcode_height <= 360 THEN "SD" ' \ + ' WHEN shmi.transcode_height <= 480 THEN "480" ' \ + ' WHEN shmi.transcode_height <= 576 THEN "576" ' \ + ' WHEN shmi.transcode_height <= 720 THEN "720" ' \ + ' WHEN shmi.transcode_height <= 1080 THEN "1080" ' \ + ' WHEN shmi.transcode_height <= 1440 THEN "QHD" ' \ + ' WHEN shmi.transcode_height <= 2160 THEN "4k" ' \ + ' ELSE "unknown" END)' \ + ' ELSE shmi.video_full_resolution END) ' \ + ' ELSE shmi.stream_video_full_resolution END) AS resolution, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN sh.d ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN sh.d ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN sh.d ELSE 0 END) AS tc_count, ' \ + 'SUM(sh.d) AS total_duration ' \ + 'FROM (SELECT *, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s ' \ + ' AND session_history.media_type IN ("movie", "episode") %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ 'GROUP BY resolution ' \ 'ORDER BY total_duration DESC ' \ - 'LIMIT 10' % (time_range, user_cond) + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -1031,8 +1045,8 @@ class Graphs(object): def get_stream_type_by_top_10_platforms(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -1047,48 +1061,38 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT session_history.platform AS platform, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'THEN 1 ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'THEN 1 ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'THEN 1 ELSE 0 END) AS tc_count, ' \ - 'COUNT(session_history.id) AS total_count ' \ - 'FROM (SELECT * FROM session_history GROUP BY %s) AS session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime") AND ' \ - '(session_history.media_type = "episode" OR ' \ - 'session_history.media_type = "movie" OR ' \ - 'session_history.media_type = "track") %s' \ - 'GROUP BY platform ' \ - 'ORDER BY total_count DESC LIMIT 10' % (group_by, time_range, user_cond) + query = 'SELECT sh.platform, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN 1 ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN 1 ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN 1 ELSE 0 END) AS tc_count, ' \ + 'COUNT(sh.id) AS total_count ' \ + 'FROM (SELECT * ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ + 'GROUP BY sh.platform ' \ + 'ORDER BY total_count DESC ' \ + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT session_history.platform AS platform, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tc_count, ' \ - 'SUM(CASE WHEN session_history.stopped > 0 ' \ - 'THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS total_duration ' \ - 'FROM session_history ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime") AND ' \ - '(session_history.media_type = "episode" OR ' \ - 'session_history.media_type = "movie" OR ' \ - 'session_history.media_type = "track") %s' \ - 'GROUP BY platform ' \ - 'ORDER BY total_duration DESC LIMIT 10' % (time_range, user_cond) + query = 'SELECT sh.platform, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN sh.d ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN sh.d ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN sh.d ELSE 0 END) AS tc_count, ' \ + 'SUM(sh.d) AS total_duration ' \ + 'FROM (SELECT *, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ + 'GROUP BY sh.platform ' \ + 'ORDER BY total_duration DESC ' \ + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: @@ -1121,8 +1125,8 @@ class Graphs(object): def get_stream_type_by_top_10_users(self, time_range='30', y_axis='plays', user_id=None, grouping=None): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + time_range = helpers.cast_to_int(time_range) or 30 + timestamp = helpers.timestamp() - time_range * 24 * 60 * 60 user_cond = '' if session.get_session_user_id() and user_id and user_id != str(session.get_session_user_id()): @@ -1137,56 +1141,44 @@ class Graphs(object): try: if y_axis == 'plays': - query = 'SELECT ' \ - 'users.user_id, users.username, ' \ - '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \ - ' THEN users.username ELSE users.friendly_name END) AS friendly_name,' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'THEN 1 ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'THEN 1 ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'THEN 1 ELSE 0 END) AS tc_count, ' \ - 'COUNT(session_history.id) AS total_count ' \ - 'FROM (SELECT * FROM session_history GROUP BY %s) AS session_history ' \ - 'JOIN users ON session_history.user_id = users.user_id ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime") AND ' \ - '(session_history.media_type = "episode" OR ' \ - 'session_history.media_type = "movie" OR ' \ - 'session_history.media_type = "track") %s' \ - 'GROUP BY username ' \ - 'ORDER BY total_count DESC LIMIT 10' % (group_by, time_range, user_cond) + query = 'SELECT u.user_id, u.username, ' \ + '(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = "" ' \ + ' THEN u.username ELSE u.friendly_name END) AS friendly_name,' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN 1 ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN 1 ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN 1 ELSE 0 END) AS tc_count, ' \ + 'COUNT(sh.id) AS total_count ' \ + 'FROM (SELECT * ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ + 'JOIN users AS u ON u.user_id = sh.user_id ' \ + 'GROUP BY u.user_id ' \ + 'ORDER BY total_count DESC ' \ + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) else: - query = 'SELECT ' \ - 'users.user_id, users.username, ' \ - '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \ - ' THEN users.username ELSE users.friendly_name END) AS friendly_name,' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "direct play" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS dp_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "copy" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS ds_count, ' \ - 'SUM(CASE WHEN session_history_media_info.transcode_decision = "transcode" ' \ - 'AND session_history.stopped > 0 THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS tc_count, ' \ - 'SUM(CASE WHEN session_history.stopped > 0 ' \ - 'THEN (session_history.stopped - session_history.started) ' \ - ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS total_duration ' \ - 'FROM session_history ' \ - 'JOIN users ON session_history.user_id = users.user_id ' \ - 'JOIN session_history_media_info ON session_history.id = session_history_media_info.id ' \ - 'WHERE datetime(started, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime") AND ' \ - '(session_history.media_type = "episode" OR ' \ - 'session_history.media_type = "movie" OR ' \ - 'session_history.media_type = "track") %s' \ - 'GROUP BY username ' \ - 'ORDER BY total_duration DESC LIMIT 10' % (time_range, user_cond) + query = 'SELECT u.user_id, u.username, ' \ + '(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = "" ' \ + ' THEN u.username ELSE u.friendly_name END) AS friendly_name,' \ + 'SUM(CASE WHEN shmi.transcode_decision = "direct play" THEN sh.d ELSE 0 END) AS dp_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "copy" THEN sh.d ELSE 0 END) AS ds_count, ' \ + 'SUM(CASE WHEN shmi.transcode_decision = "transcode" THEN sh.d ELSE 0 END) AS tc_count, ' \ + 'SUM(sh.d) AS total_duration ' \ + 'FROM (SELECT *, ' \ + ' SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ + ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ + ' AS d ' \ + ' FROM session_history ' \ + ' WHERE session_history.stopped >= %s %s ' \ + ' GROUP BY %s) AS sh ' \ + 'JOIN session_history_media_info AS shmi ON shmi.id = sh.id ' \ + 'JOIN users AS u ON u.user_id = sh.user_id ' \ + 'GROUP BY u.user_id ' \ + 'ORDER BY total_duration DESC ' \ + 'LIMIT 10' % (timestamp, user_cond, group_by) result = monitor_db.select(query) except Exception as e: