API: Allow logs to be filtered by min/max id (#12471)

* Allow logs to be filtered by min/max id

* typo
This commit is contained in:
Mathieu Poussin
2021-01-28 15:50:58 +01:00
committed by GitHub
parent 841ce1349d
commit a0882af549
2 changed files with 16 additions and 4 deletions

View File

@@ -11,8 +11,8 @@ Input:
- start: The page number to request.
- limit: The limit of results to be returned.
- from: The date and time to search from.
- to: The data and time to search to.
- from: The date and time or the event id to search from.
- to: The data and time or the event id to search to.
### `list_eventlog`

View File

@@ -2235,18 +2235,22 @@ function list_logs(Illuminate\Http\Request $request, Router $router)
$query = ' FROM eventlog LEFT JOIN `devices` ON `eventlog`.`device_id`=`devices`.`device_id` WHERE 1';
$full_query = 'SELECT `devices`.`hostname`, `devices`.`sysName`, `eventlog`.`device_id` as `host`, `eventlog`.*'; // inject host for backward compat
$timestamp = 'datetime';
$id_field = 'event_id';
} elseif ($type === 'list_syslog') {
$query = ' FROM syslog LEFT JOIN `devices` ON `syslog`.`device_id`=`devices`.`device_id` WHERE 1';
$full_query = 'SELECT `devices`.`hostname`, `devices`.`sysName`, `syslog`.*';
$timestamp = 'timestamp';
$id_field = 'seq';
} elseif ($type === 'list_alertlog') {
$query = ' FROM alert_log LEFT JOIN `devices` ON `alert_log`.`device_id`=`devices`.`device_id` WHERE 1';
$full_query = 'SELECT `devices`.`hostname`, `devices`.`sysName`, `alert_log`.*';
$timestamp = 'time_logged';
$id_field = 'id';
} elseif ($type === 'list_authlog') {
$query = ' FROM authlog WHERE 1';
$full_query = 'SELECT `authlog`.*';
$timestamp = 'datetime';
$id_field = 'id';
} else {
$query = ' FROM eventlog LEFT JOIN `devices` ON `eventlog`.`device_id`=`devices`.`device_id` WHERE 1';
$full_query = 'SELECT `devices`.`hostname`, `devices`.`sysName`, `eventlog`.*';
@@ -2264,12 +2268,20 @@ function list_logs(Illuminate\Http\Request $request, Router $router)
}
if ($from) {
$query .= " AND $timestamp >= ?";
if (is_numeric($from)) {
$query .= " AND $id_field >= ?";
} else {
$query .= " AND $timestamp >= ?";
}
$param[] = $from;
}
if ($to) {
$query .= " AND $timestamp <= ?";
if (is_numeric($to)) {
$query .= " AND $id_field <= ?";
} else {
$query .= " AND $timestamp <= ?";
}
$param[] = $to;
}