[icinga-checkins] icinga.org: icinga-web/master: * Fixed debug sorting order (fixes #2028)

git at icinga.org git at icinga.org
Mon Nov 21 17:06:32 CET 2011


Module: icinga-web
Branch: master
Commit: 1308ff7cec6229ab5f4a5c5527dc3a5b67dabc44
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=1308ff7cec6229ab5f4a5c5527dc3a5b67dabc44

Author: Jannis Mosshammer <jannis.mosshammer at netways.de>
Date:   Mon Nov 21 16:42:46 2011 +0100

* Fixed debug sorting order (fixes #2028)

---

 app/modules/AppKit/models/LogParserModel.class.php |  104 ++++++++++++++------
 .../AppKit/templates/Admin/ViewLogsSuccess.php     |    2 +-
 .../Tackle/Renderer/AdditionalURLColumnRenderer.js |    1 +
 3 files changed, 76 insertions(+), 31 deletions(-)

diff --git a/app/modules/AppKit/models/LogParserModel.class.php b/app/modules/AppKit/models/LogParserModel.class.php
index c59d40f..78b878b 100644
--- a/app/modules/AppKit/models/LogParserModel.class.php
+++ b/app/modules/AppKit/models/LogParserModel.class.php
@@ -5,8 +5,9 @@
  * @author Jannis Moßhammer <jannis.mosshammer at netways.de>
  */
 class AppKit_LogParserModel extends AppKitBaseModel {
-    private $currentMaxCount;
-
+    private static $LOG_FORMAT = "[%s] [%s] %s";
+    private $entriesRead = 0;
+    private $total = 0;
     public function initialize(AgaviContext $context, array $params = array()) {
         parent::initialize($context,$params);
 
@@ -15,54 +16,97 @@ class AppKit_LogParserModel extends AppKitBaseModel {
     public function parseLog($name,$start=0,$end=100,$dir = "desc") {
         $files = $this->getLogFilesByName($name);
         $stringToParse = $this->getEntriesFromFiles($files,$start,$end,$dir);
-        return array("total"=>$this->currentMaxCount,"result"=>$this->parseString($stringToParse,$end));
+        return array("total"=>$this->total,"result"=>$this->parseLogEntries($stringToParse,$end));
 
     }
 
-    protected function getEntriesFromFiles(array $files,$start=0,$dir = "desc") {
+    protected function getEntriesFromFiles(array $files,$start=0,$end=100,$dir = "desc") {
         if ($dir == "desc") {
-            sort($files);
-        } else {
             rsort($files);
+        } else {
+            sort($files);
         }
-
+        $string = array();
         $base = $this->getLogDir();
-        $string = "";
-        $completeCount = 0;
-        foreach($files as $file) {
-            $content = file_get_contents($base."/".$file);
+        $this->entriesRead = 0;
+        $this->total = 0;
 
-            if (($count = substr_count($content,"\n[")+1) < $start) {
-                $completeCount += $count;
-                $start -= $count;
-                continue;
-            }
+        foreach($files as $file) {
+            $currentLog = $base."/".$file;
+            $string = array_merge($string, $this->readFileDesc($currentLog,$start,$end,$this->entriesRead >= $end));
 
-            $completeCount += $count;
-            $string .= $content;
         }
+        return $string;
+    }
 
-        if ($start) {
-            $string = preg_replace("/(.*\n)/","",$string,$start);
-        }
+    private function readFileDesc($log, &$start,$end, $justCount = false) {
+        $handle = fopen($log,"r");
+        if(!$handle)
+            return array();
+        $pos = -1;
 
-        $this->currentMaxCount = $completeCount;
+        while (!feof($handle)) {
+            if ($f = fgets($handle)) {
+                if($f[0] == "[")
+                    $this->total++;
+            }
+        }
+        if($justCount)
+            return array();
+        fseek($handle,$pos,SEEK_END);
+        $string = array();
+        while (false !== ($char = fgetc($handle))) {
+
+            if(($char == "\n" ) && ($start-- <= 0)) {
+                  
+                $line = fgets($handle);
+                if($line !== false) {
+                    if($line[0] == "[") {
+                        $string [] = $line;
+                        $this->entriesRead++;
+                    } else if(count($string) > 0) {
+                        $string[count($string)-1] .= " ".$line;
+                    }
+                    if($this->entriesRead == $end)
+                        break;
+                }
+            }
+            $start = $start < 0 ? 0 : $start; // prevent underflow
+            fseek($handle,--$pos,SEEK_END);
+        }
+        fclose($handle);
         return $string;
     }
 
-    protected function parseString($str,$end = 100) {
-        $line = preg_split("/^(\[)|(\n(?:\[))/",$str,$end,PREG_SPLIT_NO_EMPTY);
+    private function readFileAsc($log, $start,$end) {
+        return array();
+    }
+
+    public function sortByTime($x,$y) {
+        if(!isset($x["Time"]) || !isset($y["Time"]) ||
+           !isset($x["Time"][0]) || !isset($y["Time"][0]))
+           return 0;
+        $tX = strtotime($x["Time"][0]);
+        $tY = strtotime($y["Time"][0]);
+        if($tX == $tY)
+            return 0;
+        else if($tX < $tY)
+            return 1;
+        else
+            return -1;
+    }
+
+    protected function parseLogEntries(array $str,$end = 100) {
 
-        if (isset($line[$end])) {
-            unset($line[$end]);
-        }
 
         $result = array();
-        foreach($line as $logEntry) {
+        foreach($str as  $logEntry) {
             $partResult = array();
-            preg_match_all('/^(?<TIME>.*?\]) *?\[(?<SEVERITY>.*?)\] *?(?<MESSAGE>.*)/',$logEntry,$partResult);
-            $result[] = array("Time"=>$partResult["TIME"],"Severity"=>$partResult["SEVERITY"],"Message"=>$partResult["MESSAGE"]);
+            $match = preg_match_all('/^\[(?<TIME>.*?)\] *?\[(?<SEVERITY>.*?)\] *?(?<MESSAGE>.*)/',$logEntry,$partResult);
+            if($match)
+                $result[] = array("Time"=>$partResult["TIME"],"Severity"=>$partResult["SEVERITY"],"Message"=>$partResult["MESSAGE"]);
         }
+        
         return $result;
     }
 
diff --git a/app/modules/AppKit/templates/Admin/ViewLogsSuccess.php b/app/modules/AppKit/templates/Admin/ViewLogsSuccess.php
index 997c2e1..6aa1feb 100755
--- a/app/modules/AppKit/templates/Admin/ViewLogsSuccess.php
+++ b/app/modules/AppKit/templates/Admin/ViewLogsSuccess.php
@@ -61,7 +61,7 @@ Cronk.util.initEnvironment('viewport-center', function() {
 		colModel: new Ext.grid.ColumnModel({
 			defaults: {
 				width: 120,
-				sortable: true
+				sortable: false
 			},
 			columns: [
 				{id: 'Time',header:_('Time'),width:100,sortable:true,dataIndex:'Time'},
diff --git a/app/modules/Cronks/lib/js/Cronks/Tackle/Renderer/AdditionalURLColumnRenderer.js b/app/modules/Cronks/lib/js/Cronks/Tackle/Renderer/AdditionalURLColumnRenderer.js
index 14262e5..8c93c04 100644
--- a/app/modules/Cronks/lib/js/Cronks/Tackle/Renderer/AdditionalURLColumnRenderer.js
+++ b/app/modules/Cronks/lib/js/Cronks/Tackle/Renderer/AdditionalURLColumnRenderer.js
@@ -60,6 +60,7 @@ Ext.ns("Icinga.Cronks.Tackle.Renderer").AdditionalURLColumnClickHandler = functi
             var curURLs = urls[urltype].data.split(" ");
             for(var i=0;i<curURLs.length;i++) {
                 var url = curURLs[i];
+                // Multiple urls are wrapped '', so we have to check those before we can use the url
                 if(/^'.*'$/.test(url)) {
                     menu.items.push({
                         text: urltype+(i+1),





More information about the icinga-checkins mailing list