From 67e8a82f3be442c2921b9af7dd02f96b65d88b1f Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Thu, 5 Sep 2019 11:50:33 +0200
Subject: [PATCH] ignore skipentries in timeline; added phpdocs

---
 classes/cronlog-renderer.class.php |  20 +++-
 classes/cronlog.class.php          | 156 +++++++++++++++++------------
 2 files changed, 110 insertions(+), 66 deletions(-)

diff --git a/classes/cronlog-renderer.class.php b/classes/cronlog-renderer.class.php
index e139dcb..7b3d175 100644
--- a/classes/cronlog-renderer.class.php
+++ b/classes/cronlog-renderer.class.php
@@ -10,12 +10,15 @@ require_once 'cronlog.class.php';
  */
 class cronlogrenderer extends cronlog{
 
-    
+    /**
+     * minimal length for execution time of a job to be rendered in the timeline; value is in seconds
+     * @var integer
+     */
     protected $_iMinTime4Timeline = 60;
     
     /**
-     * show date of last data and last access
-     * @param type $iLast
+     * show date of last data and last access; used in rendering methods to display it on top
+     * @param type $iLast  timestamp of last log entry
      * @return string
      */
     protected function _renderAccessAndAge($iLast){
@@ -249,6 +252,11 @@ class cronlogrenderer extends cronlog{
         return $sHtml;
     }
    
+    /**
+     * get html code for a joblist of the selected server
+     * it uses the filter for hidden joblog entries (aHidelogs in config)
+     * @return string
+     */
     public function renderJoblistOfAllServers(){
         $aData=array();
         foreach (array_keys($this->getServers()) as $sServer){
@@ -283,7 +291,7 @@ class cronlogrenderer extends cronlog{
         }
         $iGraphCounter++;
         if(!$aData){
-            $aData=$this->getServerJobHistory();
+            $aData=$this->getServerJobHistory(false);
         }
         $sDivId='vis-timeline-'.$iGraphCounter;
         
@@ -448,6 +456,10 @@ class cronlogrenderer extends cronlog{
        return $sHtml;
    }
     
+    /**
+     * get html code for a select box with all servers
+     * @return string
+     */
     public function renderServerlist($sSelectedItem=false){
         $sHtml='';
         $sHtml.='<option value="ALL"'
diff --git a/classes/cronlog.class.php b/classes/cronlog.class.php
index 50ffabb..acaf22a 100644
--- a/classes/cronlog.class.php
+++ b/classes/cronlog.class.php
@@ -21,13 +21,20 @@ class cronlog {
     protected $_iExpiredJobsFailAfter = 60*30; // in sec
     protected $_aSkipJoblogs = array();
     
-    protected $_aServers = false;
+    protected $_aServers = array();
     protected $_sActiveServer = false;
     
     protected $_sFileFilter_serverjoblog = '*joblog*.done';
     protected $_sFileFilter_serverlog = '*.log';
 
-    
+    // ----------------------------------------------------------------------
+    // MAIN
+    // ----------------------------------------------------------------------
+
+    /**
+     * init
+     * @return boolean
+     */
     public function __construct() {
 
         // read config
@@ -52,7 +59,7 @@ class cronlog {
 
     
     /**
-     * get the full path of directory for caching
+     * chaching: get the full path of directory for caching
      * @return type
      */
     protected function _getCacheDir(){
@@ -60,7 +67,7 @@ class cronlog {
     }
 
     /**
-     * get full path of a caching item
+     * caching: get full path of a caching item
      * @param type $sTaskId
      * @return type
      */
@@ -69,13 +76,18 @@ class cronlog {
     }
 
     /**
-     * get full path to a servers cronjob logdata
+     * read logs: get full path to a servers cronjob logdata
      * @return type
      */
     protected function _getServerlogDir(){
         return $this->_sDataDir.'/'.$this->_sActiveServer;
     }
 
+    /**
+     * caching: get cached data if they exist and aren't expired
+     * @param  string  $sTaskId
+     * @return boolean|array
+     */
     protected function _getCacheData($sTaskId){
         // DISABLE CACHE return false;
         $sFile=$this->_getCacheFile($sTaskId);
@@ -90,6 +102,72 @@ class cronlog {
         }
         return false;
     }
+    /**
+     * 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
+     * @return array
+     */
+    protected function _parseJoblogLine($sLine){
+        $aReturn=array();
+        // echo "DEBUG $sLine<br>";
+        // job=dok-kvm-instances:host=kalium:start=1538358001:end=1538358001:exectime=0:ttl=60:rc=0
+        $sLine=str_replace("\n", '', $sLine);
+        $sLine=str_replace(':', '", "', $sLine);
+        $sLine=str_replace('=', '": "', $sLine);
+        $sLine='{"'.$sLine.'"}';
+        // echo "DEBUG $sLine<br><br>";
+        $aReturn=json_decode($sLine, 1);
+        if(!is_array($aReturn)){
+            echo "not a JSON string<br>";
+            echo "DEBUG $sLine<br><br>";
+            die();
+        }
+        return $aReturn;
+    }
+
+    /**
+     * read logs: parse the whole cronwrapper logfile and return a hash
+     * @param  string  $sFile  filename with full path
+     */
+    protected function _parseLogfile($sFile) {
+        $aReturn=array(
+            'SCRIPTNAME'=>false,
+            'SCRIPTTTL'=>false,
+            'SCRIPTSTARTTIME'=>false,
+            'SCRIPTLABEL'=>false,
+            'SCRIPTENDTIME'=>false,
+            'SCRIPTEXECTIME'=>false,
+            'SCRIPTRC'=>false,
+            // 'SCRIPTOUT'=>array(),
+        );
+        $fileHandle = fopen($sFile, "r");
+        while (($line = fgets($fileHandle)) !== false) {
+            // get key ... the part before "="
+            $sKey=trim(preg_replace('/=.*/', '', $line));
+            if($sKey && isset($aReturn[$sKey])){
+                // add value ... the part behind "="
+                $aReturn[$sKey]=preg_replace('/^([A-Z]*\=)/', '', $line);
+            }
+        }
+        fclose($fileHandle);
+        
+        // fetch unit timestamp from date values (they are like "2018-09-30 03:40:05, 1538278805")
+        $aReturn['SCRIPTSTARTTIME']=(int)preg_replace('/.*,\ /', '', $aReturn['SCRIPTSTARTTIME']);
+        $aReturn['SCRIPTENDTIME']=(int)preg_replace('/.*,\ /', '', $aReturn['SCRIPTSTARTTIME']);
+        
+        // remove " s" from exec time value
+        $aReturn['SCRIPTEXECTIME']=preg_replace('/\ s$/', '', $aReturn['SCRIPTEXECTIME']);
+        
+        return $aReturn;
+        
+    }
+    
+    /**
+     * caching: write new data; it returns the success of write operation as bool
+     * @param  string  $sTaskId
+     * @param  [any]   $data      data to store; can be any serializable value
+     * @return boolean
+     */
     protected function _writeCacheData($sTaskId, $data){
         $sFile=$this->_getCacheFile($sTaskId);
         // echo "WRITE cache $sFile<br>";
@@ -101,7 +179,7 @@ class cronlog {
     // ----------------------------------------------------------------------
     
     /**
-     * get current server
+     * get currently selected server
      * @return string
      */
     public function getServer(){
@@ -138,32 +216,12 @@ class cronlog {
         return $this->_aServers;
     }
     
-        /**
-         * 
-         */
-        protected function _parseJoblogLine($sLine){
-            $aReturn=array();
-            // echo "DEBUG $sLine<br>";
-            // job=dok-kvm-instances:host=kalium:start=1538358001:end=1538358001:exectime=0:ttl=60:rc=0
-            $sLine=str_replace("\n", '', $sLine);
-            $sLine=str_replace(':', '", "', $sLine);
-            $sLine=str_replace('=', '": "', $sLine);
-            $sLine='{"'.$sLine.'"}';
-            // echo "DEBUG $sLine<br><br>";
-            $aReturn=json_decode($sLine, 1);
-            if(!is_array($aReturn)){
-                echo "not a JSON string<br>";
-                echo "DEBUG $sLine<br><br>";
-                die();
-            }
-            return $aReturn;
-        }
-    
     /**
      * get logs from jobfilea of the current or given server
+     * @param  boolean  $bUseSkip  hide jobs if their label matches the skip list; default: true
      * @return array
      */
-    public function getServerJobHistory(){
+    public function getServerJobHistory($bUseSkip=true){
         $aReturn=array();
         $sTaskId=__FUNCTION__.'-'.$this->_sActiveServer;
 
@@ -180,7 +238,7 @@ class cronlog {
             while (($line = fgets($fileHandle)) !== false) {
                 // send the current file part to the browser
                 $aData=$this->_parseJoblogLine($line);
-                if(array_search($aData['job'], $this->_aSkipJoblogs)===false){
+                if(!$bUseSkip && array_search($aData['job'], $this->_aSkipJoblogs)===false){
                     $aReturn[$aData['start']]=$aData;
                 }
             }
@@ -191,39 +249,7 @@ class cronlog {
         return $aReturn;
     }
     
-    protected function _parseLogfile($sFile) {
-        $aReturn=array(
-            'SCRIPTNAME'=>false,
-            'SCRIPTTTL'=>false,
-            'SCRIPTSTARTTIME'=>false,
-            'SCRIPTLABEL'=>false,
-            'SCRIPTENDTIME'=>false,
-            'SCRIPTEXECTIME'=>false,
-            'SCRIPTRC'=>false,
-            // 'SCRIPTOUT'=>array(),
-        );
-        $fileHandle = fopen($sFile, "r");
-        while (($line = fgets($fileHandle)) !== false) {
-            // get key ... the part before "="
-            $sKey=trim(preg_replace('/=.*/', '', $line));
-            if($sKey && isset($aReturn[$sKey])){
-                // add value ... the part behind "="
-                $aReturn[$sKey]=preg_replace('/^([A-Z]*\=)/', '', $line);
-            }
-        }
-        fclose($fileHandle);
-        
-        // fetch unit timestamp from date values (they are like "2018-09-30 03:40:05, 1538278805")
-        $aReturn['SCRIPTSTARTTIME']=(int)preg_replace('/.*,\ /', '', $aReturn['SCRIPTSTARTTIME']);
-        $aReturn['SCRIPTENDTIME']=(int)preg_replace('/.*,\ /', '', $aReturn['SCRIPTSTARTTIME']);
-        
-        // remove " s" from exec time value
-        $aReturn['SCRIPTEXECTIME']=preg_replace('/\ s$/', '', $aReturn['SCRIPTEXECTIME']);
-        
-        return $aReturn;
-        
-    }
-    
+
     /**
      * get logs from jobfilea of the current or given server
      * @return array
@@ -246,6 +272,12 @@ class cronlog {
     // public setter
     // ----------------------------------------------------------------------
    
+    /**
+     * set which server is selected
+     * The given server must exist as directory (that contains its logs)
+     * @param  string  $sServer  server name
+     * @return string
+     */
     public function setServer($sServer){
         $this->_sActiveServer=false;
         if($sServer==='ALL'){
-- 
GitLab