diff --git a/CHANGELOG.md b/CHANGELOG.md index 310c0e2..6ec9b0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +2.3.3 (unreleased) +===== + +* (improvement) Simplify task run duration calculation. + + + 2.3.2 ===== diff --git a/src/Duration/DurationCalculator.php b/src/Duration/DurationCalculator.php new file mode 100644 index 0000000..92d3494 --- /dev/null +++ b/src/Duration/DurationCalculator.php @@ -0,0 +1,25 @@ +diff($end, true); + + $seconds = $diff->days * 24 * 60 * 60 + + $diff->h * 60 * 60 + + $diff->i * 60 + + $diff->s + + $diff->f; + + return $seconds / 1e9; + } +} diff --git a/src/Entity/TaskRun.php b/src/Entity/TaskRun.php index 3e96751..fefc519 100644 --- a/src/Entity/TaskRun.php +++ b/src/Entity/TaskRun.php @@ -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; @@ -55,11 +56,6 @@ class TaskRun */ #[ORM\Column(type: Types::TEXT, nullable: true)] private ?string $output = null; - - /** - * - */ - private ?float $start = null; // endregion /** @@ -68,7 +64,6 @@ public function __construct (TaskLog $taskLog) { $this->taskLog = $taskLog; $this->timeStarted = now(); - $this->start = hrtime(true); } // region Accessors @@ -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()); } } diff --git a/tests/Duration/DurationCalculatorTest.php b/tests/Duration/DurationCalculatorTest.php new file mode 100644 index 0000000..eb63f6c --- /dev/null +++ b/tests/Duration/DurationCalculatorTest.php @@ -0,0 +1,60 @@ + [ + "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)); + } +}