diff --git a/backend/lib/Interpreter/Interpreter.php b/backend/lib/Interpreter/Interpreter.php index a6c3671..70afa2b 100644 --- a/backend/lib/Interpreter/Interpreter.php +++ b/backend/lib/Interpreter/Interpreter.php @@ -39,7 +39,9 @@ use Doctrine\ORM\Query; */ abstract class Interpreter implements InterpreterInterface { protected $channel; - protected $em; + + /** @var Database connection */ + protected $conn; protected $from; protected $to; @@ -54,7 +56,9 @@ abstract class Interpreter implements InterpreterInterface { */ public function __construct(Model\Channel $channel, ORM\EntityManager $em, $from, $to) { $this->channel = $channel; - $this->em = $em; + + // get dbal connection from EntityManager + $this->conn = $em->getConnection(); $this->from = (isset($from)) ? self::parseDateTimeString($from, time() * 1000) : NULL; $this->to = (isset($to)) ? self::parseDateTimeString($to, (isset($this->from)) ? $this->from : time() * 1000) : NULL; @@ -71,9 +75,6 @@ abstract class Interpreter implements InterpreterInterface { * @return Volkszaehler\DataIterator */ protected function getData($tuples = NULL, $groupBy = NULL) { - // get dbal connection from EntityManager - $conn = $this->em->getConnection(); - // prepare sql $parameters = array(':id' => $this->channel->getId()); @@ -93,10 +94,10 @@ abstract class Interpreter implements InterpreterInterface { } // get total row count for grouping - $rowCount = $conn->fetchColumn($sql['rowCount'], $parameters, 0); + $rowCount = $this->conn->fetchColumn($sql['rowCount'], $parameters, 0); // query for data - $stmt = $conn->executeQuery('SELECT ' . $sql['fields'] . $sql['from'] . $sql['where'] . $sql['groupBy'] . $sql['orderBy'], $parameters); + $stmt = $this->conn->executeQuery('SELECT ' . $sql['fields'] . $sql['from'] . $sql['where'] . $sql['groupBy'] . $sql['orderBy'], $parameters); // return iterators if ($sql['groupBy'] || is_null($tuples) || $rowCount < $tuples) { diff --git a/backend/lib/Interpreter/SensorInterpreter.php b/backend/lib/Interpreter/SensorInterpreter.php index e9c3f4d..cf32d1c 100644 --- a/backend/lib/Interpreter/SensorInterpreter.php +++ b/backend/lib/Interpreter/SensorInterpreter.php @@ -53,34 +53,33 @@ class SensorInterpreter extends Interpreter { } /** - * @todo adapt to doctrine orm - * @todo untested + * Fetch the smallest value from database + * @internal doesn't fits the SQL standard * @return array (0 => timestamp, 1 => value) */ public function getMin() { - //return $this->dbh->query('SELECT value, timestamp FROM data WHERE channel_id = ' . (int) $this->id . self::buildFilterTime($this->from, $this->to) . ' ORDER BY value ASC', 1)->current(); + return array_map('floatval', $this->conn->fetchAssoc('SELECT value, timestamp FROM data WHERE channel_id = ' . (int) $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to) . ' ORDER BY value ASC LIMIT 1')); } /** - * @todo adapt to doctrine orm - * @todo untested + * Fetch the greatest value from the database + * @internal doesn't fits the SQL standard * @return array (0 => timestamp, 1 => value) */ public function getMax() { - //return $this->dbh->query('SELECT value, timestamp FROM data WHERE channel_id = ' . (int) $this->id . self::buildFilterTime($this->from, $this->to) . ' ORDER BY value DESC', 1)->current(); + return array_map('floatval', $this->conn->fetchAssoc('SELECT value, timestamp FROM data WHERE channel_id = ' . (int) $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to) . ' ORDER BY value DESC LIMIT 1')); } /** - * @todo adapt to doctrine orm - * @todo untested + * Fetch the average value from the database + * @internal doesn't fits the SQL standard * @return float */ public function getAverage() { - //return $this->dbh->query('SELECT AVG(value) AS value FROM data WHERE channel_id = ' . (int) $this->id . self::buildFilterTime($this->from, $this->to))->current(); + return (float) $this->conn->fetchColumn('SELECT AVG(value) FROM data WHERE channel_id = ' . (int) $this->channel->getId() . parent::buildDateTimeFilterSQL($this->from, $this->to)); } /** - * @todo to be implemented * @todo possible and/or required? * @return float */ @@ -89,4 +88,4 @@ class SensorInterpreter extends Interpreter { } } -?> \ No newline at end of file +?>