Example #1
0
 /**
  * @param Query $query
  * @return mixed
  */
 public function query(Query $query)
 {
     $time = microtime(TRUE);
     $packageName = $query->getPackage();
     $counterName = $query->getCounter();
     $from = $query->getFrom();
     $to = $query->getTo();
     $count = $query->getCount();
     $action = $query->getAction();
     $token = $query->getToken();
     $value = $query->getValue();
     $tokenCreated = FALSE;
     $result = NULL;
     $databaseFilename = $this->getDatabaseFileName($packageName);
     if (!file_exists($databaseFilename) && $action !== Query::ACTION_SAVE) {
         throw new NotFoundException(sprintf('Package %s has no counters. Save a value to initialize!', $packageName));
     }
     switch ($action) {
         case Query::ACTION_COMPARE:
             $history = (array) $this->getByCount($packageName, $counterName, 2048);
             $lastValue = $this->getLastValue($packageName, $counterName);
             $result = array(array('value' => $lastValue, 'time' => $this->getLastTimestamp($packageName, $counterName), 'deviation' => $this->getCalculator()->deviation($history, $lastValue)), array('value' => $value, 'time' => time(), 'deviation' => $this->getCalculator()->deviation($history, $value)));
             break;
         case Query::ACTION_POLL:
         case Query::ACTION_GET:
             $this->validatePackageToken($packageName, $token);
             if ($from && $count) {
                 $result = $this->getByRange($packageName, $counterName, $from, $to, $count);
             } elseif ($from) {
                 $result = $this->getByRange($packageName, $counterName, $from, $to);
             } elseif ($count) {
                 $result = $this->getByCount($packageName, $counterName, $count);
             } else {
                 $result = $this->getLastValue($packageName, $counterName);
             }
             break;
         case Query::ACTION_COUNTERS:
             $this->validatePackageToken($packageName, $token);
             $result = $this->getCountersFromPackage($packageName);
             break;
         case Query::ACTION_SAVE:
             if (!file_exists($databaseFilename)) {
                 $token = $this->createTokenForPackage($packageName, $token);
                 $tokenCreated = TRUE;
             } else {
                 $this->validatePackageToken($packageName, $token);
             }
             $result = $this->saveValue($packageName, $counterName, $value);
             break;
         default:
             throw new NumerologException(sprintf('Invalid Numerolog action: %s', $action));
     }
     $response = array('values' => $result);
     if (1 < count($result) && $action !== Query::ACTION_SAVE && $action !== Query::ACTION_COUNTERS) {
         $response['statistics'] = $this->getCalculator()->statistics($result);
     }
     if ($action === Query::ACTION_POLL) {
         // return only the indicated sub-set of statistics
         $poll = $query->getPoll();
         if (empty($poll)) {
             throw new NumerologException('Poll command used with empty poll argument');
         } elseif (isset($response[$poll])) {
             $response = $response[$poll];
         } elseif (isset($response['statistics'][$poll])) {
             $response = $response['statistics'][$poll];
         } else {
             throw new NumerologException(sprintf('Invalid polling data requested: %s', $poll));
         }
     } else {
         $response['querytime'] = round((microtime(TRUE) - $time) * 1000, 5);
     }
     if ($action === Query::ACTION_SAVE && $tokenCreated) {
         $response['token'] = $token;
     }
     return $response;
 }