From 31d043d35ba785bd602390e9c8b2e7e75c0c4b6a Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Thu, 14 Dec 2023 11:37:02 +0300 Subject: [PATCH] task viewer metrics (#40) * task viewer metrics * task viewer metrics --- src/dto/TaskView.php | 5 +++++ src/service/TaskViewer.php | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/dto/TaskView.php b/src/dto/TaskView.php index 51c058e..c8c4148 100755 --- a/src/dto/TaskView.php +++ b/src/dto/TaskView.php @@ -17,6 +17,7 @@ public function __construct( public readonly string $state, public readonly string $message, public readonly TaskMetrics $metrics, + public readonly ?TaskView $relation, public readonly string $createdAt, public readonly string $updatedAt, ) { @@ -28,6 +29,10 @@ public function toArray(): array $attributes = get_object_vars($this); $attributes['metrics'] = $this->metrics->toArray(); + if ($this->relation !== null) { + $attributes['relation'] = $this->relation->toArray(); + } + return $attributes; } } diff --git a/src/service/TaskViewer.php b/src/service/TaskViewer.php index 8e25c09..72f1c0e 100644 --- a/src/service/TaskViewer.php +++ b/src/service/TaskViewer.php @@ -4,8 +4,11 @@ namespace kuaukutsu\poc\task\service; +use Throwable; use kuaukutsu\poc\task\dto\TaskView; use kuaukutsu\poc\task\exception\NotFoundException; +use kuaukutsu\poc\task\state\TaskStateInterface; +use kuaukutsu\poc\task\state\TaskStateWaiting; use kuaukutsu\poc\task\state\TaskFlag; use kuaukutsu\poc\task\state\TaskStatePrepare; use kuaukutsu\poc\task\EntityUuid; @@ -28,19 +31,36 @@ public function get(string $taskUuid): TaskView { $uuid = new EntityUuid($taskUuid); $task = $this->taskQuery->getOne($uuid); - $state = $this->prepareState($task->state); + + try { + $state = $this->prepareState($task->state); + $relation = $this->prepareRelation($state); + } catch (Throwable) { + $state = null; + $relation = null; + } return new TaskView( uuid: $task->uuid, title: $task->title, state: $this->prepareFlag($task->flag), - message: $state->getMessage()->message, + message: $state?->getMessage()->message ?? 'unrecognized', metrics: $this->stageQuery->getMetricsByTask($uuid), + relation: $relation, createdAt: $task->createdAt, updatedAt: $task->updatedAt, ); } + private function prepareRelation(TaskStateInterface $state): ?TaskView + { + if ($state instanceof TaskStateWaiting) { + return $this->get($state->task); + } + + return null; + } + /** * @return non-empty-string */