Skip to content

Commit

Permalink
Merge pull request #335 from andig/fix-mysql-optimizer
Browse files Browse the repository at this point in the history
Fix aggregation table left boundary calculation off by 1 unit
  • Loading branch information
andig committed Jul 12, 2015
2 parents fb32439 + 7ba1dbf commit e38d45c
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/Volkszaehler/Interpreter/SQL/MySQLAggregateOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
*/
class MySQLAggregateOptimizer extends MySQLOptimizer {

static $debug = false; // development diagnosis

protected $aggregator;

protected $aggFrom;
Expand Down Expand Up @@ -91,6 +93,11 @@ private function validateAggregationUsage() {
}
}

if (self::$debug) {
$type = $this->aggValid ? 'true (' . $this->aggLevel . ')' : 'false';
echo("validateAggregationUsage " . $type . "\n");
}

return $this->aggValid;
}

Expand Down Expand Up @@ -239,12 +246,14 @@ private function getAggregationBoundary($aggFromDelta = null) {
'FROM_UNIXTIME(MIN(timestamp) / 1000, ' . $dateFormat . '), ' .
'INTERVAL ' . $aggFromDelta . ' ' . $this->aggLevel .
')) * 1000 ' .
'FROM aggregate WHERE channel_id=? AND type=? AND timestamp>=?';
'FROM aggregate WHERE channel_id=? AND type=? AND ' .
' UNIX_TIMESTAMP(FROM_UNIXTIME(timestamp / 1000, ' . $dateFormat . ')) * 1000 >=?';
}
else {
// find 'left' border of aggregate table after $from
$sql = 'SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(MIN(timestamp) / 1000, ' . $dateFormat . ')) * 1000 ' .
'FROM aggregate WHERE channel_id=? AND type=? AND timestamp>=?';
'FROM aggregate WHERE channel_id=? AND type=? AND ' .
' UNIX_TIMESTAMP(FROM_UNIXTIME(timestamp / 1000, ' . $dateFormat . ')) * 1000 >=?';
}
$this->aggFrom = $this->conn->fetchColumn($sql, $sqlParameters, 0);
$this->aggTo = null;
Expand All @@ -266,14 +275,24 @@ private function getAggregationBoundary($aggFromDelta = null) {
$this->aggTo = $this->conn->fetchColumn($sql, $sqlParameters, 0);
}

// printf("from .. aggFrom .. aggTo .. to\n", pd($this->from), pd($this->aggFrom), pd($this->aggFrom), pd($this->to));
// printf("%s |%s .. %s| %s\n", pd($this->from), pd($this->aggFrom), pd($this->aggFrom), pd($this->to));
if (self::$debug) {
printf("from .. aggFrom .. aggTo .. to\n");
printf("%s |%s .. %s| %s\n", self::pd($this->from), self::pd($this->aggFrom), self::pd($this->aggFrom), self::pd($this->to));
}

return isset($this->aggFrom) && isset($this->aggTo) &&
$this->aggFrom < $this->aggTo &&
$this->from <= $this->aggFrom && $this->aggTo <= $this->to;
}

/**
* Print formatted date
*/
private static function pd($ts) {
$date = \DateTime::createFromFormat('U', (int)($ts/1000))->setTimeZone(new \DateTimeZone('Europe/Berlin'));
return $date->format('d.m.Y H:i:s');
}

/**
* Build sql query part to filter specified time interval
*
Expand Down

0 comments on commit e38d45c

Please sign in to comment.