improved DataIterators and Interpreters

This commit is contained in:
Steffen Vogel 2010-07-31 15:41:03 +02:00
parent 63ff53ec73
commit 00abfde228
5 changed files with 27 additions and 15 deletions

View file

@ -21,7 +21,7 @@
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler;
namespace Volkszaehler\Interpreter;
use Doctrine\DBAL;
@ -75,8 +75,8 @@ class DataAggregationIterator extends DataIterator {
$this->aggregatedKey++;
$this->current = $current;
$this->current[0] = $tuple[0];
$this->current[2] = $this->packageSize;
$this->current[0] = $tuple[0]; // the last timestamp of a package
$this->current[2] = $this->packageSize; // how many pulses do we have aggregated? how accurate is our result?
}
/**

View file

@ -21,7 +21,7 @@
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler;
namespace Volkszaehler\Interpreter;
use Doctrine\DBAL;

View file

@ -42,7 +42,7 @@ class GroupInterpreter {
/**
* Constructor
*
* @param Model\Group $group should only contain channels with the same indicator
* @param Model\Group $group should only contain channels of the same indicator
* @param ORM\EntityManager $em
* @param integer $from timestamp in ms since 1970
* @param integer $to timestamp in ms since 1970

View file

@ -98,16 +98,19 @@ abstract class Interpreter implements InterpreterInterface {
$sqlFields = ' timestamp, value';
}
// get total row count for grouping
$rowCount = $conn->fetchColumn($sqlRowCount, $params, 0);
// query for data
$stmt = $conn->executeQuery('SELECT ' . $sqlFields . $sqlFrom . $sqlWhere . $sqlGroupBy . $sqlOrderBy, $params);
// return iterators
if ($sqlGroupBy || is_null($groupBy)) { // aggregation by sql or skip it
return new Volkszaehler\DataIterator($stmt, $rowCount);
return new DataIterator($stmt, $rowCount);
}
elseif (is_numeric($groupBy) ) { // aggregation by php
$tuples = (int) $groupBy;
return new Volkszaehler\DataAggregationIterator($stmt, $rowCount, $tuples);
return new DataAggregationIterator($stmt, $rowCount, $tuples);
}
else {
throw new \Exception('invalid groupBy parameter');

View file

@ -105,19 +105,12 @@ class MeterInterpreter extends Interpreter {
*/
public function getValues($groupBy = NULL) {
$pulses = parent::getData($groupBy);
$count = $pulses->count();
$values = array();
foreach ($pulses as $pulse) {
if (isset($last)) {
$delta = $pulse[0] - $last[0];
$values[] = $this->raw2differential($last, $pulse);
$last = $pulse;
$values[] = array(
(int) ($pulse[0] - $delta / 2), // timestamp
$pulse[1] * (3600000 / (($this->channel->getResolution() / 1000) * $delta)), // value
(isset($pulse[2])) ? $pulse[2] : 1
);
}
else {
$last = $pulse;
@ -126,6 +119,22 @@ class MeterInterpreter extends Interpreter {
return $values;
}
/**
* Calculates the differential quotient of two consecutive pulses
*
* @param array $last the last pulse
* @param array $next the next pulse
*/
protected function raw2differential(array $last, array $next) {
$delta = $next[0] - $last[0];
return array(
(int) ($next[0] - $delta / 2), // timestamp
$next[1] * (3600000 / (($this->channel->getResolution() / 1000) * $delta)), // value
(isset($next[2])) ? $next[2] : 1
);
}
}
?>