-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support displaying child workflows (#25)
- Loading branch information
Showing
9 changed files
with
219 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Waterline\Dto; | ||
|
||
use JsonSerializable; | ||
|
||
class ChartDataPoint implements JsonSerializable | ||
{ | ||
public function __construct( | ||
private string $x, | ||
private float | int $yMin, | ||
private float | int $yMax, | ||
private string $type, | ||
) {} | ||
|
||
public function jsonSerialize(): mixed | ||
{ | ||
return [ | ||
'x' => $this->x, | ||
'y' => [$this->yMin, $this->yMax], | ||
'type' => $this->type, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Waterline\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use SplFileObject; | ||
use Waterline\Transformer\WorkflowToChartDataTransformer; | ||
use Workflow\Models\StoredWorkflow; | ||
use Workflow\Models\StoredWorkflowException; | ||
use Workflow\Serializers\Y; | ||
|
||
/** | ||
* @mixin StoredWorkflowException | ||
*/ | ||
class StoredWorkflowExceptionResource extends JsonResource | ||
{ | ||
public static $wrap = null; | ||
|
||
public function toArray(Request $request) | ||
{ | ||
$code = null; | ||
$exception = $this->exception; | ||
|
||
$unserialized = Y::unserialize($exception); | ||
if (is_array($unserialized) | ||
&& array_key_exists('class', $unserialized) | ||
&& is_subclass_of($unserialized['class'], \Throwable::class) | ||
&& file_exists($unserialized['file']) | ||
) { | ||
$file = new SplFileObject($unserialized['file']); | ||
$file->seek($unserialized['line'] - 4); | ||
for ($line = 0; $line < 7; ++$line) { | ||
$exception->code .= $file->current(); | ||
$file->next(); | ||
if ($file->eof()) break; | ||
} | ||
$code = rtrim($exception->code); | ||
$exception = serialize($unserialized); | ||
} | ||
|
||
return [ | ||
"code" => $code, | ||
"exception" => $exception, | ||
"class" => $this->class, | ||
"created_at" => $this->created_at, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Waterline\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use Waterline\Transformer\WorkflowToChartDataTransformer; | ||
use Workflow\Models\StoredWorkflow; | ||
use Workflow\Models\StoredWorkflowLog; | ||
use Workflow\Serializers\Y; | ||
|
||
/** | ||
* @mixin StoredWorkflowLog | ||
*/ | ||
class StoredWorkflowLogResource extends JsonResource | ||
{ | ||
public static $wrap = null; | ||
|
||
public function toArray(Request $request) | ||
{ | ||
return [ | ||
"id" => $this->id, | ||
"index" => $this->index, | ||
"now" => $this->now, | ||
"class" => $this->class, | ||
"result" => serialize(Y::unserialize($this->result)), | ||
"created_at" => $this->created_at, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Waterline\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use Waterline\Transformer\WorkflowToChartDataTransformer; | ||
use Workflow\Models\StoredWorkflow; | ||
use Workflow\Serializers\Y; | ||
|
||
/** | ||
* @mixin StoredWorkflow | ||
*/ | ||
class StoredWorkflowResource extends JsonResource | ||
{ | ||
public static $wrap = null; | ||
|
||
public function toArray(Request $request) | ||
{ | ||
return [ | ||
"id" => $this->id, | ||
"class" => $this->class, | ||
"arguments" => serialize(Y::unserialize($this->arguments)), | ||
"output" => $this->output === null ? serialize(null) : serialize(Y::unserialize($this->output)), | ||
"status" => $this->status, | ||
"created_at" => $this->created_at, | ||
"updated_at" => $this->updated_at, | ||
"logs" => StoredWorkflowLogResource::collection($this->logs), | ||
"exceptions" => StoredWorkflowExceptionResource::collection($this->exceptions), | ||
"chartData" => app(WorkflowToChartDataTransformer::class)->transform($this->resource), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Waterline\Transformer; | ||
|
||
use Carbon\CarbonInterface; | ||
use DateTimeInterface; | ||
use Illuminate\Support\Arr; | ||
use Waterline\Dto\ChartDataPoint; | ||
use Workflow\Models\StoredWorkflow; | ||
use Workflow\Models\StoredWorkflowLog; | ||
use Workflow\Workflow; | ||
|
||
class WorkflowToChartDataTransformer | ||
{ | ||
/** | ||
* @return ChartDataPoint[] | ||
*/ | ||
public function transform(StoredWorkflow $storedWorkflow) : array { | ||
$data = $this->transformWorkflow($storedWorkflow); | ||
|
||
foreach ($storedWorkflow->children as $childWorkflow) { | ||
$data = array_merge( | ||
$data, | ||
$this->transform($childWorkflow), | ||
); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @return ChartDataPoint[] | ||
*/ | ||
private function transformWorkflow(StoredWorkflow $storedWorkflow) : array { | ||
$data = [ | ||
app(ChartDataPoint::class, [ | ||
'x' => $storedWorkflow->class, | ||
'yMin' => (float) $storedWorkflow->created_at->isoFormat('XSSS'), | ||
'yMax' => (float) $storedWorkflow->updated_at->isoFormat('XSSS'), | ||
'type' => 'Workflow' | ||
]) | ||
]; | ||
|
||
$prevLogCreated = $storedWorkflow->created_at; | ||
foreach ($storedWorkflow->logs as $log) { | ||
if (is_subclass_of($log->class, Workflow::class)) { | ||
continue; | ||
} | ||
|
||
$data[] = $this->transformLog($log, $prevLogCreated); | ||
$prevLogCreated = $log->created_at; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @return ChartDataPoint | ||
*/ | ||
private function transformLog(StoredWorkflowLog $storedWorkflowLog, CarbonInterface $prevLogCreated) : ChartDataPoint { | ||
|
||
return app(ChartDataPoint::class, [ | ||
'x' => $storedWorkflowLog->class, | ||
'yMin' => (float) $prevLogCreated->isoFormat('XSSS'), | ||
'yMax' => (float) $storedWorkflowLog->created_at->isoFormat('XSSS'), | ||
'type' => 'Activity', | ||
]); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters