Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2.3.3 (unreleased)
=====

* (improvement) Simplify task run duration calculation.



2.3.2
=====

Expand Down
25 changes: 25 additions & 0 deletions src/Duration/DurationCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Duration;

/**
* @final
*/
readonly class DurationCalculator
{
/**
* Returns the duration in nanoseconds
*/
public function calculateDuration (\DateTimeImmutable $start, \DateTimeImmutable $end) : float
{
$diff = $start->diff($end, true);

$seconds = $diff->days * 24 * 60 * 60
+ $diff->h * 60 * 60
+ $diff->i * 60
+ $diff->s
+ $diff->f;

return $seconds / 1e9;
}
}
14 changes: 2 additions & 12 deletions src/Entity/TaskRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Torr\TaskManager\Duration\DurationCalculator;
use Torr\TaskManager\Exception\Log\InvalidLogActionException;

use function Symfony\Component\Clock\now;
Expand Down Expand Up @@ -55,11 +56,6 @@ class TaskRun
*/
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $output = null;

/**
*
*/
private ?float $start = null;
// endregion

/**
Expand All @@ -68,7 +64,6 @@ public function __construct (TaskLog $taskLog)
{
$this->taskLog = $taskLog;
$this->timeStarted = now();
$this->start = hrtime(true);
}

// region Accessors
Expand Down Expand Up @@ -164,14 +159,9 @@ private function finalizeRun (
throw new InvalidLogActionException("Can't finalize task run #{$this->id} as it is already finished.");
}

if (null === $this->start)
{
throw new InvalidLogActionException("Can't finalize a task that wasn't started in this run.");
}

$this->success = $success;
$this->finishedProperly = $finishedProperly;
$this->output = $output;
$this->duration = hrtime(true) - $this->start;
$this->duration = new DurationCalculator()->calculateDuration($this->timeStarted, now());
}
}
60 changes: 60 additions & 0 deletions tests/Duration/DurationCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);

namespace Tests\Torr\TaskManager\Duration;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Torr\TaskManager\Duration\DurationCalculator;

/**
* @internal
*/
final class DurationCalculatorTest extends TestCase
{
/**
*
*/
public static function provideCalculation () : iterable
{
yield "hours-minutes-seconds-microseconds" => [
"2020-01-01 00:00:00.000000",
"2020-01-01 01:02:03.123456",
(3600 + 120 + 3.123456) / 1e9,
];

yield "years" => [
"2021-01-01 00:00:00.000000",
"2022-01-01 00:00:00.000000",
(365 * 24 * 60 * 60) / 1e9,
];

yield "months" => [
"2021-01-01 00:00:00.000000",
"2021-02-01 00:00:00.000000",
(31 * 24 * 60 * 60) / 1e9,
];

yield "days" => [
"2021-01-01 00:00:00.000000",
"2021-01-04 00:00:00.000000",
(3 * 24 * 60 * 60) / 1e9,
];
}

/**
*
*/
#[DataProvider("provideCalculation")]
public function testCalculation (
string $start,
string $end,
float $expected,
) : void
{
$start = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s.u", $start);
$end = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s.u", $end);
$calculator = new DurationCalculator();

self::assertSame($expected, $calculator->calculateDuration($start, $end));
}
}