Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Dec 25, 2024
1 parent 760178d commit d5bc0b7
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 478 deletions.
206 changes: 97 additions & 109 deletions www/controllers/Layout/Container/vars/tasks/log.vars.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,122 +14,110 @@
$taskId = $taskController->getLastTaskId();
}

if (!file_exists(MAIN_LOGS_DIR . '/' . $taskId . '.json')) {
throw new Exception('No log file found for this task');
}

$logfile = $taskId . '.json';
$content = json_decode(file_get_contents(MAIN_LOGS_DIR . '/' . $logfile), true);
// Get task info
$taskInfo = $taskController->getById($taskId);
$rawParams = json_decode($taskInfo['Raw_params'], true);
$repoId = null;
$snapId = null;
$envId = null;

if (empty($rawParams['action'])) {
throw new Exception('No action found in the task');
}

// Get repository info
if ($rawParams['action'] == 'create') {
// TODO
} else {
if (!empty($rawParams['repo-id'])) {
$repoId = $rawParams['repo-id'];
// If the task has a log file (old format)
if (!empty($taskInfo['Logfile']) and file_exists(MAIN_LOGS_DIR . '/' . $taskInfo['Logfile'])) {
$logfile = $taskInfo['Logfile'];
$output = file_get_contents(MAIN_LOGS_DIR . '/' . $logfile);

// If the task has a json log file (new format)
} else if (file_exists(MAIN_LOGS_DIR . '/' . $taskId . '.json')) {
$content = json_decode(file_get_contents(MAIN_LOGS_DIR . '/' . $taskId . '.json'), true);
$rawParams = json_decode($taskInfo['Raw_params'], true);
$repoId = null;
$snapId = null;
$envId = null;

if (empty($rawParams['action'])) {
throw new Exception('No action found in the task');
}
if (!empty($rawParams['snap-id'])) {
$snapId = $rawParams['snap-id'];
}
if (!empty($rawParams['env-id'])) {
$envId = $rawParams['env-id'];
}

$repoController->getAllById($repoId, $snapId, $envId);
}

/**
* Get repository info
*/

// If the action is create, the repository info is in the raw params
if ($rawParams['action'] == 'create') {
$repoController->setType($rawParams['repo-type']);

if (!empty($rawParams['source'])) {
$repoController->setSource($rawParams['source']);
}
if (!empty($rawParams['alias'])) {
$repoController->setName($rawParams['alias']);
} else {
$repoController->setName($rawParams['source']);
}
if (!empty($rawParams['dist'])) {
$repoController->setDist($rawParams['dist']);
}
if (!empty($rawParams['section'])) {
$repoController->setSection($rawParams['section']);
}
if (!empty($rawParams['releasever'])) {
$repoController->setReleasever($rawParams['releasever']);
}
if (!empty($rawParams['arch'])) {
$repoController->setArch($rawParams['arch']);
}
if (!empty($rawParams['packages-include'])) {
$repoController->setPackagesToInclude($rawParams['packages-include']);
}
if (!empty($rawParams['packages-exclude'])) {
$repoController->setPackagesToExclude($rawParams['packages-exclude']);
}
if (!empty($rawParams['package-type'])) {
$repoController->setPackageType($rawParams['package-type']);
}
if (!empty($rawParams['gpg-check'])) {
$repoController->setGpgCheck($rawParams['gpg-check']);
}
if (!empty($rawParams['gpg-sign'])) {
$repoController->setGpgSign($rawParams['gpg-sign']);
}
if (!empty($rawParams['description'])) {
$repoController->setDescription($rawParams['description']);
}
if (!empty($rawParams['group'])) {
$repoController->setGroup($rawParams['group']);
}

// Otherwise, we get the repository info from the database
} else {
if (!empty($rawParams['repo-id'])) {
$repoId = $rawParams['repo-id'];
}
if (!empty($rawParams['snap-id'])) {
$snapId = $rawParams['snap-id'];
}
if (!empty($rawParams['env-id'])) {
$envId = $rawParams['env-id'];
}

$repoController->getAllById($repoId, $snapId, $envId);
}

/**
* Include table template for the task
*/
ob_start();
include_once(ROOT . '/views/templates/tasks/' .$rawParams['action'] . '.inc.php');
$output .= ob_get_clean();

/**
* Include steps
*/
foreach ($content['steps'] as $stepName => $step) {
/**
* Include table template for the task
*/
ob_start();
include(ROOT . '/views/includes/containers/tasks/log/step.inc.php');
include_once(ROOT . '/views/templates/tasks/' . $rawParams['action'] . '.inc.php');
$output .= ob_get_clean();

/**
* Include steps
*/
foreach ($content['steps'] as $stepName => $step) {
ob_start();
include(ROOT . '/views/includes/containers/tasks/log/step.inc.php');
$output .= ob_get_clean();
}
// If no log file found
} else {
throw new Exception('No log file found for this task.');
}
} catch (Exception $e) {
$output = '<p>' . $e->getMessage() . '</p>';
$output = '<p class="note">' . $e->getMessage() . '</p>';
}

/**
* Get the log file to display from the cookie set by JS
*/
// if (!empty($_COOKIE['task-log'])) {
// $logfileCookie = \Controllers\Common::validateData($_COOKIE['task-log']);

// if (file_exists(MAIN_LOGS_DIR . '/' . $logfileCookie)) {
// $logfile = $logfileCookie;
// }
// }

/**
* If a logfile is specified in the URL, we take it
* It's the case for some links from scheduled tasks mails
*/
// if (!empty($_GET['task-log'])) {
// $logfileGet = \Controllers\Common::validateData($_GET['task-log']);

// /**
// * Logfile name must match the pattern
// */
// if (preg_match('/^(?:[0-9]{2})?[0-9]{2}-[0-3]?[0-9]-[0-3]?[0-9].*_(plan|repomanager|task)_.*.log$/', $logfileGet)) {
// if (file_exists(MAIN_LOGS_DIR . '/' . $logfileGet)) {
// $logfile = $logfileGet;

// /**
// * Rewrite cookie
// */
// setcookie('task-log', $logfile, time() + 3600 * 24 * 30, '/');
// }
// }

// if ($logfileGet == 'latest') {
// if (file_exists(MAIN_LOGS_DIR . '/latest')) {
// $logfile = '/latest';

// /**
// * Rewrite cookie
// */
// setcookie('task-log', $logfile, time() + 3600 * 24 * 30, '/');
// }
// }
// }

/**
* If no logfile is specified, we take the last one
*/
// if ($logfile == 'none') {
// $logfiles = array_diff(scandir(MAIN_LOGS_DIR, SCANDIR_SORT_DESCENDING), array('..', '.', 'latest'));

// if (!empty($logfiles[1])) {
// $logfile = $logfiles[1];
// }
// }

/**
* Get the content of the log file
*/
// if (!empty($logfile)) {
// $output = file_get_contents(MAIN_LOGS_DIR . '/' . $logfile);
// }

/**
* Remove ANSI codes (colors) in the file
*/
$output = preg_replace('/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/', "", $output);
10 changes: 7 additions & 3 deletions www/controllers/Logging/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
class Logging
{
protected $logFile;
protected $logController;

public function __construct(int $taskId)
{
$this->logController = new \Controllers\Log\Log();
$this->logFile = MAIN_LOGS_DIR . '/' . $taskId . '.json';

$this->generate();
Expand All @@ -16,13 +18,15 @@ public function __construct(int $taskId)
/**
* Generate log file
*/
public function generate()
public function generate() : void
{
if (file_exists($this->logFile)) {
return;
}

file_put_contents($this->logFile, json_encode(array('steps' => [])));
if (!file_put_contents($this->logFile, json_encode(array('steps' => [])))) {
$this->logController->log('error', 'Task logging', 'Could not create task log file', $this->logFile);
}
}

/**
Expand All @@ -31,7 +35,7 @@ public function generate()
protected function getContent()
{
if (!file_exists($this->logFile)) {
echo 'Log file not found';
$this->logController->log('error', 'Task logging', 'Task log file not found', $this->logFile);
return [];
}

Expand Down
38 changes: 35 additions & 3 deletions www/controllers/Logging/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function new(string $name, string $title)
'substeps' => []
];

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not add new step "' . $name . '" to task log file', $this->logFile);
}

unset($content);
}
Expand Down Expand Up @@ -53,7 +55,9 @@ public function completed(string|null $message = null)
$content['steps'][$name]['message'] = $message;
}

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not set step "' . $name . '" as completed in task log file', $this->logFile);
}

unset($content);
}
Expand Down Expand Up @@ -92,7 +96,35 @@ public function error(string $message)
];
}

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not set step "' . $name . '" as error in task log file', $this->logFile);
}

unset($content);
}

/**
* Set the latest step as stopped
*/
public function stopped()
{
$content = $this->getContent();

// Get latest step key name
$name = array_key_last($content['steps']);

// Set status to completed
$content['steps'][$name]['status'] = 'stopped';

// Set end date
$content['steps'][$name]['end'] = microtime(true);

// Set duration
$content['steps'][$name]['duration'] = microtime(true) - $content['steps'][$name]['start'];

if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not set step "' . $name . '" as stopped in task log file', $this->logFile);
}

unset($content);
}
Expand Down
45 changes: 41 additions & 4 deletions www/controllers/Logging/SubStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public function new(string $identifier, array $data)
$content['steps'][$name]['substeps'][$identifier]['note'] = $data['note'];
}

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not add new sub step "' . $identifier . '" to task log file', $this->logFile);
}

unset($content);
}
Expand Down Expand Up @@ -62,7 +64,9 @@ public function completed(string|null $message = '')
// Set duration
$content['steps'][$name]['substeps'][$subName]['duration'] = microtime(true) - $content['steps'][$name]['substeps'][$subName]['start'];

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not set sub step "' . $subName . '" as completed in task log file', $this->logFile);
}

unset($content);
}
Expand Down Expand Up @@ -91,7 +95,38 @@ public function warning(string $message)
// Set duration
$content['steps'][$name]['substeps'][$subName]['duration'] = microtime(true) - $content['steps'][$name]['substeps'][$subName]['start'];

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not set sub step "' . $subName . '" as warning in task log file', $this->logFile);
}

unset($content);
}

/**
* Set the latest sub step as stopped
*/
public function stopped()
{
$content = $this->getContent();

// Get latest step key name
$name = array_key_last($content['steps']);

// Get latest sub step key name
$subName = array_key_last($content['steps'][$name]['substeps']);

// Set status to stopped
$content['steps'][$name]['substeps'][$subName]['status'] = 'stopped';

// Set end date
$content['steps'][$name]['substeps'][$subName]['end'] = microtime(true);

// Set duration
$content['steps'][$name]['substeps'][$subName]['duration'] = microtime(true) - $content['steps'][$name]['substeps'][$subName]['start'];

if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not set sub step "' . $subName . '" as stopped in task log file', $this->logFile);
}

unset($content);
}
Expand Down Expand Up @@ -120,7 +155,9 @@ public function output(string $message, string|null $type = null)
'message' => $message
];

file_put_contents($this->logFile, json_encode($content));
if (!file_put_contents($this->logFile, json_encode($content))) {
$this->logController->log('error', 'Task logging', 'Could not add output to sub step "' . $subName . '" in task log file', $this->logFile);
}

unset($content);
}
Expand Down
Loading

0 comments on commit d5bc0b7

Please sign in to comment.