From 627813ac350fc18791b2155fe839817d15abc99e Mon Sep 17 00:00:00 2001 From: Justin Otherguy Date: Fri, 7 Jan 2011 15:18:31 +0100 Subject: [PATCH] added display for the current value to the frontend (side-by-side to max., min. and avg.) --- htdocs/frontend/index.html | 1 + htdocs/frontend/javascripts/frontend.js | 2 ++ lib/Interpreter/AggregatorInterpreter.php | 14 ++++++++++++++ lib/Interpreter/InterpreterInterface.php | 1 + lib/Interpreter/MeterInterpreter.php | 8 ++++++++ lib/Interpreter/SensorInterpreter.php | 9 +++++++++ lib/View/JSON.php | 1 + lib/View/XML.php | 1 + 8 files changed, 37 insertions(+) diff --git a/htdocs/frontend/index.html b/htdocs/frontend/index.html index cdd85e0..22d1a17 100644 --- a/htdocs/frontend/index.html +++ b/htdocs/frontend/index.html @@ -73,6 +73,7 @@ Min. Max. Avg. + Curr. diff --git a/htdocs/frontend/javascripts/frontend.js b/htdocs/frontend/javascripts/frontend.js index 21d9e60..8a98818 100644 --- a/htdocs/frontend/javascripts/frontend.js +++ b/htdocs/frontend/javascripts/frontend.js @@ -294,6 +294,7 @@ vz.entities.show = function() { .append($('').addClass('min')) // min .append($('').addClass('max')) // max .append($('').addClass('average')) // avg + .append($('').addClass('current')) // curr .append($('') // operations .addClass('ops') .append($('') @@ -364,6 +365,7 @@ vz.entities.loadData = function() { .text(entity.data.max.value) .attr('title', $.plot.formatDate(new Date(entity.data.max.timestamp), '%d. %b %h:%M:%S', vz.options.plot.xaxis.monthNames)); $('#entity-' + entity.uuid + ' .average').text(entity.data.average); + $('#entity-' + entity.uuid + ' .current').text(entity.data.current); } }, vz.drawPlot, 'data') ); diff --git a/lib/Interpreter/AggregatorInterpreter.php b/lib/Interpreter/AggregatorInterpreter.php index 19f1e20..23bcb4f 100644 --- a/lib/Interpreter/AggregatorInterpreter.php +++ b/lib/Interpreter/AggregatorInterpreter.php @@ -137,6 +137,20 @@ class AggregatorInterpreter implements InterpreterInterface { return ($sum / count($this->channelInterpreter)); } + /** + * Just a passthrough to the channel interpreters + * + * @return float current value + */ + public function getCurrent() { + $current = 0; + + foreach ($this->channelInterpreter as $interpreter) { + $current = $interpreter->getCurrent(); + } + return ($current($this->channelInterpreter)); + } + /* * Getter & setter */ diff --git a/lib/Interpreter/InterpreterInterface.php b/lib/Interpreter/InterpreterInterface.php index 7f89b81..f5942f2 100644 --- a/lib/Interpreter/InterpreterInterface.php +++ b/lib/Interpreter/InterpreterInterface.php @@ -34,5 +34,6 @@ interface InterpreterInterface { function getMin(); function getMax(); function getAverage(); + function getCurrent(); function getEntity(); } diff --git a/lib/Interpreter/MeterInterpreter.php b/lib/Interpreter/MeterInterpreter.php index 611dcfd..124f6a1 100644 --- a/lib/Interpreter/MeterInterpreter.php +++ b/lib/Interpreter/MeterInterpreter.php @@ -93,6 +93,14 @@ class MeterInterpreter extends Interpreter { //return $this->getConsumption() / ($this->to - $this->from) / 1000; // return W } + /** + * @return float + * @todo reimplement according to new env + */ + public function getCurrent() { + //return $this->getConsumption() / ($this->to - $this->from) / 1000; // return W + } + /** * Raw pulses to power conversion * diff --git a/lib/Interpreter/SensorInterpreter.php b/lib/Interpreter/SensorInterpreter.php index 00fb815..3869e2b 100644 --- a/lib/Interpreter/SensorInterpreter.php +++ b/lib/Interpreter/SensorInterpreter.php @@ -81,6 +81,15 @@ class SensorInterpreter extends Interpreter { return (float) $this->conn->fetchColumn('SELECT AVG(value) FROM data WHERE channel_id = ?' . parent::buildDateTimeFilterSQL($this->from, $this->to), array($this->channel->getId()), 0); } + /** + * Fetch the average value from the database + * @internal doesn't fits the SQL standard + * @return float + */ + public function getCurrent() { + return (float) $this->conn->fetchColumn('SELECT value FROM data WHERE channel_id = ?' . parent::buildDateTimeFilterSQL($this->from, $this->to). ' ORDER BY timestamp DESC', array($this->channel->getId()), 0); + } + /** * @todo possible and/or required? * @return float diff --git a/lib/View/JSON.php b/lib/View/JSON.php index 8f0ad1c..bac8dd5 100644 --- a/lib/View/JSON.php +++ b/lib/View/JSON.php @@ -224,6 +224,7 @@ class JSON extends View { 'min' => $interpreter->getMin(), 'max' => $interpreter->getMax(), 'average' => $interpreter->getAverage(), + 'current' => $interpreter->getCurrent(), 'tuples' => $data ); } diff --git a/lib/View/XML.php b/lib/View/XML.php index 7c02ef3..2551bb1 100644 --- a/lib/View/XML.php +++ b/lib/View/XML.php @@ -219,6 +219,7 @@ class XML extends View { $xmlData->appendChild($this->xmlDoc->createElement('min', $interpreter->getMin())); $xmlData->appendChild($this->xmlDoc->createElement('max', $interpreter->getMax())); $xmlData->appendChild($this->xmlDoc->createElement('average', $interpreter->getAverage())); + $xmlData->appendChild($this->xmlDoc->createElement('current', $interpreter->getCurrent())); $xmlData->appendChild($xmlTuples); $this->xmlRoot->appendChild($xmlData);