From cdf3541a073206536fe2429eaddff248bca10434 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Fri, 8 Dec 2017 21:05:38 +0100
Subject: [PATCH] memcacheMonitor: Add Twig template

---
 .../templates/memcachestat.twig               |  38 ++++
 modules/memcacheMonitor/www/memcachestat.php  | 175 +++++++++---------
 modules/memcacheMonitor/www/style.css         |  20 ++
 3 files changed, 150 insertions(+), 83 deletions(-)
 create mode 100644 modules/memcacheMonitor/templates/memcachestat.twig
 create mode 100644 modules/memcacheMonitor/www/style.css

diff --git a/modules/memcacheMonitor/templates/memcachestat.twig b/modules/memcacheMonitor/templates/memcachestat.twig
new file mode 100644
index 000000000..f3d4cbd01
--- /dev/null
+++ b/modules/memcacheMonitor/templates/memcachestat.twig
@@ -0,0 +1,38 @@
+{% set pagetitle = 'SimpleSAMLphp Memcache Monitor'|trans %}
+{% extends "base.twig" %}
+
+{% block preload %}
+    <link href="{{ baseurlpath }}style.css" rel="stylesheet" />
+{% endblock %}
+
+{% block content %}
+    <h2>{{ title }}</h2>
+    <table class="statustable">
+        <tr>
+            <th>&nbsp;</th>
+            {% for key, title in colTitles %}
+            <th>{{ title|escape('html') }}</th>
+            {% endfor %}
+        </tr>
+        {% for rowTitle, rowData in table %}
+        <tr>
+            <th class="rowtitle" style="text-align: right">{{ rowTitles[rowTitle]|trans }}</th>
+            {% for key, colTitle in colTitles %}
+            {% if rowData[colTitle] is defined %}
+            <td>{{ rowData[colTitle]|escape('html') }}</td>
+            {% else %}
+            <td>&nbsp;</td>
+            {% endif %}
+            {% endfor %}
+        </tr>
+        {% endfor %}
+    </table>
+
+    {% if usage is defined %}
+    {% for key, value in usage %}
+    <h3>Storage usage on [{{ key }}]</h3>
+        <div class="bmax" style="width: {{ maxpix }}">
+            <div class="bused" style="width: {{ usage[key] }}">Used: {{ table.bytes[key] }}</div>Total available: {{ table.limit_maxbytes[key] }}</div>
+    {% endfor %}
+    {% endif %}
+{% endblock %}
diff --git a/modules/memcacheMonitor/www/memcachestat.php b/modules/memcacheMonitor/www/memcachestat.php
index 3d8fde439..e9651ceef 100644
--- a/modules/memcacheMonitor/www/memcachestat.php
+++ b/modules/memcacheMonitor/www/memcachestat.php
@@ -1,104 +1,91 @@
 <?php
 
 function tdate($input) {
-	return date(DATE_RFC822, $input); 
+    return date(DATE_RFC822, $input); 
 }
 
 function hours($input) {
-	if ($input < 60) return number_format($input, 2) . ' sec';
-	if ($input < 60*60) return number_format(($input/60),2) . ' min';
-	if ($input < 24*60*60) return number_format(($input/(60*60)),2) . ' hours';
-	return number_format($input/(24*60*60),2) . ' days';
-	
+    if ($input < 60) {
+        return number_format($input, 2) . ' sec';
+    } else if ($input < 60*60) {
+        return number_format(($input/60),2) . ' min';
+    } else if ($input < 24*60*60) {
+        return number_format(($input/(60*60)),2) . ' hours';
+    }
+    return number_format($input/(24*60*60),2) . ' days';
 }
 
-
 function humanreadable($input) {
-	 
-	$output = "";
-	$input = abs($input);
-	
-	if ($input >= (1024*1024*1024*1024*1024*1024*1024*100)) {
-		$output = sprintf("%5ldEi", $input / (1024*1024*1024*1024*1024*1024) );		
-	} else if ($input >= (1024*1024*1024*1024*1024*1024*10)) {
-		$output = sprintf("%5.1fEi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0*1024.0) );		
-	} else if ($input >= (1024*1024*1024*1024*1024*1024)) {
-		$output = sprintf("%5.2fEi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0*1024.0) );	
-
-
-	} else if ($input >= (1024*1024*1024*1024*1024*100)) {
-		$output = sprintf("%5ldPi", $input / (1024*1024*1024*1024*1024) );		
-	} else if ($input >= (1024*1024*1024*1024*1024*10)) {
-		$output = sprintf("%5.1fPi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0) );		
-	} else if ($input >= (1024*1024*1024*1024*1024)) {
-		$output = sprintf("%5.2fPi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0) );	
-		
-	} else if ($input >= (1024*1024*1024*1024*100)) {
-		$output = sprintf("%5ldTi", $input / (1024*1024*1024*1024) );
-	} else if ($input >= (1024*1024*1024*1024*10)) {
-		$output = sprintf("%5.1fTi", $input / (1024.0*1024.0*1024.0*1024.0) );	
-	} else if ($input >= (1024*1024*1024*1024)) {
-		$output = sprintf("%5.2fTi", $input / (1024.0*1024.0*1024.0*1024.0) );
-
-
-	} else if ($input >= (1024*1024*1024*100)) {
-		$output = sprintf("%5ldGi", $input / (1024*1024*1024) );		
-	} else if ($input >= (1024*1024*1024*10)) {
-		$output = sprintf("%5.1fGi", $input / (1024.0*1024.0*1024.0) );		
-	} else if ($input >= (1024*1024*1024)) {
-		$output = sprintf("%5.2fGi", $input / (1024.0*1024.0*1024.0) );	
-		
-	} else if ($input >= (1024*1024*100)) {
-		$output = sprintf("%5ldMi", $input / (1024*1024) );
-	} else if ($input >= (1024*1024*10)) {
-		$output = sprintf("%5.1fM", $input / (1024.0*1024.0) );	
-	} else if ($input >= (1024*1024)) {
-		$output = sprintf("%5.2fMi", $input / (1024.0*1024.0) );		
-		
-	} else if ($input >= (1024 * 100)) {
-		$output = sprintf("%5ldKi", $input / (1024) );
-	} else if ($input >= (1024 * 10)) {
-		$output = sprintf("%5.1fKi", $input / 1024.0 );
-	} else if ($input >= (1024)) {
-		$output = sprintf("%5.2fKi", $input / 1024.0 );
-		
-	} else {
-		$output = sprintf("%5ld", $input );
-	}
-
-	return $output;
+    $output = "";
+    $input = abs($input);
+
+    if ($input >= (1024*1024*1024*1024*1024*1024*1024*100)) {
+        $output = sprintf("%5ldEi", $input / (1024*1024*1024*1024*1024*1024) );		
+    } else if ($input >= (1024*1024*1024*1024*1024*1024*10)) {
+        $output = sprintf("%5.1fEi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0*1024.0) );		
+    } else if ($input >= (1024*1024*1024*1024*1024*1024)) {
+        $output = sprintf("%5.2fEi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0*1024.0) );	
+    } else if ($input >= (1024*1024*1024*1024*1024*100)) {
+        $output = sprintf("%5ldPi", $input / (1024*1024*1024*1024*1024) );		
+    } else if ($input >= (1024*1024*1024*1024*1024*10)) {
+        $output = sprintf("%5.1fPi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0) );		
+    } else if ($input >= (1024*1024*1024*1024*1024)) {
+        $output = sprintf("%5.2fPi", $input / (1024.0*1024.0*1024.0*1024.0*1024.0) );	
+    } else if ($input >= (1024*1024*1024*1024*100)) {
+        $output = sprintf("%5ldTi", $input / (1024*1024*1024*1024) );
+    } else if ($input >= (1024*1024*1024*1024*10)) {
+        $output = sprintf("%5.1fTi", $input / (1024.0*1024.0*1024.0*1024.0) );	
+    } else if ($input >= (1024*1024*1024*1024)) {
+        $output = sprintf("%5.2fTi", $input / (1024.0*1024.0*1024.0*1024.0) );
+    } else if ($input >= (1024*1024*1024*100)) {
+        $output = sprintf("%5ldGi", $input / (1024*1024*1024) );		
+    } else if ($input >= (1024*1024*1024*10)) {
+        $output = sprintf("%5.1fGi", $input / (1024.0*1024.0*1024.0) );		
+    } else if ($input >= (1024*1024*1024)) {
+        $output = sprintf("%5.2fGi", $input / (1024.0*1024.0*1024.0) );	
+    } else if ($input >= (1024*1024*100)) {
+        $output = sprintf("%5ldMi", $input / (1024*1024) );
+    } else if ($input >= (1024*1024*10)) {
+        $output = sprintf("%5.1fM", $input / (1024.0*1024.0) );	
+    } else if ($input >= (1024*1024)) {
+        $output = sprintf("%5.2fMi", $input / (1024.0*1024.0) );		
+    } else if ($input >= (1024 * 100)) {
+        $output = sprintf("%5ldKi", $input / (1024) );
+    } else if ($input >= (1024 * 10)) {
+        $output = sprintf("%5.1fKi", $input / 1024.0 );
+    } else if ($input >= (1024)) {
+        $output = sprintf("%5.2fKi", $input / 1024.0 );
+    } else {
+        $output = sprintf("%5ld", $input );
+    }
+
+    return $output;
 }
 
-
-
-
 $config = SimpleSAML_Configuration::getInstance();
 
 // Make sure that the user has admin access rights
 SimpleSAML\Utils\Auth::requireAdmin();
 
-
 $formats = array(
-	'bytes' => 'humanreadable',
-	'bytes_read' => 'humanreadable',
-	'bytes_written' => 'humanreadable',
-	'limit_maxbytes' => 'humanreadable',
-	'time' => 'tdate',
-	'uptime' => 'hours',
+    'bytes' => 'humanreadable',
+    'bytes_read' => 'humanreadable',
+    'bytes_written' => 'humanreadable',
+    'limit_maxbytes' => 'humanreadable',
+    'time' => 'tdate',
+    'uptime' => 'hours',
 );
 
-$statsraw = SimpleSAML_Memcache::getStats();
-
-$stats = $statsraw;
-
-foreach($stats AS $key => &$entry) {
-	if (array_key_exists($key, $formats)) {
-		$func = $formats[$key];
-		foreach($entry AS $k => $val) {
-			$entry[$k] = $func($val);
-		}
-	}
+$statsRaw = SimpleSAML_Memcache::getStats();
+$stats = $statsRaw;
 
+foreach ($stats as $key => &$entry) {
+    if (array_key_exists($key, $formats)) {
+        $func = $formats[$key];
+        foreach ($entry as $k => $val) {
+            $entry[$k] = $func($val);
+        }
+    }
 }
 
 $t = new SimpleSAML_XHTML_Template($config, 'memcacheMonitor:memcachestat.tpl.php');
@@ -140,8 +127,30 @@ $rowTitles = array(
     'uptime' => $t->noop('{memcacheMonitor:memcachestat:uptime}'),
     'version' => $t->noop('{memcacheMonitor:memcachestat:version}'),
 );
+
+// Identify column headings
+$colTitles = array();
+foreach ($stats as $rowTitle => $rowData) {
+    foreach ($rowData as $colTitle => $foo) {
+        if (!in_array($colTitle, $colTitles, true)) {
+            $colTitles[] = $colTitle;
+        }
+    }
+}
+
+if (array_key_exists('bytes', $statsRaw) && array_key_exists('limit_maxbytes', $statsRaw)) {
+    $usage = array();
+    $maxpix = 400;
+    foreach ($statsRaw['bytes'] as $key => $row_data) {
+        $pix = floor($statsRaw['bytes'][$key] * $maxpix / $statsRaw['limit_maxbytes'][$key]);
+        $usage[$key] = $pix . 'px';
+    }
+    $t->data['maxpix'] = $maxpix . 'px';
+    $t->data['usage'] = $usage;
+}
+
 $t->data['title'] = 'Memcache stats';
-$t->data['rowtitles'] = $rowTitles;
+$t->data['rowTitles'] = $rowTitles;
+$t->data['colTitles'] = $colTitles;
 $t->data['table'] = $stats;
-$t->data['statsraw'] = $statsraw;
 $t->show();
diff --git a/modules/memcacheMonitor/www/style.css b/modules/memcacheMonitor/www/style.css
new file mode 100644
index 000000000..b57c37654
--- /dev/null
+++ b/modules/memcacheMonitor/www/style.css
@@ -0,0 +1,20 @@
+table.statustable td, table.statustable th {
+    border: 1px solid #eee;
+    padding: 2px 6px;
+}
+
+table.statustable {
+    border-collapse: collapse;
+}
+
+.bmax {
+    border: 1px solid #555;
+    background: #eee;
+}
+
+.bused {
+    border-right: 1px solid #555;
+    border-bottom: 1px solid #555;
+    color: white;
+    background: #833;
+}
-- 
GitLab