Changes output to JSON serialization.

This commit is contained in:
Joao Patricio
2023-04-07 15:58:45 +01:00
parent 2a8d603f98
commit e6f0059e5e
10 changed files with 62 additions and 130 deletions

View File

@@ -20,12 +20,12 @@ class DispatchRemoteProcess
->withProperties($properties)
->performedOn($remoteProcessArgs->model)
->event($remoteProcessArgs->type)
->log("");
->log("[]");
} else {
$this->activity = activity()
->withProperties($remoteProcessArgs->toArray())
->event($remoteProcessArgs->type)
->log("");
->log("[]");
}
}

View File

@@ -27,11 +27,6 @@ class RunRemoteProcess
protected int $counter = 1;
public const MARK_START = "|--";
public const MARK_END = "--|";
public const SEPARATOR = '|';
public const MARK_REGEX = "/(\|--\d+\|\d+\|(?:out|err)--\|)/";
/**
* Create a new job instance.
*/
@@ -93,7 +88,7 @@ class RunRemoteProcess
$this->currentTime = $this->elapsedTime();
$this->activity->description .= $this->encodeOutput($type, $output);
$this->activity->description = $this->encodeOutput($type, $output);
if ($this->isAfterLastThrottle()) {
// Let's write to database.
@@ -106,12 +101,37 @@ class RunRemoteProcess
public function encodeOutput($type, $output)
{
return
static::MARK_START . $this->counter++ .
static::SEPARATOR . $this->elapsedTime() .
static::SEPARATOR . $type .
static::MARK_END .
$output;
$outputStack = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR);
$outputStack[] = [
'type' => $type,
'output' => $output,
'elapsed_tim' => $this->elapsedTime(),
'order' => $this->counter++,
];
return json_encode($outputStack, flags: JSON_THROW_ON_ERROR);
}
public static function decodeOutput(?Activity $activity = null): string
{
if(is_null($activity)) {
return '';
}
try {
$decoded = json_decode(data_get($activity, 'description'),
associative: true,
flags: JSON_THROW_ON_ERROR
);
} catch (\JsonException $exception) {
return '';
}
return collect($decoded)
->sortBy(fn($i) => $i['order'])
->map(fn($i) => $i['output'])
->implode("\n");
}
/**

View File

@@ -1,90 +0,0 @@
<?php
namespace App\Actions\RemoteProcess;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity;
class TidyOutput
{
protected $output;
public function __construct(
protected Activity $activity
)
{
}
public function __invoke()
{
$chunks = preg_split(
RunRemoteProcess::MARK_REGEX,
$this->activity->description,
flags: PREG_SPLIT_DELIM_CAPTURE
);
$tidyRows = $this
->joinMarksWithFollowingItem($chunks)
->reject(fn($i) => $i === '')
->map(function ($i) {
if (!preg_match('/\|--(\d+)\|(\d+)\|(out|err)--\|(.*)/', $i, $matches)) {
return $i;
}
[$wholeLine, $sequence, $elapsedTime, $type, $output] = $matches;
return [
'sequence' => $sequence,
'time' => $elapsedTime,
'type' => $type,
'output' => $output,
];
});
return $tidyRows
->sortBy(fn($i) => $i['sequence'])
->map(fn($i) => $i['output'])
->implode("\n");
}
/**
* Function to join the defined mark, with the output
* that is the following element in the array.
*
* Turns this:
* [
* "|--1|149|out--|",
* "/root\n",
* "|--2|251|out--|",
* "Welcome 1 times 1\n",
* "|--3|366|out--|",
* "Welcome 2 times 2\n",
* "|--4|466|out--|",
* "Welcome 3 times 3\n",
* ]
*
* into this:
*
* [
* "|--1|149|out--|/root\n",
* "|--2|251|out--|Welcome 1 times 1\n",
* "|--3|366|out--|Welcome 2 times 2\n",
* "|--4|466|out--|Welcome 3 times 3\n",
* ]
*/
public function joinMarksWithFollowingItem($chunks): Collection
{
return collect($chunks)->reduce(function ($carry, $item) {
$last = $carry->last();
if (preg_match(RunRemoteProcess::MARK_REGEX, $last) && !preg_match(RunRemoteProcess::MARK_REGEX, $item)) {
// If the last element is a delimiter and the current element is not,
// join them together and replace the last element with the joined string
$carry->pop();
$joined = $last . $item;
$carry->push($joined);
} else {
// Otherwise, just add the current element to the result array
$carry->push($item);
}
return $carry;
}, collect());
}
}