From 1c4d01d6ec2264c2ec2f5e7c0c8d9399402e8baa Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Fri, 9 Aug 2019 19:15:53 -0700 Subject: [PATCH] Fix libraries/users table respect grouping setting --- API.md | 2 ++ plexpy/libraries.py | 9 +++++++-- plexpy/users.py | 9 +++++++-- plexpy/webserve.py | 10 ++++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index dcc4a452..a2ebc994 100644 --- a/API.md +++ b/API.md @@ -833,6 +833,7 @@ Required parameters: None Optional parameters: + grouping (int): 0 or 1 order_column (str): "library_thumb", "section_name", "section_type", "count", "parent_count", "child_count", "last_accessed", "last_played", "plays", "duration" order_dir (str): "desc" or "asc" @@ -2341,6 +2342,7 @@ Required parameters: None Optional parameters: + grouping (int): 0 or 1 order_column (str): "user_thumb", "friendly_name", "last_seen", "ip_address", "platform", "player", "last_played", "plays", "duration" order_dir (str): "desc" or "asc" diff --git a/plexpy/libraries.py b/plexpy/libraries.py index 5c203c8e..0adf8052 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -239,7 +239,7 @@ class Libraries(object): def __init__(self): pass - def get_datatables_list(self, kwargs=None): + def get_datatables_list(self, kwargs=None, grouping=None): default_return = {'recordsFiltered': 0, 'recordsTotal': 0, 'draw': 0, @@ -250,9 +250,14 @@ class Libraries(object): custom_where = [['library_sections.deleted_section', 0]] + if grouping is None: + grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES + if session.get_session_shared_libraries(): custom_where.append(['library_sections.section_id', session.get_session_shared_libraries()]) + group_by = 'session_history.reference_id' if grouping else 'session_history.id' + columns = ['library_sections.section_id', 'library_sections.section_name', 'library_sections.section_type', @@ -262,7 +267,7 @@ class Libraries(object): 'library_sections.thumb AS library_thumb', 'library_sections.custom_thumb_url AS custom_thumb', 'library_sections.art', - 'COUNT(session_history.id) AS plays', + 'COUNT(DISTINCT %s) AS plays' % group_by, 'SUM(CASE WHEN session_history.stopped > 0 THEN (session_history.stopped - session_history.started) \ ELSE 0 END) - SUM(CASE WHEN session_history.paused_counter IS NULL THEN 0 ELSE \ session_history.paused_counter END) AS duration', diff --git a/plexpy/users.py b/plexpy/users.py index 4e747a64..1c627378 100644 --- a/plexpy/users.py +++ b/plexpy/users.py @@ -70,7 +70,7 @@ class Users(object): def __init__(self): pass - def get_datatables_list(self, kwargs=None): + def get_datatables_list(self, kwargs=None, grouping=None): default_return = {'recordsFiltered': 0, 'recordsTotal': 0, 'draw': 0, @@ -81,18 +81,23 @@ class Users(object): custom_where = [['users.deleted_user', 0]] + if grouping is None: + grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES + if session.get_session_user_id(): custom_where.append(['users.user_id', session.get_session_user_id()]) if kwargs.get('user_id'): custom_where.append(['users.user_id', kwargs.get('user_id')]) + group_by = 'session_history.reference_id' if grouping else 'session_history.id' + columns = ['users.user_id', '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" \ THEN users.username ELSE users.friendly_name END) AS friendly_name', 'users.thumb AS user_thumb', 'users.custom_avatar_url AS custom_thumb', - 'COUNT(session_history.id) AS plays', + 'COUNT(DISTINCT %s) AS plays' % group_by, 'SUM(CASE WHEN session_history.stopped > 0 THEN (session_history.stopped - session_history.started) \ ELSE 0 END) - SUM(CASE WHEN session_history.paused_counter IS NULL THEN 0 ELSE \ session_history.paused_counter END) AS duration', diff --git a/plexpy/webserve.py b/plexpy/webserve.py index a6f8ffb8..937ea232 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -353,7 +353,7 @@ class WebInterface(object): @requireAuth() @sanitize_out() @addtoapi("get_libraries_table") - def get_library_list(self, **kwargs): + def get_library_list(self, grouping=None, **kwargs): """ Get the data on the Tautulli libraries table. ``` @@ -361,6 +361,7 @@ class WebInterface(object): None Optional parameters: + grouping (int): 0 or 1 order_column (str): "library_thumb", "section_name", "section_type", "count", "parent_count", "child_count", "last_accessed", "last_played", "plays", "duration" order_dir (str): "desc" or "asc" @@ -423,7 +424,7 @@ class WebInterface(object): kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "section_name") library_data = libraries.Libraries() - library_list = library_data.get_datatables_list(kwargs=kwargs) + library_list = library_data.get_datatables_list(kwargs=kwargs, grouping=grouping) return library_list @@ -1016,7 +1017,7 @@ class WebInterface(object): @requireAuth() @sanitize_out() @addtoapi("get_users_table") - def get_user_list(self, **kwargs): + def get_user_list(self, grouping=None, **kwargs): """ Get the data on Tautulli users table. ``` @@ -1024,6 +1025,7 @@ class WebInterface(object): None Optional parameters: + grouping (int): 0 or 1 order_column (str): "user_thumb", "friendly_name", "last_seen", "ip_address", "platform", "player", "last_played", "plays", "duration" order_dir (str): "desc" or "asc" @@ -1082,7 +1084,7 @@ class WebInterface(object): kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "friendly_name") user_data = users.Users() - user_list = user_data.get_datatables_list(kwargs=kwargs) + user_list = user_data.get_datatables_list(kwargs=kwargs, grouping=grouping) return user_list