From 447c50fd03341933b30d7f202f0973f7647a9515 Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Sat, 19 Dec 2015 20:40:30 -0800 Subject: [PATCH] Group watch statistics history --- data/interfaces/default/css/plexpy.css | 6 +- data/interfaces/default/settings.html | 4 +- plexpy/datafactory.py | 307 +++++++++++-------------- plexpy/webserve.py | 6 +- 4 files changed, 150 insertions(+), 173 deletions(-) diff --git a/data/interfaces/default/css/plexpy.css b/data/interfaces/default/css/plexpy.css index fc1449a6..5ae38fe4 100644 --- a/data/interfaces/default/css/plexpy.css +++ b/data/interfaces/default/css/plexpy.css @@ -487,7 +487,8 @@ textarea.form-control:focus { .users-poster-face { overflow: hidden; float: left; - background-size: contain; + background-size: cover; + background-position: center; height: 40px; width: 40px; -webkit-border-radius: 50%; @@ -1537,7 +1538,8 @@ a:hover .item-children-poster { float: left; margin-top: 15px; margin-right: 15px; - background-size: contain; + background-size: cover; + background-position: center; height: 80px; width: 80px; -webkit-border-radius: 50%; diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index 08d76466..2683860b 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -86,9 +86,9 @@ available_notification_agents = sorted(notifiers.available_notification_agents()
-

Group successive play history by the same user as a single entry in tables.

+

Group successive play history by the same user as a single entry in the tables and watch statistics.

diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 4d7a4fdb..0e489684 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -37,11 +37,12 @@ class DataFactory(object): 'MIN(started) AS started', 'MAX(stopped) AS stopped', 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - \ - SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS duration', + SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS duration', 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter', 'session_history.user_id', 'session_history.user', - '(CASE WHEN users.friendly_name IS NULL THEN users.username ELSE users.friendly_name END) as friendly_name', + '(CASE WHEN users.friendly_name IS NULL THEN users.username ELSE users.friendly_name END) \ + AS friendly_name', 'platform', 'player', 'ip_address', @@ -58,7 +59,8 @@ class DataFactory(object): 'session_history_metadata.parent_thumb', 'session_history_metadata.grandparent_thumb', '((CASE WHEN view_offset IS NULL THEN 0.1 ELSE view_offset * 1.0 END) / \ - (CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 ELSE session_history_metadata.duration * 1.0 END) * 100) AS percent_complete', + (CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 \ + ELSE session_history_metadata.duration * 1.0 END) * 100) AS percent_complete', 'session_history_media_info.video_decision', 'session_history_media_info.audio_decision', 'COUNT(*) AS group_count', @@ -156,9 +158,10 @@ class DataFactory(object): return dict - def get_home_stats(self, time_range='30', stats_type=0, stats_count='5', stats_cards='', notify_watched_percent='85'): + def get_home_stats(self, grouping=0, time_range='30', stats_type=0, stats_count='5', stats_cards='', notify_watched_percent='85'): monitor_db = database.MonitorDatabase() + group_by = 'session_history.reference_id' if grouping else 'session_history.id' sort_type = 'total_plays' if stats_type == 0 else 'total_duration' home_stats = [] @@ -167,23 +170,20 @@ class DataFactory(object): if stat == 'top_tv': top_tv = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history_metadata.grandparent_title, ' \ - 'COUNT(session_history_metadata.grandparent_title) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'session_history_metadata.grandparent_rating_key, ' \ - 'MAX(session_history.started) as last_watch,' \ - 'session_history_metadata.grandparent_thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history on session_history_metadata.id = session_history.id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND session_history_metadata.media_type = "episode" ' \ - 'GROUP BY session_history_metadata.grandparent_title ' \ - 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) + query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND session_history.media_type = "episode" ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.grandparent_title ' \ + 'ORDER BY %s DESC ' \ + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: top_tv.") @@ -213,25 +213,21 @@ class DataFactory(object): elif stat == 'popular_tv': popular_tv = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history_metadata.grandparent_title, ' \ - 'COUNT(DISTINCT session_history.user_id) as users_watched, ' \ - 'session_history_metadata.grandparent_rating_key, ' \ - 'MAX(session_history.started) as last_watch, ' \ - 'COUNT(session_history.id) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'session_history_metadata.grandparent_thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history ON session_history_metadata.id = session_history.id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND session_history_metadata.media_type = "episode" ' \ - 'GROUP BY session_history_metadata.grandparent_title ' \ + query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, ' \ + 'COUNT(DISTINCT t.user_id) AS users_watched, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) as total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND session_history.media_type = "episode" ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.grandparent_title ' \ 'ORDER BY users_watched DESC, %s DESC ' \ - 'LIMIT %s' % (time_range, sort_type, stats_count) + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: popular_tv.") @@ -259,23 +255,20 @@ class DataFactory(object): elif stat == 'top_movies': top_movies = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history_metadata.full_title, ' \ - 'COUNT(session_history_metadata.full_title) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'session_history_metadata.rating_key, ' \ - 'MAX(session_history.started) as last_watch,' \ - 'session_history_metadata.thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history on session_history_metadata.id = session_history.id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND session_history_metadata.media_type = "movie" ' \ - 'GROUP BY session_history_metadata.full_title ' \ - 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) + query = 'SELECT t.id, t.full_title, t.rating_key, t.thumb, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND session_history.media_type = "movie" ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.full_title ' \ + 'ORDER BY %s DESC ' \ + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: top_movies.") @@ -297,7 +290,6 @@ class DataFactory(object): 'row_id': item['id'] } top_movies.append(row) - home_stats.append({'stat_id': stat, 'stat_type': sort_type, 'rows': top_movies}) @@ -305,25 +297,21 @@ class DataFactory(object): elif stat == 'popular_movies': popular_movies = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history_metadata.full_title, ' \ - 'COUNT(DISTINCT session_history.user_id) as users_watched, ' \ - 'session_history_metadata.rating_key, ' \ - 'MAX(session_history.started) as last_watch, ' \ - 'COUNT(session_history.id) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'session_history_metadata.thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history ON session_history_metadata.id = session_history.id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND session_history_metadata.media_type = "movie" ' \ - 'GROUP BY session_history_metadata.full_title ' \ + query = 'SELECT t.id, t.full_title, t.rating_key, t.thumb, ' \ + 'COUNT(DISTINCT t.user_id) AS users_watched, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) as total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND session_history.media_type = "movie" ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.full_title ' \ 'ORDER BY users_watched DESC, %s DESC ' \ - 'LIMIT %s' % (time_range, sort_type, stats_count) + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: popular_movies.") @@ -351,23 +339,20 @@ class DataFactory(object): elif stat == 'top_music': top_music = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history_metadata.grandparent_title, ' \ - 'COUNT(session_history_metadata.grandparent_title) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'session_history_metadata.grandparent_rating_key, ' \ - 'MAX(session_history.started) as last_watch,' \ - 'session_history_metadata.grandparent_thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history on session_history_metadata.id = session_history.id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND session_history_metadata.media_type = "track" ' \ - 'GROUP BY session_history_metadata.grandparent_title ' \ - 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) + query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND session_history.media_type = "track" ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.grandparent_title ' \ + 'ORDER BY %s DESC ' \ + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: top_music.") @@ -397,25 +382,21 @@ class DataFactory(object): elif stat == 'popular_music': popular_music = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history_metadata.grandparent_title, ' \ - 'COUNT(DISTINCT session_history.user_id) as users_watched, ' \ - 'session_history_metadata.grandparent_rating_key, ' \ - 'MAX(session_history.started) as last_watch, ' \ - 'COUNT(session_history.id) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'session_history_metadata.grandparent_thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history ON session_history_metadata.id = session_history.id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND session_history_metadata.media_type = "track" ' \ - 'GROUP BY session_history_metadata.grandparent_title ' \ + query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, ' \ + 'COUNT(DISTINCT t.user_id) AS users_watched, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) as total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND session_history.media_type = "track" ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.grandparent_title ' \ 'ORDER BY users_watched DESC, %s DESC ' \ - 'LIMIT %s' % (time_range, sort_type, stats_count) + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: popular_music.") @@ -443,24 +424,22 @@ class DataFactory(object): elif stat == 'top_users': top_users = [] try: - query = 'SELECT session_history.user, ' \ - '(case when users.friendly_name is null then users.username else ' \ - 'users.friendly_name end) as friendly_name,' \ - 'COUNT(session_history.id) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'MAX(session_history.started) as last_watch, ' \ - 'users.custom_avatar_url as thumb, ' \ - 'users.user_id ' \ - 'FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'LEFT OUTER JOIN users ON session_history.user_id = users.user_id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") >= ' \ - 'datetime("now", "-%s days", "localtime") '\ - 'GROUP BY session_history.user_id ' \ - 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) + query = 'SELECT t.user, t.user_id, t.custom_avatar_url as thumb, ' \ + '(CASE WHEN t.friendly_name IS NULL THEN t.username ELSE t.friendly_name END) ' \ + ' AS friendly_name, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' LEFT OUTER JOIN users ON session_history.user_id = users.user_id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.user_id ' \ + 'ORDER BY %s DESC ' \ + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: top_users.") @@ -497,18 +476,19 @@ class DataFactory(object): top_platform = [] try: - query = 'SELECT session_history.platform, ' \ - 'COUNT(session_history.id) as total_plays, ' \ - 'SUM(case when session_history.stopped > 0 ' \ - 'then (session_history.stopped - session_history.started) ' \ - ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \ - 'else 0 end) as total_duration, ' \ - 'MAX(session_history.started) as last_watch ' \ - 'FROM session_history ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'GROUP BY session_history.platform ' \ - 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) + query = 'SELECT t.platform, ' \ + 'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, ' \ + 'SUM(CASE WHEN t.stopped > 0 THEN (t.stopped - t.started) ' \ + ' - (CASE WHEN t.paused_counter IS NULL THEN 0 ELSE t.paused_counter END) ELSE 0 END) ' \ + ' AS total_duration ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' GROUP BY %s) AS t ' \ + 'GROUP BY t.platform ' \ + 'ORDER BY %s DESC ' \ + 'LIMIT %s ' % (time_range, group_by, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: top_platforms.") @@ -541,33 +521,26 @@ class DataFactory(object): elif stat == 'last_watched': last_watched = [] try: - query = 'SELECT session_history_metadata.id, ' \ - 'session_history.user, ' \ - '(case when users.friendly_name is null then users.username else ' \ - 'users.friendly_name end) as friendly_name,' \ - 'users.user_id, ' \ - 'users.custom_avatar_url as user_thumb, ' \ - 'session_history_metadata.full_title, ' \ - 'session_history_metadata.rating_key, ' \ - 'session_history_metadata.thumb, ' \ - 'session_history_metadata.grandparent_thumb, ' \ - 'MAX(session_history.started) as last_watch, ' \ - 'session_history.player, ' \ - '((CASE WHEN session_history.view_offset IS NULL THEN 0.1 ELSE \ - session_history.view_offset * 1.0 END) / \ - (CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 ELSE \ - session_history_metadata.duration * 1.0 END) * 100) as percent_complete ' \ - 'FROM session_history_metadata ' \ - 'JOIN session_history ON session_history_metadata.id = session_history.id ' \ - 'LEFT OUTER JOIN users ON session_history.user_id = users.user_id ' \ - 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ - '>= datetime("now", "-%s days", "localtime") ' \ - 'AND (session_history_metadata.media_type = "movie" ' \ - 'OR session_history_metadata.media_type = "episode") ' \ - 'AND percent_complete >= %s ' \ - 'GROUP BY session_history.id ' \ + query = 'SELECT t.id, t.full_title, t.rating_key, t.thumb, t.grandparent_thumb, ' \ + 't.user, t.user_id, t.custom_avatar_url as user_thumb, t.player, ' \ + '(CASE WHEN t.friendly_name IS NULL THEN t.username ELSE t.friendly_name END) ' \ + ' AS friendly_name, ' \ + 'MAX(t.started) AS last_watch, ' \ + '((CASE WHEN t.view_offset IS NULL THEN 0.1 ELSE t.view_offset * 1.0 END) / ' \ + ' (CASE WHEN t.duration IS NULL THEN 1.0 ELSE t.duration * 1.0 END) * 100) ' \ + ' AS percent_complete ' \ + 'FROM (SELECT * FROM session_history ' \ + ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ + ' LEFT OUTER JOIN users ON session_history.user_id = users.user_id ' \ + ' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \ + ' >= datetime("now", "-%s days", "localtime") ' \ + ' AND (session_history.media_type = "movie" ' \ + ' OR session_history_metadata.media_type = "episode") ' \ + ' GROUP BY %s) AS t ' \ + 'WHERE percent_complete >= %s ' \ + 'GROUP BY t.id ' \ 'ORDER BY last_watch DESC ' \ - 'LIMIT %s' % (time_range, notify_watched_percent, stats_count) + 'LIMIT %s' % (time_range, group_by, notify_watched_percent, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query for get_home_stats: last_watched.") @@ -1102,7 +1075,7 @@ class DataFactory(object): try: query = 'SELECT SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - ' \ - 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS total_duration ' \ + 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS total_duration ' \ 'FROM session_history %s ' % where result = monitor_db.select(query) except: diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 902e9286..0ee08a97 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -128,13 +128,15 @@ class WebInterface(object): def home_stats(self, **kwargs): data_factory = datafactory.DataFactory() + grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES time_range = plexpy.CONFIG.HOME_STATS_LENGTH stats_type = plexpy.CONFIG.HOME_STATS_TYPE stats_count = plexpy.CONFIG.HOME_STATS_COUNT stats_cards = plexpy.CONFIG.HOME_STATS_CARDS.split(', ') notify_watched_percent = plexpy.CONFIG.NOTIFY_WATCHED_PERCENT - stats_data = data_factory.get_home_stats(time_range=time_range, + stats_data = data_factory.get_home_stats(grouping=grouping, + time_range=time_range, stats_type=stats_type, stats_count=stats_count, stats_cards=stats_cards, @@ -613,7 +615,7 @@ class WebInterface(object): custom_where.append(['session_history.grandparent_rating_key', rating_key]) if 'start_date' in kwargs: start_date = kwargs.get('start_date', "") - custom_where.append(['strftime("%Y-%m-%d", datetime(date, "unixepoch", "localtime"))', start_date]) + custom_where.append(['strftime("%Y-%m-%d", datetime(started, "unixepoch", "localtime"))', start_date]) if 'reference_id' in kwargs: reference_id = kwargs.get('reference_id', "") custom_where.append(['session_history.reference_id', reference_id])