/**
  * Validate use of aggregation table
  *
  * 	1. find matching aggregation level <= $this->groupBy or highest level
  * 	2. calculate if matching timestamps found
  *
  * @return boolean	Aggregation table usage validity
  */
 private function validateAggregationUsage()
 {
     if ($this->aggValid === null) {
         $this->aggValid = false;
         $aggregationLevel = $this->aggregator->getOptimalAggregationLevel($this->channel->getUuid(), $this->groupBy);
         if ($aggregationLevel) {
             // choose highest level
             $this->aggLevel = $aggregationLevel[0]['level'];
             // numeric value of desired aggregation level
             $this->aggType = Util\Aggregation::getAggregationLevelTypeValue($this->aggLevel);
             // valid boundaries?
             $this->aggValid = $this->getAggregationBoundary();
         }
     }
     return $this->aggValid;
 }
 /**
  * Validate use of aggregation table
  *
  * 	1. find matching aggregation level <= $this->groupBy or highest level
  * 	2. calculate if matching timestamps found
  *
  * @return boolean	Aggregation table usage validity
  */
 private function validateAggregationUsage()
 {
     if ($this->aggValid === null) {
         $this->aggValid = false;
         $aggregationLevels = $this->aggregator->getOptimalAggregationLevel($this->channel->getUuid(), $this->groupBy);
         if ($aggregationLevels) {
             // choose highest level
             $this->aggLevel = $aggregationLevels[0]['level'];
             // numeric value of desired aggregation level
             $this->aggType = Util\Aggregation::getAggregationLevelTypeValue($this->aggLevel);
             // valid boundaries?
             $this->aggValid = $this->getAggregationBoundary();
         }
     }
     if (self::$debug) {
         $type = $this->aggValid ? 'true (' . $this->aggLevel . ')' : 'false';
         echo "validateAggregationUsage " . $type . "\n";
     }
     return $this->aggValid;
 }
 /**
  * @depends testGetBaseline
  * @group aggregation
  */
 function testAggregateOptimizer()
 {
     $agg = new Util\Aggregation(self::$conn);
     // at this point we have aggregates for 'hour' and 'day'
     $agg->aggregate(self::$uuid, 'hour', 'delta');
     $typeHour = Util\Aggregation::getAggregationLevelTypeValue('hour');
     $typeDay = Util\Aggregation::getAggregationLevelTypeValue('day');
     // day: 2 rows of aggregation data, day first
     $opt = $agg->getOptimalAggregationLevel(self::$uuid);
     $ref = array(array('level' => 'day', 'type' => $typeDay, 'count' => $this->countAggregationRows(self::$uuid, $typeDay)), array('level' => 'hour', 'type' => $typeHour, 'count' => $this->countAggregationRows(self::$uuid, $typeHour)));
     $this->assertEquals($ref, $opt);
     // hour: 1 row of aggregation data
     $opt = $agg->getOptimalAggregationLevel(self::$uuid, 'hour');
     $ref = array(array('level' => 'hour', 'type' => $typeHour, 'count' => $this->countAggregationRows(self::$uuid, $typeHour)));
     $this->assertEquals($ref, $opt);
     // minute: no aggregation data => false
     $typeMinute = Util\Aggregation::getAggregationLevelTypeValue('minute');
     $opt = $agg->getOptimalAggregationLevel(self::$uuid, 'minute');
     $this->assertFalse($opt);
     // 3 data, cannot use daily aggregates for hourly request
     $this->getTuplesRaw(strtotime('2 days ago 0:00') * 1000, strtotime('1 days ago 0:00') * 1000, 'hour');
     $this->assertEquals(3, $this->json->data->rows, 'Possibly wrong aggregation level chosen by optimizer');
 }