From 3fc2368078fd744a40b8fffec3ad1d2f0b79ef4c Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Mon, 3 Jan 2022 12:28:48 -0800 Subject: [PATCH] Add total time result to library user stats --- plexpy/libraries.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plexpy/libraries.py b/plexpy/libraries.py index a3ea52ab..985ec0f7 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -978,13 +978,14 @@ class Libraries(object): 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.username, users.thumb, users.custom_avatar_url AS custom_thumb, ' \ - 'COUNT(DISTINCT %s) AS user_count ' \ + 'COUNT(DISTINCT %s) AS total_plays, (SUM(stopped - started) - ' \ + 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time ' \ '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' % group_by + 'ORDER BY total_plays DESC, total_time DESC' % group_by result = monitor_db.select(query, args=[section_id]) else: result = [] @@ -1004,7 +1005,8 @@ class Libraries(object): 'user_id': item['user_id'], 'user_thumb': user_thumb, 'username': item['username'], - 'total_plays': item['user_count'] + 'total_plays': item['total_plays'], + 'total_time': item['total_time'] } user_stats.append(row)