When loading prev/next event, sort by StartDateTime, not Id. Id's are not guaranteed to be time-wise sequential. Maybe Fixes #4186

pull/3825/merge
Isaac Connor 2024-11-27 13:55:50 -05:00
parent d42040d7c3
commit 359330b384
1 changed files with 17 additions and 39 deletions

View File

@ -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;