From faea12442b573acb37dc60c71ca66a3edea56510 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Tue, 4 Oct 2022 08:41:16 +0200
Subject: [PATCH] add support for multiple instances

---
 classes/cronlog-renderer.class.php | 20 ++++++++++++++++++++
 classes/cronlog.class.php          |  4 ++++
 config/inc_cronlog.php.dist        | 11 ++++++++++-
 get.php                            |  3 +++
 index.html                         |  5 ++++-
 js/functions.js                    |  9 +++++++++
 main.css                           |  8 +++++++-
 7 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/classes/cronlog-renderer.class.php b/classes/cronlog-renderer.class.php
index 53751c4..96b8637 100644
--- a/classes/cronlog-renderer.class.php
+++ b/classes/cronlog-renderer.class.php
@@ -217,6 +217,26 @@ class cronlogrenderer extends cronlog{
         return $this->renderCronlogs($aData);
     }
     
+    /**
+     * get html code for a switcher of multiple instances
+     * 
+     * @param array  $aData   result of $oCL->getServerLogs()
+     * @return string
+     */
+    public function renderInstances(){
+        if(count($this->_aInstances) < 2){
+            return false;
+        }
+        $sReturn='';
+        $sServer=isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : false;
+        
+        foreach($this->_aInstances as $sInstance => $sUrl){
+            $sHost=parse_url($sUrl, PHP_URL_HOST);
+            $sClass=($sServer && $sServer==$sHost) ? 'active' : '';
+            $sReturn.='<a class="'.$sClass.'" href="'.$sUrl.'" title="'.$sUrl.'">'.$sInstance.'</a> ';
+        }
+        return $sReturn;
+    }
     /**
      * get html code for a table with history of executed cronjobs
      * 
diff --git a/classes/cronlog.class.php b/classes/cronlog.class.php
index 2ab134f..d0a0afe 100644
--- a/classes/cronlog.class.php
+++ b/classes/cronlog.class.php
@@ -47,6 +47,8 @@ class cronlog {
     protected $_iMinTtl = 0; // in sec
     protected $_aSkipJoblogs = array();
     
+    protected $_aInstances = array();
+
     protected $_aServers = array();
     protected $_sActiveServer = false;
     
@@ -71,6 +73,8 @@ class cronlog {
             $this->_iMinTtl = isset($aCfgTemp['iMinTtl']) ? (int)$aCfgTemp['iMinTtl'] : $this->_iMinTtl;
             $this->_iExpiredJobsFailAfter = isset($aCfgTemp['iExpiredJobsFailAfter']) ? (int)$aCfgTemp['iExpiredJobsFailAfter'] : $this->_iExpiredJobsFailAfter;
             $this->_aSkipJoblogs = isset($aCfgTemp['aHidelogs']) && is_array($aCfgTemp['aHidelogs']) ? $aCfgTemp['aHidelogs'] : $this->_aSkipJoblogs;        
+            $this->_aInstances = isset($aCfgTemp['instances']) ? $aCfgTemp['instances'] : [];
+
         }
         $this->_sDataDir = str_replace("__APPDIR__", dirname(dirname(__FILE__)), $this->_sDataDir);
         $this->_sDataDir = str_replace('\\', '/', $this->_sDataDir);
diff --git a/config/inc_cronlog.php.dist b/config/inc_cronlog.php.dist
index 2d9be22..bcacdd6 100644
--- a/config/inc_cronlog.php.dist
+++ b/config/inc_cronlog.php.dist
@@ -33,5 +33,14 @@ return array(
     // hide these cronjob labels in the web gui tab "history"
     'aHidelogs'=>array(
         'copycronlogs',
-    )
+    ),
+
+    // if you have multiple instances: add label and url for a switcher menu
+    /*
+    'instances'=>[
+        'Docker'=>'http://localhost:8006',
+        'Intern'=>'https://cronlogs.intern.example.com/',
+        'Extern'=>'https://cronlogs.extern.example.com/',
+    ],
+    */    
 );
diff --git a/get.php b/get.php
index e786ffe..6c44582 100644
--- a/get.php
+++ b/get.php
@@ -67,6 +67,9 @@ switch ($sItem){
             $sHtml.=$oCL->renderJobGraph();
         }
         break;
+    case 'instances':
+        $sHtml.=$oCL->renderInstances($sServer);
+        break;
     case 'selectserver':
         $sHtml.=$oCL->renderServerlist($sServer);
         break;
diff --git a/index.html b/index.html
index 3a9c45a..40e3f99 100644
--- a/index.html
+++ b/index.html
@@ -22,7 +22,10 @@
     </head>
     <body>
         <div id="errorlog"></div>
-        <h1><a href="?"><span class="imllogo"></span> CronjobViewer</a></h1>
+        <header>
+            <div id="instances">...</div>
+            <h1><a href="?"><span class="imllogo"></span> CronjobViewer</a></h1>
+        </header>
         <nav class="servers">
             <div id="selectserver" class="active">...</div>
         </nav>
diff --git a/js/functions.js b/js/functions.js
index d1e9381..e49d809 100644
--- a/js/functions.js
+++ b/js/functions.js
@@ -272,6 +272,14 @@ $(document).ready(function () {
             'taskparam': 'item',
 
             'tasks':{
+                'instances':{
+                    domid: 'instances',
+                    apimore: '?',
+                    params: [
+                        'instances'
+                    ],
+                    interval: 0
+                },
                 'selectserverAll':{
                     domid: 'selectserver',
                     apimore: '?',
@@ -299,6 +307,7 @@ $(document).ready(function () {
         oAjaxHelper.setVar('server', sSELECTEDSERVER);
         oAjaxHelper.setVar('tab', sACTIVESERVERTAB);
         oAjaxHelper.execTask('selectserver');
+        oAjaxHelper.execTask('instances');
         // oAjaxHelper.execTask('selectserverAll');
 });
 
diff --git a/main.css b/main.css
index 49cf719..47b0e17 100644
--- a/main.css
+++ b/main.css
@@ -19,9 +19,15 @@ button:active{box-shadow: none; }
 
 footer{position: fixed; bottom: 1em; right: 1em; background: var(--bg-header); border-top: 1px solid var(--border); padding: 1em; text-align: right;}
 
-h1{border-bottom: 1px solid var(--border); background:var(--bg-header); margin: 0 0 0.3em; padding: 1em 0.3em;}
+header{border-bottom: 1px solid var(--border); background:var(--bg-header); margin: 0 0 0.3em; padding: 1.5em 1em;}
+h1{margin: 0 0 0.3em; padding: 0; margin: 0;}
 h1 a{color: #38a;}
 h1 a:hover{text-decoration: none;}
+  #instances{float: right;}
+  #instances a{border: 0px solid; padding: 0.4em;}
+  #instances a:hover{background: var(--bg-main);}
+  #instances a.active{background: var(--bg-main);border-top: 1px solid var(--border); border-left: 1px solid var(--border);}
+
 h2{color:#379;}
 h2 i{color:#abc; font-size: 140%;}
 h3{color: #39b;}
-- 
GitLab