mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-08 06:00:51 -07:00
Rewrite get_history query for grouping
This commit is contained in:
parent
8791babf8e
commit
179eaf1bbe
2 changed files with 123 additions and 65 deletions
|
@ -26,56 +26,104 @@ class DataFactory(object):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
def get_history(self, kwargs=None, custom_where=None):
|
||||
def get_history(self, kwargs=None, custom_where=None, grouping=0):
|
||||
data_tables = datatables.DataTables()
|
||||
|
||||
group_by = 'group_start_id' if grouping else 'id'
|
||||
|
||||
from_table = '(SELECT ' \
|
||||
' /* Session info */ ' \
|
||||
' (CASE ' \
|
||||
' /* IF rating_key AND user are NOT EQUAL to previous row */ ' \
|
||||
' WHEN t1.rating_key <> ( ' \
|
||||
' SELECT rating_key FROM session_history WHERE id = ( ' \
|
||||
' SELECT MAX(id) FROM session_history WHERE id < t1.id)) ' \
|
||||
' AND t1.user <> ( ' \
|
||||
' SELECT user FROM session_history WHERE id = ( ' \
|
||||
' SELECT MAX(id) FROM session_history WHERE id < t1.id)) ' \
|
||||
' /* THEN select the row */ ' \
|
||||
' THEN t1.id ' \
|
||||
' /* IF rating_key OR user are NOT EQUAL to previous row */ ' \
|
||||
' WHEN ( ' \
|
||||
' SELECT MIN(id) FROM session_history WHERE id > ( ' \
|
||||
' SELECT MAX(id) FROM session_history ' \
|
||||
' WHERE (rating_key <> t1.rating_key OR user <> t1.user) AND id < t1.id)) IS NULL /* First row */ ' \
|
||||
' /* THEN select the first row */ ' \
|
||||
' THEN (SELECT MIN(id) FROM session_history) ' \
|
||||
' /* ELSE select the row where the rating key or user changed */ ' \
|
||||
' ELSE (SELECT MIN(id) FROM session_history ' \
|
||||
' WHERE id > (SELECT MAX(id) FROM session_history ' \
|
||||
' WHERE (rating_key <> t1.rating_key OR user <> t1.user) AND id < t1.id)) ' \
|
||||
' END) AS group_start_id, ' \
|
||||
' t1.id, ' \
|
||||
' t1.started as date, ' \
|
||||
' t1.started, ' \
|
||||
' t1.stopped, ' \
|
||||
' (CASE WHEN t1.stopped > 0 THEN (t1.stopped - t1.started) ELSE 0 END) AS duration, ' \
|
||||
' (CASE WHEN t1.paused_counter IS NULL THEN 0 ELSE t1.paused_counter END) AS paused_counter, ' \
|
||||
' /* User and player info */ ' \
|
||||
' t1.user_id, ' \
|
||||
' t1.user, ' \
|
||||
' (CASE WHEN t2.friendly_name IS NULL THEN t1.user ELSE t2.friendly_name END) as friendly_name, ' \
|
||||
' t1.player, ' \
|
||||
' t1.ip_address, ' \
|
||||
' /* Metadata info */ ' \
|
||||
' t3.media_type, ' \
|
||||
' t3.rating_key, ' \
|
||||
' t3.parent_rating_key, ' \
|
||||
' t3.grandparent_rating_key, ' \
|
||||
' t3.full_title, ' \
|
||||
' t3.parent_title, ' \
|
||||
' t3.year, ' \
|
||||
' t3.media_index, ' \
|
||||
' t3.parent_media_index, ' \
|
||||
' t3.thumb, ' \
|
||||
' t3.parent_thumb, ' \
|
||||
' t3.grandparent_thumb, ' \
|
||||
' /* Stream info */ ' \
|
||||
' ((CASE WHEN t1.view_offset IS NULL THEN 0.1 ELSE t1.view_offset * 1.0 END) / ' \
|
||||
' (CASE WHEN t3.duration IS NULL THEN 1.0 ELSE t3.duration * 1.0 END) * 100) as percent_complete, ' \
|
||||
' t4.video_decision ' \
|
||||
'FROM session_history AS t1 ' \
|
||||
' LEFT OUTER JOIN users AS t2 ON t1.user_id = t2.user_id ' \
|
||||
' JOIN session_history_metadata AS t3 ON t1.id = t3.id ' \
|
||||
' JOIN session_history_media_info AS t4 ON t1.id = t4.id) '
|
||||
|
||||
columns = ['session_history.id',
|
||||
'session_history.started as date',
|
||||
'(CASE WHEN users.friendly_name IS NULL THEN session_history'
|
||||
'.user ELSE users.friendly_name END) as friendly_name',
|
||||
'session_history.player',
|
||||
'session_history.ip_address',
|
||||
'session_history_metadata.full_title as full_title',
|
||||
'session_history_metadata.thumb',
|
||||
'session_history_metadata.parent_thumb',
|
||||
'session_history_metadata.grandparent_thumb',
|
||||
'session_history_metadata.media_index',
|
||||
'session_history_metadata.parent_media_index',
|
||||
'session_history_metadata.parent_title',
|
||||
'session_history_metadata.year',
|
||||
'session_history.started',
|
||||
'session_history.paused_counter',
|
||||
'session_history.stopped',
|
||||
'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) as duration',
|
||||
'((CASE WHEN session_history.view_offset IS NULL THEN 0.1 ELSE \
|
||||
session_history.view_offset * 1.0 END) / \
|
||||
(CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 ELSE \
|
||||
session_history_metadata.duration * 1.0 END) * 100) as percent_complete',
|
||||
'session_history.grandparent_rating_key as grandparent_rating_key',
|
||||
'session_history.parent_rating_key as parent_rating_key',
|
||||
'session_history.rating_key as rating_key',
|
||||
'session_history.user',
|
||||
'session_history_metadata.media_type',
|
||||
'session_history_media_info.video_decision',
|
||||
'session_history.user_id as user_id'
|
||||
columns = ['group_start_id',
|
||||
'id',
|
||||
'date',
|
||||
'MIN(started) AS started',
|
||||
'MAX(stopped) AS stopped',
|
||||
'SUM(duration) - SUM(paused_counter) AS duration',
|
||||
'SUM(paused_counter) AS paused_counter',
|
||||
'user_id',
|
||||
'user',
|
||||
'friendly_name',
|
||||
'player',
|
||||
'ip_address',
|
||||
'media_type',
|
||||
'rating_key',
|
||||
'parent_rating_key',
|
||||
'grandparent_rating_key',
|
||||
'full_title',
|
||||
'parent_title',
|
||||
'year',
|
||||
'media_index',
|
||||
'parent_media_index',
|
||||
'thumb',
|
||||
'parent_thumb',
|
||||
'grandparent_thumb',
|
||||
'percent_complete',
|
||||
'video_decision',
|
||||
'COUNT(*) AS group_count'
|
||||
]
|
||||
try:
|
||||
query = data_tables.ssp_query(table_name='session_history',
|
||||
query = data_tables.ssp_query(table_name=from_table,
|
||||
columns=columns,
|
||||
custom_where=custom_where,
|
||||
group_by=[],
|
||||
join_types=['LEFT OUTER JOIN',
|
||||
'JOIN',
|
||||
'JOIN'],
|
||||
join_tables=['users',
|
||||
'session_history_metadata',
|
||||
'session_history_media_info'],
|
||||
join_evals=[['session_history.user_id', 'users.user_id'],
|
||||
['session_history.id', 'session_history_metadata.id'],
|
||||
['session_history.id', 'session_history_media_info.id']],
|
||||
group_by=[group_by],
|
||||
join_types=[],
|
||||
join_tables=[],
|
||||
kwargs=kwargs)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -86,7 +134,7 @@ class DataFactory(object):
|
|||
'error': 'Unable to execute database query.'}
|
||||
|
||||
history = query['result']
|
||||
|
||||
|
||||
rows = []
|
||||
for item in history:
|
||||
if item["media_type"] == 'episode' and item["parent_thumb"]:
|
||||
|
@ -96,33 +144,35 @@ class DataFactory(object):
|
|||
else:
|
||||
thumb = item["thumb"]
|
||||
|
||||
row = {"id": item['id'],
|
||||
"date": item['date'],
|
||||
"friendly_name": item['friendly_name'],
|
||||
"player": item["player"],
|
||||
"ip_address": item["ip_address"],
|
||||
"full_title": item["full_title"],
|
||||
"thumb": thumb,
|
||||
"media_index": item["media_index"],
|
||||
"parent_media_index": item["parent_media_index"],
|
||||
"parent_title": item["parent_title"],
|
||||
"year": item["year"],
|
||||
row = {"group_start_id": item["group_start_id"],
|
||||
"id": item["id"],
|
||||
"date": item["date"],
|
||||
"started": item["started"],
|
||||
"paused_counter": item["paused_counter"],
|
||||
"stopped": item["stopped"],
|
||||
"duration": item["duration"],
|
||||
"percent_complete": item["percent_complete"],
|
||||
"grandparent_rating_key": item["grandparent_rating_key"],
|
||||
"parent_rating_key": item["parent_rating_key"],
|
||||
"rating_key": item["rating_key"],
|
||||
"paused_counter": item["paused_counter"],
|
||||
"user_id": item["user_id"],
|
||||
"user": item["user"],
|
||||
"friendly_name": item["friendly_name"],
|
||||
"player": item["player"],
|
||||
"ip_address": item["ip_address"],
|
||||
"media_type": item["media_type"],
|
||||
"rating_key": item["rating_key"],
|
||||
"parent_rating_key": item["parent_rating_key"],
|
||||
"grandparent_rating_key": item["grandparent_rating_key"],
|
||||
"full_title": item["full_title"],
|
||||
"parent_title": item["parent_title"],
|
||||
"year": item["year"],
|
||||
"media_index": item["media_index"],
|
||||
"parent_media_index": item["parent_media_index"],
|
||||
"thumb": thumb,
|
||||
"video_decision": item["video_decision"],
|
||||
"user_id": item["user_id"]
|
||||
"percent_complete": item["percent_complete"],
|
||||
"group_count": item["group_count"]
|
||||
}
|
||||
|
||||
rows.append(row)
|
||||
|
||||
|
||||
dict = {'recordsFiltered': query['filteredCount'],
|
||||
'recordsTotal': query['totalCount'],
|
||||
'data': rows,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue