Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #16 from ph-ash/fix-error-tiles-priority
Browse files Browse the repository at this point in the history
fixed a bug where error tiles growing over time do not return to norm…
  • Loading branch information
MollocH authored Apr 2, 2019
2 parents 46d54e3 + 7a93e67 commit 51043f3
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 30 deletions.
13 changes: 13 additions & 0 deletions src/Enum/MonitoringStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Enum;

use MyCLabs\Enum\Enum;

class MonitoringStatus extends Enum
{
private const OK = 'ok';
private const ERROR = 'error';
}
3 changes: 2 additions & 1 deletion src/Repository/MonitoringDataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Repository;

use App\Document\MonitoringData;
use App\Enum\MonitoringStatus;
use App\Exception\PersistenceLayerException;
use App\Repository\MonitoringData as MonitoringDataInterface;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function findAll(): array

public function findAllErroneousMonitorings(): array
{
return $this->findBy(['status' => 'error']);
return $this->findBy(['status' => MonitoringStatus::ERROR()->getValue()]);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Service/Persistance/PriorityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service\Persistance;

use App\Dto\MonitoringData;
use App\Enum\MonitoringStatus;
use App\Repository\MonitoringDataRepository;

class PriorityService implements Priority
Expand All @@ -21,7 +22,10 @@ public function calculate(MonitoringData $monitoringData): int
$oldMonitoringDataDocument = $this->monitoringDataRepository->find($monitoringData->getId());
$priority = $monitoringData->getPriority();

if ($oldMonitoringDataDocument && $oldMonitoringDataDocument->getPriority() > $monitoringData->getPriority()) {
if ($oldMonitoringDataDocument &&
$oldMonitoringDataDocument->getPriority() > $monitoringData->getPriority() &&
$monitoringData->getStatus() === MonitoringStatus::ERROR()->getValue()
) {
$priority = $oldMonitoringDataDocument->getPriority();
}

Expand Down
3 changes: 0 additions & 3 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,6 @@
"symfony/templating": {
"version": "v4.1.7"
},
"symfony/test-pack": {
"version": "v1.0.4"
},
"symfony/twig-bridge": {
"version": "v4.1.7"
},
Expand Down
119 changes: 94 additions & 25 deletions tests/Unit/Service/Persistance/PriorityServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,108 @@ public function setUp()
$this->subject = new PriorityService($this->monitoringDataRepository->reveal());
}

public function testCalculateNoPersistedDocument(): void
{
$monitoringDataDto = new MonitoringDataDto(
'id', 'satus', 'payload', 1, 60, new DateTimeImmutable(), 'root.branch.leaf', 5, '* 2'
);
$this->monitoringDataRepository->find('id')->shouldBeCalledOnce()->willReturn(null);
/**
* @dataProvider provideMonitoringData
*/
public function testCalculate(
MonitoringDataDto $newMonitoringData,
?MonitoringDataDocument $oldMonitoringData,
int $expectedPriority
): void {
$this->monitoringDataRepository->find('id')->shouldBeCalledOnce()->willReturn($oldMonitoringData);

self::assertSame(1, $this->subject->calculate($monitoringDataDto));
self::assertSame($expectedPriority, $this->subject->calculate($newMonitoringData));
}

public function testCalculateSameOrLowerPriority(): void
public function provideMonitoringData(): array
{
$monitoringDataDto = new MonitoringDataDto(
'id', 'satus', 'payload', 15, 60, new DateTimeImmutable(), 'root.branch.leaf', 5, '* 2'
);
$monitoringData = new MonitoringDataDocument(
'id', 'satus', new DateTime('2019-01-01 00:00:00'), 'payload', 9, 60, new DateTimeImmutable(), 'root.branch.leaf', 5, '* 2'
);
$this->monitoringDataRepository->find('id')->shouldBeCalledOnce()->willReturn($monitoringData);

self::assertSame(15, $this->subject->calculate($monitoringDataDto));
return [
'old monitoring prio > new monitoring prio (both error)' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 10),
$oldMonitoringData = $this->createMonitoringDataDocument('error', 20),
20
],
'old monitoring prio = new monitoring prio (both error)' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 20),
$oldMonitoringData = $this->createMonitoringDataDocument('error', 20),
20
],
'old monitoring prio < new monitoring prio (both error)' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 20),
$oldMonitoringData = $this->createMonitoringDataDocument('error', 10),
20
],
'old monitoring prio OK > new monitoring prio ERROR' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 10),
$oldMonitoringData = $this->createMonitoringDataDocument('ok', 20),
20
],
'old monitoring prio OK < new monitoring prio ERROR' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 20),
$oldMonitoringData = $this->createMonitoringDataDocument('ok', 10),
20
],
'old monitoring prio OK = new monitoring prio ERROR' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 20),
$oldMonitoringData = $this->createMonitoringDataDocument('ok', 20),
20
],
'old monitoring prio ERROR > new monitoring prio OK' => [
$newMonitoringData = $this->createMonitoringDataDto('ok', 10),
$oldMonitoringData = $this->createMonitoringDataDocument('error', 20),
10
],
'old monitoring prio ERROR = new monitoring prio OK' => [
$newMonitoringData = $this->createMonitoringDataDto('ok', 20),
$oldMonitoringData = $this->createMonitoringDataDocument('error', 20),
20
],
'old monitoring prio ERROR < new monitoring prio OK' => [
$newMonitoringData = $this->createMonitoringDataDto('ok', 20),
$oldMonitoringData = $this->createMonitoringDataDocument('error', 10),
20
],
'no old monitoring | new monitoring error' => [
$newMonitoringData = $this->createMonitoringDataDto('error', 20),
null,
20
],
'no old monitoring | new monitoring ok' => [
$newMonitoringData = $this->createMonitoringDataDto('ok', 10),
null,
10
],
];
}

public function testCalculate(): void
private function createMonitoringDataDto(string $status, int $priority): MonitoringDataDto
{
$monitoringDataDto = new MonitoringDataDto(
'id', 'satus', 'payload', 15, 60, new DateTimeImmutable(), 'root.branch.leaf', 5, '* 2'
return new MonitoringDataDto(
'id',
$status,
'payload',
$priority,
60,
new DateTimeImmutable(),
'root.branch.leaf',
5,
'* 2'
);
$monitoringData = new MonitoringDataDocument(
'id', 'satus', new DateTime('2019-01-01 00:00:00'), 'payload', 27, 60, new DateTimeImmutable(), 'root.branch.leaf', 5, '* 2'
);
$this->monitoringDataRepository->find('id')->shouldBeCalledOnce()->willReturn($monitoringData);
}

self::assertSame(27, $this->subject->calculate($monitoringDataDto));
private function createMonitoringDataDocument(string $status, int $priority): MonitoringDataDocument
{
return new MonitoringDataDocument(
'id',
$status,
new DateTime('2019-01-01 00:00:00'),
'payload',
$priority,
60,
new DateTimeImmutable(),
'root.branch.leaf',
5,
'* 2'
);
}
}

0 comments on commit 51043f3

Please sign in to comment.