Group plays on user and library pages

This commit is contained in:
JonnyWong16 2018-06-21 18:52:56 -07:00
parent f393a015e8
commit 0fb79cba9f
4 changed files with 64 additions and 36 deletions

8
API.md
View file

@ -982,7 +982,7 @@ Required parameters:
section_id (str): The id of the Plex library section
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -1010,7 +1010,7 @@ Required parameters:
section_id (str): The id of the Plex library section
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -2229,7 +2229,7 @@ Required parameters:
user_id (str): The id of the Plex user
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -2257,7 +2257,7 @@ Required parameters:
user_id (str): The id of the Plex user
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:

View file

@ -757,37 +757,42 @@ class Libraries(object):
# If there is no library data we must return something
return default_return
def get_watch_time_stats(self, section_id=None):
def get_watch_time_stats(self, section_id=None, grouping=None):
if not session.allow_session_library(section_id):
return []
if grouping is None:
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
monitor_db = database.MonitorDatabase()
time_queries = [1, 7, 30, 0]
library_watch_time_stats = []
group_by = 'session_history.reference_id' if grouping else 'session_history.id'
for days in time_queries:
try:
if days > 0:
if str(section_id).isdigit():
query = 'SELECT (SUM(stopped - started) - ' \
'SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
'COUNT(session_history.id) AS total_plays ' \
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
'COUNT(DISTINCT %s) AS total_plays ' \
'FROM session_history ' \
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
'WHERE datetime(stopped, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") ' \
'AND section_id = ?' % days
'AND section_id = ?' % (group_by, days)
result = monitor_db.select(query, args=[section_id])
else:
result = []
else:
if str(section_id).isdigit():
query = 'SELECT (SUM(stopped - started) - ' \
'SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
'COUNT(session_history.id) AS total_plays ' \
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
'COUNT(DISTINCT %s) AS total_plays ' \
'FROM session_history ' \
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
'WHERE section_id = ?'
'WHERE section_id = ?' % group_by
result = monitor_db.select(query, args=[section_id])
else:
result = []
@ -812,25 +817,30 @@ class Libraries(object):
return library_watch_time_stats
def get_user_stats(self, section_id=None):
def get_user_stats(self, section_id=None, grouping=None):
if not session.allow_session_library(section_id):
return []
if grouping is None:
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
monitor_db = database.MonitorDatabase()
user_stats = []
group_by = 'session_history.reference_id' if grouping else 'session_history.id'
try:
if str(section_id).isdigit():
query = 'SELECT (CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \
'THEN users.username ELSE users.friendly_name END) AS friendly_name, ' \
'users.user_id, users.thumb, COUNT(user) AS user_count ' \
'users.user_id, users.thumb, COUNT(DISTINCT %s) AS user_count ' \
'FROM session_history ' \
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
'JOIN users ON users.user_id = session_history.user_id ' \
'WHERE section_id = ? ' \
'GROUP BY users.user_id ' \
'ORDER BY user_count DESC'
'ORDER BY user_count DESC' % group_by
result = monitor_db.select(query, args=[section_id])
else:
result = []

View file

@ -415,35 +415,40 @@ class Users(object):
# Use "Local" user to retain compatibility with PlexWatch database value
return default_return
def get_watch_time_stats(self, user_id=None):
def get_watch_time_stats(self, user_id=None, grouping=None):
if not session.allow_session_user(user_id):
return []
if grouping is None:
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
monitor_db = database.MonitorDatabase()
time_queries = [1, 7, 30, 0]
user_watch_time_stats = []
group_by = 'reference_id' if grouping else 'id'
for days in time_queries:
try:
if days > 0:
if str(user_id).isdigit():
query = 'SELECT (SUM(stopped - started) - ' \
' SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
'COUNT(id) AS total_plays ' \
' SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
'COUNT(DISTINCT %s) AS total_plays ' \
'FROM session_history ' \
'WHERE datetime(stopped, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") ' \
'AND user_id = ?' % days
'AND user_id = ? ' % (group_by, days)
result = monitor_db.select(query, args=[user_id])
else:
result = []
else:
if str(user_id).isdigit():
query = 'SELECT (SUM(stopped - started) - ' \
' SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
'COUNT(id) AS total_plays ' \
' SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
'COUNT(DISTINCT %s) AS total_plays ' \
'FROM session_history ' \
'WHERE user_id = ?'
'WHERE user_id = ? ' % group_by
result = monitor_db.select(query, args=[user_id])
else:
result = []
@ -468,22 +473,27 @@ class Users(object):
return user_watch_time_stats
def get_player_stats(self, user_id=None):
def get_player_stats(self, user_id=None, grouping=None):
if not session.allow_session_user(user_id):
return []
if grouping is None:
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
monitor_db = database.MonitorDatabase()
player_stats = []
result_id = 0
group_by = 'reference_id' if grouping else 'id'
try:
if str(user_id).isdigit():
query = 'SELECT player, COUNT(player) as player_count, platform ' \
query = 'SELECT player, COUNT(DISTINCT %s) as player_count, platform ' \
'FROM session_history ' \
'WHERE user_id = ? ' \
'GROUP BY player ' \
'ORDER BY player_count DESC'
'ORDER BY player_count DESC' % group_by
result = monitor_db.select(query, args=[user_id])
else:
result = []

View file

@ -803,7 +803,7 @@ class WebInterface(object):
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@addtoapi()
def get_library_watch_time_stats(self, section_id=None, **kwargs):
def get_library_watch_time_stats(self, section_id=None, grouping=None, **kwargs):
""" Get a library's watch time statistics.
```
@ -811,7 +811,7 @@ class WebInterface(object):
section_id (str): The id of the Plex library section
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -834,9 +834,11 @@ class WebInterface(object):
]
```
"""
grouping = int(grouping) if str(grouping).isdigit() else grouping
if section_id:
library_data = libraries.Libraries()
result = library_data.get_watch_time_stats(section_id=section_id)
result = library_data.get_watch_time_stats(section_id=section_id, grouping=grouping)
if result:
return result
else:
@ -848,7 +850,7 @@ class WebInterface(object):
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@addtoapi()
def get_library_user_stats(self, section_id=None, **kwargs):
def get_library_user_stats(self, section_id=None, grouping=None, **kwargs):
""" Get a library's user statistics.
```
@ -856,7 +858,7 @@ class WebInterface(object):
section_id (str): The id of the Plex library section
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -875,9 +877,11 @@ class WebInterface(object):
]
```
"""
grouping = int(grouping) if str(grouping).isdigit() else grouping
if section_id:
library_data = libraries.Libraries()
result = library_data.get_user_stats(section_id=section_id)
result = library_data.get_user_stats(section_id=section_id, grouping=grouping)
if result:
return result
else:
@ -1411,7 +1415,7 @@ class WebInterface(object):
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@addtoapi()
def get_user_watch_time_stats(self, user_id=None, **kwargs):
def get_user_watch_time_stats(self, user_id=None, grouping=None, **kwargs):
""" Get a user's watch time statistics.
```
@ -1419,7 +1423,7 @@ class WebInterface(object):
user_id (str): The id of the Plex user
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -1442,9 +1446,11 @@ class WebInterface(object):
]
```
"""
grouping = int(grouping) if str(grouping).isdigit() else grouping
if user_id:
user_data = users.Users()
result = user_data.get_watch_time_stats(user_id=user_id)
result = user_data.get_watch_time_stats(user_id=user_id, grouping=grouping)
if result:
return result
else:
@ -1456,7 +1462,7 @@ class WebInterface(object):
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@addtoapi()
def get_user_player_stats(self, user_id=None, **kwargs):
def get_user_player_stats(self, user_id=None, grouping=None, **kwargs):
""" Get a user's player statistics.
```
@ -1464,7 +1470,7 @@ class WebInterface(object):
user_id (str): The id of the Plex user
Optional parameters:
None
grouping (int): 0 or 1
Returns:
json:
@ -1483,9 +1489,11 @@ class WebInterface(object):
]
```
"""
grouping = int(grouping) if str(grouping).isdigit() else grouping
if user_id:
user_data = users.Users()
result = user_data.get_player_stats(user_id=user_id)
result = user_data.get_player_stats(user_id=user_id, grouping=grouping)
if result:
return result
else: