Skip to content

Commit 93c529c

Browse files
authored
Performance stats should be calculated at the end of the request (#80)
1 parent 74c30b5 commit 93c529c

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

src/Entity/Output/LogRecordsOutput.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33

44
namespace FD\LogViewer\Entity\Output;
55

6-
use FD\LogViewer\Entity\Index\LogRecordCollection;
6+
use FD\LogViewer\Entity\Index\LogRecord;
7+
use FD\LogViewer\Entity\Index\Paginator;
78
use FD\LogViewer\Entity\Index\PerformanceStats;
89
use JsonSerializable;
910

1011
class LogRecordsOutput implements JsonSerializable
1112
{
12-
public function __construct(private readonly LogRecordCollection $recordCollection, private readonly PerformanceStats $performance)
13-
{
13+
/**
14+
* @param LogRecord[] $records
15+
*/
16+
public function __construct(
17+
private readonly array $records,
18+
private readonly ?Paginator $paginator,
19+
private readonly PerformanceStats $performance
20+
) {
1421
}
1522

1623
/**
@@ -19,8 +26,8 @@ public function __construct(private readonly LogRecordCollection $recordCollecti
1926
public function jsonSerialize(): array
2027
{
2128
return [
22-
'logs' => $this->recordCollection->getRecords(),
23-
'paginator' => $this->recordCollection->getPaginator(),
29+
'logs' => $this->records,
30+
'paginator' => $this->paginator,
2431
'performance' => $this->performance
2532
];
2633
}

src/Service/File/LogRecordsOutputProvider.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,22 @@ public function provideForFiles(array $files, LogQueryDto $logQuery): LogRecords
3939
$recordIterator = new LimitIterator($recordIterator, $logQuery->perPage);
4040
$logRecordCollection = new LogRecordCollection($recordIterator, null);
4141

42-
return new LogRecordsOutput($logRecordCollection, $this->performanceService->getPerformanceStats());
42+
return new LogRecordsOutput(
43+
$logRecordCollection->getRecords(),
44+
$logRecordCollection->getPaginator(),
45+
$this->performanceService->getPerformanceStats()
46+
);
4347
}
4448

4549
public function provide(LogFile $file, LogQueryDto $logQuery): LogRecordsOutput
4650
{
47-
$config = $file->folder->collection->config;
48-
$logIndex = $this->logParserProvider->get($config->type)->getLogIndex($config, $file, $logQuery);
49-
50-
return new LogRecordsOutput($logIndex, $this->performanceService->getPerformanceStats());
51+
$config = $file->folder->collection->config;
52+
$logRecordCollection = $this->logParserProvider->get($config->type)->getLogIndex($config, $file, $logQuery);
53+
54+
return new LogRecordsOutput(
55+
$logRecordCollection->getRecords(),
56+
$logRecordCollection->getPaginator(),
57+
$this->performanceService->getPerformanceStats()
58+
);
5159
}
5260
}

tests/Unit/Entity/Output/LogRecordsOutputTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

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

6-
use ArrayIterator;
76
use FD\LogViewer\Entity\Index\LogRecord;
8-
use FD\LogViewer\Entity\Index\LogRecordCollection;
97
use FD\LogViewer\Entity\Index\Paginator;
108
use FD\LogViewer\Entity\Index\PerformanceStats;
119
use FD\LogViewer\Entity\Output\DirectionEnum;
@@ -18,12 +16,11 @@ class LogRecordsOutputTest extends TestCase
1816
{
1917
public function testJsonSerialize(): void
2018
{
21-
$paginator = new Paginator(DirectionEnum::Asc, true, true, 123);
22-
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
23-
$recordCollection = new LogRecordCollection(new ArrayIterator([$record]), fn() => $paginator);
24-
$performance = $this->createMock(PerformanceStats::class);
19+
$paginator = new Paginator(DirectionEnum::Asc, true, true, 123);
20+
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
21+
$performance = $this->createMock(PerformanceStats::class);
2522

26-
$logRecordsOutput = new LogRecordsOutput($recordCollection, $performance);
23+
$logRecordsOutput = new LogRecordsOutput([$record], $paginator, $performance);
2724

2825
static::assertSame(
2926
[

tests/Unit/Service/File/LogRecordsOutputProviderTest.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55

66
use ArrayIterator;
77
use Exception;
8+
use FD\LogViewer\Entity\Index\LogRecord;
89
use FD\LogViewer\Entity\Index\LogRecordCollection;
910
use FD\LogViewer\Entity\Index\PerformanceStats;
1011
use FD\LogViewer\Entity\Output\LogRecordsOutput;
1112
use FD\LogViewer\Entity\Request\LogQueryDto;
12-
use FD\LogViewer\Iterator\DeduplicationIterator;
13-
use FD\LogViewer\Iterator\LimitIterator;
14-
use FD\LogViewer\Iterator\MultiLogRecordIterator;
1513
use FD\LogViewer\Service\File\LogFileParserInterface;
1614
use FD\LogViewer\Service\File\LogFileParserProvider;
17-
use FD\LogViewer\Service\File\LogRecordDateComparator;
1815
use FD\LogViewer\Service\File\LogRecordsOutputProvider;
1916
use FD\LogViewer\Service\PerformanceService;
2017
use FD\LogViewer\Tests\Utility\TestEntityTrait;
@@ -46,13 +43,16 @@ public function testProvide(): void
4643
$logQuery = new LogQueryDto(['identifier']);
4744
$file = $this->createLogFile();
4845
$config = $file->folder->collection->config;
46+
$record = new LogRecord('id', 111111, 'debug', 'request', 'message', [], []);
4947
$recordCollection = $this->createMock(LogRecordCollection::class);
50-
$performance = new PerformanceStats('1', '2', '3');
48+
$recordCollection->method('getRecords')->willReturn([$record]);
49+
$recordCollection->method('getPaginator')->willReturn(null);
50+
$performance = new PerformanceStats('1', '2', '3');
5151

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

55-
$expected = new LogRecordsOutput($recordCollection, $performance);
55+
$expected = new LogRecordsOutput([$record], null, $performance);
5656

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

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

77-
$expected = new LogRecordsOutput(
78-
new LogRecordCollection(
79-
new LimitIterator(
80-
new DeduplicationIterator(new MultiLogRecordIterator([$iterator], new LogRecordDateComparator($logQuery->direction))),
81-
$logQuery->perPage
82-
),
83-
null
84-
),
85-
$performance
86-
);
78+
$expected = new LogRecordsOutput([$record], null, $performance);
8779

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

0 commit comments

Comments
 (0)