New watch statistics cards

This commit is contained in:
JonnyWong16 2017-11-05 00:30:44 -07:00
parent de589f59a1
commit 8d2c9a7764
3 changed files with 488 additions and 1110 deletions

View file

@ -281,104 +281,7 @@ class DataFactory(object):
home_stats = []
for stat in stats_cards:
if stat == 'top_tv':
top_tv = []
try:
query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, t.section_id, ' \
't.art, t.media_type, t.content_rating, t.labels, t.started, ' \
'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, SUM(t.d) AS total_duration ' \
'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \
' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \
' AS d ' \
' FROM session_history ' \
' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
' >= datetime("now", "-%s days", "localtime") ' \
' AND session_history.media_type = "episode" ' \
' GROUP BY %s) AS t ' \
'GROUP BY t.grandparent_title ' \
'ORDER BY %s DESC, started DESC ' \
'LIMIT %s ' % (time_range, group_by, sort_type, stats_count)
result = monitor_db.select(query)
except Exception as e:
logger.warn(u"PlexPy DataFactory :: Unable to execute database query for get_home_stats: top_tv: %s." % e)
return None
for item in result:
row = {'title': item['grandparent_title'],
'total_plays': item['total_plays'],
'total_duration': item['total_duration'],
'users_watched': '',
'rating_key': item['grandparent_rating_key'],
'last_play': item['last_watch'],
'grandparent_thumb': item['grandparent_thumb'],
'thumb': '',
'art': item['art'],
'section_id': item['section_id'],
'media_type': item['media_type'],
'content_rating': item['content_rating'],
'labels': item['labels'].split(';') if item['labels'] else (),
'user': '',
'friendly_name': '',
'platform_type': '',
'platform': '',
'row_id': item['id']
}
top_tv.append(row)
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'rows': session.mask_session_info(top_tv)})
elif stat == 'popular_tv':
popular_tv = []
try:
query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, t.section_id, ' \
't.art, t.media_type, t.content_rating, t.labels, t.started, ' \
'COUNT(DISTINCT t.user_id) AS users_watched, ' \
'MAX(t.started) AS last_watch, COUNT(t.id) as total_plays, SUM(t.d) AS total_duration ' \
'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \
' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \
' AS d ' \
' FROM session_history ' \
' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
' >= datetime("now", "-%s days", "localtime") ' \
' AND session_history.media_type = "episode" ' \
' GROUP BY %s) AS t ' \
'GROUP BY t.grandparent_title ' \
'ORDER BY users_watched DESC, %s DESC, started DESC ' \
'LIMIT %s ' % (time_range, group_by, sort_type, stats_count)
result = monitor_db.select(query)
except Exception as e:
logger.warn(u"PlexPy DataFactory :: Unable to execute database query for get_home_stats: popular_tv: %s." % e)
return None
for item in result:
row = {'title': item['grandparent_title'],
'users_watched': item['users_watched'],
'rating_key': item['grandparent_rating_key'],
'last_play': item['last_watch'],
'total_plays': item['total_plays'],
'grandparent_thumb': item['grandparent_thumb'],
'thumb': '',
'art': item['art'],
'section_id': item['section_id'],
'media_type': item['media_type'],
'content_rating': item['content_rating'],
'labels': item['labels'].split(';') if item['labels'] else (),
'user': '',
'friendly_name': '',
'platform_type': '',
'platform': '',
'row_id': item['id']
}
popular_tv.append(row)
home_stats.append({'stat_id': stat,
'rows': session.mask_session_info(popular_tv)})
elif stat == 'top_movies':
if stat == 'top_movies':
top_movies = []
try:
query = 'SELECT t.id, t.full_title, t.rating_key, t.thumb, t.section_id, ' \
@ -425,6 +328,7 @@ class DataFactory(object):
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'stat_title': 'Most Watched Movies',
'rows': session.mask_session_info(top_movies)})
elif stat == 'popular_movies':
@ -473,8 +377,108 @@ class DataFactory(object):
popular_movies.append(row)
home_stats.append({'stat_id': stat,
'stat_title': 'Most Popular Movies',
'rows': session.mask_session_info(popular_movies)})
elif stat == 'top_tv':
top_tv = []
try:
query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, t.section_id, ' \
't.art, t.media_type, t.content_rating, t.labels, t.started, ' \
'MAX(t.started) AS last_watch, COUNT(t.id) AS total_plays, SUM(t.d) AS total_duration ' \
'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \
' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \
' AS d ' \
' FROM session_history ' \
' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
' >= datetime("now", "-%s days", "localtime") ' \
' AND session_history.media_type = "episode" ' \
' GROUP BY %s) AS t ' \
'GROUP BY t.grandparent_title ' \
'ORDER BY %s DESC, started DESC ' \
'LIMIT %s ' % (time_range, group_by, sort_type, stats_count)
result = monitor_db.select(query)
except Exception as e:
logger.warn(u"PlexPy DataFactory :: Unable to execute database query for get_home_stats: top_tv: %s." % e)
return None
for item in result:
row = {'title': item['grandparent_title'],
'total_plays': item['total_plays'],
'total_duration': item['total_duration'],
'users_watched': '',
'rating_key': item['grandparent_rating_key'],
'last_play': item['last_watch'],
'grandparent_thumb': item['grandparent_thumb'],
'thumb': item['grandparent_thumb'],
'art': item['art'],
'section_id': item['section_id'],
'media_type': item['media_type'],
'content_rating': item['content_rating'],
'labels': item['labels'].split(';') if item['labels'] else (),
'user': '',
'friendly_name': '',
'platform_type': '',
'platform': '',
'row_id': item['id']
}
top_tv.append(row)
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'stat_title': 'Most Watched TV Shows',
'rows': session.mask_session_info(top_tv)})
elif stat == 'popular_tv':
popular_tv = []
try:
query = 'SELECT t.id, t.grandparent_title, t.grandparent_rating_key, t.grandparent_thumb, t.section_id, ' \
't.art, t.media_type, t.content_rating, t.labels, t.started, ' \
'COUNT(DISTINCT t.user_id) AS users_watched, ' \
'MAX(t.started) AS last_watch, COUNT(t.id) as total_plays, SUM(t.d) AS total_duration ' \
'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \
' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \
' AS d ' \
' FROM session_history ' \
' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
' WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
' >= datetime("now", "-%s days", "localtime") ' \
' AND session_history.media_type = "episode" ' \
' GROUP BY %s) AS t ' \
'GROUP BY t.grandparent_title ' \
'ORDER BY users_watched DESC, %s DESC, started DESC ' \
'LIMIT %s ' % (time_range, group_by, sort_type, stats_count)
result = monitor_db.select(query)
except Exception as e:
logger.warn(u"PlexPy DataFactory :: Unable to execute database query for get_home_stats: popular_tv: %s." % e)
return None
for item in result:
row = {'title': item['grandparent_title'],
'users_watched': item['users_watched'],
'rating_key': item['grandparent_rating_key'],
'last_play': item['last_watch'],
'total_plays': item['total_plays'],
'grandparent_thumb': item['grandparent_thumb'],
'thumb': item['grandparent_thumb'],
'art': item['art'],
'section_id': item['section_id'],
'media_type': item['media_type'],
'content_rating': item['content_rating'],
'labels': item['labels'].split(';') if item['labels'] else (),
'user': '',
'friendly_name': '',
'platform_type': '',
'platform': '',
'row_id': item['id']
}
popular_tv.append(row)
home_stats.append({'stat_id': stat,
'stat_title': 'Most Popular TV Shows',
'rows': session.mask_session_info(popular_tv)})
elif stat == 'top_music':
top_music = []
try:
@ -506,7 +510,7 @@ class DataFactory(object):
'rating_key': item['grandparent_rating_key'],
'last_play': item['last_watch'],
'grandparent_thumb': item['grandparent_thumb'],
'thumb': '',
'thumb': item['grandparent_thumb'],
'art': item['art'],
'section_id': item['section_id'],
'media_type': item['media_type'],
@ -522,6 +526,7 @@ class DataFactory(object):
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'stat_title': 'Most Played Artists',
'rows': session.mask_session_info(top_music)})
elif stat == 'popular_music':
@ -555,7 +560,7 @@ class DataFactory(object):
'last_play': item['last_watch'],
'total_plays': item['total_plays'],
'grandparent_thumb': item['grandparent_thumb'],
'thumb': '',
'thumb': item['grandparent_thumb'],
'art': item['art'],
'section_id': item['section_id'],
'media_type': item['media_type'],
@ -570,6 +575,7 @@ class DataFactory(object):
popular_music.append(row)
home_stats.append({'stat_id': stat,
'stat_title': 'Most Popular Artists',
'rows': session.mask_session_info(popular_music)})
elif stat == 'top_users':
@ -610,8 +616,10 @@ class DataFactory(object):
'total_plays': item['total_plays'],
'total_duration': item['total_duration'],
'last_play': item['last_watch'],
'thumb': user_thumb,
'user_thumb': user_thumb,
'grandparent_thumb': '',
'art': '',
'users_watched': '',
'rating_key': '',
'title': '',
@ -623,6 +631,7 @@ class DataFactory(object):
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'stat_title': 'Most Active Users',
'rows': session.mask_session_info(top_users, mask_metadata=False)})
elif stat == 'top_platforms':
@ -659,6 +668,7 @@ class DataFactory(object):
'title': '',
'thumb': '',
'grandparent_thumb': '',
'art': '',
'users_watched': '',
'rating_key': '',
'user': '',
@ -669,6 +679,7 @@ class DataFactory(object):
home_stats.append({'stat_id': stat,
'stat_type': sort_type,
'stat_title': 'Most Active Platforms',
'rows': session.mask_session_info(top_platform, mask_metadata=False)})
elif stat == 'last_watched':
@ -727,6 +738,7 @@ class DataFactory(object):
last_watched.append(row)
home_stats.append({'stat_id': stat,
'stat_title': 'Last Watched Items',
'rows': session.mask_session_info(last_watched)})
elif stat == 'most_concurrent':
@ -807,6 +819,7 @@ class DataFactory(object):
return None
home_stats.append({'stat_id': stat,
'stat_title': 'Most Concurrent Streams',
'rows': most_concurrent})
return home_stats