mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-11 07:46:07 -07:00
Group plays on user and library pages
This commit is contained in:
parent
f393a015e8
commit
0fb79cba9f
4 changed files with 64 additions and 36 deletions
8
API.md
8
API.md
|
@ -982,7 +982,7 @@ Required parameters:
|
||||||
section_id (str): The id of the Plex library section
|
section_id (str): The id of the Plex library section
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -1010,7 +1010,7 @@ Required parameters:
|
||||||
section_id (str): The id of the Plex library section
|
section_id (str): The id of the Plex library section
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -2229,7 +2229,7 @@ Required parameters:
|
||||||
user_id (str): The id of the Plex user
|
user_id (str): The id of the Plex user
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -2257,7 +2257,7 @@ Required parameters:
|
||||||
user_id (str): The id of the Plex user
|
user_id (str): The id of the Plex user
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
|
|
@ -757,37 +757,42 @@ class Libraries(object):
|
||||||
# If there is no library data we must return something
|
# If there is no library data we must return something
|
||||||
return default_return
|
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):
|
if not session.allow_session_library(section_id):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
if grouping is None:
|
||||||
|
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
|
||||||
|
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
time_queries = [1, 7, 30, 0]
|
time_queries = [1, 7, 30, 0]
|
||||||
library_watch_time_stats = []
|
library_watch_time_stats = []
|
||||||
|
|
||||||
|
group_by = 'session_history.reference_id' if grouping else 'session_history.id'
|
||||||
|
|
||||||
for days in time_queries:
|
for days in time_queries:
|
||||||
try:
|
try:
|
||||||
if days > 0:
|
if days > 0:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT (SUM(stopped - started) - ' \
|
query = 'SELECT (SUM(stopped - started) - ' \
|
||||||
'SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
|
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
|
||||||
'COUNT(session_history.id) AS total_plays ' \
|
'COUNT(DISTINCT %s) AS total_plays ' \
|
||||||
'FROM session_history ' \
|
'FROM session_history ' \
|
||||||
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
||||||
'WHERE datetime(stopped, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") ' \
|
'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])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
else:
|
else:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT (SUM(stopped - started) - ' \
|
query = 'SELECT (SUM(stopped - started) - ' \
|
||||||
'SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
|
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
|
||||||
'COUNT(session_history.id) AS total_plays ' \
|
'COUNT(DISTINCT %s) AS total_plays ' \
|
||||||
'FROM session_history ' \
|
'FROM session_history ' \
|
||||||
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
'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])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
@ -812,25 +817,30 @@ class Libraries(object):
|
||||||
|
|
||||||
return library_watch_time_stats
|
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):
|
if not session.allow_session_library(section_id):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
if grouping is None:
|
||||||
|
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
|
||||||
|
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
user_stats = []
|
user_stats = []
|
||||||
|
|
||||||
|
group_by = 'session_history.reference_id' if grouping else 'session_history.id'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT (CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \
|
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, ' \
|
'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 ' \
|
'FROM session_history ' \
|
||||||
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
||||||
'JOIN users ON users.user_id = session_history.user_id ' \
|
'JOIN users ON users.user_id = session_history.user_id ' \
|
||||||
'WHERE section_id = ? ' \
|
'WHERE section_id = ? ' \
|
||||||
'GROUP BY users.user_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])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
|
|
@ -415,35 +415,40 @@ class Users(object):
|
||||||
# Use "Local" user to retain compatibility with PlexWatch database value
|
# Use "Local" user to retain compatibility with PlexWatch database value
|
||||||
return default_return
|
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):
|
if not session.allow_session_user(user_id):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
if grouping is None:
|
||||||
|
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
|
||||||
|
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
time_queries = [1, 7, 30, 0]
|
time_queries = [1, 7, 30, 0]
|
||||||
user_watch_time_stats = []
|
user_watch_time_stats = []
|
||||||
|
|
||||||
|
group_by = 'reference_id' if grouping else 'id'
|
||||||
|
|
||||||
for days in time_queries:
|
for days in time_queries:
|
||||||
try:
|
try:
|
||||||
if days > 0:
|
if days > 0:
|
||||||
if str(user_id).isdigit():
|
if str(user_id).isdigit():
|
||||||
query = 'SELECT (SUM(stopped - started) - ' \
|
query = 'SELECT (SUM(stopped - started) - ' \
|
||||||
' SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
|
' SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
|
||||||
'COUNT(id) AS total_plays ' \
|
'COUNT(DISTINCT %s) AS total_plays ' \
|
||||||
'FROM session_history ' \
|
'FROM session_history ' \
|
||||||
'WHERE datetime(stopped, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") ' \
|
'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])
|
result = monitor_db.select(query, args=[user_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
else:
|
else:
|
||||||
if str(user_id).isdigit():
|
if str(user_id).isdigit():
|
||||||
query = 'SELECT (SUM(stopped - started) - ' \
|
query = 'SELECT (SUM(stopped - started) - ' \
|
||||||
' SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \
|
' SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
|
||||||
'COUNT(id) AS total_plays ' \
|
'COUNT(DISTINCT %s) AS total_plays ' \
|
||||||
'FROM session_history ' \
|
'FROM session_history ' \
|
||||||
'WHERE user_id = ?'
|
'WHERE user_id = ? ' % group_by
|
||||||
result = monitor_db.select(query, args=[user_id])
|
result = monitor_db.select(query, args=[user_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
@ -468,22 +473,27 @@ class Users(object):
|
||||||
|
|
||||||
return user_watch_time_stats
|
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):
|
if not session.allow_session_user(user_id):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
if grouping is None:
|
||||||
|
grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES
|
||||||
|
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
player_stats = []
|
player_stats = []
|
||||||
result_id = 0
|
result_id = 0
|
||||||
|
|
||||||
|
group_by = 'reference_id' if grouping else 'id'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if str(user_id).isdigit():
|
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 ' \
|
'FROM session_history ' \
|
||||||
'WHERE user_id = ? ' \
|
'WHERE user_id = ? ' \
|
||||||
'GROUP BY player ' \
|
'GROUP BY player ' \
|
||||||
'ORDER BY player_count DESC'
|
'ORDER BY player_count DESC' % group_by
|
||||||
result = monitor_db.select(query, args=[user_id])
|
result = monitor_db.select(query, args=[user_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
|
|
@ -803,7 +803,7 @@ class WebInterface(object):
|
||||||
@cherrypy.tools.json_out()
|
@cherrypy.tools.json_out()
|
||||||
@requireAuth(member_of("admin"))
|
@requireAuth(member_of("admin"))
|
||||||
@addtoapi()
|
@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.
|
""" Get a library's watch time statistics.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -811,7 +811,7 @@ class WebInterface(object):
|
||||||
section_id (str): The id of the Plex library section
|
section_id (str): The id of the Plex library section
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -834,9 +834,11 @@ class WebInterface(object):
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
grouping = int(grouping) if str(grouping).isdigit() else grouping
|
||||||
|
|
||||||
if section_id:
|
if section_id:
|
||||||
library_data = libraries.Libraries()
|
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:
|
if result:
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
|
@ -848,7 +850,7 @@ class WebInterface(object):
|
||||||
@cherrypy.tools.json_out()
|
@cherrypy.tools.json_out()
|
||||||
@requireAuth(member_of("admin"))
|
@requireAuth(member_of("admin"))
|
||||||
@addtoapi()
|
@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.
|
""" Get a library's user statistics.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -856,7 +858,7 @@ class WebInterface(object):
|
||||||
section_id (str): The id of the Plex library section
|
section_id (str): The id of the Plex library section
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -875,9 +877,11 @@ class WebInterface(object):
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
grouping = int(grouping) if str(grouping).isdigit() else grouping
|
||||||
|
|
||||||
if section_id:
|
if section_id:
|
||||||
library_data = libraries.Libraries()
|
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:
|
if result:
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
|
@ -1411,7 +1415,7 @@ class WebInterface(object):
|
||||||
@cherrypy.tools.json_out()
|
@cherrypy.tools.json_out()
|
||||||
@requireAuth(member_of("admin"))
|
@requireAuth(member_of("admin"))
|
||||||
@addtoapi()
|
@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.
|
""" Get a user's watch time statistics.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -1419,7 +1423,7 @@ class WebInterface(object):
|
||||||
user_id (str): The id of the Plex user
|
user_id (str): The id of the Plex user
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -1442,9 +1446,11 @@ class WebInterface(object):
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
grouping = int(grouping) if str(grouping).isdigit() else grouping
|
||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
user_data = users.Users()
|
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:
|
if result:
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
|
@ -1456,7 +1462,7 @@ class WebInterface(object):
|
||||||
@cherrypy.tools.json_out()
|
@cherrypy.tools.json_out()
|
||||||
@requireAuth(member_of("admin"))
|
@requireAuth(member_of("admin"))
|
||||||
@addtoapi()
|
@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.
|
""" Get a user's player statistics.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -1464,7 +1470,7 @@ class WebInterface(object):
|
||||||
user_id (str): The id of the Plex user
|
user_id (str): The id of the Plex user
|
||||||
|
|
||||||
Optional parameters:
|
Optional parameters:
|
||||||
None
|
grouping (int): 0 or 1
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
json:
|
json:
|
||||||
|
@ -1483,9 +1489,11 @@ class WebInterface(object):
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
grouping = int(grouping) if str(grouping).isdigit() else grouping
|
||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
user_data = users.Users()
|
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:
|
if result:
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue