Skip to content

Commit 1bafd75

Browse files
committed
Fix #15 clear multiple same errors
1 parent 911ac1c commit 1bafd75

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

src/Entity/Record.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BugCatcher\Entity;
44

55
use ApiPlatform\Metadata\ApiProperty;
6+
use DateTime;
67
use DateTimeImmutable;
78
use DateTimeInterface;
89
use Symfony\Component\Serializer\Annotation\Groups;
@@ -49,6 +50,7 @@ public function __construct(DateTimeInterface $date = null) {
4950
}
5051

5152
private int $count = 1;
53+
private ?DateTime $firstOccurrence = null;
5254

5355
public function getId(): ?Uuid {
5456
return $this->id;
@@ -96,6 +98,17 @@ public function setCount(int $count): self {
9698
return $this;
9799
}
98100

101+
public function getFirstOccurrence(): ?DateTime
102+
{
103+
return $this->firstOccurrence;
104+
}
105+
106+
public function setFirstOccurrence(?DateTime $firstOccurrence): self
107+
{
108+
$this->firstOccurrence = $firstOccurrence;
109+
return $this;
110+
}
111+
99112
public function getProjectCode(): ?string {
100113
return $this->projectCode;
101114
}
@@ -122,4 +135,5 @@ abstract function getComponentName(): string;
122135

123136
abstract function isError(): bool;
124137

138+
125139
}

src/Twig/Components/LogList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function init(): void
8585
foreach ($records as $row) {
8686
$record = $logs[$row->getHash()] = $logs[$row->getHash()] ?? $row->setCount(0);
8787
$record->setCount($record->getCount() + 1);
88+
$record->setFirstOccurrence($row->getDate());
8889
}
8990

9091
$this->logs = array_values($logs);

src/Twig/Components/LogList/RecordLog.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
*/
88
namespace BugCatcher\Twig\Components\LogList;
99

10+
use DateTimeInterface;
1011
use Doctrine\Persistence\ManagerRegistry;
1112
use BugCatcher\Entity\Record;
1213
use BugCatcher\Repository\RecordRepository;
1314
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1415
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\Attribute\MapDateTime;
1517
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1618
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1719
use Symfony\UX\LiveComponent\Attribute\LiveArg;
@@ -37,15 +39,16 @@ public function __construct(
3739

3840
#[LiveAction]
3941
public function clearOne(
40-
#[LiveArg] string $status
42+
#[LiveArg] string $status,
43+
#[LiveArg] #[MapDateTime(format: "Y-m-d-H-i-s")] DateTimeInterface $from,
4144
) {
4245
if (!$this->log) {
4346
return;
4447
}
4548
$class = $this->log::class;
4649
$repo = $this->registry->getRepository($class);
4750
assert($repo instanceof RecordRepository);
48-
$repo->setStatus($this->log, $this->log->getDate(), $status, $this->status, true);
51+
$repo->setStatus($this->log, $from, $status, $this->status, true);
4952
$this->log = null;
5053
}
5154
}

templates/components/LogList/RecordLog.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
data-action="live#action"
3131
data-live-action-param="clearOne"
3232
data-live-status-param="resolved"
33+
data-live-from-param="{{ log.firstOccurrence|date('Y-m-d-H-i-s') }}"
3334
title="{{ 'Fix it'|trans }}"
3435
>
3536
<twig:ux:icon name="game-icons:magic-broom" width="20px" height="20px"/>

tests/Integration/Twig/LogList/RecordLogTest.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use BugCatcher\Tests\App\KernelTestCase;
1414
use BugCatcher\Tests\Integration\Trait\GetStackTrace;
1515
use BugCatcher\Twig\Components\LogList\RecordLog;
16+
use DateTimeImmutable;
1617
use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents;
1718
use Zenstruck\Foundry\Test\Factories;
1819
use Zenstruck\Foundry\Test\ResetDatabase;
@@ -25,22 +26,23 @@ class RecordLogTest extends KernelTestCase {
2526

2627
public function testClearOne() {
2728

28-
RecordLogFactory::createMany(10, [
29+
$startDate = new DateTime("2022-01-01 00:00:00");
30+
RecordLogFactory::createMany(10, [
2931
"status" => "new",
3032
"hash" => "hash-2",
31-
"date" => new DateTime("2022-01-01 00:00:00"),
33+
"date" => $startDate,
3234
]);
3335

3436
$record = RecordLogFactory::createMany(10, [
3537
"status" => "new",
3638
"hash" => "hash",
37-
"date" => new DateTime("2022-01-01 00:00:00"),
39+
"date" => $startDate,
3840
])[0];
3941

4042
$rendered = $this->mountTwigComponent('LogList:RecordLog', ['log' => $record->_real(), "status" => "new"]);
4143
$this->assertInstanceOf(RecordLog::class, $rendered);
4244

43-
$rendered->clearOne("resolved");
45+
$rendered->clearOne("resolved", $startDate);
4446

4547
$count = RecordLogFactory::count([
4648
"hash" => "hash",
@@ -56,17 +58,55 @@ public function testClearOne() {
5658

5759
}
5860

61+
function testClearOneStack(): void
62+
{
63+
$startDate = new DateTimeImmutable("2022-01-01 00:00:00");
64+
65+
for ($i = 1; $i <= 10; $i++) {
66+
RecordLogFactory::createOne([
67+
"status" => "new",
68+
"hash" => "hash",
69+
"date" => $startDate->modify("+{$i} day"),
70+
]);
71+
}
72+
$lastDate = $startDate->modify("+11 day");
73+
$record = RecordLogFactory::createOne([
74+
"status" => "new",
75+
"hash" => "hash",
76+
"date" => $lastDate,
77+
]);
78+
79+
$rendered = $this->mountTwigComponent('LogList:RecordLog', ['log' => $record->_real(), "status" => "new"]);
80+
$this->assertInstanceOf(RecordLog::class, $rendered);
81+
82+
$count = RecordLogFactory::count([
83+
"hash" => "hash",
84+
"status" => "new",
85+
]);
86+
$this->assertEquals(11, $count);
87+
88+
$rendered->clearOne("resolved", $startDate);
89+
90+
$count = RecordLogFactory::count([
91+
"hash" => "hash",
92+
"status" => "new",
93+
]);
94+
$this->assertEquals(0, $count);
95+
}
96+
5997
public function testClearStackTrace() {
98+
$startDate = new DateTimeImmutable("2022-01-01 00:00:00");
6099
$record = RecordLogTraceFactory::createOne([
61100
"stackTrace" => $this->getStackTrace(),
62101
"status" => "new",
63102
"hash" => "hash",
103+
"date" => $startDate,
64104
]);
65105

66106
$rendered = $this->mountTwigComponent('LogList:RecordLog', ['log' => $record->_real(), "status" => "new"]);
67107
$this->assertInstanceOf(RecordLog::class, $rendered);
68108

69-
$rendered->clearOne("resolved");
109+
$rendered->clearOne("resolved", $startDate);
70110
$record->_refresh();
71111
$this->assertNull($record->getStackTrace());
72112

0 commit comments

Comments
 (0)