From 359330b3840676ff2f5e032d4f0889f5db06360c Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 27 Nov 2024 13:55:50 -0500 Subject: [PATCH] When loading prev/next event, sort by StartDateTime, not Id. Id's are not guaranteed to be time-wise sequential. Maybe Fixes #4186 --- web/ajax/status.php | 56 ++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/web/ajax/status.php b/web/ajax/status.php index a2b274bf4..185d061e7 100644 --- a/web/ajax/status.php +++ b/web/ajax/status.php @@ -212,7 +212,7 @@ $statusData = array( 'EventId' => true, 'Type' => true, 'TimeStamp' => true, - 'TimeStampShort' => array( 'sql' => 'date_format( StartDateTime, \''.MYSQL_FMT_DATETIME_SHORT.'\' )' ), + 'TimeStampShort' => array( 'sql' => 'date_format( StartDateTime, \''.MYSQL_FMT_DATETIME_SHORT.'\' )' ), 'Delta' => true, 'Score' => true, //'Image' => array( 'postFunc' => 'getFrameImage' ), @@ -515,34 +515,23 @@ function getNearEvents() { } $sql = ' - SELECT - E.Id - AS Id, - E.StartDateTime - AS StartDateTime - FROM Events - AS E - INNER JOIN Monitors - AS M - ON E.MonitorId = M.Id - LEFT JOIN Events_Tags - AS ET - ON E.Id = ET.EventId - LEFT JOIN Tags - AS T - ON T.Id = ET.TagId - WHERE '.$sortColumn.' + SELECT E.Id AS Id, E.StartDateTime AS StartDateTime + FROM Events AS E + INNER JOIN Monitors AS M ON E.MonitorId = M.Id + LEFT JOIN Events_Tags AS ET ON E.Id = ET.EventId + LEFT JOIN Tags AS T ON T.Id = ET.TagId + WHERE '.$sortColumn.' '.($sortOrder=='ASC'?'<=':'>=').' \''.$event[$_REQUEST['sort_field']].'\''; if ($filter->sql()) { $sql .= ' AND ('.$filter->sql().')'; } - $sql .= ' AND E.Id<'.$event['Id'] . ' ORDER BY '.$sortColumn.' '.($sortOrder=='ASC'?'DESC':'ASC'); + $sql .= ' AND E.StartDateTime < ? ORDER BY '.$sortColumn.' '.($sortOrder=='ASC'?'DESC':'ASC'); if ( $sortColumn != 'E.Id' ) { # When sorting by starttime, if we have two events with the same starttime (different monitors) then we should sort secondly by Id $sql .= ', E.Id DESC'; } $sql .= ' LIMIT 1'; - $result = dbQuery($sql); + $result = dbQuery($sql, [$event['StartDateTime']]); if ( !$result ) { ZM\Error('Failed to load previous event using '.$sql); return $NearEvents; @@ -551,34 +540,23 @@ function getNearEvents() { $prevEvent = dbFetchNext($result); $sql = ' - SELECT - E.Id - AS Id, - E.StartDateTime - AS StartDateTime - FROM Events - AS E - INNER JOIN Monitors - AS M - ON E.MonitorId = M.Id - LEFT JOIN Events_Tags - AS ET - ON E.Id = ET.EventId - LEFT JOIN Tags - AS T - ON T.Id = ET.TagId - WHERE '.$sortColumn.' + SELECT E.Id AS Id, E.StartDateTime AS StartDateTime + FROM Events AS E + INNER JOIN Monitors AS M ON E.MonitorId = M.Id + LEFT JOIN Events_Tags AS ET ON E.Id = ET.EventId + LEFT JOIN Tags AS T ON T.Id = ET.TagId + WHERE '.$sortColumn.' '.($sortOrder=='ASC'?'>=':'<=').' \''.$event[$_REQUEST['sort_field']].'\''; if ($filter->sql()) { $sql .= ' AND ('.$filter->sql().')'; } - $sql .= ' AND E.Id>'.$event['Id'] . ' ORDER BY '.$sortColumn.' '.($sortOrder=='ASC'?'ASC':'DESC'); + $sql .= ' AND E.StartDateTime > ? ORDER BY '.$sortColumn.' '.($sortOrder=='ASC'?'ASC':'DESC'); if ( $sortColumn != 'E.Id' ) { # When sorting by starttime, if we have two events with the same starttime (different monitors) then we should sort secondly by Id $sql .= ', E.Id ASC'; } $sql .= ' LIMIT 1'; - $result = dbQuery($sql); + $result = dbQuery($sql, [$event['StartDateTime']]); if ( !$result ) { ZM\Error('Failed to load next event using '.$sql); return $NearEvents;