Skip to content

Commit

Permalink
Fix grouping live tv history
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyWong16 committed Apr 2, 2024
1 parent 10e62ca commit 4582ff4
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions plexpy/activity_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,24 +494,28 @@ def group_history(self, last_id, session, metadata=None):
prev_watched = None

if session['live']:
# Check if we should group the session, select the last guid from the user
# Check if we should group the session, select the last guid from the user within the last day
query = "SELECT session_history.id, session_history_metadata.guid, session_history.reference_id " \
"FROM session_history " \
"JOIN session_history_metadata ON session_history.id == session_history_metadata.id " \
"WHERE session_history.id <= ? AND session_history.user_id = ? ORDER BY session_history.id DESC LIMIT 1 "
"WHERE session_history.id <= ? AND session_history.user_id = ? " \
"AND datetime(session_history.started, 'unixepoch', 'localtime') > datetime('now', '-1 day') " \
"ORDER BY session_history.id DESC LIMIT 1 "

args = [last_id, session['user_id']]

result = self.db.select(query=query, args=args)

if len(result) > 0:
new_session = {'id': last_id,
'guid': metadata['guid'] if metadata else session['guid'],
'reference_id': last_id}
'guid': metadata['guid'] if metadata else session['guid'],
'reference_id': last_id}

prev_session = {'id': result[0]['id'],
'guid': result[0]['guid'],
'reference_id': result[0]['reference_id']}

prev_watched = False

else:
# Check if we should group the session, select the last two rows from the user
Expand All @@ -524,9 +528,9 @@ def group_history(self, last_id, session, metadata=None):

if len(result) > 1:
new_session = {'id': result[0]['id'],
'rating_key': result[0]['rating_key'],
'view_offset': helpers.cast_to_int(result[0]['view_offset']),
'reference_id': result[0]['reference_id']}
'rating_key': result[0]['rating_key'],
'view_offset': helpers.cast_to_int(result[0]['view_offset']),
'reference_id': result[0]['reference_id']}

prev_session = {'id': result[1]['id'],
'rating_key': result[1]['rating_key'],
Expand All @@ -550,8 +554,10 @@ def group_history(self, last_id, session, metadata=None):
# and new session view offset is greater,
# then set the reference_id to the previous row,
# else set the reference_id to the new id
if (prev_watched is False and prev_session['view_offset'] <= new_session['view_offset'] or
session['live'] and prev_session['guid'] == new_session['guid']):
if prev_watched is False and (
not session['live'] and prev_session['view_offset'] <= new_session['view_offset'] or
session['live'] and prev_session['guid'] == new_session['guid']
):
if metadata:
logger.debug("Tautulli ActivityProcessor :: Grouping history for sessionKey %s", session['session_key'])
args = [prev_session['reference_id'], new_session['id']]
Expand Down

0 comments on commit 4582ff4

Please sign in to comment.