Skip to content

Commit

Permalink
Performance stats should be calculated at the end of the request (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdekker authored Mar 29, 2024
1 parent 74c30b5 commit 93c529c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
17 changes: 12 additions & 5 deletions src/Entity/Output/LogRecordsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@

namespace FD\LogViewer\Entity\Output;

use FD\LogViewer\Entity\Index\LogRecordCollection;
use FD\LogViewer\Entity\Index\LogRecord;
use FD\LogViewer\Entity\Index\Paginator;
use FD\LogViewer\Entity\Index\PerformanceStats;
use JsonSerializable;

class LogRecordsOutput implements JsonSerializable
{
public function __construct(private readonly LogRecordCollection $recordCollection, private readonly PerformanceStats $performance)
{
/**
* @param LogRecord[] $records
*/
public function __construct(
private readonly array $records,
private readonly ?Paginator $paginator,
private readonly PerformanceStats $performance
) {
}

/**
Expand All @@ -19,8 +26,8 @@ public function __construct(private readonly LogRecordCollection $recordCollecti
public function jsonSerialize(): array
{
return [
'logs' => $this->recordCollection->getRecords(),
'paginator' => $this->recordCollection->getPaginator(),
'logs' => $this->records,
'paginator' => $this->paginator,
'performance' => $this->performance
];
}
Expand Down
18 changes: 13 additions & 5 deletions src/Service/File/LogRecordsOutputProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ public function provideForFiles(array $files, LogQueryDto $logQuery): LogRecords
$recordIterator = new LimitIterator($recordIterator, $logQuery->perPage);
$logRecordCollection = new LogRecordCollection($recordIterator, null);

return new LogRecordsOutput($logRecordCollection, $this->performanceService->getPerformanceStats());
return new LogRecordsOutput(
$logRecordCollection->getRecords(),
$logRecordCollection->getPaginator(),
$this->performanceService->getPerformanceStats()
);
}

public function provide(LogFile $file, LogQueryDto $logQuery): LogRecordsOutput
{
$config = $file->folder->collection->config;
$logIndex = $this->logParserProvider->get($config->type)->getLogIndex($config, $file, $logQuery);

return new LogRecordsOutput($logIndex, $this->performanceService->getPerformanceStats());
$config = $file->folder->collection->config;
$logRecordCollection = $this->logParserProvider->get($config->type)->getLogIndex($config, $file, $logQuery);

return new LogRecordsOutput(
$logRecordCollection->getRecords(),
$logRecordCollection->getPaginator(),
$this->performanceService->getPerformanceStats()
);
}
}
11 changes: 4 additions & 7 deletions tests/Unit/Entity/Output/LogRecordsOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace FD\LogViewer\Tests\Unit\Entity\Output;

use ArrayIterator;
use FD\LogViewer\Entity\Index\LogRecord;
use FD\LogViewer\Entity\Index\LogRecordCollection;
use FD\LogViewer\Entity\Index\Paginator;
use FD\LogViewer\Entity\Index\PerformanceStats;
use FD\LogViewer\Entity\Output\DirectionEnum;
Expand All @@ -18,12 +16,11 @@ class LogRecordsOutputTest extends TestCase
{
public function testJsonSerialize(): void
{
$paginator = new Paginator(DirectionEnum::Asc, true, true, 123);
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
$recordCollection = new LogRecordCollection(new ArrayIterator([$record]), fn() => $paginator);
$performance = $this->createMock(PerformanceStats::class);
$paginator = new Paginator(DirectionEnum::Asc, true, true, 123);
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
$performance = $this->createMock(PerformanceStats::class);

$logRecordsOutput = new LogRecordsOutput($recordCollection, $performance);
$logRecordsOutput = new LogRecordsOutput([$record], $paginator, $performance);

static::assertSame(
[
Expand Down
30 changes: 11 additions & 19 deletions tests/Unit/Service/File/LogRecordsOutputProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@

use ArrayIterator;
use Exception;
use FD\LogViewer\Entity\Index\LogRecord;
use FD\LogViewer\Entity\Index\LogRecordCollection;
use FD\LogViewer\Entity\Index\PerformanceStats;
use FD\LogViewer\Entity\Output\LogRecordsOutput;
use FD\LogViewer\Entity\Request\LogQueryDto;
use FD\LogViewer\Iterator\DeduplicationIterator;
use FD\LogViewer\Iterator\LimitIterator;
use FD\LogViewer\Iterator\MultiLogRecordIterator;
use FD\LogViewer\Service\File\LogFileParserInterface;
use FD\LogViewer\Service\File\LogFileParserProvider;
use FD\LogViewer\Service\File\LogRecordDateComparator;
use FD\LogViewer\Service\File\LogRecordsOutputProvider;
use FD\LogViewer\Service\PerformanceService;
use FD\LogViewer\Tests\Utility\TestEntityTrait;
Expand Down Expand Up @@ -46,13 +43,16 @@ public function testProvide(): void
$logQuery = new LogQueryDto(['identifier']);
$file = $this->createLogFile();
$config = $file->folder->collection->config;
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
$recordCollection = $this->createMock(LogRecordCollection::class);
$performance = new PerformanceStats('1', '2', '3');
$recordCollection->method('getRecords')->willReturn([$record]);
$recordCollection->method('getPaginator')->willReturn(null);
$performance = new PerformanceStats('1', '2', '3');

$this->logParser->expects(self::once())->method('getLogIndex')->with($config, $file, $logQuery)->willReturn($recordCollection);
$this->performanceService->expects(self::once())->method('getPerformanceStats')->willReturn($performance);

$expected = new LogRecordsOutput($recordCollection, $performance);
$expected = new LogRecordsOutput([$record], null, $performance);

$result = $this->provider->provide($file, $logQuery);
static::assertEquals($expected, $result);
Expand All @@ -66,24 +66,16 @@ public function testProvideForFiles(): void
$logQuery = new LogQueryDto(['identifier']);
$file = $this->createLogFile();
$config = $file->folder->collection->config;
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
$recordCollection = $this->createMock(LogRecordCollection::class);
$iterator = new ArrayIterator([]);
$performance = new PerformanceStats('1', '2', '3');
$recordCollection->method('getIterator')->willReturn(new ArrayIterator([$record]));
$recordCollection->method('getPaginator')->willReturn(null);
$performance = new PerformanceStats('1', '2', '3');

$this->logParser->expects(self::once())->method('getLogIndex')->with($config, $file, $logQuery)->willReturn($recordCollection);
$recordCollection->expects(self::once())->method('getIterator')->willReturn($iterator);
$this->performanceService->expects(self::once())->method('getPerformanceStats')->willReturn($performance);

$expected = new LogRecordsOutput(
new LogRecordCollection(
new LimitIterator(
new DeduplicationIterator(new MultiLogRecordIterator([$iterator], new LogRecordDateComparator($logQuery->direction))),
$logQuery->perPage
),
null
),
$performance
);
$expected = new LogRecordsOutput([$record], null, $performance);

$result = $this->provider->provideForFiles(['foo' => $file], $logQuery);
static::assertEquals($expected, $result);
Expand Down

0 comments on commit 93c529c

Please sign in to comment.