generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Naoray/feature/added-deduplication-stores
Feature/added deduplication stores
- Loading branch information
Showing
29 changed files
with
1,356 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Naoray\LaravelGithubMonolog\Deduplication; | ||
|
||
use Illuminate\Support\Collection; | ||
use Monolog\Handler\BufferHandler; | ||
use Monolog\Handler\HandlerInterface; | ||
use Monolog\Level; | ||
use Monolog\LogRecord; | ||
use Naoray\LaravelGithubMonolog\Deduplication\Stores\StoreInterface; | ||
|
||
class DeduplicationHandler extends BufferHandler | ||
{ | ||
public function __construct( | ||
HandlerInterface $handler, | ||
protected StoreInterface $store, | ||
protected SignatureGeneratorInterface $signatureGenerator, | ||
int|string|Level $level = Level::Error, | ||
int $bufferLimit = 0, | ||
bool $bubble = true, | ||
bool $flushOnOverflow = false, | ||
) { | ||
parent::__construct( | ||
handler: $handler, | ||
bufferLimit: $bufferLimit, | ||
level: $level, | ||
bubble: $bubble, | ||
flushOnOverflow: $flushOnOverflow, | ||
); | ||
} | ||
|
||
public function flush(): void | ||
{ | ||
if ($this->bufferSize === 0) { | ||
return; | ||
} | ||
|
||
collect($this->buffer) | ||
->map(function (LogRecord $record) { | ||
$signature = $this->signatureGenerator->generate($record); | ||
|
||
// Create new record with signature in extra data | ||
$record = $record->with(extra: ['github_issue_signature' => $signature] + $record->extra); | ||
|
||
// If the record is a duplicate, we don't want to add it to the store | ||
if ($this->store->isDuplicate($record, $signature)) { | ||
return null; | ||
} | ||
|
||
$this->store->add($record, $signature); | ||
|
||
return $record; | ||
}) | ||
->filter() | ||
->pipe(fn (Collection $records) => $this->handler->handleBatch($records->toArray())); | ||
|
||
$this->clear(); | ||
} | ||
} |
5 changes: 2 additions & 3 deletions
5
src/DefaultSignatureGenerator.php → ...duplication/DefaultSignatureGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/Contracts/SignatureGenerator.php → ...plication/SignatureGeneratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Naoray\LaravelGithubMonolog\Deduplication\Stores; | ||
|
||
use Illuminate\Support\Carbon; | ||
use Monolog\LogRecord; | ||
|
||
abstract class AbstractStore implements StoreInterface | ||
{ | ||
public function __construct( | ||
protected int $time = 60 | ||
) {} | ||
|
||
protected function buildEntry(string $signature, int $timestamp): string | ||
{ | ||
return $timestamp.':'.$signature; | ||
} | ||
|
||
public function isDuplicate(LogRecord $record, string $signature): bool | ||
{ | ||
$foundDuplicate = false; | ||
|
||
foreach ($this->get() as $entry) { | ||
[$timestamp, $storedSignature] = explode(':', $entry, 2); | ||
$timestamp = (int) $timestamp; | ||
|
||
if ($this->isExpired($timestamp)) { | ||
continue; | ||
} | ||
|
||
if ($storedSignature === $signature) { | ||
$foundDuplicate = true; | ||
} | ||
} | ||
|
||
return $foundDuplicate; | ||
} | ||
|
||
protected function isExpired(int $timestamp): bool | ||
{ | ||
return $this->getTimestampValidity() > $timestamp; | ||
} | ||
|
||
protected function getTimestampValidity(): int | ||
{ | ||
return $this->getTimestamp() - $this->time; | ||
} | ||
|
||
protected function getTimestamp(): int | ||
{ | ||
return Carbon::now()->timestamp; | ||
} | ||
} |
Oops, something went wrong.