Skip to content

Commit

Permalink
feat(TimeWardenSummary): Summary in array or json format
Browse files Browse the repository at this point in the history
To be able to obtain a summary of the actions recorded by TimeWarden, both in array and JSON format.
  • Loading branch information
tomloprod committed Aug 11, 2024
1 parent fa1ed8c commit 76b1452
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/Concerns/HasTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,28 @@ public function getLastTask(): ?Task

return ($lastTask instanceof Task) ? $lastTask : null;
}

public function toArray(): array
{
/** @var array<string, mixed> $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;
}
}
5 changes: 5 additions & 0 deletions src/Contracts/Taskable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public function getTasks(): array;
public function getLastTask(): ?Task;

public function getDuration(): float;

/** @return array<string, mixed> */
public function toArray(): array;

public function toJson(): string;
}
12 changes: 10 additions & 2 deletions src/Services/TimeWardenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tomloprod\TimeWarden;

use DateTime;
use DateTimeImmutable;
use Tomloprod\TimeWarden\Contracts\Taskable;

Expand Down Expand Up @@ -167,4 +168,24 @@ public function setTestEndTimestamp(float $microtime): void
{
$this->endTimestamp = $microtime;
}

/** @return array<string, mixed> */
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,
];
}
}
33 changes: 33 additions & 0 deletions src/TimeWardenSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Tomloprod\TimeWarden;

final class TimeWardenSummary
{
/** @return array<string, mixed> */
public function toArray(): array
{
/** @var array<string, mixed> $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;
}
}
40 changes: 40 additions & 0 deletions tests/Contracts/TaskableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
$this->tasksClass = new class implements Taskable
{
use HasTasks;

public string $name = 'default';
};
});

Expand All @@ -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));
});
9 changes: 9 additions & 0 deletions tests/Services/TimeWardenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
61 changes: 61 additions & 0 deletions tests/TimeWardenSummaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

use Tomloprod\TimeWarden\TimeWardenSummary;

it('can obtain an array/json', function (): void {
timeWarden()->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));
});

0 comments on commit 76b1452

Please sign in to comment.