Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Dec 28, 2024
1 parent 26351e6 commit 5199779
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 380 deletions.
237 changes: 76 additions & 161 deletions www/controllers/Task/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,61 @@

class Step
{
/**
* Get and return the steps name, status and HTML content
*/
public function getSteps(int $taskId)
private $taskId;
private $jsonContent;

public function __construct(int $taskId)
{
try {
/**
* If task log does not exist, throw an exception
*/
if (!file_exists(MAIN_LOGS_DIR . '/' . $taskId . '.json')) {
throw new Exception('no log file found for this task.');
}
$this->taskId = $taskId;

/**
* Load task log content
*/
$content = file_get_contents(MAIN_LOGS_DIR . '/' . $taskId . '.json');
/**
* If task log does not exist, throw an exception
*/
if (!file_exists(MAIN_LOGS_DIR . '/' . $this->taskId . '.json')) {
throw new Exception('No log file found for this task.');
}

if ($content === false) {
throw new Exception('could not read log file.');
}
/**
* Load task log content
*/
$content = file_get_contents(MAIN_LOGS_DIR . '/' . $this->taskId . '.json');

/**
* Decode JSON content
*/
$jsonContent = json_decode($content, true);
if ($content === false) {
throw new Exception('Could not read task log file.');
}

if ($jsonContent === null) {
throw new Exception('could not decode log file.');
}
/**
* Decode JSON content
*/
$this->jsonContent = json_decode($content, true);

if (!empty($jsonContent['steps'])) {
foreach ($jsonContent['steps'] as $stepName => $step) {
if ($this->jsonContent === null) {
throw new Exception('Could not decode task log file.');
}
}

/**
* Get and return the steps name, status and HTML content
*/
public function getSteps()
{
try {
// For the needs of the include file
$taskId = $this->taskId;

if (!empty($this->jsonContent['steps'])) {
foreach ($this->jsonContent['steps'] as $stepName => $step) {
// Get HTML content of the step
ob_start();
include(ROOT . '/views/includes/containers/tasks/log/step.inc.php');
$content = ob_get_clean();

// Add step name, status and HTML content to the steps array
$jsonContent['steps'][$stepName]['html'] = $content;
$this->jsonContent['steps'][$stepName]['html'] = $content;
}
}

return json_encode($jsonContent);
return json_encode($this->jsonContent);
} catch (Exception $e) {
throw new Exception('Cannot retrieve steps status: ' . $e->getMessage());
}
Expand All @@ -58,44 +69,21 @@ public function getSteps(int $taskId)
/**
* Get and return the content of a specific step
*/
public function getStepContent(int $taskId, string $stepName, bool $autoscroll)
public function getStepContent(string $stepName, bool $autoscroll)
{
try {
$data = '';

/**
* If task log does not exist, throw an exception
*/
if (!file_exists(MAIN_LOGS_DIR . '/' . $taskId . '.json')) {
throw new Exception('no log file found for this task.');
}

/**
* Load task log content
*/
$content = file_get_contents(MAIN_LOGS_DIR . '/' . $taskId . '.json');

if ($content === false) {
throw new Exception('could not read log file.');
}

/**
* Decode JSON content
*/
$content = json_decode($content, true);

if ($content === null) {
throw new Exception('could not decode log file.');
}

/**
* If step does not exist in the log, throw an exception
*/
if (!array_key_exists($stepName, $content['steps'])) {
if (!array_key_exists($stepName, $this->jsonContent['steps'])) {
throw new Exception('step does not exist in the log.');
}

$step = $content['steps'][$stepName];
// For the needs of the include file
$taskId = $this->taskId;
$step = $this->jsonContent['steps'][$stepName];

ob_start();
include(ROOT . '/views/includes/containers/tasks/log/step-content.inc.php');
Expand All @@ -108,146 +96,73 @@ public function getStepContent(int $taskId, string $stepName, bool $autoscroll)
}

/**
* Get task previous log lines
* Get task log lines
*/
public function getPreviousLogLines(int $taskId, string $step, string $substepFirstKey)
public function getLogLines(string $step, string $direction, string|null $key = null)
{
try {
/**
* If task log does not exist, throw an exception
* If step does not exist in the log, throw an exception
*/
if (!file_exists(MAIN_LOGS_DIR . '/' . $taskId . '.json')) {
throw new Exception('no log file found for this task.');
if (!array_key_exists($step, $this->jsonContent['steps'])) {
throw new Exception('no step ' . $step . ' found in the log.');
}

/**
* Load task log content
* If a substep key is provided
*/
$content = file_get_contents(MAIN_LOGS_DIR . '/' . $taskId . '.json');
if (!empty($key)) {
// If the substep does not exist in the log, throw an exception
if (!array_key_exists($key, $this->jsonContent['steps'][$step]['substeps'])) {
throw new Exception('no substep ' . $key . ' found in the log.');
}

if ($content === false) {
throw new Exception('could not read log file.');
// If the substep key is the first one, return an empty content (because we cannot go up anymore)
if ($key == array_key_first($this->jsonContent['steps'][$step]['substeps'])) {
return '';
}
}

/**
* Decode JSON content
* If direction is 'top', get the very first 30 substeps
*/
$content = json_decode($content, true);

if ($content === null) {
throw new Exception('could not decode log file.');
if ($direction == 'top') {
$substeps = array_slice($this->jsonContent['steps'][$step]['substeps'], 0, 30, true);
}

/**
* If step does not exist in the log, throw an exception
* If direction is 'up', get the 10 substeps before the provided substep key
*/
if (!array_key_exists($step, $content['steps'])) {
throw new Exception('step does not exist in the log.');
if ($direction == 'up') {
$substeps = array_slice($this->jsonContent['steps'][$step]['substeps'], 0, array_search($key, array_keys($this->jsonContent['steps'][$step]['substeps'])), true);
// Only keep the 10 previous substeps
$substeps = array_slice($substeps, -10, 10, true);
}

/**
* If substep does not exist in the log, throw an exception
* If direction is 'down', get the 10 substeps after the provided substep key
*/
if (!isset($substepFirstKey, $content['steps'][$step]['substeps'][$substepFirstKey])) {
throw new Exception('substep does not exist in the log.');
if ($direction == 'down') {
$substeps = array_slice($this->jsonContent['steps'][$step]['substeps'], array_search($key, array_keys($this->jsonContent['steps'][$step]['substeps'])) + 1, 10, true);
}

/**
* If the substep key is the first one, return an empty array
* If direction is 'bottom', get the very last 30 substeps
*/
if ($substepFirstKey == array_key_first($content['steps'][$step]['substeps'])) {
return '';
if ($direction == 'bottom') {
$substeps = array_slice($this->jsonContent['steps'][$step]['substeps'], -30, 30, true);
}

/**
* Get all substeps before the provided substep key
*/
$substeps = array_slice($content['steps'][$step]['substeps'], 0, array_search($substepFirstKey, array_keys($content['steps'][$step]['substeps'])), true);

/**
* If there is no more substep to display, return an empty string
* If there is no substep to display, return an empty string
*/
if (empty($substeps)) {
return '';
}

// Only keep the 10 previous substeps
$substeps = array_slice($substeps, -10, 10, true);

ob_start();

foreach ($substeps as $substepKey => $substep) {
$substepTitle = $substep['title'];
$substepNote = $substep['note'];
$substepStatus = $substep['status'];
$substepOutput = $substep['output'];
$substepStart = $substep['start'];
$substepDuration = $substep['duration'];

// Include substep template
include(ROOT . '/views/includes/containers/tasks/log/substep.inc.php');
}

return ob_get_clean();
} catch (Exception $e) {
throw new Exception('Cannot load more logs: ' . $e->getMessage());
}
}

/**
* Get task next log lines
*/
public function getNextLogLines(int $taskId, string $step, string $substepLastKey)
{
try {
/**
* If task log does not exist, throw an exception
*/
if (!file_exists(MAIN_LOGS_DIR . '/' . $taskId . '.json')) {
throw new Exception('no log file found for this task.');
}

/**
* Load task log content
* Load each substep content
*/
$content = file_get_contents(MAIN_LOGS_DIR . '/' . $taskId . '.json');

if ($content === false) {
throw new Exception('could not read log file.');
}

/**
* Decode JSON content
*/
$content = json_decode($content, true);

if ($content === null) {
throw new Exception('could not decode log file.');
}

/**
* If step does not exist in the log, throw an exception
*/
if (!array_key_exists($step, $content['steps'])) {
throw new Exception('step does not exist in the log.');
}

/**
* If substep does not exist in the log, throw an exception
*/
if (!isset($substepLastKey, $content['steps'][$step]['substeps'][$substepLastKey])) {
throw new Exception('substep does not exist in the log.');
}

/**
* Return the 20 next substeps after the provided substep key
*/
$substeps = array_slice($content['steps'][$step]['substeps'], array_search($substepLastKey, array_keys($content['steps'][$step]['substeps'])) + 1, 10, true);

if (empty($substeps)) {
return '';
}

ob_start();

foreach ($substeps as $substepKey => $substep) {
Expand Down
30 changes: 8 additions & 22 deletions www/controllers/ajax/task.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
*/
if ($_POST['action'] == 'get-steps' and !empty($_POST['taskId'])) {
try {
$taskStepController = new \Controllers\Task\Step();
$content = $taskStepController->getSteps($_POST['taskId']);
$taskStepController = new \Controllers\Task\Step($_POST['taskId']);
$content = $taskStepController->getSteps();
} catch (\Exception $e) {
response(HTTP_BAD_REQUEST, $e->getMessage());
}
Expand All @@ -106,8 +106,8 @@
*/
if ($_POST['action'] == 'get-step-content' and !empty($_POST['taskId']) and !empty($_POST['stepName']) and !empty($_POST['autoscroll'])) {
try {
$taskStepController = new \Controllers\Task\Step();
$content = $taskStepController->getStepContent($_POST['taskId'], $_POST['stepName'], $_POST['autoscroll']);
$taskStepController = new \Controllers\Task\Step($_POST['taskId']);
$content = $taskStepController->getStepContent($_POST['stepName'], $_POST['autoscroll']);
} catch (\Exception $e) {
response(HTTP_BAD_REQUEST, $e->getMessage());
}
Expand All @@ -116,26 +116,12 @@
}

/**
* Get task previous log lines on scroll
* Get and return previous or next log lines of a specific task step
*/
if ($_POST['action'] == 'get-previous-log-lines' and !empty($_POST['taskId']) and !empty($_POST['step']) and !empty($_POST['substepFirstKey'])) {
if ($_POST['action'] == 'get-log-lines' and !empty($_POST['taskId']) and !empty($_POST['step']) and !empty($_POST['direction']) and isset($_POST['key'])) {
try {
$taskStepController = new \Controllers\Task\Step();
$content = $taskStepController->getPreviousLogLines($_POST['taskId'], $_POST['step'], $_POST['substepFirstKey']);
} catch (\Exception $e) {
response(HTTP_BAD_REQUEST, $e->getMessage());
}

response(HTTP_OK, $content);
}

/**
* Get task next log lines on scroll
*/
if ($_POST['action'] == 'get-next-log-lines' and !empty($_POST['taskId']) and !empty($_POST['step']) and !empty($_POST['substepLastKey'])) {
try {
$taskStepController = new \Controllers\Task\Step();
$content = $taskStepController->getNextLogLines($_POST['taskId'], $_POST['step'], $_POST['substepLastKey']);
$taskStepController = new \Controllers\Task\Step($_POST['taskId']);
$content = $taskStepController->getLogLines($_POST['step'], $_POST['direction'], $_POST['key']);
} catch (\Exception $e) {
response(HTTP_BAD_REQUEST, $e->getMessage());
}
Expand Down
Loading

0 comments on commit 5199779

Please sign in to comment.