Skip to content

Commit a6ee7c5

Browse files
committed
FEATURE: add TaskResults filter API, callable from Eel
This is useful for building custom UIs which display the pipeline
1 parent e8102f4 commit a6ee7c5

2 files changed

Lines changed: 138 additions & 5 deletions

File tree

Classes/Dto/TaskResult.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class TaskResult
1919
private bool $errored;
2020
private ?string $error;
2121

22+
public const STATUS_WAITING = 'waiting';
23+
public const STATUS_RUNNING = 'running';
24+
public const STATUS_SKIPPED = 'skipped';
25+
public const STATUS_DONE = 'done';
26+
public const STATUS_ERROR = 'error';
27+
public const STATUS_CANCELED = 'canceled';
28+
2229
private function __construct(string $name, string $status, ?\DateTimeImmutable $start, ?\DateTimeImmutable $end, bool $skipped, int $exitCode, bool $errored, ?string $error)
2330
{
2431
$this->name = $name;
@@ -55,6 +62,8 @@ public function getName(): string
5562
}
5663

5764
/**
65+
* see the STATUS_* constants here.
66+
*
5867
* @return string
5968
*/
6069
public function getStatus(): string

Classes/Dto/TaskResults.php

Lines changed: 129 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,141 @@
11
<?php
22

3-
43
namespace Flowpack\Prunner\Dto;
54

5+
use Neos\Eel\ProtectedContextAwareInterface;
66
use Neos\Flow\Annotations as Flow;
77

88
/**
99
* @Flow\Proxy(false)
1010
*/
11-
class TaskResults implements \IteratorAggregate
11+
final class TaskResults implements \IteratorAggregate, \Countable, ProtectedContextAwareInterface
1212
{
13+
/**
14+
* @var TaskResult[]
15+
*/
1316
protected array $taskResults;
1417

18+
private function __construct(array $taskResults)
19+
{
20+
$this->taskResults = $taskResults;
21+
}
22+
23+
1524
public function fromJsonArray(array $in): self
1625
{
1726
$converted = [];
1827
foreach ($in as $el) {
1928
$converted[] = TaskResult::fromJsonArray($el);
2029
}
30+
return new self($converted);
31+
}
32+
33+
/**
34+
* return a new, immutable list containing only the tasks matching a certain prefix. Especially useful if you want to filter
35+
* the lists before rendering them in Fusion
36+
*
37+
* @param string $prefix
38+
* @return $this
39+
*/
40+
public function filteredByPrefix(string $prefix): self
41+
{
42+
$filtered = [];
43+
foreach ($this->taskResults as $taskResult) {
44+
if (strpos($taskResult->getName(), $prefix) === 0) {
45+
$filtered[] = $taskResult;
46+
}
47+
}
48+
return new self($filtered);
49+
}
50+
51+
/**
52+
* return a new, immutable list containing all the tasks EXCEPT $taskNamesToSkip. Especially useful if you want to filter
53+
* the lists before rendering them in Fusion
54+
*
55+
* @param string $prefix
56+
* @return $this
57+
*/
58+
public function withoutTasks(string ...$taskNamesToSkip): self
59+
{
60+
$filtered = [];
61+
foreach ($this->taskResults as $taskResult) {
62+
if (!in_array($taskResult->getName(), $taskNamesToSkip)) {
63+
$filtered[] = $taskResult;
64+
}
65+
}
66+
return new self($filtered);
67+
}
68+
69+
/**
70+
* an aggregated status string - useful for rendering a status in a UI:
71+
* - If one of the elements is ERROR, it is failed.
72+
* - If one of the elements is RUNNING, it is running.
73+
* - If one element is CANCELED, it is cancelled.
74+
* - If all elements are DONE, it is done.
75+
* - If all elements are WAITING, it is waiting.
76+
* - If all elements are SKIPPED, it is skipped.
77+
* - otherwise, UNKNOWN
78+
*
79+
* @return string
80+
*/
81+
public function getAggregatedStatus(): string
82+
{
83+
foreach ($this->taskResults as $taskResult) {
84+
if ($taskResult->getStatus() === TaskResult::STATUS_ERROR) {
85+
return TaskResult::STATUS_ERROR;
86+
}
87+
}
88+
89+
foreach ($this->taskResults as $taskResult) {
90+
if ($taskResult->getStatus() === TaskResult::STATUS_RUNNING) {
91+
return TaskResult::STATUS_RUNNING;
92+
}
93+
}
94+
95+
foreach ($this->taskResults as $taskResult) {
96+
if ($taskResult->getStatus() === TaskResult::STATUS_CANCELED) {
97+
return TaskResult::STATUS_CANCELED;
98+
}
99+
}
100+
101+
$allDone = true;
102+
foreach ($this->taskResults as $taskResult) {
103+
if ($taskResult->getStatus() !== TaskResult::STATUS_DONE) {
104+
$allDone = false;
105+
}
106+
}
107+
if ($allDone) {
108+
return TaskResult::STATUS_DONE;
109+
}
110+
111+
foreach ($this->taskResults as $taskResult) {
112+
if ($taskResult->getStatus() === TaskResult::STATUS_WAITING) {
113+
return TaskResult::STATUS_WAITING;
114+
}
115+
}
116+
117+
$allSkipped = true;
118+
foreach ($this->taskResults as $taskResult) {
119+
if ($taskResult->getStatus() !== TaskResult::STATUS_SKIPPED) {
120+
$allSkipped = false;
121+
}
122+
}
123+
if ($allSkipped) {
124+
return TaskResult::STATUS_SKIPPED;
125+
}
126+
127+
return 'unknown';
128+
}
21129

130+
public function get(string $taskName): ?TaskResult
131+
{
132+
foreach ($this->taskResults as $taskResult) {
133+
if ($taskResult->getName() === $taskName) {
134+
return $taskResult;
135+
}
136+
}
137+
return null;
22138

23-
$taskResults = new self();
24-
$taskResults->taskResults = $converted;
25-
return $taskResults;
26139
}
27140

28141
/**
@@ -32,4 +145,15 @@ public function getIterator()
32145
{
33146
return new \ArrayIterator($this->taskResults);
34147
}
148+
149+
public function count()
150+
{
151+
return count($this->taskResults);
152+
}
153+
154+
public function allowsCallOfMethod($methodName)
155+
{
156+
return true;
157+
}
158+
35159
}

0 commit comments

Comments
 (0)