Skip to content

Commit

Permalink
task viewer metrics (#40)
Browse files Browse the repository at this point in the history
* task viewer metrics

* task viewer metrics
  • Loading branch information
kuaukutsu committed Dec 14, 2023
1 parent a94f5ed commit 31d043d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/dto/TaskView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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;
}
}
24 changes: 22 additions & 2 deletions src/service/TaskViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down

0 comments on commit 31d043d

Please sign in to comment.