Change months in Plays by Month graph

Add an option on the Graphs > Play Totals > Plays by Month page to change the amount of months the graph shows data for.
This commit is contained in:
Peter Kums 2017-02-26 23:53:24 +01:00
parent 3742f33d08
commit b9a22461c1
4 changed files with 60 additions and 18 deletions

View file

@ -2833,6 +2833,14 @@ div[id^='media_info_child'] div[id^='media_info_child'] div.dataTables_scrollHea
width: 75px; width: 75px;
height: 34px; height: 34px;
} }
#months-selection label {
margin-bottom: 0;
}
#graph-months {
margin: 0;
width: 75px;
height: 34px;
}
.card-sortable { .card-sortable {
height: 36px; height: 36px;
padding: 0 20px 0 0; padding: 0 20px 0 0;

View file

@ -42,6 +42,11 @@
<input type="number" name="graph-days" id="graph-days" value="${config['graph_days']}" min="1" /> days <input type="number" name="graph-days" id="graph-days" value="${config['graph_days']}" min="1" /> days
</label> </label>
</div> </div>
<div class="btn-group" id="months-selection">
<label>
<input type="number" name="graph-months" id="graph-months" value="${config['graph_months']}" min="1" /> months
</label>
</div>
</div> </div>
</div> </div>
<div class='table-card-back'> <div class='table-card-back'>
@ -226,7 +231,7 @@
% endif % endif
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<h4><i class="fa fa-calendar"></i> Plays by Month <small>Last 12 months</small></h4> <h4><i class="fa fa-calendar"></i> Plays by Month <small>Last <span class="months">12</span> months</small></h4>
<p class="help-block"> <p class="help-block">
The combined total of tv, movies, and music by month. The combined total of tv, movies, and music by month.
</p> </p>
@ -318,9 +323,11 @@
// Initial values for graph from config // Initial values for graph from config
var yaxis = "${config['graph_type']}"; var yaxis = "${config['graph_type']}";
var current_range = ${config['graph_days']}; var current_range = ${config['graph_days']};
var current_month_range = ${config['graph_months']};
var current_tab = "${'#' + config['graph_tab']}"; var current_tab = "${'#' + config['graph_tab']}";
$('.days').html(current_range); $('.days').html(current_range);
$('.months').html(current_month_range);
// Load user ids and names (for the selector) // Load user ids and names (for the selector)
$.ajax({ $.ajax({
@ -352,6 +359,7 @@
function loadGraphsTab1(time_range, yaxis) { function loadGraphsTab1(time_range, yaxis) {
$('#days-selection').show(); $('#days-selection').show();
$('#months-selection').hide();
setGraphFormat(yaxis); setGraphFormat(yaxis);
@ -442,6 +450,7 @@
function loadGraphsTab2(time_range, yaxis) { function loadGraphsTab2(time_range, yaxis) {
$('#days-selection').show(); $('#days-selection').show();
$('#months-selection').hide();
setGraphFormat(yaxis); setGraphFormat(yaxis);
@ -525,15 +534,16 @@
}); });
} }
function loadGraphsTab3(yaxis) { function loadGraphsTab3(time_range, yaxis) {
$('#days-selection').hide(); $('#days-selection').hide();
$('#months-selection').show();
setGraphFormat(yaxis); setGraphFormat(yaxis);
$.ajax({ $.ajax({
url: "get_plays_per_month", url: "get_plays_per_month",
type: 'get', type: 'get',
data: { y_axis: yaxis, user_id: selected_user_id }, data: { time_range: time_range, y_axis: yaxis, user_id: selected_user_id },
dataType: "json", dataType: "json",
success: function(data) { success: function(data) {
if (yaxis === 'duration') { dataSecondsToHours(data); } if (yaxis === 'duration') { dataSecondsToHours(data); }
@ -549,7 +559,7 @@
// Set initial state // Set initial state
if (current_tab == '#tabs-1') { loadGraphsTab1(current_range, yaxis); } if (current_tab == '#tabs-1') { loadGraphsTab1(current_range, yaxis); }
if (current_tab == '#tabs-2') { loadGraphsTab2(current_range, yaxis); } if (current_tab == '#tabs-2') { loadGraphsTab2(current_range, yaxis); }
if (current_tab == '#tabs-3') { loadGraphsTab3(yaxis); } if (current_tab == '#tabs-3') { loadGraphsTab3(current_month_range, yaxis); }
// Tab1 opened // Tab1 opened
$('#graph-tabs a[href="#tabs-1"]').on('shown.bs.tab', function (e) { $('#graph-tabs a[href="#tabs-1"]').on('shown.bs.tab', function (e) {
@ -579,7 +589,7 @@
$('#graph-tabs a[href="#tabs-3"]').on('shown.bs.tab', function (e) { $('#graph-tabs a[href="#tabs-3"]').on('shown.bs.tab', function (e) {
e.preventDefault(); e.preventDefault();
current_tab = $(this).attr('href'); current_tab = $(this).attr('href');
loadGraphsTab3(yaxis); loadGraphsTab3(current_month_range, yaxis);
$.ajax({ $.ajax({
url: 'set_graph_config', url: 'set_graph_config',
data: { graph_tab: current_tab.replace('#','') }, data: { graph_tab: current_tab.replace('#','') },
@ -603,13 +613,29 @@
async: true async: true
}); });
}); });
// Month range changed
$('#graph-months').on('change', function() {
current_month_range = $(this).val();
if (current_month_range < 1) {
$(this).val(12);
current_month_range = 12;
}
if (current_tab == '#tabs-3') { loadGraphsTab3(current_month_range, yaxis); }
$('.months').html(current_month_range);
$.ajax({
url: 'set_graph_config',
data: { graph_months: current_month_range},
async: true
});
});
// User changed // User changed
$('#graph-user').on('change', function() { $('#graph-user').on('change', function() {
selected_user_id = $(this).val() || null; selected_user_id = $(this).val() || null;
if (current_tab == '#tabs-1') { loadGraphsTab1(current_range, yaxis); } if (current_tab == '#tabs-1') { loadGraphsTab1(current_range, yaxis); }
if (current_tab == '#tabs-2') { loadGraphsTab2(current_range, yaxis); } if (current_tab == '#tabs-2') { loadGraphsTab2(current_range, yaxis); }
if (current_tab == '#tabs-3') { loadGraphsTab3(yaxis); } if (current_tab == '#tabs-3') { loadGraphsTab3(current_month_range, yaxis); }
}); });
// Y-axis changed // Y-axis changed
@ -617,7 +643,7 @@
yaxis = $('input[name=yaxis-options]:checked', '#yaxis-selection').val(); yaxis = $('input[name=yaxis-options]:checked', '#yaxis-selection').val();
if (current_tab == '#tabs-1') { loadGraphsTab1(current_range, yaxis); } if (current_tab == '#tabs-1') { loadGraphsTab1(current_range, yaxis); }
if (current_tab == '#tabs-2') { loadGraphsTab2(current_range, yaxis); } if (current_tab == '#tabs-2') { loadGraphsTab2(current_range, yaxis); }
if (current_tab == '#tabs-3') { loadGraphsTab3(yaxis); } if (current_tab == '#tabs-3') { loadGraphsTab3(current_month_range,yaxis); }
$.ajax({ $.ajax({
url: 'set_graph_config', url: 'set_graph_config',
data: { graph_type: yaxis}, data: { graph_type: yaxis},

View file

@ -291,8 +291,11 @@ class Graphs(object):
'series': [series_1_output, series_2_output, series_3_output]} 'series': [series_1_output, series_2_output, series_3_output]}
return output return output
def get_total_plays_per_month(self, y_axis='plays', user_id=None): def get_total_plays_per_month(self, time_range='12', y_axis='plays', user_id=None):
import time as time import time as time
if not time_range.isdigit():
time_range = '12'
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
@ -309,9 +312,9 @@ class Graphs(object):
'SUM(CASE WHEN media_type = "movie" THEN 1 ELSE 0 END) AS movie_count, ' \ 'SUM(CASE WHEN media_type = "movie" THEN 1 ELSE 0 END) AS movie_count, ' \
'SUM(CASE WHEN media_type = "track" THEN 1 ELSE 0 END) AS music_count ' \ 'SUM(CASE WHEN media_type = "track" THEN 1 ELSE 0 END) AS music_count ' \
'FROM session_history ' \ 'FROM session_history ' \
'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-12 months", "localtime") %s' \ 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s months", "localtime") %s' \
'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) ' \ 'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) ' \
'ORDER BY datestring DESC LIMIT 12' % (user_cond) 'ORDER BY datestring DESC LIMIT %s' % (time_range, user_cond, time_range)
result = monitor_db.select(query) result = monitor_db.select(query)
else: else:
@ -323,18 +326,20 @@ class Graphs(object):
'SUM(CASE WHEN media_type = "track" AND stopped > 0 THEN (stopped - started) ' \ 'SUM(CASE WHEN media_type = "track" AND stopped > 0 THEN (stopped - started) ' \
' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count ' \ ' - (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) AS music_count ' \
'FROM session_history ' \ 'FROM session_history ' \
'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-12 months", "localtime") %s' \ 'WHERE datetime(started, "unixepoch", "localtime") >= datetime("now", "-%s months", "localtime") %s' \
'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) ' \ 'GROUP BY strftime("%%Y-%%m", datetime(started, "unixepoch", "localtime")) ' \
'ORDER BY datestring DESC LIMIT 12' % (user_cond) 'ORDER BY datestring DESC LIMIT %s' % (time_range, user_cond, time_range)
result = monitor_db.select(query) result = monitor_db.select(query)
except Exception as e: except Exception as e:
logger.warn(u"PlexPy Graphs :: Unable to execute database query for get_total_plays_per_month: %s." % e) logger.warn(u"PlexPy Graphs :: Unable to execute database query for get_total_plays_per_month: %s." % e)
return None return None
# create our date range as some months may not have any data # create our date range as some months may not have any data
# but we still want to display them # but we still want to display them
x = 12 time_range = int(time_range)
x = time_range
base = time.localtime() base = time.localtime()
month_range = [time.localtime( month_range = [time.localtime(
time.mktime((base.tm_year, base.tm_mon - n, 1, 0, 0, 0, 0, 0, 0))) for n in range(x)] time.mktime((base.tm_year, base.tm_mon - n, 1, 0, 0, 0, 0, 0, 0))) for n in range(x)]

View file

@ -1747,6 +1747,7 @@ class WebInterface(object):
config = { config = {
"graph_type": plexpy.CONFIG.GRAPH_TYPE, "graph_type": plexpy.CONFIG.GRAPH_TYPE,
"graph_days": plexpy.CONFIG.GRAPH_DAYS, "graph_days": plexpy.CONFIG.GRAPH_DAYS,
"graph_months": 12,
"graph_tab": plexpy.CONFIG.GRAPH_TAB, "graph_tab": plexpy.CONFIG.GRAPH_TAB,
"music_logging_enable": plexpy.CONFIG.MUSIC_LOGGING_ENABLE "music_logging_enable": plexpy.CONFIG.MUSIC_LOGGING_ENABLE
} }
@ -1755,13 +1756,15 @@ class WebInterface(object):
@cherrypy.expose @cherrypy.expose
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
def set_graph_config(self, graph_type=None, graph_days=None, graph_tab=None, **kwargs): def set_graph_config(self, graph_type=None, graph_days=None, graph_months=None, graph_tab=None, **kwargs):
if graph_type: if graph_type:
plexpy.CONFIG.__setattr__('GRAPH_TYPE', graph_type) plexpy.CONFIG.__setattr__('GRAPH_TYPE', graph_type)
plexpy.CONFIG.write() plexpy.CONFIG.write()
if graph_days: if graph_days:
plexpy.CONFIG.__setattr__('GRAPH_DAYS', graph_days) plexpy.CONFIG.__setattr__('GRAPH_DAYS', graph_days)
plexpy.CONFIG.write() plexpy.CONFIG.write()
if graph_months:
pass
if graph_tab: if graph_tab:
plexpy.CONFIG.__setattr__('GRAPH_TAB', graph_tab) plexpy.CONFIG.__setattr__('GRAPH_TAB', graph_tab)
plexpy.CONFIG.write() plexpy.CONFIG.write()
@ -1908,7 +1911,7 @@ class WebInterface(object):
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth() @requireAuth()
@addtoapi() @addtoapi()
def get_plays_per_month(self, y_axis='plays', user_id=None, **kwargs): def get_plays_per_month(self, time_range='12', y_axis='plays', user_id=None, **kwargs):
""" Get graph data by month. """ Get graph data by month.
``` ```
@ -1916,7 +1919,7 @@ class WebInterface(object):
None None
Optional parameters: Optional parameters:
time_range (str): The number of days of data to return time_range (str): The number of months of data to return
y_axis (str): "plays" or "duration" y_axis (str): "plays" or "duration"
user_id (str): The user id to filter the data user_id (str): The user id to filter the data
@ -1933,7 +1936,7 @@ class WebInterface(object):
``` ```
""" """
graph = graphs.Graphs() graph = graphs.Graphs()
result = graph.get_total_plays_per_month(y_axis=y_axis, user_id=user_id) result = graph.get_total_plays_per_month(time_range=time_range, y_axis=y_axis, user_id=user_id)
if result: if result:
return result return result