diff --git a/api/chalicelib/core/usability_testing/service.py b/api/chalicelib/core/usability_testing/service.py index 91537b8a5..86811a2a1 100644 --- a/api/chalicelib/core/usability_testing/service.py +++ b/api/chalicelib/core/usability_testing/service.py @@ -117,13 +117,15 @@ def get_ut_test(project_id: int, test_id: int): """ live_count_sql = """ - SELECT COUNT(*) FROM ( - SELECT DISTINCT ON (uts.session_id) uts.test_id - FROM ut_tests_signals uts - WHERE uts.duration IS NULL AND uts.task_id IS NULL - ORDER BY uts.session_id, uts.timestamp DESC - ) AS latest_signals - WHERE latest_signals.test_id = ut.test_id + WITH RankedSessions AS ( + SELECT *, + ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY timestamp DESC) as rn + FROM ut_tests_signals + WHERE test_id = %(test_id)s AND task_id IS NULL + ) + SELECT COUNT(DISTINCT session_id) AS live_count + FROM RankedSessions + WHERE rn = 1 AND status = 'begin' """ select_columns = [