From 76b1452ef34662cf1d0012b1bb5565698675db88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20L=C3=B3pez?= Date: Sun, 11 Aug 2024 13:04:47 +0200 Subject: [PATCH] feat(TimeWardenSummary): Summary in array or json format To be able to obtain a summary of the actions recorded by TimeWarden, both in array and JSON format. --- src/Concerns/HasTasks.php | 24 ++++++++++ src/Contracts/Taskable.php | 5 ++ src/Services/TimeWardenManager.php | 12 ++++- src/Task.php | 21 ++++++++ src/TimeWardenSummary.php | 33 +++++++++++++ tests/Contracts/TaskableTest.php | 40 ++++++++++++++++ tests/Services/TimeWardenManagerTest.php | 9 ++++ tests/TimeWardenSummaryTest.php | 61 ++++++++++++++++++++++++ 8 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 src/TimeWardenSummary.php create mode 100644 tests/TimeWardenSummaryTest.php diff --git a/src/Concerns/HasTasks.php b/src/Concerns/HasTasks.php index 3ec3679..819b170 100644 --- a/src/Concerns/HasTasks.php +++ b/src/Concerns/HasTasks.php @@ -52,4 +52,28 @@ public function getLastTask(): ?Task return ($lastTask instanceof Task) ? $lastTask : null; } + + public function toArray(): array + { + /** @var array $tasksInfo */ + $tasksInfo = []; + + /** @var Task $task */ + foreach ($this->getTasks() as $task) { + $tasksInfo[] = $task->toArray(); + } + + return [ + 'name' => $this->name, + 'duration' => $this->getDuration(), + 'tasks' => $tasksInfo, + ]; + } + + public function toJson(): string + { + $json = json_encode($this->toArray()); + + return ($json === false) ? '[]' : $json; + } } diff --git a/src/Contracts/Taskable.php b/src/Contracts/Taskable.php index efbf1ff..f53021a 100644 --- a/src/Contracts/Taskable.php +++ b/src/Contracts/Taskable.php @@ -18,4 +18,9 @@ public function getTasks(): array; public function getLastTask(): ?Task; public function getDuration(): float; + + /** @return array */ + public function toArray(): array; + + public function toJson(): string; } diff --git a/src/Services/TimeWardenManager.php b/src/Services/TimeWardenManager.php index dcf95a3..438a15c 100644 --- a/src/Services/TimeWardenManager.php +++ b/src/Services/TimeWardenManager.php @@ -12,11 +12,14 @@ use Tomloprod\TimeWarden\Contracts\Taskable; use Tomloprod\TimeWarden\Group; use Tomloprod\TimeWarden\Task; +use Tomloprod\TimeWarden\TimeWardenSummary; final class TimeWardenManager implements Taskable { use HasTasks; + public string $name = 'default'; + private static TimeWardenManager $instance; /** @@ -125,6 +128,13 @@ public function getGroups(): array return $this->groups; } + public function getSummary(): TimeWardenSummary + { + $this->stop(); + + return new TimeWardenSummary(); + } + public function output(): string { $this->stop(); @@ -200,9 +210,7 @@ public function output(): string ->setHeaders($columns) ->setRows($rows) ->setStyle('box-double') - // ->setFooterTitle('Thanks for using TimeWarden') ->setFooterTitle('Total: '.round($totalDuration, 2).' ms') - ->setHeaderTitle('TIMEWARDEN'); $table->render(); diff --git a/src/Task.php b/src/Task.php index 1da3ddd..c68d225 100644 --- a/src/Task.php +++ b/src/Task.php @@ -4,6 +4,7 @@ namespace Tomloprod\TimeWarden; +use DateTime; use DateTimeImmutable; use Tomloprod\TimeWarden\Contracts\Taskable; @@ -167,4 +168,24 @@ public function setTestEndTimestamp(float $microtime): void { $this->endTimestamp = $microtime; } + + /** @return array */ + public function toArray(): array + { + /** @var ?DateTimeImmutable $startDateTime */ + $startDateTime = $this->getStartDateTime(); + + /** @var ?DateTimeImmutable $endDateTime */ + $endDateTime = $this->getEndDateTime(); + + return [ + 'name' => $this->name, + 'duration' => $this->getDuration(), + 'friendly_duration' => $this->getFriendlyDuration(), + 'start_timestamp' => $this->startTimestamp, + 'end_timestamp' => $this->endTimestamp, + 'start_datetime' => ($startDateTime instanceof DateTimeImmutable) ? $startDateTime->format(DateTime::ATOM) : null, + 'end_datetime' => ($endDateTime instanceof DateTimeImmutable) ? $endDateTime->format(DateTime::ATOM) : null, + ]; + } } diff --git a/src/TimeWardenSummary.php b/src/TimeWardenSummary.php new file mode 100644 index 0000000..0c897d6 --- /dev/null +++ b/src/TimeWardenSummary.php @@ -0,0 +1,33 @@ + */ + public function toArray(): array + { + /** @var array $tasksInfo */ + $tasksInfo = []; + + if (timeWarden()->getTasks() !== []) { + $tasksInfo[] = timeWarden()->toArray(); + } + + /** @var Group $group */ + foreach (timeWarden()->getGroups() as $group) { + $tasksInfo[] = $group->toArray(); + } + + return $tasksInfo; + } + + public function toJson(): string + { + $json = json_encode($this->toArray()); + + return ($json === false) ? '[]' : $json; + } +} diff --git a/tests/Contracts/TaskableTest.php b/tests/Contracts/TaskableTest.php index d74865b..c8e52b9 100644 --- a/tests/Contracts/TaskableTest.php +++ b/tests/Contracts/TaskableTest.php @@ -9,6 +9,8 @@ $this->tasksClass = new class implements Taskable { use HasTasks; + + public string $name = 'default'; }; }); @@ -31,3 +33,41 @@ expect($this->tasksClass->getLastTask()) ->toBeNull(); }); + +it('can obtain an array/json', function (): void { + $task1 = $this->tasksClass->createTask('TaskName1'); + $task1->setTestStartTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); + $task1->setTestEndTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0190000'))); + + $task2 = $this->tasksClass->createTask('TaskName2'); + $task2->setTestStartTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); + $task2->setTestEndTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0230000'))); + + $summaryArray = [ + 'name' => 'default', + 'duration' => 42.0, + 'tasks' => [ + [ + 'name' => 'TaskName1', + 'duration' => 19.0, + 'friendly_duration' => '19ms', + 'start_timestamp' => 1496664000.0, + 'end_timestamp' => 1496664000.019, + 'start_datetime' => '2017-06-05T12:00:00+00:00', + 'end_datetime' => '2017-06-05T12:00:00+00:00', + ], + [ + 'name' => 'TaskName2', + 'duration' => 23.0, + 'friendly_duration' => '23ms', + 'start_timestamp' => 1496664000.0, + 'end_timestamp' => 1496664000.023, + 'start_datetime' => '2017-06-05T12:00:00+00:00', + 'end_datetime' => '2017-06-05T12:00:00+00:00', + ], + ], + ]; + + expect($this->tasksClass->toArray())->toBe($summaryArray); + expect($this->tasksClass->toJson())->toBe(json_encode($summaryArray)); +}); diff --git a/tests/Services/TimeWardenManagerTest.php b/tests/Services/TimeWardenManagerTest.php index 9d4cbaf..0628cab 100644 --- a/tests/Services/TimeWardenManagerTest.php +++ b/tests/Services/TimeWardenManagerTest.php @@ -5,6 +5,7 @@ use Tomloprod\TimeWarden\Group; use Tomloprod\TimeWarden\Services\TimeWardenManager; use Tomloprod\TimeWarden\Task; +use Tomloprod\TimeWarden\TimeWardenSummary; beforeEach(function (): void { TimeWardenManager::instance()->reset(); @@ -189,3 +190,11 @@ ->toContain('Group 2') ->toContain('G2 - Task 1'); }); + +it('can obtain a TimeWardenSummary', function (): void { + $instance = TimeWardenManager::instance(); + + $instance->task('Task1')->task('Task2'); + + expect($instance->getSummary())->toBeInstanceOf(TimeWardenSummary::class); +}); diff --git a/tests/TimeWardenSummaryTest.php b/tests/TimeWardenSummaryTest.php new file mode 100644 index 0000000..45c5df8 --- /dev/null +++ b/tests/TimeWardenSummaryTest.php @@ -0,0 +1,61 @@ +reset(); + + timeWarden()->task('Generic Task')->start()->stop(); + + $task = timeWarden()->getTasks()[0]; + $task->setTestStartTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); + $task->setTestEndTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); + + timeWarden()->group('Group1')->task('TaskName1')->start()->stop(); + + $groupTask = timeWarden()->getGroups()[0]->getTasks()[0]; + $groupTask->setTestStartTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); + $groupTask->setTestEndTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0320000'))); + + /** @var TimeWardenSummary $summary */ + $summary = timeWarden()->getSummary(); + + $summaryArray = [ + [ + 'name' => 'default', + 'duration' => 0.0, + 'tasks' => [ + [ + 'name' => 'Generic Task', + 'duration' => 0.0, + 'friendly_duration' => '0ms', + 'start_timestamp' => 1496664000.0, + 'end_timestamp' => 1496664000.0, + 'start_datetime' => '2017-06-05T12:00:00+00:00', + 'end_datetime' => '2017-06-05T12:00:00+00:00', + ], + ], + ], + [ + 'name' => 'Group1', + 'duration' => 32.0, + 'tasks' => [ + [ + 'name' => 'TaskName1', + 'duration' => 32.0, + 'friendly_duration' => '32ms', + 'start_timestamp' => 1496664000.0, + 'end_timestamp' => 1496664000.032, + 'start_datetime' => '2017-06-05T12:00:00+00:00', + 'end_datetime' => '2017-06-05T12:00:00+00:00', + ], + ], + ], + ]; + + expect($summary->toArray())->toBe($summaryArray); + + expect($summary->toJson())->toBe(json_encode($summaryArray)); +});