From 0224c709b0dee26e0b36c7c9aae52bf15e488045 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 24 Jun 2015 00:10:09 +0200 Subject: [PATCH] Add new graphs for watches by day of week and hour of day. Clean up some graph styling --- data/interfaces/default/css/plexwatch.css | 6 +- data/interfaces/default/graphs.html | 59 +++++++++++++- .../default/js/graphs/plays_by_dayofweek.js | 45 +++++++++++ .../default/js/graphs/plays_by_hourofday.js | 45 +++++++++++ plexpy/plexwatch.py | 81 +++++++++++++++++++ plexpy/webserve.py | 24 ++++++ 6 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 data/interfaces/default/js/graphs/plays_by_dayofweek.js create mode 100644 data/interfaces/default/js/graphs/plays_by_hourofday.js diff --git a/data/interfaces/default/css/plexwatch.css b/data/interfaces/default/css/plexwatch.css index 93d495c1..f4fa8d96 100644 --- a/data/interfaces/default/css/plexwatch.css +++ b/data/interfaces/default/css/plexwatch.css @@ -8152,7 +8152,7 @@ ol.test >li { } .graphs-instance { - height: 350px; + height: 300px; padding: 20px; background-color: #282828; margin-right: 20px; @@ -8160,7 +8160,9 @@ ol.test >li { } .watch-chart { - height: 360px; + margin-top: 10px; + height: 300px; + width: 100%; } .graphs-load { diff --git a/data/interfaces/default/graphs.html b/data/interfaces/default/graphs.html index 9707134e..2e3c01f5 100644 --- a/data/interfaces/default/graphs.html +++ b/data/interfaces/default/graphs.html @@ -30,13 +30,42 @@ from plexpy import helpers -
+
Loading chart...

+ +
+
+
+
+
+

Watches by day of week (Past 30 days)

+
+
+
+
+
Loading chart...

+
+
+
+
+
+
+

Watches by hour of day (Past 30 days)

+
+
+
+
+
Loading chart...

+
+
+
+
+
@@ -47,7 +76,10 @@ from plexpy import helpers + + \ No newline at end of file diff --git a/data/interfaces/default/js/graphs/plays_by_dayofweek.js b/data/interfaces/default/js/graphs/plays_by_dayofweek.js new file mode 100644 index 00000000..326b5592 --- /dev/null +++ b/data/interfaces/default/js/graphs/plays_by_dayofweek.js @@ -0,0 +1,45 @@ +var hc_plays_by_dayofweek_options = { + chart: { + type: 'column', + backgroundColor: 'rgba(0,0,0,0)', + renderTo: 'chart_div_plays_by_dayofweek' + }, + title: { + text: '' + }, + plotOptions: { + column: { + pointPadding: 0.2, + borderWidth: 0 + } + }, + legend: { + enabled: true, + itemStyle: { + font: '9pt "Open Sans", sans-serif', + color: '#A0A0A0' + }, + itemHoverStyle: { + color: '#FFF' + }, + itemHiddenStyle: { + color: '#444' + } + }, + credits: { + enabled: false + }, + colors: ['#F9AA03', '#FFFFFF'], + xAxis: { + categories: [{}] + }, + yAxis: { + title: { + text: null + } + }, + tooltip: { + + }, + series: [{}] +}; \ No newline at end of file diff --git a/data/interfaces/default/js/graphs/plays_by_hourofday.js b/data/interfaces/default/js/graphs/plays_by_hourofday.js new file mode 100644 index 00000000..a1c4f977 --- /dev/null +++ b/data/interfaces/default/js/graphs/plays_by_hourofday.js @@ -0,0 +1,45 @@ +var hc_plays_by_hourofday_options = { + chart: { + type: 'column', + backgroundColor: 'rgba(0,0,0,0)', + renderTo: 'chart_div_plays_by_hourofday' + }, + title: { + text: '' + }, + plotOptions: { + column: { + pointPadding: 0.2, + borderWidth: 0 + } + }, + legend: { + enabled: true, + itemStyle: { + font: '9pt "Open Sans", sans-serif', + color: '#A0A0A0' + }, + itemHoverStyle: { + color: '#FFF' + }, + itemHiddenStyle: { + color: '#444' + } + }, + credits: { + enabled: false + }, + colors: ['#F9AA03', '#FFFFFF'], + xAxis: { + categories: [{}] + }, + yAxis: { + title: { + text: null + } + }, + tooltip: { + + }, + series: [{}] +}; \ No newline at end of file diff --git a/plexpy/plexwatch.py b/plexpy/plexwatch.py index 69180882..8cfd5ae4 100644 --- a/plexpy/plexwatch.py +++ b/plexpy/plexwatch.py @@ -792,6 +792,87 @@ class PlexWatch(object): 'series': [series_1_output, series_2_output]} return output + def get_total_plays_per_dayofweek(self, time_range='30'): + myDB = db.DBConnection() + + if not time_range.isdigit(): + time_range = '30' + + query = 'SELECT strftime("%w", datetime(time, "unixepoch", "localtime")) as daynumber, ' \ + 'case cast (strftime("%w", datetime(time, "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, ' \ + 'COUNT(id) as total_plays ' \ + 'from ' + self.get_user_table_name() + ' ' + \ + 'WHERE datetime(stopped, "unixepoch", "localtime") >= ' \ + 'datetime("now", "-' + time_range + ' days", "localtime") ' \ + 'GROUP BY dayofweek ' \ + 'ORDER BY daynumber' + + result = myDB.select(query) + + categories = [] + series_1 = [] + + for item in result: + categories.append(item[1]) + series_1.append(item[2]) + + series_1_output = {'name': 'Total plays', + 'data': series_1} + + output = {'categories': categories, + 'series': [series_1_output]} + return output + + def get_total_plays_per_hourofday(self, time_range='30'): + myDB = db.DBConnection() + + if not time_range.isdigit(): + time_range = '30' + + query = 'select strftime("%H", datetime(time, "unixepoch", "localtime")) as hourofday, ' \ + 'COUNT(id) ' \ + 'FROM ' + self.get_user_table_name() + ' ' + \ + 'WHERE datetime(stopped, "unixepoch", "localtime") >= ' \ + 'datetime("now", "-' + time_range + ' days", "localtime") ' \ + 'GROUP BY hourofday ' \ + 'ORDER BY hourofday' + + result = myDB.select(query) + + hours_list = ['00','01','02','03','04','05', + '06','07','08','09','10','11', + '12','13','14','15','16','17', + '18','19','20','21','22','23'] + + categories = [] + series_1 = [] + + for hour_item in hours_list: + categories.append(hour_item) + series_1_value = 0 + for item in result: + if hour_item == item[0]: + series_1_value = item[1] + break + else: + series_1_value = 0 + + series_1.append(series_1_value) + + series_1_output = {'name': 'Total plays', + 'data': series_1} + + output = {'categories': categories, + 'series': [series_1_output]} + return output + # Taken from: # https://stackoverflow.com/questions/18066269/group-by-and-aggregate-the-values-of-a-list-of-dictionaries-in-python @staticmethod diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 9bdd9d8a..4ac81d08 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -738,3 +738,27 @@ class WebInterface(object): return json.dumps(result) else: logger.warn('Unable to retrieve data.') + + @cherrypy.expose + def get_plays_by_dayofweek(self, time_range='30', **kwargs): + + plex_watch = plexwatch.PlexWatch() + result = plex_watch.get_total_plays_per_dayofweek(time_range) + + if result: + cherrypy.response.headers['Content-type'] = 'application/json' + return json.dumps(result) + else: + logger.warn('Unable to retrieve data.') + + @cherrypy.expose + def get_plays_by_hourofday(self, time_range='30', **kwargs): + + plex_watch = plexwatch.PlexWatch() + result = plex_watch.get_total_plays_per_hourofday(time_range) + + if result: + cherrypy.response.headers['Content-type'] = 'application/json' + return json.dumps(result) + else: + logger.warn('Unable to retrieve data.')