From 71bfe883d62c1fb50eb3765d3ac24c7484b57599 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 16 Mar 2011 14:45:22 +0100 Subject: [PATCH] changed default behavior of DataController --- lib/Interpreter/DataIterator.php | 21 ++++++++++-------- lib/Interpreter/Interpreter.php | 32 ++++++++++++++++------------ lib/Interpreter/MeterInterpreter.php | 19 +++++++---------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/lib/Interpreter/DataIterator.php b/lib/Interpreter/DataIterator.php index 7bce138..8cf39f3 100644 --- a/lib/Interpreter/DataIterator.php +++ b/lib/Interpreter/DataIterator.php @@ -45,8 +45,8 @@ class DataIterator implements \Iterator, \Countable { * Constructor * * @param \PDOStatement $stmt - * @param integer $size - * @param integer $tuples + * @param integer $rowCount total num of rows in $stmt + * @param integer $tupleCount set to NULL to get all rows */ public function __construct(\PDOStatement $stmt, $rowCount, $tupleCount) { $this->rowCount = $rowCount; @@ -55,14 +55,17 @@ class DataIterator implements \Iterator, \Countable { $this->stmt = $stmt; $this->stmt->setFetchMode(\PDO::FETCH_NUM); - if ($this->rowCount > $this->tupleCount) { // sumarize - $this->packageSize = floor($this->rowCount / $this->tupleCount); - $this->tupleCount = floor($this->rowCount / $this->packageSize) + 1; - } - else { // passthrough + if (empty($this->tupleCount) || $this->rowCount < $this->tupleCount) { // get all rows $this->packageSize = 1; $this->tupleCount = $this->rowCount; - + } + else { // sumarize + $this->packageSize = floor($this->rowCount / $this->tupleCount); + $this->tupleCount = floor($this->rowCount / $this->packageSize); + + if (fmod($this->rowCount, $this->packageSize) > 0) { + $this->tupleCount++; + } } } @@ -102,7 +105,7 @@ class DataIterator implements \Iterator, \Countable { } public function valid() { - return $this->key < $this->tupleCount; + return $this->key <= $this->tupleCount; } /** diff --git a/lib/Interpreter/Interpreter.php b/lib/Interpreter/Interpreter.php index 869baed..3c33407 100644 --- a/lib/Interpreter/Interpreter.php +++ b/lib/Interpreter/Interpreter.php @@ -24,9 +24,7 @@ namespace Volkszaehler\Interpreter; use Volkszaehler\Util; -use Volkszaehler\Interpreter\Iterator; use Volkszaehler\Model; -use Doctrine\ORM\Query; use Doctrine\ORM; /** @@ -45,8 +43,8 @@ abstract class Interpreter { protected $to; protected $groupBy; - protected $rowCount = NULL; - protected $tupleCount = NULL; + protected $rowCount; + protected $tupleCount; /** * Constructor @@ -58,14 +56,11 @@ abstract class Interpreter { */ public function __construct(Model\Channel $channel, ORM\EntityManager $em, $from, $to, $tupleCount, $groupBy) { $this->channel = $channel; - $this->tupleCount = $tupleCount; $this->groupBy = $groupBy; - - // get dbal connection from EntityManager - $this->conn = $em->getConnection(); - + $this->tupleCount = $tupleCount; $this->from = $from; $this->to = $to; + $this->conn = $em->getConnection(); // get dbal connection from EntityManager // parse interval if (isset($from)) { @@ -106,11 +101,16 @@ abstract class Interpreter { // get total row count for grouping $this->rowCount = $this->conn->fetchColumn($sql['rowCount'], array($this->channel->getId()), 0); + + if ($this->rowCount > 0) { + // query for data + $stmt = $this->conn->executeQuery('SELECT ' . $sql['fields'] . $sql['from'] . $sql['where'] . $sql['groupBy'] . $sql['orderBy'], array($this->channel->getId())); - // query for data - $stmt = $this->conn->executeQuery('SELECT ' . $sql['fields'] . $sql['from'] . $sql['where'] . $sql['groupBy'] . $sql['orderBy'], array($this->channel->getId())); - - return new DataIterator($stmt, $this->rowCount, $this->tupleCount); + return new DataIterator($stmt, $this->rowCount, $this->tupleCount); + } + else { // no data available + return new \EmptyIterator(); + } } /** @@ -189,7 +189,7 @@ abstract class Interpreter { * @return float */ protected static function parseDateTimeString($string, $now) { - if (ctype_digit($string)) { + if (ctype_digit($string)) { // handling as ms timestamp return (float) $string; } elseif ($ts = strtotime($string, $now / 1000)) { @@ -205,6 +205,10 @@ abstract class Interpreter { */ public function getEntity() { return $this->channel; } + public function getRowCount() { return $this->rowCount; } + public function getTupleCount() { return $this->tupleCount; } + public function getFrom() { return $this->from; } + public function getTo() { return $this->to; } } ?> diff --git a/lib/Interpreter/MeterInterpreter.php b/lib/Interpreter/MeterInterpreter.php index c5379b1..8fc8ce8 100644 --- a/lib/Interpreter/MeterInterpreter.php +++ b/lib/Interpreter/MeterInterpreter.php @@ -42,56 +42,53 @@ class MeterInterpreter extends Interpreter { /** * Calculates the consumption + * * @return float total consumption in Wh */ public function getConsumption() { - if (is_null($this->tupleCount)) throw new \Exception('Data has to be processed first!'); - return 1000 * $this->pulseCount / $this->resolution; } /** * Get minimum + * * @return array (0 => timestamp, 1 => value) */ public function getMin() { - if (is_null($this->tupleCount)) throw new \Exception('Data has to be processed first!'); - return ($this->min) ? array_map('floatval', array_slice($this->min, 0 , 2)) : NULL; } /** * Get maximum + * * @return array (0 => timestamp, 1 => value) */ public function getMax() { - if (is_null($this->tupleCount)) throw new \Exception('Data has to be processed first!'); - return ($this->max) ? array_map('floatval', array_slice($this->max, 0 , 2)) : NULL; } /** * Get Average - * @return float + * + * @return float 3600: 3600 s/h; 1000: ms -> s */ public function getAverage() { - // 3600: 3600 s/h; 1000: ms -> s return (3600 * 1000 * $this->getConsumption()) / ($this->to - $this->from); } /** * Raw pulses to power conversion * - * @todo untested + * @param $callback a callback called each iteration for output * @return array with timestamp, values, and pulse count */ public function processData($callback) { + $tuples = array(); $pulses = parent::getData(); $this->resolution = $this->channel->getProperty('resolution'); $this->pulseCount = 0; - - $tuples = array(); + $last = $pulses->rewind(); $next = $pulses->next();