Skip to content
Snippets Groups Projects
Commit 1a9da681 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

added type declarations; update php docs

parent b46a0ed5
No related branches found
No related tags found
1 merge request!18OP#7365 PHP-8.1 - Upgrade - cronlog Viewer (monitors.ascii)https://projects.iml.unibe.ch/work_packages/7365
...@@ -32,21 +32,25 @@ require_once 'cronlog.class.php'; ...@@ -32,21 +32,25 @@ require_once 'cronlog.class.php';
* *
* @license GNU GPL 3.0 * @license GNU GPL 3.0
* @author Axel Hahn <axel.hahn@iml.unibe.ch> * @author Axel Hahn <axel.hahn@iml.unibe.ch>
*
* 2024-09-20 <axel.hahn@unibe.ch> added type declarations; update php docs
*/ */
class cronlogrenderer extends cronlog{ class cronlogrenderer extends cronlog
{
/** /**
* minimal length for execution time of a job to be rendered in the timeline; value is in seconds * minimal length for execution time of a job to be rendered in the timeline; value is in seconds
* @var integer * @var integer
*/ */
protected $_iMinTime4Timeline = 60; protected int $_iMinTime4Timeline = 60;
/** /**
* show date of last data and last access; used in rendering methods to display it on top * Show date of last data and last access; used in rendering methods to display it on top
* @param integer $iLast unix timestamp of last log entry * @param integer $iLast unix timestamp of last log entry
* @return string * @return string
*/ */
protected function _renderAccessAndAge($iLast){ protected function _renderAccessAndAge(int $iLast): string
{
if (!$iLast) { if (!$iLast) {
return ''; return '';
} }
...@@ -59,22 +63,24 @@ class cronlogrenderer extends cronlog{ ...@@ -59,22 +63,24 @@ class cronlogrenderer extends cronlog{
} }
/** /**
* get onclick value to filter a datatable * Get onclick value to filter a datatable
* @param string $sDatatable id of table * @param string $sDatatable id of table
* @param string $sFiltertext text to filter * @param string $sFiltertext text to filter
* @return string * @return string
* *
*/ */
protected function _filterDatatable($sDatatable, $sFiltertext){ protected function _filterDatatable($sDatatable, $sFiltertext)
{
return '$(\'#' . $sDatatable . '\').dataTable().fnFilter(\'' . $sFiltertext . '\'); return false;'; return '$(\'#' . $sDatatable . '\').dataTable().fnFilter(\'' . $sFiltertext . '\'); return false;';
} }
/** /**
* helper function to be used in methods that render datatable tables * Helper function to be used in methods that render datatable tables
* get javascript code to be added in init options and set language specifi texts * Get javascript code to be added in init options and set language specifi texts
* @return string * @return string
*/ */
protected function _getDatatableLanguage(){ protected function _getDatatableLanguage(): string
{
return $this->t("dt-USE") return $this->t("dt-USE")
? ', "oLanguage":{ ? ', "oLanguage":{
"sProcessing":"' . $this->t("dt-sProcessing") . '", "sProcessing":"' . $this->t("dt-sProcessing") . '",
...@@ -97,12 +103,13 @@ class cronlogrenderer extends cronlog{ ...@@ -97,12 +103,13 @@ class cronlogrenderer extends cronlog{
; ;
} }
/** /**
* get html code for a table with events of executed cronjobs * Get html code for a table with events of executed cronjobs
* *
* @param array $aData result of $this->getServerLogs() * @param array $aData result of $this->getServerLogs()
* @return string * @return string
*/ */
public function renderCronlogs($aData=false){ public function renderCronlogs(array $aData = []): string
{
$sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer; $sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer;
$sHtml = $this->_getCacheData($sTaskId); $sHtml = $this->_getCacheData($sTaskId);
if ($sHtml) { if ($sHtml) {
...@@ -110,7 +117,7 @@ class cronlogrenderer extends cronlog{ ...@@ -110,7 +117,7 @@ class cronlogrenderer extends cronlog{
} }
$sHtml = ''; $sHtml = '';
if(!$aData){ if (!count($aData)) {
$aData = array_merge($this->getRunningJobs(), $this->getServersLastLog()); $aData = array_merge($this->getRunningJobs(), $this->getServersLastLog());
} }
$sTblHead = ''; $sTblHead = '';
...@@ -120,7 +127,7 @@ class cronlogrenderer extends cronlog{ ...@@ -120,7 +127,7 @@ class cronlogrenderer extends cronlog{
// Array ( [SCRIPTNAME] => apt-get update [SCRIPTTTL] => 1440 [SCRIPTSTARTTIME] => 2016-06-21 06:00:02, 1466481602 [SCRIPTLABEL] => apt-get [SCRIPTENDTIME] => 2016-06-21 06:00:49, 1466481649 [SCRIPTEXECTIME] => 47 s [SCRIPTRC] => 0 ) // Array ( [SCRIPTNAME] => apt-get update [SCRIPTTTL] => 1440 [SCRIPTSTARTTIME] => 2016-06-21 06:00:02, 1466481602 [SCRIPTLABEL] => apt-get [SCRIPTENDTIME] => 2016-06-21 06:00:49, 1466481649 [SCRIPTEXECTIME] => 47 s [SCRIPTRC] => 0 )
foreach ($aData as $sDtakey => $aEntry) { foreach ($aData as $sDtakey => $aEntry) {
if (!$sTblHead) { if (!$sTblHead) {
foreach(array( foreach ([
$this->t("col-starting-time"), $this->t("col-starting-time"),
$this->t("col-label"), $this->t("col-label"),
$this->t("col-server"), $this->t("col-server"),
...@@ -129,15 +136,15 @@ class cronlogrenderer extends cronlog{ ...@@ -129,15 +136,15 @@ class cronlogrenderer extends cronlog{
$this->t("col-rc"), $this->t("col-rc"),
$this->t("col-expired"), $this->t("col-expired"),
$this->t("col-status"), $this->t("col-status"),
) as $sKey){ ] as $sKey) {
$sTblHead.='<th>'.$sKey.'</th>'; $sTblHead .= "<th>$sKey</th>";
} }
} }
// $sViewerUrl='viewer.php?host='.$aEntry['host'].'&job='.$aEntry['job']; // $sViewerUrl='viewer.php?host='.$aEntry['host'].'&job='.$aEntry['job'];
// $sClass='message-'.($aEntry['SCRIPTRC']?'error':'ok'); // $sClass='message-'.($aEntry['SCRIPTRC']?'error':'ok');
$iLast=max(array($iLast, date("U", $aEntry['SCRIPTSTARTTIME']))); $iLast = max([$iLast, date("U", $aEntry['SCRIPTSTARTTIME'])]);
$aErrors=array(); $aErrors = [];
$iTtlUsed = max($aEntry['SCRIPTTTL'], $this->_iMinTtl); $iTtlUsed = max($aEntry['SCRIPTTTL'], $this->_iMinTtl);
$iNextRun = $aEntry['SCRIPTSTARTTIME'] + ((int) $aEntry['SCRIPTTTL'] * 60); $iNextRun = $aEntry['SCRIPTSTARTTIME'] + ((int) $aEntry['SCRIPTTTL'] * 60);
$iNextRunWarn = $aEntry['SCRIPTSTARTTIME'] + ((int) $iTtlUsed * 60); $iNextRunWarn = $aEntry['SCRIPTSTARTTIME'] + ((int) $iTtlUsed * 60);
...@@ -298,13 +305,14 @@ class cronlogrenderer extends cronlog{ ...@@ -298,13 +305,14 @@ class cronlogrenderer extends cronlog{
} }
/** /**
* get html code for a table with events of executed cronjobs for ALL servers * Get html code for a table with events of executed cronjobs for ALL servers
* *
* @param array $aData result of $this->getServerLogs() * @param array $aData result of $this->getServerLogs()
* @return string * @return string
*/ */
public function renderCronlogsOfAllServers(){ public function renderCronlogsOfAllServers(): string
$aData=array(); {
$aData = [];
foreach (array_keys($this->getServers()) as $sServer) { foreach (array_keys($this->getServers()) as $sServer) {
$this->setServer($sServer); $this->setServer($sServer);
$aData = array_merge($aData, $this->getRunningJobs(), $this->getServersLastLog()); $aData = array_merge($aData, $this->getRunningJobs(), $this->getServersLastLog());
...@@ -314,14 +322,16 @@ class cronlogrenderer extends cronlog{ ...@@ -314,14 +322,16 @@ class cronlogrenderer extends cronlog{
} }
/** /**
* get html code for a switcher of multiple instances * Get html code for a switcher of multiple instances.
* It returns an empty string if there is no / only one instance
* *
* @param array $aData result of $oCL->getServerLogs() * @param array $aData result of $oCL->getServerLogs()
* @return string * @return string
*/ */
public function renderInstances(){ public function renderInstances(): string
{
if (count($this->_aInstances) < 2) { if (count($this->_aInstances) < 2) {
return false; return '';
} }
$sReturn = ''; $sReturn = '';
$sServer = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : false; $sServer = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : false;
...@@ -337,12 +347,13 @@ class cronlogrenderer extends cronlog{ ...@@ -337,12 +347,13 @@ class cronlogrenderer extends cronlog{
return $sReturn; return $sReturn;
} }
/** /**
* get html code for a table with history of executed cronjobs * Get html code for a table with history of executed cronjobs
* *
* @param array $aData result of $oCL->getServerLogs() * @param array $aData result of $oCL->getServerLogs()
* @return string * @return string
*/ */
public function renderJoblist($aData=false){ public function renderJoblist(array $aData = []): string
{
$sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer; $sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer;
$sHtml = $this->_getCacheData($sTaskId); $sHtml = $this->_getCacheData($sTaskId);
if ($sHtml) { if ($sHtml) {
...@@ -361,7 +372,7 @@ class cronlogrenderer extends cronlog{ ...@@ -361,7 +372,7 @@ class cronlogrenderer extends cronlog{
// job=dok-kvm-instances:host=kalium:start=1538358001:end=1538358001:exectime=0:ttl=60:rc=0 // job=dok-kvm-instances:host=kalium:start=1538358001:end=1538358001:exectime=0:ttl=60:rc=0
foreach ($aData as $aEntry) { foreach ($aData as $aEntry) {
if (!$sTblHead) { if (!$sTblHead) {
foreach(array( foreach ([
$this->t("col-starting-time"), $this->t("col-starting-time"),
$this->t("col-label"), $this->t("col-label"),
$this->t("col-server"), $this->t("col-server"),
...@@ -369,12 +380,11 @@ class cronlogrenderer extends cronlog{ ...@@ -369,12 +380,11 @@ class cronlogrenderer extends cronlog{
$this->t("col-ttl"), $this->t("col-ttl"),
$this->t("col-rc"), $this->t("col-rc"),
$this->t("col-status"), $this->t("col-status"),
) as $sKey){ ] as $sKey) {
$sTblHead .= '<th>' . $sKey . '</th>'; $sTblHead .= '<th>' . $sKey . '</th>';
} }
} }
$iLast=max(array($iLast, date("U", $aEntry['start']))); $iLast = max([$iLast, date("U", $aEntry['start'])]);
$sClass='message-'.($aEntry['rc']?'error':'ok');
if ($aEntry['rc']) { if ($aEntry['rc']) {
$iErrors++; $iErrors++;
...@@ -448,10 +458,12 @@ class cronlogrenderer extends cronlog{ ...@@ -448,10 +458,12 @@ class cronlogrenderer extends cronlog{
/** /**
* get html code for a joblist of the selected server * get html code for a joblist of the selected server
* it uses the filter for hidden joblog entries (aHidelogs in config) * it uses the filter for hidden joblog entries (aHidelogs in config)
*
* @return string * @return string
*/ */
public function renderJoblistOfAllServers(){ public function renderJoblistOfAllServers(): string
$aData=array(); {
$aData = [];
foreach (array_keys($this->getServers()) as $sServer) { foreach (array_keys($this->getServers()) as $sServer) {
$this->setServer($sServer); $this->setServer($sServer);
$aData = array_merge($aData, $this->getServerJobHistory()); $aData = array_merge($aData, $this->getServerJobHistory());
...@@ -470,7 +482,8 @@ class cronlogrenderer extends cronlog{ ...@@ -470,7 +482,8 @@ class cronlogrenderer extends cronlog{
* @param array $aData result of $oCL->getServerLogs() * @param array $aData result of $oCL->getServerLogs()
* @return string * @return string
*/ */
public function renderJobGraph($aData=false){ public function renderJobGraph(array $aData = []): string
{
$sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer; $sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer;
$sHtml = $this->_getCacheData($sTaskId); $sHtml = $this->_getCacheData($sTaskId);
if ($sHtml) { if ($sHtml) {
...@@ -482,19 +495,19 @@ class cronlogrenderer extends cronlog{ ...@@ -482,19 +495,19 @@ class cronlogrenderer extends cronlog{
$iGraphCounter = 0; $iGraphCounter = 0;
} }
$iGraphCounter++; $iGraphCounter++;
if(!$aData){ if (!count($aData)) {
$aData = $this->getServerJobHistory(false); $aData = $this->getServerJobHistory(false);
} }
$sDivId = 'vis-timeline-' . $iGraphCounter; $sDivId = 'vis-timeline-' . $iGraphCounter;
$aDataset=array(); $aDataset = [];
$iLast = false; $iLast = false;
$iEntry = 0; $iEntry = 0;
foreach ($aData as $aEntry) { foreach ($aData as $aEntry) {
if ($aEntry['exectime'] > $this->_iMinTime4Timeline) { if ($aEntry['exectime'] > $this->_iMinTime4Timeline) {
$iEntry++; $iEntry++;
$iLast=max(array($iLast, date("U", $aEntry['start']))); $iLast = max([$iLast, date("U", $aEntry['start'])]);
$aDataset[]=array( $aDataset[] = [
'id' => $iEntry, 'id' => $iEntry,
/* /*
'start'=>(int)date("U", $aEntry['start']), 'start'=>(int)date("U", $aEntry['start']),
...@@ -520,7 +533,7 @@ class cronlogrenderer extends cronlog{ ...@@ -520,7 +533,7 @@ class cronlogrenderer extends cronlog{
. '<br>' . '<br>'
. 'rc = ' . $aEntry['rc'] . '<br>' . 'rc = ' . $aEntry['rc'] . '<br>'
, ,
); ];
// if($iEntry>=265){break;} // if($iEntry>=265){break;}
} }
} }
...@@ -577,8 +590,13 @@ class cronlogrenderer extends cronlog{ ...@@ -577,8 +590,13 @@ class cronlogrenderer extends cronlog{
; ;
} }
public function renderJobGraphOfAllServers(){ /**
$aData=array(); * Get html code for the job graph of all servers
* @return string
*/
public function renderJobGraphOfAllServers(): string
{
$aData = [];
foreach (array_keys($this->getServers()) as $sServer) { foreach (array_keys($this->getServers()) as $sServer) {
$this->setServer($sServer); $this->setServer($sServer);
$aData = array_merge($aData, $this->getServerJobHistory()); $aData = array_merge($aData, $this->getServerJobHistory());
...@@ -588,10 +606,12 @@ class cronlogrenderer extends cronlog{ ...@@ -588,10 +606,12 @@ class cronlogrenderer extends cronlog{
} }
/** /**
* used in config/page-replacements.php
* generate an array of javascript lang texts * generate an array of javascript lang texts
* used in config/page-replacements.php
* @return string
*/ */
public function renderJSLang(){ public function renderJSLang(): string
{
$aReturn = []; $aReturn = [];
foreach ($this->_aLang as $sKey => $sText) { foreach ($this->_aLang as $sKey => $sText) {
...@@ -606,12 +626,13 @@ class cronlogrenderer extends cronlog{ ...@@ -606,12 +626,13 @@ class cronlogrenderer extends cronlog{
} }
/** /**
* show a single log file * Get html code to show a single log file with syntax highligt an marking the reeturn code
* *
* @param string $sLogfile logfile; [server]/[filename.log] * @param string $sLogfile logfile; [server]/[filename.log]
* @return string * @return string
*/ */
public function renderLogfile($sLogfile){ public function renderLogfile(string $sLogfile): string
{
$sHtml = '' $sHtml = ''
. '<button style="position: fixed;" onclick="closeOverlay();" class="btn btn-default"><i class="fas fa-chevron-left"></i> ' . $this->t('back') . '</button><br><br>' . '<button style="position: fixed;" onclick="closeOverlay();" class="btn btn-default"><i class="fas fa-chevron-left"></i> ' . $this->t('back') . '</button><br><br>'
. '<h3>' . $this->t('logfile') . ' ' . basename($sLogfile) . '</h3>' . '<h3>' . $this->t('logfile') . ' ' . basename($sLogfile) . '</h3>'
...@@ -628,7 +649,6 @@ class cronlogrenderer extends cronlog{ ...@@ -628,7 +649,6 @@ class cronlogrenderer extends cronlog{
return $sHtml . sprintf($this->t('error-logfile-not-found'), $sMyFile); return $sHtml . sprintf($this->t('error-logfile-not-found'), $sMyFile);
} }
if ($fileHandle = fopen($sMyFile, "r")) { if ($fileHandle = fopen($sMyFile, "r")) {
$sHtml .= '<div style="float: left;"><pre>'; $sHtml .= '<div style="float: left;"><pre>';
while (($line = fgets($fileHandle)) !== false) { while (($line = fgets($fileHandle)) !== false) {
...@@ -665,14 +685,15 @@ class cronlogrenderer extends cronlog{ ...@@ -665,14 +685,15 @@ class cronlogrenderer extends cronlog{
} }
/** /**
* get html code for a select box with all servers * Get html code for a select box with all servers
* @return string * @return string
*/ */
public function renderServerlist($sSelectedItem=false){ public function renderServerlist(string $sSelectedItem = ''): string
{
$sHtml = ''; $sHtml = '';
$iMaxItemsToShow = 30; $iMaxItemsToShow = 30;
$sHtml .= '<option value="ALL"' $sHtml .= '<option value="ALL"'
.($sSelectedItem===false || $sSelectedItem==='ALL' ? ' selected="selected"' : '') . ($sSelectedItem === '' || $sSelectedItem === 'ALL' ? ' selected="selected"' : '')
. '>[' . $this->t('ALL') . ' (' . count($this->getServers()) . ')]</option>'; . '>[' . $this->t('ALL') . ' (' . count($this->getServers()) . ')]</option>';
foreach ($this->getServers() as $sServer => $aData) { foreach ($this->getServers() as $sServer => $aData) {
$sHtml .= '<option value="' . $sServer . '"' $sHtml .= '<option value="' . $sServer . '"'
...@@ -689,7 +710,7 @@ class cronlogrenderer extends cronlog{ ...@@ -689,7 +710,7 @@ class cronlogrenderer extends cronlog{
><button onclick="$(\'#serverfiltertext\').val(\'\'); filterServers();">X</button><br><br>' ><button onclick="$(\'#serverfiltertext\').val(\'\'); filterServers();">X</button><br><br>'
*/ */
. '<select' . '<select'
. ' size="'.( min(array(count($this->getServers())+1 , $iMaxItemsToShow)) ).'"' . ' size="' . (min([count($this->getServers()) + 1, $iMaxItemsToShow])) . '"'
// . ' size="1"' // . ' size="1"'
. ' onchange="setServer(this.value); return false;"' . ' onchange="setServer(this.value); return false;"'
. '>' . $sHtml . '</select>' . '>' . $sHtml . '</select>'
......
...@@ -31,44 +31,46 @@ ...@@ -31,44 +31,46 @@
* *
* @license GNU GPL 3.0 * @license GNU GPL 3.0
* @author Axel Hahn <axel.hahn@iml.unibe.ch> * @author Axel Hahn <axel.hahn@iml.unibe.ch>
*
* 2024-09-20 <axel.hahn@unibe.ch> added type declarations; update php docs
*/ */
class cronlog { class cronlog
{
protected $_sDataDir = "__APPDIR__/data"; protected string $_sDataDir = "__APPDIR__/data";
protected $_iTtlCache = 60; // in sec protected int $_iTtlCache = 60; // in sec
/** /**
* when show an error for expired jobs (latency to execute job and sync logs) * when show an error for expired jobs (latency to execute job and sync logs)
* @var integer * @var integer
*/ */
protected $_iExpiredJobsFailAfter = 60*30; // in sec protected int $_iExpiredJobsFailAfter = 60 * 30; // in sec
protected $_iMinTtl = 0; // in sec protected int $_iMinTtl = 0; // in sec
protected $_aSkipJoblogs = array(); protected array $_aSkipJoblogs = [];
protected $_aInstances = array(); protected array $_aInstances = [];
protected $_aServers = array(); protected array $_aServers = [];
protected $_sActiveServer = false; protected string $_sActiveServer = '';
protected $_sFileFilter_serverjoblog = '*joblog*.done'; protected string $_sFileFilter_serverjoblog = '*joblog*.done';
protected $_sFileFilter_joblog = '*.log'; protected string $_sFileFilter_joblog = '*.log';
protected $_sFileFilter_jobrunning = '*.log.running*'; protected string $_sFileFilter_jobrunning = '*.log.running*';
protected $_sLang = ''; // language ... read from config file protected string $_sLang = ''; // language ... read from config file
protected $_aLang = []; // language data protected array $_aLang = []; // language data
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// MAIN // MAIN
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* init * Constructor
* @return boolean
*/ */
public function __construct() { public function __construct()
{
// read config // read config
if (file_exists(__DIR__ . '/../config/inc_cronlog.php')) { if (file_exists(__DIR__ . '/../config/inc_cronlog.php')) {
$aCfgTemp = include(__DIR__ . '/../config/inc_cronlog.php'); $aCfgTemp = include(__DIR__ . '/../config/inc_cronlog.php');
...@@ -91,8 +93,6 @@ class cronlog { ...@@ -91,8 +93,6 @@ class cronlog {
$this->_sDataDir = str_replace('\\', '/', $this->_sDataDir); $this->_sDataDir = str_replace('\\', '/', $this->_sDataDir);
$this->getServers(); $this->getServers();
return true;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
...@@ -104,33 +104,37 @@ class cronlog { ...@@ -104,33 +104,37 @@ class cronlog {
* chaching: get the full path of directory for caching * chaching: get the full path of directory for caching
* @return string * @return string
*/ */
protected function _getCacheDir(){ protected function _getCacheDir(): string
{
return $this->_sDataDir . '/__cache'; return $this->_sDataDir . '/__cache';
} }
/** /**
* caching: get full path of a caching item * Caching: get full path of a caching item
* @param string $sTaskId * @param string $sTaskId Name of a task
* @return string * @return string
*/ */
protected function _getCacheFile($sTaskId){ protected function _getCacheFile(string $sTaskId): string
{
return $this->_getCacheDir() . '/' . $sTaskId; return $this->_getCacheDir() . '/' . $sTaskId;
} }
/** /**
* read logs: get full path to a servers cronjob logdata * Read logs: get full path to a servers cronjob logdata
* @return string * @return string
*/ */
protected function _getServerlogDir(){ protected function _getServerlogDir(): string
{
return $this->_sDataDir . '/' . $this->_sActiveServer; return $this->_sDataDir . '/' . $this->_sActiveServer;
} }
/** /**
* caching: get cached data if they exist and aren't expired * Caching: get cached data if they exist and aren't expired
* @param string $sTaskId * @param string $sTaskId Name of the task
* @return mixed boolean|array * @return mixed boolean|array
*/ */
protected function _getCacheData($sTaskId){ protected function _getCacheData(string $sTaskId): string
{
// DISABLE CACHE return false; // DISABLE CACHE return false;
$sFile = $this->_getCacheFile($sTaskId); $sFile = $this->_getCacheFile($sTaskId);
if (file_exists($sFile)) { if (file_exists($sFile)) {
...@@ -142,15 +146,16 @@ class cronlog { ...@@ -142,15 +146,16 @@ class cronlog {
unlink($sFile); unlink($sFile);
} }
} }
return false; return '';
} }
/** /**
* read logs: parse a single line in the joblog and return has with all key value items * Read logs: parse a single line in the joblog and return has with all key value items
* @param string $sLine single line in the log * @param string $sLine single line in the log
* @return array * @return array
*/ */
protected function _parseJoblogLine($sLine){ protected function _parseJoblogLine(string $sLine): array
$aReturn=array(); {
$aReturn = [];
// echo "DEBUG $sLine<br>"; // echo "DEBUG $sLine<br>";
// job=dok-kvm-instances:host=kalium:start=1538358001:end=1538358001:exectime=0:ttl=60:rc=0 // job=dok-kvm-instances:host=kalium:start=1538358001:end=1538358001:exectime=0:ttl=60:rc=0
$sLine = str_replace("\n", '', $sLine); $sLine = str_replace("\n", '', $sLine);
...@@ -168,11 +173,13 @@ class cronlog { ...@@ -168,11 +173,13 @@ class cronlog {
} }
/** /**
* read logs: parse the whole cronwrapper logfile and return a hash * Read logs: parse the whole cronwrapper logfile and return a hash
* @param string $sFile filename with full path * @param string $sFile filename with full path
* @return array
*/ */
protected function _parseLogfile($sFile) { protected function _parseLogfile(string $sFile): array
$aReturn=array( {
$aReturn = [
'SCRIPTNAME' => false, 'SCRIPTNAME' => false,
'SCRIPTTTL' => false, 'SCRIPTTTL' => false,
'SCRIPTSTARTTIME' => false, 'SCRIPTSTARTTIME' => false,
...@@ -180,8 +187,8 @@ class cronlog { ...@@ -180,8 +187,8 @@ class cronlog {
'SCRIPTENDTIME' => false, 'SCRIPTENDTIME' => false,
'SCRIPTEXECTIME' => false, 'SCRIPTEXECTIME' => false,
'SCRIPTRC' => false, 'SCRIPTRC' => false,
// 'SCRIPTOUT'=>array(), // 'SCRIPTOUT'=>[],
); ];
$fileHandle = fopen($sFile, "r"); $fileHandle = fopen($sFile, "r");
while (($line = fgets($fileHandle)) !== false) { while (($line = fgets($fileHandle)) !== false) {
// get key ... the part before "=" // get key ... the part before "="
...@@ -207,10 +214,11 @@ class cronlog { ...@@ -207,10 +214,11 @@ class cronlog {
/** /**
* caching: write new data; it returns the success of write operation as bool * caching: write new data; it returns the success of write operation as bool
* @param string $sTaskId * @param string $sTaskId
* @param [any] $data data to store; can be any serializable value * @param mixed $data data to store; can be any serializable value
* @return boolean * @return boolean|integer
*/ */
protected function _writeCacheData($sTaskId, $data){ protected function _writeCacheData(string $sTaskId, mixed $data): bool|int
{
$sFile = $this->_getCacheFile($sTaskId); $sFile = $this->_getCacheFile($sTaskId);
// echo "WRITE cache $sFile<br>"; // echo "WRITE cache $sFile<br>";
return file_put_contents($sFile, serialize($data)); return file_put_contents($sFile, serialize($data));
...@@ -221,24 +229,26 @@ class cronlog { ...@@ -221,24 +229,26 @@ class cronlog {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* get currently selected server * Get currently selected server
* @return string * @return string
*/ */
public function getServer(){ public function getServer(): string
{
return $this->_sActiveServer; return $this->_sActiveServer;
} }
/** /**
* get array with existing servers in data dir * Get array with existing servers in data dir
* @return array * @return array
*/ */
public function getServers(){ public function getServers(): array
{
if (is_array($this->_aServers) && count($this->_aServers)) { if (is_array($this->_aServers) && count($this->_aServers)) {
return $this->_aServers; return $this->_aServers;
} }
$this->_aServers=array(); $this->_aServers = [];
// echo "DEBUG DATADIR: " . $this->_sDataDir."<br>"; // echo "DEBUG DATADIR: " . $this->_sDataDir."<br>";
if (!is_dir($this->_sDataDir)) { if (!is_dir($this->_sDataDir)) {
echo "WARNING: no data. Check sDatadir in the config and set it to an existing directory.<br>"; echo "WARNING: no data. Check sDatadir in the config and set it to an existing directory.<br>";
...@@ -249,7 +259,7 @@ class cronlog { ...@@ -249,7 +259,7 @@ class cronlog {
while (false !== ($entry = readdir($handle))) { while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != "__cache" && is_dir($this->_sDataDir . '/' . $entry)) { if ($entry != "." && $entry != ".." && $entry != "__cache" && is_dir($this->_sDataDir . '/' . $entry)) {
// echo "DEBUG $entry<br>\n"; // echo "DEBUG $entry<br>\n";
$this->_aServers[$entry]=array(); $this->_aServers[$entry] = [];
} }
} }
closedir($handle); closedir($handle);
...@@ -259,12 +269,13 @@ class cronlog { ...@@ -259,12 +269,13 @@ class cronlog {
} }
/** /**
* get logs from jobfilea of the current or given server * Get logs from jobfilea of the current or given server
* @param boolean $bUseSkip hide jobs if their label matches the skip list; default: true * @param boolean $bUseSkip hide jobs if their label matches the skip list; default: true
* @return array * @return array
*/ */
public function getServerJobHistory($bUseSkip=true){ public function getServerJobHistory(bool $bUseSkip = true): bool|array
$aReturn=array(); {
$aReturn = [];
$sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer; $sTaskId = __FUNCTION__ . '-' . $this->_sActiveServer;
$aData = $this->_getCacheData($sTaskId); $aData = $this->_getCacheData($sTaskId);
...@@ -272,7 +283,7 @@ class cronlog { ...@@ -272,7 +283,7 @@ class cronlog {
return $aData; return $aData;
} }
$aData=array(); $aData = [];
foreach (glob($this->_getServerlogDir() . '/' . $this->_sFileFilter_serverjoblog) as $sMyJobfile) { foreach (glob($this->_getServerlogDir() . '/' . $this->_sFileFilter_serverjoblog) as $sMyJobfile) {
// echo "DEBUG: $sMyJobfile<br>"; // echo "DEBUG: $sMyJobfile<br>";
...@@ -293,12 +304,13 @@ class cronlog { ...@@ -293,12 +304,13 @@ class cronlog {
/** /**
* get logs from jobfilea of the current or given server * Get logs from jobfilea of the current or given server
* @return array * @return array
*/ */
public function getServersLastLog(){ public function getServersLastLog(): array
$aReturn=array(); {
$aData=array(); $aReturn = [];
$aData = [];
foreach (glob($this->_getServerlogDir() . '/' . $this->_sFileFilter_joblog) as $sMyJobfile) { foreach (glob($this->_getServerlogDir() . '/' . $this->_sFileFilter_joblog) as $sMyJobfile) {
// echo "DEBUG: log file $sMyJobfile<br>"; // echo "DEBUG: log file $sMyJobfile<br>";
$aData = $this->_parseLogfile($sMyJobfile); $aData = $this->_parseLogfile($sMyJobfile);
...@@ -309,13 +321,15 @@ class cronlog { ...@@ -309,13 +321,15 @@ class cronlog {
rsort($aReturn); rsort($aReturn);
return $aReturn; return $aReturn;
} }
/** /**
* get logs from jobfilea of the current or given server * get logs from jobfilea of the current or given server
* @return array * @return array
*/ */
public function getRunningJobs(){ public function getRunningJobs(): array
$aReturn=array(); {
$aData=array(); $aReturn = [];
$aData = [];
foreach (glob($this->_getServerlogDir() . '/' . $this->_sFileFilter_jobrunning) as $sMyJobfile) { foreach (glob($this->_getServerlogDir() . '/' . $this->_sFileFilter_jobrunning) as $sMyJobfile) {
// echo "DEBUG: log file $sMyJobfile<br>"; // echo "DEBUG: log file $sMyJobfile<br>";
$aData = $this->_parseLogfile($sMyJobfile); $aData = $this->_parseLogfile($sMyJobfile);
...@@ -328,26 +342,27 @@ class cronlog { ...@@ -328,26 +342,27 @@ class cronlog {
} }
/** /**
* translate ... get a language specific text of a given key * Translate: get a language specific text of a given key
* @param string $id id of language text * @param string $id id of language text
* @return string * @return string
*/ */
public function t($id){ public function t($id): string
return '' {
.(isset($this->_aLang[$id]) ? $this->_aLang[$id] : '['.$id.'] ???') return $this->_aLang[$id] ?? '[' . $id . '] ???';
;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// public setter // public setter
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
/** /**
* set which server is selected * Set which server is selected
* The given server must exist as directory (that contains its logs) * The given server must exist as directory (that contains its logs)
* It returns false if the given server does not exist or has value 'ALL'
* @param string $sServer server name * @param string $sServer server name
* @return string * @return bool|string
*/ */
public function setServer($sServer){ public function setServer(string $sServer): bool|string
{
$this->_sActiveServer = false; $this->_sActiveServer = false;
if ($sServer === 'ALL') { if ($sServer === 'ALL') {
return false; return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment