-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFilterQuery.php
137 lines (126 loc) Β· 3.22 KB
/
FilterQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\LogReader\Log;
class FilterQuery {
const CMP_EQ = 1;
const CMP_LT = 2;
const CMP_GT = 3;
const CMP_LET = 4;
const CMP_GET = 5;
const CMP_NEQ = 6;
const COMPARISONS = [
self::CMP_NEQ => '!=',
self::CMP_LET => '<=',
self::CMP_GET => '>=',
self::CMP_EQ => '=',
self::CMP_LT => '<',
self::CMP_GT => '>',
];
/**
* @var array{field: string, cmp: int, value: string}[]
*/
private array $comparisons;
public function __construct(string $query) {
$parts = array_filter(str_getcsv(str_replace("'", '"', $query), ' '));
$this->comparisons = array_map([$this, 'parsePart'], $parts);
}
/**
* @param string $part
* @return array{field: string, cmp: int, value: string}
*/
private function parsePart(string $part): array {
$field = 'message';
$value = $part;
$cmp = self::CMP_EQ;
foreach (self::COMPARISONS as $cmpVal => $cmpStr) {
if (str_contains($part, $cmpStr)) {
[$field, $value] = explode($cmpStr, $part);
switch ($field) {
case "level":
$value = $this->parseLogLevel($value);
break;
case "time":
$value = new \DateTimeImmutable($value);
break;
}
$cmp = $cmpVal;
break;
}
}
return [
'field' => $field,
'cmp' => $cmp,
'value' => $value,
];
}
public function matches(array $logItem): bool {
foreach ($this->comparisons as $comparison) {
$logValue = $logItem[$comparison['field']] ?? $logItem['data'][$comparison['field']] ?? null;
if (!$this->compare($logValue, $comparison['value'], $comparison['cmp'])) {
return false;
}
}
return true;
}
private function compare($a, $b, int $cmp): bool {
switch ($cmp) {
case self::CMP_EQ:
if (is_string($a)) {
return str_contains($a, $b);
} else {
return $a == $b;
}
case self::CMP_LT:
return $a < $b;
case self::CMP_GT:
return $a > $b;
case self::CMP_LET:
return $a <= $b;
case self::CMP_GET:
return $a >= $b;
case self::CMP_NEQ:
return !$this->compare($a, $b, self::CMP_EQ);
default:
return false;
}
}
private static function parseLogLevel(string $level): int {
if (is_numeric($level)) {
return (int)$level;
}
switch (strtoupper($level)) {
case "DEBUG":
return 0;
case "INFO":
return 1;
case "WARN":
case "WARNING":
return 2;
case "ERROR":
return 3;
case "FATAL":
return 4;
default:
throw new \Exception("Unknown log level $level");
}
}
}