diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..cc147fd --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,42 @@ +# Upgrade Guide + +## 0.5.0 + +Two new `integer` columns were added to the `processed_messages` table: +`wait_time` and `handle_time`. You will need to create a migration to +add these columns to your database. They are not nullable so your +migration will need to account for existing data. You can either +truncate (purge) the `processed_messages` table have your migration +calculate these values based on the existing data. + +Here's a calculation example for MySQL: + +```php +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +final class VersionXXX extends AbstractMigration +{ + public function getDescription(): string + { + return 'Add processed_messages.wait_time and handle_time columns'; + } + + public function up(Schema $schema): void + { + // Add the columns as nullable + $this->addSql('ALTER TABLE processed_messages ADD wait_time INT DEFAULT NULL, ADD handle_time INT DEFAULT NULL'); + + // set the times from existing data + $this->addSql('UPDATE processed_messages SET wait_time = TIMESTAMPDIFF(SECOND, dispatched_at, received_at), handle_time = TIMESTAMPDIFF(SECOND, received_at, finished_at)'); + + // Make the columns not nullable + $this->addSql('ALTER TABLE processed_messages CHANGE wait_time wait_time INT NOT NULL, CHANGE handle_time handle_time INT NOT NULL'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE processed_messages DROP wait_time, DROP handle_time'); + } +} +``` diff --git a/config/doctrine/mapping/ProcessedMessage.orm.xml b/config/doctrine/mapping/ProcessedMessage.orm.xml index f2408fe..a03e201 100644 --- a/config/doctrine/mapping/ProcessedMessage.orm.xml +++ b/config/doctrine/mapping/ProcessedMessage.orm.xml @@ -11,6 +11,8 @@ + + diff --git a/src/History/Model/ProcessedMessage.php b/src/History/Model/ProcessedMessage.php index 268cfd7..88198ec 100644 --- a/src/History/Model/ProcessedMessage.php +++ b/src/History/Model/ProcessedMessage.php @@ -37,6 +37,8 @@ abstract class ProcessedMessage private int $memoryUsage; private string $transport; private ?string $tags; + private int $waitTime; + private int $handleTime; /** @var class-string<\Throwable> */ private ?string $failureType = null; @@ -61,6 +63,8 @@ public function __construct(Envelope $envelope, Results $results, ?\Throwable $e $this->transport = $monitorStamp->transport(); $this->tags = $tags->count() ? (string) $tags : null; $this->results = $results; + $this->waitTime = \max(0, $this->receivedAt->getTimestamp() - $this->dispatchedAt->getTimestamp()); + $this->handleTime = \max(0, $this->finishedAt->getTimestamp() - $this->receivedAt->getTimestamp()); if ($retryStamp = $envelope->last(RedeliveryStamp::class)) { $this->attempt += $retryStamp->getRetryCount(); @@ -143,17 +147,17 @@ final public function isFailure(): bool final public function timeInQueue(): int { - return \max(0, $this->receivedAt->getTimestamp() - $this->dispatchedAt->getTimestamp()); + return $this->waitTime; } final public function timeToHandle(): int { - return \max(0, $this->finishedAt->getTimestamp() - $this->receivedAt->getTimestamp()); + return $this->handleTime; } final public function timeToProcess(): int { - return \max(0, $this->finishedAt->getTimestamp() - $this->dispatchedAt->getTimestamp()); + return $this->waitTime + $this->handleTime; } final public function memoryUsage(): Bytes diff --git a/src/History/Storage/ORMStorage.php b/src/History/Storage/ORMStorage.php index bbf1863..f67c71f 100644 --- a/src/History/Storage/ORMStorage.php +++ b/src/History/Storage/ORMStorage.php @@ -92,7 +92,7 @@ public function averageWaitTime(Specification $specification): ?float { $qb = $this ->queryBuilderFor($specification, false) - ->select('AVG(m.receivedAt - m.dispatchedAt)') + ->select('AVG(m.waitTime)') ; return (new EntityResult($qb))->asFloat()->first(); @@ -102,7 +102,7 @@ public function averageHandlingTime(Specification $specification): ?float { $qb = $this ->queryBuilderFor($specification, false) - ->select('AVG(m.finishedAt - m.receivedAt)') + ->select('AVG(m.handleTime)') ; return (new EntityResult($qb))->asFloat()->first(); diff --git a/tests/Fixture/Factory/ProcessedMessageFactory.php b/tests/Fixture/Factory/ProcessedMessageFactory.php index 09eca70..20b06b7 100644 --- a/tests/Fixture/Factory/ProcessedMessageFactory.php +++ b/tests/Fixture/Factory/ProcessedMessageFactory.php @@ -29,6 +29,17 @@ protected function initialize(): static { return parent::initialize() ->instantiateWith(Instantiator::withoutConstructor()->alwaysForce()) + ->beforeInstantiate(function(array $attributes) { + if (!isset($attributes['waitTime'])) { + $attributes['waitTime'] = \max(0, $attributes['receivedAt']->getTimestamp() - $attributes['dispatchedAt']->getTimestamp()); + } + + if (!isset($attributes['processingTime'])) { + $attributes['handleTime'] = \max(0, $attributes['finishedAt']->getTimestamp() - $attributes['receivedAt']->getTimestamp()); + } + + return $attributes; + }) ; }