From eecae181c25d076db0f7d21364b6bc2b041ee119 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Thu, 5 Mar 2009 12:39:09 +0000
Subject: [PATCH] Fix for zero values and added plugin api for adding
 presentation plugins of colummn fields

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1374 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/statistics/lib/Aggregator.php         | 11 +++--
 .../lib/Statistics/FieldPresentation/Base.php | 20 +++++++++
 .../Statistics/FieldPresentation/Entity.php   | 20 +++++++++
 .../statistics/templates/statistics-tpl.php   | 11 ++++-
 modules/statistics/www/showstats.php          | 45 +++++++++++++++++--
 5 files changed, 99 insertions(+), 8 deletions(-)
 create mode 100644 modules/statistics/lib/Statistics/FieldPresentation/Base.php
 create mode 100644 modules/statistics/lib/Statistics/FieldPresentation/Entity.php

diff --git a/modules/statistics/lib/Aggregator.php b/modules/statistics/lib/Aggregator.php
index 9be2b4e2d..576e0227b 100644
--- a/modules/statistics/lib/Aggregator.php
+++ b/modules/statistics/lib/Aggregator.php
@@ -109,6 +109,10 @@ class sspmod_statistics_Aggregator {
 		// Iterate the first level of results, which is per rule, as defined in the config.
 		foreach ($results AS $rulename => $ruleresults) {
 		
+			
+			$filenos = array_keys($ruleresults);
+			$lastfile = $filenos[count($filenos)-1];
+			
 			// Iterate the second level of results, which is the fileslot.
 			foreach ($ruleresults AS $fileno => $fileres) {
 			
@@ -129,10 +133,11 @@ class sspmod_statistics_Aggregator {
 						$filledresult[$slot] = $fileres[$slot];
 					} else {
 						#echo('SLot [' . $slot . '] of [' . $maxslot . ']' . "\n");
-						if ($slot > $maxslot) {
-							$filledresult[$slot] = array('_' => -1);
-						} else {
+						if ($lastfile == $fileno && $slot > $maxslot) {
+						#if ($slot > $maxslot) {
 							$filledresult[$slot] = array('_' => NULL);
+						} else {
+							$filledresult[$slot] = array('_' => 0);
 						}				
 					}
 					#print_r($filledresult[$slot]);
diff --git a/modules/statistics/lib/Statistics/FieldPresentation/Base.php b/modules/statistics/lib/Statistics/FieldPresentation/Base.php
new file mode 100644
index 000000000..18eb0bdec
--- /dev/null
+++ b/modules/statistics/lib/Statistics/FieldPresentation/Base.php
@@ -0,0 +1,20 @@
+<?php
+
+class sspmod_statistics_Statistics_FieldPresentation_Base {
+	
+	protected $fields;
+	protected $template;
+	protected $config;
+	
+	function __construct($fields, $config, $template) {
+		$this->fields = $fields;
+		$this->template = $template;
+		$this->config = $config;
+	}
+	
+	public function getPresentation() {
+		return array('_' => 'Total');
+	}
+	
+	
+}
\ No newline at end of file
diff --git a/modules/statistics/lib/Statistics/FieldPresentation/Entity.php b/modules/statistics/lib/Statistics/FieldPresentation/Entity.php
new file mode 100644
index 000000000..27b13b989
--- /dev/null
+++ b/modules/statistics/lib/Statistics/FieldPresentation/Entity.php
@@ -0,0 +1,20 @@
+<?php
+
+class sspmod_statistics_Statistics_FieldPresentation_Entity extends sspmod_statistics_Statistics_FieldPresentation_Base {
+	
+	
+	public function getPresentation() {
+		$mh = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
+		$metadata = $mh->getList($this->config);
+		
+		$translation = array('_' => 'All services');
+		foreach($this->fields AS $field) {
+			if (array_key_exists($field, $metadata))
+				$translation[$field] = $this->template->t($metadata[$field]['name'], array(), FALSE);
+		}
+		return $translation;
+	}
+
+	
+	
+}
\ No newline at end of file
diff --git a/modules/statistics/templates/statistics-tpl.php b/modules/statistics/templates/statistics-tpl.php
index caf06c1ed..5e498e52a 100644
--- a/modules/statistics/templates/statistics-tpl.php
+++ b/modules/statistics/templates/statistics-tpl.php
@@ -73,17 +73,24 @@ echo '</td>';
 
 // Select delimiter
 echo '<td style="text-align: right">';
+
+#echo('<pre>here'); print_r($this->data['delimiterPresentation']); echo('</pre>');
+
 echo '<form style="display: inline">';
 echo '<input type="hidden" name="rule" value="' . $this->data['selected.rule'] . '" />';
 echo '<input type="hidden" name="time" value="' . $this->data['selected.time'] . '" />';
 echo '<select onChange="submit();" name="d">';
 foreach ($this->data['availdelimiters'] AS $key => $delim) {
+
+	$delimName = $delim;
+	if(array_key_exists($delim, $this->data['delimiterPresentation'])) $delimName = $this->data['delimiterPresentation'][$delim];
+
 	if ($key == '_') {
 		echo '<option value="_">Total</option>';
 	} elseif ($delim == $_REQUEST['d']) {
-		echo '<option selected="selected" value="' . $delim . '">' . $delim . '</option>';
+		echo '<option selected="selected" value="' . htmlentities($delim) . '">' . htmlentities($delimName) . '</option>';
 	} else {
-		echo '<option  value="' . $delim . '">' . $delim . '</option>';
+		echo '<option  value="' . htmlentities($delim) . '">' . htmlentities($delimName) . '</option>';
 	}
 }
 echo '</select></form>';
diff --git a/modules/statistics/www/showstats.php b/modules/statistics/www/showstats.php
index 43ee123ae..b1f05db4f 100644
--- a/modules/statistics/www/showstats.php
+++ b/modules/statistics/www/showstats.php
@@ -92,6 +92,13 @@ if(array_key_exists('rule', $_GET)) {
 	}
 }
 
+
+
+
+
+
+
+
 /*
  * Get list of avaiable times in current file (rule)
  */
@@ -226,8 +233,11 @@ function getPercentValues($results, $delimiter) {
 	$dataset = array();
 	foreach($results AS $slot => $res) {
 		#echo ('<p>new value: ' . number_format(100*$res[$delimiter] / $max, 2));
+// 		echo('<hr><p>delimiter [<tt>' .$delimiter . '</tt>].');
+// 		echo('<p>Res <pre>'); print_r($res); echo( '</pre>');
+// 		echo('<p>return <pre>'); print_r(isset($res[$delimiter]) ? $res[$delimiter] : 'NO'); echo('</pre>');
 		if (array_key_exists($delimiter, $res)) {
-			if ($res[$delimiter] === -1) {
+			if ($res[$delimiter] === NULL) {
 				$dataset[] = -1;
 			} else {
 				$dataset[] = number_format(100*$res[$delimiter] / $max, 2);
@@ -253,13 +263,23 @@ function getPercentValues($results, $delimiter) {
 
 
 
-$datasets[] = getPercentValues($results, '_');
+$datasets[] = getPercentValues($results, $delimiter);
 if ($delimiter !== '_') {
 	if (array_key_exists('graph.total', $statrules[$rule]) && $statrules[$rule]['graph.total'] === TRUE) {
-		$datasets[] = getPercentValues($results, $delimiter);
+		$datasets[] = getPercentValues($results, '_');
 	}
 }
 
+
+
+
+
+
+
+
+
+
+
 #echo('<pre>'); print_r($datasets); exit;
 
 #echo 'set axis on lastslot [' . $lastslot . ']';
@@ -280,6 +300,24 @@ SimpleSAML_Module::callHooks('htmlinject', $hookinfo);
 
 
 $t = new SimpleSAML_XHTML_Template($config, 'statistics:statistics-tpl.php');
+
+
+/*
+ * Create a delimiter presentation filter for this rule...
+ */
+$delimiterPresentation = NULL;
+if (array_key_exists('fieldPresentation', $statrules[$rule])) {
+	$classname = SimpleSAML_Module::resolveClass( $statrules[$rule]['fieldPresentation']['class'], 'Statistics_FieldPresentation');
+	if (!class_exists($classname))
+		throw new Exception('Could not find field presentation plugin [' . $classname . ']: No class found');
+	
+	$presentationHandler = new $classname(array_keys($availdelimiters), $statrules[$rule]['fieldPresentation']['config'], $t);
+	$delimiterPresentation = $presentationHandler->getPresentation();
+}
+
+
+
+
 $t->data['header'] = 'stat';
 $t->data['imgurl'] = $grapher->show($axis, $axispos, $datasets, $max);
 $t->data['available.rules'] = $available_rules;
@@ -295,5 +333,6 @@ $t->data['selected.time'] = $fileslot;
 $t->data['debugdata'] = $debugdata;
 $t->data['summaryDataset'] = $summaryDataset;
 $t->data['availdelimiters'] = array_keys($availdelimiters);
+$t->data['delimiterPresentation'] = $delimiterPresentation;
 $t->show();
 
-- 
GitLab