diff --git a/README.md b/README.md index 76cb34a..332bde0 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,10 @@ foreach ($customers as $customer) { // Perform long process... } +/** + * You can print the results directly or obtain a + * summary with the `getSummary()` method + */ echo timeWarden()->output(); ``` **Result:** @@ -105,6 +109,10 @@ foreach ($customers as $customer) { timeWarden()->task('Other customer process')->start(); Bar::foo(); +/** + * You can print the results directly or obtain a + * summary with the `getSummary()` method + */ echo timeWarden()->output(); ``` **Result:** @@ -159,8 +167,10 @@ TimeWarden::reset(): TimeWarden // Creates a new group. TimeWarden::group(string $groupName): TimeWarden -// Creates a new task inside the last created group -// or within the TimeWarden instance itself. +/** + * Creates a new task inside the last created group + * or within the TimeWarden instance itself. + */ TimeWarden::task(string $taskName): TimeWarden // Starts the last created task @@ -172,8 +182,20 @@ TimeWarden::stop(): TimeWarden // Obtains all the created groups TimeWarden::getGroups(): array -// Returns a table with execution time debugging info -// (ideal for displaying in the console). +/** + * It allows you to obtain a TimeWardenSummary instance, + * which is useful for getting a summary of all groups + * and tasks generated by TimeWarden. + * + * Through that instance, you can retrieve the summary + * in array or string (JSON) format. + */ +TimeWarden::getSummary(): TimeWardenSummary; + +/** + * Returns a table with execution time debugging info + * (ideal for displaying in the console). + */ TimeWarden::output(): string ``` Additionally, it has all the methods of the [Taskable](#taskable) interface. @@ -209,6 +231,9 @@ $task->getEndDateTime(): ?DateTimeImmutable $task->getStartTimestamp(): float $task->getEndTimestamp(): float +/** @return array */ +$task->toArray(): array + // Reactive execution time methods $task->onExceedsMilliseconds(float $milliseconds, callable $fn): ?Task $task->onExceedsSeconds(float $seconds, callable $fn): ?Task @@ -245,6 +270,28 @@ $taskable->getLastTask(): ?Task; // Return the total time in milliseconds of all tasks within the taskable. $taskable->getDuration(): float; + +$taskable->toArray(): array; + +$taskable->toJson(): string; +``` + +### `TimeWardenSummary` +`Tomloprod\TimeWarden\TimeWardenSummary` is a class that allows obtaining a general summary of groups and their tasks generated with TimeWarden. + +It is useful for obtaining a summary in array or string (JSON) format. + +You can obtain an instance of `TimeWardenSummary` as follows: +```php +/** @var Tomloprod\TimeWarden\TimeWardenSummary $timeWardenSummary */ +$timeWardenSummary = timeWarden()->getSummary(); +``` + +#### Methods +```php + +$timeWardenSummary->toArray(): array; +$timeWardenSummary->toJson(): string; ``` ## **🚀 Installation & Requirements** diff --git a/rector.php b/rector.php index 0c54648..6fde4e1 100644 --- a/rector.php +++ b/rector.php @@ -3,16 +3,13 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector; return RectorConfig::configure() ->withPaths([ __DIR__.'/src', __DIR__.'/tests', ]) - ->withSkip([ - AddOverrideAttributeToOverriddenMethodsRector::class, - ]) + ->withSkip([]) ->withPreparedSets( deadCode: true, codeQuality: true, 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)); +});