|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Opcodes\LogViewer\Concerns\LogIndex; |
| 4 | + |
| 5 | +use Carbon\CarbonImmutable; |
| 6 | +use Carbon\CarbonInterface; |
| 7 | + |
| 8 | +trait PreservesIndexingProgress |
| 9 | +{ |
| 10 | + public function setLastScannedFilePosition(int $position): void |
| 11 | + { |
| 12 | + $this->lastScannedFilePosition = $position; |
| 13 | + } |
| 14 | + |
| 15 | + public function getLastScannedFilePosition(): int |
| 16 | + { |
| 17 | + if (! isset($this->lastScannedFilePosition)) { |
| 18 | + $this->loadMetadata(); |
| 19 | + } |
| 20 | + |
| 21 | + return $this->lastScannedFilePosition; |
| 22 | + } |
| 23 | + |
| 24 | + public function setLastScannedIndex(int $index): void |
| 25 | + { |
| 26 | + $this->lastScannedIndex = $index; |
| 27 | + } |
| 28 | + |
| 29 | + public function getLastScannedIndex(): int |
| 30 | + { |
| 31 | + if (! isset($this->lastScannedIndex)) { |
| 32 | + $this->loadMetadata(); |
| 33 | + } |
| 34 | + |
| 35 | + return $this->lastScannedIndex; |
| 36 | + } |
| 37 | + |
| 38 | + public function incomplete(): bool |
| 39 | + { |
| 40 | + return $this->file->size() !== $this->getLastScannedFilePosition(); |
| 41 | + } |
| 42 | + |
| 43 | + public function getEarliestTimestamp(): ?int |
| 44 | + { |
| 45 | + $earliestTimestamp = null; |
| 46 | + |
| 47 | + if ($this->hasFilters()) { |
| 48 | + // because it has filters, we can no longer use our chunk definitions, which has |
| 49 | + // values for the whole index and not just particular levels/dates. |
| 50 | + foreach ($this->get() as $timestamp => $tsIndex) { |
| 51 | + $earliestTimestamp = min($timestamp, $earliestTimestamp ?? $timestamp); |
| 52 | + } |
| 53 | + } else { |
| 54 | + foreach ($this->getChunkDefinitions() as $chunkDefinition) { |
| 55 | + if (! isset($chunkDefinition['earliest_timestamp'])) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + $earliestTimestamp = min( |
| 60 | + $chunkDefinition['earliest_timestamp'], |
| 61 | + $earliestTimestamp ?? $chunkDefinition['earliest_timestamp'] |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return $earliestTimestamp; |
| 67 | + } |
| 68 | + |
| 69 | + public function getEarliestDate(): CarbonInterface |
| 70 | + { |
| 71 | + return CarbonImmutable::createFromTimestamp($this->getEarliestTimestamp()); |
| 72 | + } |
| 73 | + |
| 74 | + public function getLatestTimestamp(): ?int |
| 75 | + { |
| 76 | + $latestTimestamp = null; |
| 77 | + |
| 78 | + if ($this->hasFilters()) { |
| 79 | + // because it has filters, we can no longer use our chunk definitions, which has |
| 80 | + // values for the whole index and not just particular levels/dates. |
| 81 | + foreach ($this->get() as $timestamp => $tsIndex) { |
| 82 | + $latestTimestamp = max($timestamp, $latestTimestamp ?? $timestamp); |
| 83 | + } |
| 84 | + } else { |
| 85 | + foreach ($this->getChunkDefinitions() as $chunkDefinition) { |
| 86 | + if (! isset($chunkDefinition['latest_timestamp'])) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $latestTimestamp = max( |
| 91 | + $chunkDefinition['latest_timestamp'], |
| 92 | + $latestTimestamp ?? $chunkDefinition['latest_timestamp'] |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return $latestTimestamp; |
| 98 | + } |
| 99 | + |
| 100 | + public function getLatestDate(): CarbonInterface |
| 101 | + { |
| 102 | + return CarbonImmutable::createFromTimestamp($this->getLatestTimestamp()); |
| 103 | + } |
| 104 | +} |
0 commit comments