Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 166
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 3
LineLocation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
ReadResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
BaseLogsChannel
0.00% covered (danger)
0.00%
0 / 164
0.00% covered (danger)
0.00%
0 / 15
2550
0.00% covered (danger)
0.00%
0 / 1
 getId
n/a
0 / 0
n/a
0 / 0
0
 getName
n/a
0 / 0
n/a
0 / 0
0
 getLogFileBefore
n/a
0 / 0
n/a
0 / 0
0
 getLogFileAfter
n/a
0 / 0
n/a
0 / 0
0
 getLineLocationForDateTime
n/a
0 / 0
n/a
0 / 0
0
 continueReading
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 readAroundDateTime
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getOrCreateIndex
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 getIndexFilePath
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 indexFile
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
30
 readIndexFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 writeIndexFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 readMatchingLinesBefore
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
 readMatchingLinesAfter
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
90
 isLineMatching
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 isLineMatchingMinLogLevel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 isLineMatchingTextSearch
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 escapeSpecialChars
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 parseDateTimeOfLine
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getDateMaxPosition
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Olz\Apps\Logs\Utils;
4
5use Olz\Utils\WithUtilsTrait;
6use Psr\Log\LogLevel;
7
8class LineLocation {
9    /** @param int<-1, 1> $comparison */
10    public function __construct(
11        public LogFileInterface $logFile,
12        public int $lineNumber, // TODO still necessary? -1 = last line
13        public int $comparison,
14    ) {
15    }
16}
17
18class ReadResult {
19    /** @param array<string> $lines */
20    public function __construct(
21        public array $lines,
22        public ?LineLocation $previous,
23        public ?LineLocation $next,
24    ) {
25    }
26}
27
28abstract class BaseLogsChannel {
29    use WithUtilsTrait;
30
31    public const LOG_LEVELS = [
32        LogLevel::DEBUG,
33        LogLevel::INFO,
34        LogLevel::NOTICE,
35        LogLevel::WARNING,
36        LogLevel::ERROR,
37        LogLevel::CRITICAL,
38        LogLevel::ALERT,
39        LogLevel::EMERGENCY,
40    ];
41
42    public const INDEX_FILE_VERSION = '1.1';
43
44    abstract public static function getId(): string;
45
46    abstract public static function getName(): string;
47
48    // Page size; number of lines of log entires.
49    public static int $pageSize = 1000;
50
51    // Assumes there are files, though...
52    abstract protected function getLogFileBefore(LogFileInterface $log_file): LogFileInterface;
53
54    abstract protected function getLogFileAfter(LogFileInterface $log_file): LogFileInterface;
55
56    abstract protected function getLineLocationForDateTime(
57        \DateTime $date_time,
58    ): LineLocation;
59
60    /** @param array{targetDate?: ?string, firstDate?: ?string, lastDate?: ?string, minLogLevel?: ?string, textSearch?: ?string, pageToken?: ?string} $query */
61    public function continueReading(
62        LineLocation $line_location,
63        string $mode,
64        array $query,
65    ): ReadResult {
66        if ($mode === 'previous') {
67            $line_limit = self::$pageSize;
68            $time_limit = time() + 10;
69            return $this->readMatchingLinesBefore($line_location, $query, $line_limit, $time_limit);
70        }
71        if ($mode === 'next') {
72            $line_limit = self::$pageSize;
73            $time_limit = time() + 10;
74            return $this->readMatchingLinesAfter($line_location, $query, $line_limit, $time_limit);
75        }
76        throw new \Exception("Mode must be 'previous' or 'next', was '{$mode}'.");
77    }
78
79    /** @param array{targetDate?: ?string, firstDate?: ?string, lastDate?: ?string, minLogLevel?: ?string, textSearch?: ?string, pageToken?: ?string} $query */
80    public function readAroundDateTime(\DateTime $date_time, array $query): ReadResult {
81        $line_location = $this->getLineLocationForDateTime($date_time);
82        $line_limit = intval(self::$pageSize / 2);
83        $time_limit = time() + 5;
84        $lines_before = $this->readMatchingLinesBefore($line_location, $query, $line_limit, $time_limit);
85        $line_limit = self::$pageSize - count($lines_before->lines) + 1;
86        $time_limit = time() + 5;
87        $lines_after = $this->readMatchingLinesAfter($line_location, $query, $line_limit, $time_limit);
88        return new ReadResult([
89            ...$lines_before->lines,
90            '---',
91            ...$lines_after->lines,
92        ], $lines_before->previous, $lines_after->next);
93    }
94
95    /** @return array{version?: string, modified: int, start_date: ?string, lines: array<int>} */
96    protected function getOrCreateIndex(LogFileInterface $log_file): array {
97        $index_path = $this->getIndexFilePath($log_file);
98        if (is_file($index_path)) {
99            $index = $this->readIndexFile($index_path);
100            $version_matches = self::INDEX_FILE_VERSION === ($index['version'] ?? '1.0');
101            $modified_matches = $log_file->modified() === $index['modified'];
102            $is_cache_hit = $version_matches && $modified_matches;
103            if ($is_cache_hit) {
104                return $index;
105            }
106            $this->log()->debug("Obsolete existing index {$index_path}");
107            unlink($index_path);
108        }
109        // cache miss
110        $this->log()->debug("Create new index {$index_path}");
111        $index = $this->indexFile($log_file);
112        try {
113            $this->writeIndexFile($index_path, $index);
114        } catch (\Throwable $th) {
115            $this->log()->warning("Failed to write index file {$index_path}");
116        }
117        return $index;
118    }
119
120    protected function getIndexFilePath(LogFileInterface $log_file): string {
121        $index_path = $log_file->getIndexPath();
122        return "{$index_path}.index.json.gz";
123    }
124
125    /** @return array{version?: string, modified: int, start_date: ?string, lines: array<int>} */
126    protected function indexFile(LogFileInterface $log_file): array {
127        $index = [];
128        $index['version'] = self::INDEX_FILE_VERSION;
129        $index['modified'] = $log_file->modified();
130        $index['start_date'] = null;
131        $index['lines'] = [0];
132        $fp = $log_file->open('r');
133        $log_file->seek($fp, 0, SEEK_END);
134        $file_size = $log_file->tell($fp);
135        $log_file->seek($fp, 0, SEEK_SET);
136        while (!$log_file->eof($fp)) {
137            $line = $log_file->gets($fp);
138            if ($index['start_date'] === null) {
139                $truncated_line = substr($line ?? '', 0, $this->getDateMaxPosition());
140                $date_time = $this->parseDateTimeOfLine($truncated_line);
141                if ($date_time) {
142                    $index['start_date'] = $date_time->format('Y-m-d H:i:s');
143                }
144            }
145            $line_index = $log_file->tell($fp);
146            if ($line_index !== $file_size) {
147                $index['lines'][] = $line_index;
148            }
149        }
150        fclose($fp);
151        $index['lines'][] = $file_size;
152        return $index;
153    }
154
155    /** @return array{version?: string, modified: int, start_date: ?string, lines: array<int>} */
156    protected function readIndexFile(string $index_path): array {
157        // @phpstan-ignore-next-line
158        return json_decode(gzdecode(file_get_contents($index_path) ?: ''), true);
159    }
160
161    /** @param array{version?: string, modified: int, start_date: ?string, lines: array<int>} $content */
162    protected function writeIndexFile(string $index_path, array $content): void {
163        file_put_contents($index_path, gzencode(json_encode($content) ?: '{}'));
164    }
165
166    /** @param array{targetDate?: ?string, firstDate?: ?string, lastDate?: ?string, minLogLevel?: ?string, textSearch?: ?string, pageToken?: ?string} $query */
167    protected function readMatchingLinesBefore(
168        LineLocation $line_location,
169        array $query,
170        int $line_limit,
171        float $time_limit,
172    ): ReadResult {
173        $log_file = $line_location->logFile;
174        $file_index = $this->getOrCreateIndex($log_file);
175        $fp = $log_file->open('r');
176
177        $continuation_location = clone $line_location;
178        if ($continuation_location->lineNumber === -1) {
179            // last line is empty
180            $continuation_location->lineNumber = count($file_index['lines']) - 2;
181        }
182        if ($continuation_location->comparison <= 0) {
183            $continuation_location->lineNumber--;
184        }
185        $matching_lines = [];
186        while (count($matching_lines) < $line_limit && time() <= $time_limit) {
187            if ($continuation_location->lineNumber >= 0) {
188                $index = $file_index['lines'][$continuation_location->lineNumber];
189                $log_file->seek($fp, $index);
190                $line = $this->escapeSpecialChars($log_file->gets($fp));
191                if ($this->isLineMatching($line, $query)) {
192                    array_unshift($matching_lines, $line);
193                }
194                $continuation_location->lineNumber--;
195            }
196            $continuation_location->comparison = -1;
197            if ($continuation_location->lineNumber < 0) {
198                try {
199                    $log_file_before = $this->getLogFileBefore($log_file);
200                    $this->log()->debug("log_file_before {$log_file_before->getPath()}");
201                    $prev_file_location = new LineLocation($log_file_before, -1, 1);
202                    $new_line_limit = $line_limit - count($matching_lines);
203                    $result = $this->readMatchingLinesBefore($prev_file_location, $query, $new_line_limit, $time_limit);
204                    $matching_lines = [
205                        ...$result->lines,
206                        ...$matching_lines,
207                    ];
208                    $continuation_location = $result->previous;
209                } catch (\Throwable $th) {
210                    // Then, that's all we can do
211                    $continuation_location = null;
212                }
213                break;
214            }
215        }
216
217        $log_file->close($fp);
218        return new ReadResult($matching_lines, $continuation_location, $line_location);
219    }
220
221    /** @param array{targetDate?: ?string, firstDate?: ?string, lastDate?: ?string, minLogLevel?: ?string, textSearch?: ?string, pageToken?: ?string} $query */
222    protected function readMatchingLinesAfter(
223        LineLocation $line_location,
224        array $query,
225        int $line_limit,
226        float $time_limit,
227    ): ReadResult {
228        $log_file = $line_location->logFile;
229        $file_index = $this->getOrCreateIndex($log_file);
230        $fp = $log_file->open('r');
231        // last line is empty
232        $number_of_lines = count($file_index['lines']) - 1;
233
234        $continuation_location = clone $line_location;
235        if ($continuation_location->lineNumber === -1) {
236            // last line is empty
237            $continuation_location->lineNumber = count($file_index['lines']) - 2;
238        }
239        if ($continuation_location->comparison > 0) {
240            $continuation_location->lineNumber++;
241        }
242        $matching_lines = [];
243        while (count($matching_lines) < $line_limit && time() <= $time_limit) {
244            if ($continuation_location->lineNumber < $number_of_lines) {
245                $index = $file_index['lines'][$continuation_location->lineNumber];
246                $log_file->seek($fp, $index);
247                $line = $this->escapeSpecialChars($log_file->gets($fp));
248                if ($this->isLineMatching($line, $query)) {
249                    array_push($matching_lines, $line);
250                }
251                $continuation_location->lineNumber++;
252            }
253            $continuation_location->comparison = 1;
254            if ($continuation_location->lineNumber >= $number_of_lines) {
255                try {
256                    $log_file_after = $this->getLogFileAfter($log_file);
257                    $this->log()->debug("log_file_after {$log_file_after->getPath()}");
258                    $next_file_location = new LineLocation($log_file_after, 0, -1);
259                    $new_line_limit = $line_limit - count($matching_lines);
260                    $result = $this->readMatchingLinesAfter($next_file_location, $query, $new_line_limit, $time_limit);
261                    $matching_lines = [
262                        ...$matching_lines,
263                        ...$result->lines,
264                    ];
265                    $continuation_location = $result->next;
266                } catch (\Throwable $th) {
267                    // Then, that's all we can do
268                    $continuation_location = null;
269                }
270                break;
271            }
272        }
273
274        $log_file->close($fp);
275        return new ReadResult($matching_lines, $line_location, $continuation_location);
276    }
277
278    /** @param array{targetDate?: ?string, firstDate?: ?string, lastDate?: ?string, minLogLevel?: ?string, textSearch?: ?string, pageToken?: ?string} $query */
279    protected function isLineMatching(string $line, array $query): bool {
280        $min_log_level = $query['minLogLevel'] ?? null;
281        if (!$this->isLineMatchingMinLogLevel($line, $min_log_level)) {
282            return false;
283        }
284        $text_search = $query['textSearch'] ?? null;
285        if (!$this->isLineMatchingTextSearch($line, $text_search)) {
286            return false;
287        }
288        return true;
289    }
290
291    protected function isLineMatchingMinLogLevel(string $line, ?string $min_log_level): bool {
292        if (!$min_log_level) {
293            return true;
294        }
295        $log_levels = self::LOG_LEVELS;
296        $level_pos = array_search($min_log_level, $log_levels);
297        if ($level_pos === false) {
298            return true;
299        }
300        $matching_log_levels = array_slice($log_levels, $level_pos);
301        $log_levels_regex = implode('|', array_map(function ($log_level) {
302            return '\.'.strtoupper($log_level);
303        }, $matching_log_levels));
304        return (bool) preg_match("/{$log_levels_regex}/", $line);
305    }
306
307    protected function isLineMatchingTextSearch(string $line, ?string $text_search): bool {
308        if (!$text_search) {
309            return true;
310        }
311        $esc_text_search = preg_quote($text_search, '/');
312        return (bool) preg_match("/{$esc_text_search}/i", $line);
313    }
314
315    protected function escapeSpecialChars(?string $line): string {
316        $line = iconv('UTF-8', "UTF-8//IGNORE", $line ?? '');
317        $this->generalUtils()->checkNotBool($line, 'BaseLogsChannel::escapeSpecialChars iconv failed');
318        return html_entity_decode(htmlspecialchars($line));
319    }
320
321    // Override this function, if you have a different date format.
322    protected function parseDateTimeOfLine(string $line): ?\DateTime {
323        $res = preg_match('/(\d{4}\-\d{2}\-\d{2})(T|\s+)(\d{2}\:\d{2}\:\d{2})/', $line, $matches);
324        if (!$res) {
325            return null;
326        }
327        try {
328            return new \DateTime("{$matches[1]} {$matches[3]}");
329        } catch (\Throwable $th) {
330            return null;
331        }
332    }
333
334    // Override this function, if you have a different date placement within the log line.
335    protected function getDateMaxPosition(): int {
336        return 22;
337    }
338}