From 78f959d39a8c2d2d0b2a0e680c465c9d1fbf9232 Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Fri, 18 Sep 2015 18:54:48 -0700 Subject: [PATCH] Clean up passing unnecessary configs to homepage --- data/interfaces/default/index.html | 12 +++------ plexpy/datafactory.py | 43 +++++++++++++++--------------- plexpy/webserve.py | 20 +++++++++----- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/data/interfaces/default/index.html b/data/interfaces/default/index.html index fab5d5b3..e18cfdf8 100644 --- a/data/interfaces/default/index.html +++ b/data/interfaces/default/index.html @@ -84,15 +84,12 @@ currentActivity(); setInterval(currentActivity, 15000); - function getHomeStats(days, stat_type, stat_count, notify_watched_percent) { + function getHomeStats(days) { $.ajax({ url: 'home_stats', cache: false, async: true, - data: {time_range: days, - stat_type: stat_type, - stat_count: stat_count, - notify_watched_percent: notify_watched_percent}, + data: { }, complete: function(xhr, status) { $("#home-stats").html(xhr.responseText); } @@ -170,10 +167,7 @@ } }); - getHomeStats(${config['home_stats_length']}, - ${config['home_stats_type']}, - ${config['home_stats_count']}, - ${config['notify_watched_percent']}); + getHomeStats(); getLibraryStatsHeader(); getLibraryStats(); diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 5412922d..ebf7f339 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -131,19 +131,12 @@ class DataFactory(object): return dict - def get_home_stats(self, time_range='30', stat_type='0', stat_count='5', notify_watched_percent='85'): + def get_home_stats(self, time_range='30', stats_type='0', stats_count='5', stats_cards='', notify_watched_percent='85'): monitor_db = database.MonitorDatabase() - if not time_range.isdigit(): - time_range = '30' + sort_type = 'total_plays' if stats_type == '0' else 'total_duration' - sort_type = 'total_plays' if stat_type == '0' else 'total_duration' - - if not time_range.isdigit(): - stat_count = '5' - - # This actually determines the output order in the home page - stats_queries = ["top_tv", "popular_tv", "top_movies", "popular_movies", "top_users", "top_platforms", "last_watched"] + stats_queries = stats_cards.split(', ') home_stats = [] for stat in stats_queries: @@ -166,7 +159,7 @@ class DataFactory(object): '>= 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, stat_count) + 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") @@ -202,6 +195,10 @@ class DataFactory(object): '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 ' \ @@ -209,8 +206,8 @@ class DataFactory(object): '>= datetime("now", "-%s days", "localtime") ' \ 'AND session_history_metadata.media_type = "episode" ' \ 'GROUP BY session_history_metadata.grandparent_title ' \ - 'ORDER BY users_watched DESC, total_plays DESC ' \ - 'LIMIT %s' % (time_range, stat_count) + 'ORDER BY users_watched DESC, %s DESC ' \ + 'LIMIT %s' % (time_range, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") @@ -222,7 +219,7 @@ class DataFactory(object): 'rating_key': item[3], 'last_play': item[4], 'total_plays': item[5], - 'grandparent_thumb': item[6], + 'grandparent_thumb': item[7], 'thumb': '', 'user': '', 'friendly_name': '', @@ -254,7 +251,7 @@ class DataFactory(object): '>= 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, stat_count) + 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") @@ -290,6 +287,10 @@ class DataFactory(object): '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 ' \ @@ -297,8 +298,8 @@ class DataFactory(object): '>= datetime("now", "-%s days", "localtime") ' \ 'AND session_history_metadata.media_type = "movie" ' \ 'GROUP BY session_history_metadata.full_title ' \ - 'ORDER BY users_watched DESC, total_plays DESC ' \ - 'LIMIT %s' % (time_range, stat_count) + 'ORDER BY users_watched DESC, %s DESC ' \ + 'LIMIT %s' % (time_range, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") @@ -311,7 +312,7 @@ class DataFactory(object): 'last_play': item[4], 'total_plays': item[5], 'grandparent_thumb': '', - 'thumb': item[6], + 'thumb': item[7], 'user': '', 'friendly_name': '', 'platform_type': '', @@ -343,7 +344,7 @@ class DataFactory(object): '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, stat_count) + 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") @@ -391,7 +392,7 @@ class DataFactory(object): '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, stat_count) + 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") @@ -447,7 +448,7 @@ class DataFactory(object): 'AND percent_complete >= %s ' \ 'GROUP BY session_history_metadata.full_title ' \ 'ORDER BY last_watch DESC ' \ - 'LIMIT %s' % (time_range, notify_watched_percent, stat_count) + 'LIMIT %s' % (time_range, notify_watched_percent, stats_count) result = monitor_db.select(query) except: logger.warn("Unable to execute database query.") diff --git a/plexpy/webserve.py b/plexpy/webserve.py index c1507dee..42dd50e0 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -66,10 +66,8 @@ class WebInterface(object): def home(self): config = { "home_stats_length": plexpy.CONFIG.HOME_STATS_LENGTH, - "home_stats_type": plexpy.CONFIG.HOME_STATS_TYPE, - "home_stats_count": plexpy.CONFIG.HOME_STATS_COUNT, - "pms_identifier": plexpy.CONFIG.PMS_IDENTIFIER, - "notify_watched_percent": plexpy.CONFIG.NOTIFY_WATCHED_PERCENT + "home_stats_cards": plexpy.CONFIG.HOME_STATS_CARDS, + "pms_identifier": plexpy.CONFIG.PMS_IDENTIFIER } return serve_template(templatename="index.html", title="Home", config=config) @@ -122,11 +120,19 @@ class WebInterface(object): return json.dumps(formats) @cherrypy.expose - def home_stats(self, time_range='30', stat_type='0', stat_count='5', notify_watched_percent='85', **kwargs): + def home_stats(self, **kwargs): data_factory = datafactory.DataFactory() + + 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 + notify_watched_percent = plexpy.CONFIG.NOTIFY_WATCHED_PERCENT + stats_data = data_factory.get_home_stats(time_range=time_range, - stat_type=stat_type, - stat_count=stat_count, + stats_type=stats_type, + stats_count=stats_count, + stats_cards=stats_cards, notify_watched_percent=notify_watched_percent) return serve_template(templatename="home_stats.html", title="Stats", data=stats_data)