Merge pull request #195 from JonnyWong16/home-stats

Selective hiding home homepage statistics
This commit is contained in:
drzoidberg33 2015-09-19 12:29:08 +02:00
commit 22ed8a3a95
9 changed files with 466 additions and 67 deletions

View file

@ -33,6 +33,33 @@ select.input-sm {
color: #999;
outline: none;
}
select[multiple] {
height: 125px;
margin: 5px 0 5px 0;
color: #fff;
border: 0px solid #444;
background: #555;
padding: 2px 2px;
background-color: #555;
border-radius: 3px;
transition: background-color .3s;
}
select[multiple]:focus {
outline: 0;
outline: thin dotted \9;
color: #555;
background-color: #fff;
transition: background-color .3s;
}
select[multiple]:focus::-webkit-scrollbar-thumb {
background-color: rgba(0,0,0,.15);
}
select[multiple] option {
padding: 6px 10px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
}
img {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;

View file

@ -17,19 +17,19 @@ data[array_index]['rows'] :: Usable parameters
row_id Return the db row id for a metadata item if one exists
== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_movies' or 'popular_movies' or 'last_watched' ==
== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_movies' or 'popular_movies' or 'top_music' or 'popular_music' or 'last_watched' ==
thumb Return the thumb for the media item.
== Only if 'stat_id' is 'top_tv' or 'popular_tv' ==
== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_music' or 'popular_music' ==
grandparent_thumb Returns location of the item's thumbnail. Use with pms_image_proxy.
rating_key Returns the unique identifier for the media item.
title Returns the title for the associated stat.
== Only if 'stat_id' is 'top_tv' or 'top_movies' or 'top_user' or 'top_platform' ==
== Only if 'stat_id' is 'top_tv' or 'top_movies' or 'top_music' or 'top_user' or 'top_platform' ==
total_plays Returns the count for the associated stat.
total_duration Returns the total duration for the associated stat.
== Only of 'stat_id' is 'popular_tv' or 'popular_movies' ==
== Only of 'stat_id' is 'popular_tv' or 'popular_movies' or 'popular_music' ==
users_watched Returns the count for the associated stat.
== Only if 'stat_id' is 'top_user' or 'last_watched' ==
@ -372,6 +372,158 @@ DOCUMENTATION :: END
% endif
</li>
</div>
% elif top_stat['stat_id'] == 'top_music' and top_stat['rows']:
<div class="home-platforms-instance">
<li>
<div class="home-platforms-instance-info">
<div class="home-platforms-instance-name">
<h4>Most Listened to Artist</h4>
</div>
<div class="home-platforms-instance-playcount">
<h4>
<a href="info?item_id=${top_stat['rows'][0]['rating_key']}">
${top_stat['rows'][0]['title']}
</a>
</h4>
% if top_stat['stat_type'] == 'total_plays':
<h3>${top_stat['rows'][0]['total_plays']}</h3>
<p> plays</p>
% else:
${top_stat['rows'][0]['total_duration'] | hd}
% endif
</div>
</div>
<a href="info?item_id=${top_stat['rows'][0]['rating_key']}">
% if top_stat['rows'][0]['grandparent_thumb']:
<div class="home-platforms-instance-poster">
<div class="home-platforms-poster-face" style="background-image: url(pms_image_proxy?img=${top_stat['rows'][0]['grandparent_thumb']}&width=300&height=300&fallback=poster);"></div>
</div>
% else:
<div class="home-platforms-instance-poster">
<div class="home-platforms-poster-face" style="background-image: url(interfaces/default/images/poster.png);"></div>
</div>
% endif
</a>
%if len(top_stat['rows']) > 1:
<div class="home-platforms-instance-list-chevron"><i class="fa fa-chevron-down"></i></div>
<ul class="list-unstyled">
<div class="slider">
<div class="home-platforms-instance-list">
% for row in top_stat['rows']:
%if loop.index > 0:
<li>
<div class="home-platforms-instance-list-info">
<div class="home-platforms-instance-list-name">
<h5>
<a href="info?item_id=${top_stat['rows'][loop.index]['rating_key']}">
${top_stat['rows'][loop.index]['title']}
</a>
</h5>
</div>
<div class="home-platforms-instance-list-playcount">
% if top_stat['stat_type'] == 'total_plays':
<h3>${top_stat['rows'][loop.index]['total_plays']}</h3>
<p> plays</p>
% else:
${top_stat['rows'][loop.index]['total_duration'] | hd}
% endif
</div>
</div>
<a href="info?item_id=${top_stat['rows'][loop.index]['rating_key']}">
% if top_stat['rows'][loop.index]['grandparent_thumb']:
<div class="home-platforms-instance-list-poster">
<div class="home-platforms-list-poster-face" style="background-image: url(pms_image_proxy?img=${top_stat['rows'][loop.index]['grandparent_thumb']}&width=300&height=300&fallback=poster);"></div>
</div>
% else:
<div class="home-platforms-instance-poster2">
<div class="home-platforms-list-poster-face" style="background-image: url(interfaces/default/images/poster.png);"></div>
</div>
% endif
</a>
<div class="home-platforms-instance-list-number">
<h4>${loop.index + 1}</h4>
</div>
</li>
% endif
% endfor
</div>
</div>
</ul>
% endif
</li>
</div>
% elif top_stat['stat_id'] == 'popular_music' and top_stat['rows']:
<div class="home-platforms-instance">
<li>
<div class="home-platforms-instance-info">
<div class="home-platforms-instance-name">
<h4>Most Popular Artist</h4>
</div>
<div class="home-platforms-instance-playcount">
<h4>
<a href="info?item_id=${top_stat['rows'][0]['rating_key']}">
${top_stat['rows'][0]['title']}
</a>
</h4>
<h3>${top_stat['rows'][0]['users_watched']}</h3>
<p> users</p>
</div>
</div>
<a href="info?item_id=${top_stat['rows'][0]['rating_key']}">
% if top_stat['rows'][0]['grandparent_thumb'] != '':
<div class="home-platforms-instance-poster">
<div class="home-platforms-poster-face" style="background-image: url(pms_image_proxy?img=${top_stat['rows'][0]['grandparent_thumb']}&width=300&height=300&fallback=poster);"></div>
</div>
% else:
<div class="home-platforms-instance-poster">
<div class="home-platforms-poster-face" style="background-image: url(interfaces/default/images/poster.png);"></div>
</div>
% endif
</a>
%if len(top_stat['rows']) > 1:
<div class="home-platforms-instance-list-chevron"><i class="fa fa-chevron-down"></i></div>
<ul class="list-unstyled">
<div class="slider">
<div class="home-platforms-instance-list">
% for row in top_stat['rows']:
%if loop.index > 0:
<li>
<div class="home-platforms-instance-list-info">
<div class="home-platforms-instance-list-name">
<h5>
<a href="info?item_id=${top_stat['rows'][loop.index]['rating_key']}">
${top_stat['rows'][loop.index]['title']}
</a>
</h5>
</div>
<div class="home-platforms-instance-list-playcount">
<h3>${top_stat['rows'][loop.index]['users_watched']}</h3>
<p> users</p>
</div>
</div>
<a href="info?item_id=${top_stat['rows'][loop.index]['rating_key']}">
% if top_stat['rows'][loop.index]['grandparent_thumb']:
<div class="home-platforms-instance-list-poster">
<div class="home-platforms-list-poster-face" style="background-image: url(pms_image_proxy?img=${top_stat['rows'][loop.index]['grandparent_thumb']}&width=300&height=300&fallback=poster);"></div>
</div>
% else:
<div class="home-platforms-instance-poster2">
<div class="home-platforms-list-poster-face" style="background-image: url(interfaces/default/images/poster.png);"></div>
</div>
% endif
</a>
<div class="home-platforms-instance-list-number">
<h4>${loop.index + 1}</h4>
</div>
</li>
% endif
% endfor
</div>
</div>
</ul>
% endif
</li>
</div>
% elif top_stat['stat_id'] == 'top_users' and top_stat['rows']:
<div class="home-platforms-instance">
<li>

View file

@ -16,6 +16,7 @@
</div>
</div>
</div>
% if config['home_stats_cards'] > 'watch_statistics':
<div class="row">
<div class="col-md-12">
<div class="padded-header">
@ -27,6 +28,8 @@
</div>
</div>
</div>
% endif
% if config['home_library_cards'] > 'library_statistics':
<div class="row">
<div class="col-md-12">
<div class="padded-header" id="library-statistics-header">
@ -38,6 +41,7 @@
</div>
</div>
</div>
% endif
<div class='row'>
<div class="col-md-12">
<div class="padded-header">
@ -82,15 +86,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);
}
@ -168,10 +169,7 @@
}
});
getHomeStats(${config['home_stats_length']},
${config['home_stats_type']},
${config['home_stats_count']},
${config['notify_watched_percent']});
getHomeStats();
getLibraryStatsHeader();
getLibraryStats();

View file

@ -73,6 +73,6 @@ DOCUMENTATION :: END
% endfor
</ul>
% else:
<div class="text-muted">Unable to retrieve data from database. Please check your <a href="settings">settings</a>.
<div class="text-muted">Unable to retrieve data from server. Please check your <a href="settings">settings</a>.
</div><br>
% endif

View file

@ -34,14 +34,15 @@ available_notification_agents = notifiers.available_notification_agents()
<div class="col-md-4">
<ul class="nav-settings list-unstyled" role="tablist">
<li role="presentation" class="active"><a href="#tabs-1" aria-controls="tabs-1" role="tab" data-toggle="tab">General</a></li>
<li role="presentation"><a href="#tabs-2" aria-controls="tabs-2" role="tab" data-toggle="tab">Web Interface</a></li>
<li role="presentation"><a href="#tabs-3" aria-controls="tabs-3" role="tab" data-toggle="tab">Access Control</a></li>
<li role="presentation"><a href="#tabs-4" aria-controls="tabs-4" role="tab" data-toggle="tab">Plex Media Server</a></li>
<li role="presentation"><a href="#tabs-5" aria-controls="tabs-5" role="tab" data-toggle="tab">Plex.tv Account</a></li>
<li role="presentation"><a href="#tabs-6" aria-controls="tabs-6" role="tab" data-toggle="tab">Extra Settings</a></li>
<li role="presentation"><a href="#tabs-7" aria-controls="tabs-7" role="tab" data-toggle="tab">Monitoring</a></li>
<li role="presentation"><a href="#tabs-8" aria-controls="tabs-8" role="tab" data-toggle="tab">Notifications</a></li>
<li role="presentation"><a href="#tabs-9" aria-controls="tabs-9" role="tab" data-toggle="tab">Notification Agents</a></li>
<li role="presentation"><a href="#tabs-2" aria-controls="tabs-2" role="tab" data-toggle="tab">Homepage Statistics</a></li>
<li role="presentation"><a href="#tabs-3" aria-controls="tabs-3" role="tab" data-toggle="tab">Web Interface</a></li>
<li role="presentation"><a href="#tabs-4" aria-controls="tabs-4" role="tab" data-toggle="tab">Access Control</a></li>
<li role="presentation"><a href="#tabs-5" aria-controls="tabs-5" role="tab" data-toggle="tab">Plex Media Server</a></li>
<li role="presentation"><a href="#tabs-6" aria-controls="tabs-6" role="tab" data-toggle="tab">Plex.tv Account</a></li>
<li role="presentation"><a href="#tabs-7" aria-controls="tabs-7" role="tab" data-toggle="tab">Extra Settings</a></li>
<li role="presentation"><a href="#tabs-8" aria-controls="tabs-8" role="tab" data-toggle="tab">Monitoring</a></li>
<li role="presentation"><a href="#tabs-9" aria-controls="tabs-9" role="tab" data-toggle="tab">Notifications</a></li>
<li role="presentation"><a href="#tabs-10" aria-controls="tabs-10" role="tab" data-toggle="tab">Notification Agents</a></li>
</ul>
</div>
<div class="col-md-8">
@ -83,10 +84,33 @@ available_notification_agents = notifiers.available_notification_agents()
</div>
<p class="help-block">Set your preferred time format. <a href="javascript:void(0)" data-target="#dateTimeOptionsModal" data-toggle="modal">Click here</a> to see the parameter list.</p>
</div>
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-2">
<div class="padded-header">
<h3>Homepage Statistics</h3>
<h3>Watch Statistics</h3>
</div>
<div class="form-group">
<label for="home_stats_cards">Cards</label>
<div class="row">
<div class="col-md-6">
<select multiple class="form-control" id="home_stats_cards" name="home_stats_cards" data-parsley-trigger="change">
<option id="card-watch_statistics" value="watch_statistics" class="hidden" selected>Watch Statistics</option>
<option id="card-top_tv" value="top_tv">Most Watched TV</option>
<option id="card-popular_tv" value="popular_tv">Most Popular TV</option>
<option id="card-top_movies" value="top_movies">Most Watched Movie</option>
<option id="card-popular_movies" value="popular_movies">Most Popular Movie</option>
<option id="card-top_music" value="top_music">Most Listened Music</option>
<option id="card-popular_music" value="popular_music">Most Popular Music</option>
<option id="card-top_users" value="top_users">Most Active User</option>
<option id="card-top_platforms" value="top_platforms">Most Active Platform</option>
<option id="card-last_watched" value="last_watched">Last Watched</option>
</select>
</div>
</div>
<p class="help-block">Select the cards to show in the watch statistics on the home page. Select none to disable.</p>
</div>
<div class="form-group">
<label for="home_stats_length">Time Frame</label>
<div class="row">
@ -95,7 +119,7 @@ available_notification_agents = notifiers.available_notification_agents()
</div>
<div id="home_stats_length_error" class="alert alert-danger settings-alert" role="alert"></div>
</div>
<p class="help-block">Specify the number of days for the statistics on the home page. Default is 30 days.</p>
<p class="help-block">Specify the number of days for the watch statistics on the home page. Default is 30 days.</p>
</div>
<div class="form-group">
<label for="home_stats_count">Top Lists</label>
@ -105,7 +129,7 @@ available_notification_agents = notifiers.available_notification_agents()
</div>
<div id="home_stats_count_error" class="alert alert-danger settings-alert" role="alert"></div>
</div>
<p class="help-block">Specify the number of items to show in the top lists for the statistics on the home page. Max is 10 items, default is 5 items, 0 to disable.</p>
<p class="help-block">Specify the number of items to show in the top lists for the watch statistics on the home page. Max is 10 items, default is 5 items, 0 to disable.</p>
</div>
<div class="checkbox">
<label>
@ -113,9 +137,26 @@ available_notification_agents = notifiers.available_notification_agents()
</label>
<p class="help-block">Use play duration instead of play count to generate statistics.</p>
</div>
<div class="padded-header">
<h3>Library Statistics</h3>
</div>
<div class="form-group">
<label for="home_library_cards">Cards</label>
<div class="row">
<div class="col-md-6">
<select multiple class="form-control" id="home_library_cards" name="home_library_cards" data-parsley-trigger="change">
<option id="card-library_statistics" value="library_statistics" class="hidden" selected>Library Statistics</option>
</select>
</div>
</div>
<p class="help-block">Select the cards to show in the library statistics on the home page. Select none to disable.</p>
</div>
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-2">
<div role="tabpanel" class="tab-pane" id="tabs-3">
<div class="padded-header">
<h3>Web Interface</h3>
</div>
@ -164,7 +205,7 @@ available_notification_agents = notifiers.available_notification_agents()
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-3">
<div role="tabpanel" class="tab-pane" id="tabs-4">
<div class="padded-header">
<h3>Authentication</h3>
@ -216,7 +257,7 @@ available_notification_agents = notifiers.available_notification_agents()
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-4">
<div role="tabpanel" class="tab-pane" id="tabs-5">
<div class="padded-header">
<h3>Plex Media Server</h3>
@ -272,7 +313,7 @@ available_notification_agents = notifiers.available_notification_agents()
<input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully">
</div>
<div role="tabpanel" class="tab-pane" id="tabs-5">
<div role="tabpanel" class="tab-pane" id="tabs-6">
<div class="padded-header">
<h3>Plex.tv Authentication</h3>
@ -315,7 +356,7 @@ available_notification_agents = notifiers.available_notification_agents()
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-6">
<div role="tabpanel" class="tab-pane" id="tabs-7">
<div class="padded-header">
<h3>Extra Settings</h3>
</div>
@ -334,7 +375,7 @@ available_notification_agents = notifiers.available_notification_agents()
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-7">
<div role="tabpanel" class="tab-pane" id="tabs-8">
<div class="padded-header">
<h3>Monitoring Settings</h3>
@ -416,7 +457,7 @@ available_notification_agents = notifiers.available_notification_agents()
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-8">
<div role="tabpanel" class="tab-pane" id="tabs-9">
<div class="padded-header">
<h3>Global Notification Toggles</h3>
@ -566,7 +607,7 @@ available_notification_agents = notifiers.available_notification_agents()
<p><input type="button" class="btn btn-bright save-button" value="Save" data-success="Changes saved successfully"></p>
</div>
<div role="tabpanel" class="tab-pane" id="tabs-9">
<div role="tabpanel" class="tab-pane" id="tabs-10">
<div class="padded-header">
<h3>Notification Agents</h3>
@ -1178,6 +1219,47 @@ $(document).ready(function() {
}
var accordion = new Accordion($('#accordion'), false);
var cards = "${config['home_stats_cards']}".split(/[\s,]+/);
cards.forEach(function (item) {
$('#card-'+item).prop('selected', !$(this).prop('selected'));
});
$('#home_stats_cards').on('mousedown', function(e) {
e.preventDefault();
var scroll = this.scrollTop;
e.target.selected = !e.target.selected;
this.scrollTop = scroll;
}).on('mousemove', function(e) {
e.preventDefault()
});
$.ajax({
url: 'get_server_children',
data: { },
async: true,
complete: function (xhr, status) {
server_children_info = $.parseJSON(xhr.responseText);
libraries_list = server_children_info.libraries_list;
for (var i in libraries_list) {
title = libraries_list[i].title;
key = libraries_list[i].key;
$('#home_library_cards').append('<option id="card-' + key + '" value="' + key + '">' + title + '</option>')
}
var cards = "${config['home_library_cards']}".split(/[\s,]+/);
cards.forEach(function (item) {
$('#card-'+item).prop('selected', !$(this).prop('selected'));
});
}
});
$('#home_library_cards').on('mousedown', function(e) {
e.preventDefault();
var scroll = this.scrollTop;
e.target.selected = !e.target.selected;
this.scrollTop = scroll;
}).on('mousemove', function(e) {
e.preventDefault()
});
});
</script>
</%def>

View file

@ -82,9 +82,11 @@ _CONFIG_DEFINITIONS = {
'GROWL_ON_RESUME': (int, 'Growl', 0),
'GROWL_ON_BUFFER': (int, 'Growl', 0),
'GROWL_ON_WATCHED': (int, 'Growl', 0),
'HOME_LIBRARY_CARDS': (str, 'General', 'library_statistics_first'),
'HOME_STATS_LENGTH': (int, 'General', 30),
'HOME_STATS_TYPE': (int, 'General', 0),
'HOME_STATS_COUNT': (int, 'General', 5),
'HOME_STATS_CARDS': (str, 'General', 'watch_statistics, top_tv, popular_tv, top_movies, popular_movies, top_music, popular_music, top_users, top_platforms, last_watched'),
'HTTPS_CERT': (str, 'General', ''),
'HTTPS_KEY': (str, 'General', ''),
'HTTP_HOST': (str, 'General', '0.0.0.0'),

View file

@ -131,22 +131,14 @@ 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"]
home_stats = []
for stat in stats_queries:
for stat in stats_cards:
if 'top_tv' in stat:
top_tv = []
try:
@ -166,7 +158,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 +194,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 +205,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 +218,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 +250,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 +286,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 +297,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 +311,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': '',
@ -323,6 +323,98 @@ class DataFactory(object):
home_stats.append({'stat_id': stat,
'rows': popular_movies})
elif 'top_music' in stat:
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)
result = monitor_db.select(query)
except:
logger.warn("Unable to execute database query.")
return None
for item in result:
row = {'title': item[1],
'total_plays': item[2],
'total_duration': item[3],
'users_watched': '',
'rating_key': item[4],
'last_play': item[5],
'grandparent_thumb': item[6],
'thumb': '',
'user': '',
'friendly_name': '',
'platform_type': '',
'platform': '',
'row_id': item[0]
}
top_music.append(row)
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'rows': top_music})
elif 'popular_music' in stat:
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 ' \
'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.")
return None
for item in result:
row = {'title': item[1],
'users_watched': item[2],
'rating_key': item[3],
'last_play': item[4],
'total_plays': item[5],
'grandparent_thumb': item[7],
'thumb': '',
'user': '',
'friendly_name': '',
'platform_type': '',
'platform': '',
'row_id': item[0]
}
popular_music.append(row)
home_stats.append({'stat_id': stat,
'rows': popular_music})
elif 'top_users' in stat:
top_users = []
try:
@ -343,7 +435,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 +483,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 +539,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.")

View file

@ -1208,7 +1208,7 @@ class PmsConnect(object):
'title': helpers.get_xml_attr(xml_head[0], 'title1'),
'libraries_list': libraries_list
}
return output
"""
@ -1270,11 +1270,11 @@ class PmsConnect(object):
return output
"""
Return processed and validated server statistics.
Return processed and validated library statistics.
Output: array
"""
def get_library_stats(self):
def get_library_stats(self, library_cards=''):
server_libraries = self.get_server_children()
server_library_stats = []
@ -1285,7 +1285,10 @@ class PmsConnect(object):
for library in libraries_list:
library_type = library['type']
section_key = library['key']
library_list = self.get_library_children(library_type, section_key)
if section_key in library_cards:
library_list = self.get_library_children(library_type, section_key)
else:
continue
if library_list['library_count'] != '0':
library_stats = {'title': library['title'],

View file

@ -66,10 +66,9 @@ 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,
"home_library_cards": plexpy.CONFIG.HOME_LIBRARY_CARDS,
"pms_identifier": plexpy.CONFIG.PMS_IDENTIFIER
}
return serve_template(templatename="index.html", title="Home", config=config)
@ -122,11 +121,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.split(', ')
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)
@ -134,7 +141,21 @@ class WebInterface(object):
@cherrypy.expose
def library_stats(self, **kwargs):
pms_connect = pmsconnect.PmsConnect()
stats_data = pms_connect.get_library_stats()
library_cards = plexpy.CONFIG.HOME_LIBRARY_CARDS.split(', ')
if library_cards == ['library_statistics_first']:
library_cards = ['library_statistics']
server_children = pms_connect.get_server_children()
server_libraries = server_children['libraries_list']
for library in server_libraries:
library_cards.append(library['key'])
plexpy.CONFIG.HOME_LIBRARY_CARDS = ', '.join(library_cards)
plexpy.CONFIG.write()
stats_data = pms_connect.get_library_stats(library_cards=library_cards)
return serve_template(templatename="library_stats.html", title="Library Stats", data=stats_data)
@ -472,6 +493,8 @@ class WebInterface(object):
"home_stats_length": plexpy.CONFIG.HOME_STATS_LENGTH,
"home_stats_type": checked(plexpy.CONFIG.HOME_STATS_TYPE),
"home_stats_count": plexpy.CONFIG.HOME_STATS_COUNT,
"home_stats_cards": plexpy.CONFIG.HOME_STATS_CARDS,
"home_library_cards": plexpy.CONFIG.HOME_LIBRARY_CARDS,
"buffer_threshold": plexpy.CONFIG.BUFFER_THRESHOLD,
"buffer_wait": plexpy.CONFIG.BUFFER_WAIT
}
@ -524,6 +547,14 @@ class WebInterface(object):
if kwargs['pms_ip'] != plexpy.CONFIG.PMS_IP:
refresh_users = True
if 'home_stats_cards' in kwargs:
if kwargs['home_stats_cards'] != 'watch_statistics':
kwargs['home_stats_cards'] = ', '.join(kwargs['home_stats_cards'])
if 'home_library_cards' in kwargs:
if kwargs['home_library_cards'] != 'library_statistics':
kwargs['home_library_cards'] = ', '.join(kwargs['home_library_cards'])
plexpy.CONFIG.process_kwargs(kwargs)
# Write the config
@ -1121,6 +1152,18 @@ class WebInterface(object):
else:
logger.warn('Unable to retrieve data.')
@cherrypy.expose
def get_server_children(self, **kwargs):
pms_connect = pmsconnect.PmsConnect()
result = pms_connect.get_server_children()
if result:
cherrypy.response.headers['Content-type'] = 'application/json'
return json.dumps(result)
else:
logger.warn('Unable to retrieve data.')
@cherrypy.expose
def get_activity(self, **kwargs):