implemented & tested statistic calculations for sensor interpreter (min, max & average)

This commit is contained in:
Steffen Vogel 2010-10-20 12:51:38 +02:00
parent ac8f48b23f
commit 48121fc9ae
2 changed files with 18 additions and 18 deletions

View file

@ -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) {

View file

@ -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 {
}
}
?>
?>