mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-14 09:12:58 -07:00
Clean up watch statistics duration database queries
This commit is contained in:
parent
7b9210a5fc
commit
92a868c3c6
2 changed files with 60 additions and 63 deletions
|
@ -51,16 +51,17 @@ DOCUMENTATION :: END
|
||||||
from plexpy import helpers
|
from plexpy import helpers
|
||||||
|
|
||||||
# Human readable duration
|
# Human readable duration
|
||||||
def hd(minutes):
|
def hd(seconds):
|
||||||
if int(minutes) > 60:
|
minutes = helpers.cast_to_float(seconds) / 60
|
||||||
hours = int(helpers.cast_to_float(minutes) / 60)
|
if minutes > 60:
|
||||||
minutes = int(helpers.cast_to_float(minutes) % 60 )
|
hours = int(minutes / 60)
|
||||||
|
minutes = int(minutes % 60)
|
||||||
if minutes > 0:
|
if minutes > 0:
|
||||||
return "<h3>" + str(hours) + "</h3><p>hrs</p><h3>" + str(minutes) + "</h3><p>mins</p>"
|
return "<h3>" + str(hours) + "</h3><p>hrs</p><h3>" + str(minutes) + "</h3><p>mins</p>"
|
||||||
else:
|
else:
|
||||||
return "<h3>" + str(hours) + "</h3><p>hrs</p>"
|
return "<h3>" + str(hours) + "</h3><p>hrs</p>"
|
||||||
else:
|
else:
|
||||||
return "<h3>" + minutes + "</h3><p>mins</p>"
|
return "<h3>" + str(int(minutes)) + "</h3><p>mins</p>"
|
||||||
%>
|
%>
|
||||||
|
|
||||||
% if data:
|
% if data:
|
||||||
|
|
|
@ -153,10 +153,9 @@ class DataFactory(object):
|
||||||
query = 'SELECT session_history_metadata.id, ' \
|
query = 'SELECT session_history_metadata.id, ' \
|
||||||
'session_history_metadata.grandparent_title, ' \
|
'session_history_metadata.grandparent_title, ' \
|
||||||
'COUNT(session_history_metadata.grandparent_title) as total_plays, ' \
|
'COUNT(session_history_metadata.grandparent_title) as total_plays, ' \
|
||||||
'cast(round(SUM(round((julianday(datetime(session_history.stopped, "unixepoch", "localtime")) - ' \
|
'SUM(case when session_history.stopped > 0 ' \
|
||||||
'julianday(datetime(session_history.started, "unixepoch", "localtime"))) * 86400) - ' \
|
'then (session_history.stopped - session_history.started) ' \
|
||||||
'(CASE WHEN session_history.paused_counter IS NULL THEN 0 ' \
|
' - (case when paused_counter is NULL then 0 else paused_counter end) else 0 end) as total_duration, ' \
|
||||||
'ELSE session_history.paused_counter END))/60) as integer) as total_duration,' \
|
|
||||||
'session_history_metadata.grandparent_rating_key, ' \
|
'session_history_metadata.grandparent_rating_key, ' \
|
||||||
'MAX(session_history.started) as last_watch,' \
|
'MAX(session_history.started) as last_watch,' \
|
||||||
'session_history_metadata.grandparent_thumb ' \
|
'session_history_metadata.grandparent_thumb ' \
|
||||||
|
@ -193,52 +192,6 @@ class DataFactory(object):
|
||||||
'stat_type': sort_type,
|
'stat_type': sort_type,
|
||||||
'rows': top_tv})
|
'rows': top_tv})
|
||||||
|
|
||||||
elif 'top_movies' in stat:
|
|
||||||
top_movies = []
|
|
||||||
try:
|
|
||||||
query = 'SELECT session_history_metadata.id, ' \
|
|
||||||
'session_history_metadata.full_title, ' \
|
|
||||||
'COUNT(session_history_metadata.full_title) as total_plays, ' \
|
|
||||||
'cast(round(SUM(round((julianday(datetime(session_history.stopped, "unixepoch", "localtime")) - ' \
|
|
||||||
'julianday(datetime(session_history.started, "unixepoch", "localtime"))) * 86400) - ' \
|
|
||||||
'(CASE WHEN session_history.paused_counter IS NULL THEN 0 ' \
|
|
||||||
'ELSE session_history.paused_counter END))/60) as integer) 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, stat_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': '',
|
|
||||||
'thumb': item[6],
|
|
||||||
'user': '',
|
|
||||||
'friendly_name': '',
|
|
||||||
'platform_type': '',
|
|
||||||
'platform': '',
|
|
||||||
'row_id': item[0]
|
|
||||||
}
|
|
||||||
top_movies.append(row)
|
|
||||||
|
|
||||||
home_stats.append({'stat_id': stat,
|
|
||||||
'stat_type': sort_type,
|
|
||||||
'rows': top_movies})
|
|
||||||
|
|
||||||
elif 'popular_tv' in stat:
|
elif 'popular_tv' in stat:
|
||||||
popular_tv = []
|
popular_tv = []
|
||||||
try:
|
try:
|
||||||
|
@ -281,6 +234,51 @@ class DataFactory(object):
|
||||||
home_stats.append({'stat_id': stat,
|
home_stats.append({'stat_id': stat,
|
||||||
'rows': popular_tv})
|
'rows': popular_tv})
|
||||||
|
|
||||||
|
elif 'top_movies' in stat:
|
||||||
|
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 paused_counter is NULL then 0 else 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, stat_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': '',
|
||||||
|
'thumb': item[6],
|
||||||
|
'user': '',
|
||||||
|
'friendly_name': '',
|
||||||
|
'platform_type': '',
|
||||||
|
'platform': '',
|
||||||
|
'row_id': item[0]
|
||||||
|
}
|
||||||
|
top_movies.append(row)
|
||||||
|
|
||||||
|
home_stats.append({'stat_id': stat,
|
||||||
|
'stat_type': sort_type,
|
||||||
|
'rows': top_movies})
|
||||||
|
|
||||||
elif 'popular_movies' in stat:
|
elif 'popular_movies' in stat:
|
||||||
popular_movies = []
|
popular_movies = []
|
||||||
try:
|
try:
|
||||||
|
@ -330,10 +328,9 @@ class DataFactory(object):
|
||||||
'(case when users.friendly_name is null then session_history.user else ' \
|
'(case when users.friendly_name is null then session_history.user else ' \
|
||||||
'users.friendly_name end) as friendly_name,' \
|
'users.friendly_name end) as friendly_name,' \
|
||||||
'COUNT(session_history.id) as total_plays, ' \
|
'COUNT(session_history.id) as total_plays, ' \
|
||||||
'cast(round(SUM(round((julianday(datetime(session_history.stopped, "unixepoch", "localtime")) - ' \
|
'SUM(case when session_history.stopped > 0 ' \
|
||||||
'julianday(datetime(session_history.started, "unixepoch", "localtime"))) * 86400) - ' \
|
'then (session_history.stopped - session_history.started) ' \
|
||||||
'(CASE WHEN session_history.paused_counter IS NULL THEN 0 ' \
|
' - (case when paused_counter is NULL then 0 else paused_counter end) else 0 end) as total_duration, ' \
|
||||||
'ELSE session_history.paused_counter END))/60) as integer) as total_duration,' \
|
|
||||||
'MAX(session_history.started) as last_watch, ' \
|
'MAX(session_history.started) as last_watch, ' \
|
||||||
'users.custom_avatar_url as thumb, ' \
|
'users.custom_avatar_url as thumb, ' \
|
||||||
'users.user_id ' \
|
'users.user_id ' \
|
||||||
|
@ -382,10 +379,9 @@ class DataFactory(object):
|
||||||
try:
|
try:
|
||||||
query = 'SELECT session_history.platform, ' \
|
query = 'SELECT session_history.platform, ' \
|
||||||
'COUNT(session_history.id) as total_plays, ' \
|
'COUNT(session_history.id) as total_plays, ' \
|
||||||
'cast(round(SUM(round((julianday(datetime(session_history.stopped, "unixepoch", "localtime")) - ' \
|
'SUM(case when session_history.stopped > 0 ' \
|
||||||
'julianday(datetime(session_history.started, "unixepoch", "localtime"))) * 86400) - ' \
|
'then (session_history.stopped - session_history.started) ' \
|
||||||
'(CASE WHEN session_history.paused_counter IS NULL THEN 0 ' \
|
' - (case when paused_counter is NULL then 0 else paused_counter end) else 0 end) as total_duration, ' \
|
||||||
'ELSE session_history.paused_counter END))/60) as integer) as total_duration,' \
|
|
||||||
'MAX(session_history.started) as last_watch ' \
|
'MAX(session_history.started) as last_watch ' \
|
||||||
'FROM session_history ' \
|
'FROM session_history ' \
|
||||||
'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
|
'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue