Пример #1
0
 /**
  * Add exception to output queue
  *
  * @param \Exception $exception
  * @param boolean $debug
  */
 protected function addException(\Exception $exception)
 {
     echo get_class($exception) . '# [' . $exception->getCode() . ']' . ':' . $exception->getMessage() . PHP_EOL;
     if (Util\Debug::isActivated()) {
         echo "file:\t" . $exception->getFile() . PHP_EOL;
         echo "line:\t" . $exception->getLine() . PHP_EOL;
     }
 }
Пример #2
0
 /**
  * Render response and send it to the client
  */
 public function send()
 {
     if (Util\Debug::isActivated()) {
         $this->add(Util\Debug::getInstance());
     }
     $this->response->setContent($this->render());
     return $this->response;
 }
Пример #3
0
 /**
  * Add exception to output queue
  *
  * @param \Exception $exception
  * @param boolean $debug
  */
 protected function addException(\Exception $exception)
 {
     $exceptionType = explode('\\', get_class($exception));
     $exceptionInfo = array('message' => $exception->getMessage(), 'type' => end($exceptionType), 'code' => $exception->getCode());
     if (Util\Debug::isActivated()) {
         $debugInfo = array('file' => $exception->getFile(), 'line' => $exception->getLine(), 'backtrace' => $exception->getTrace());
         $this->json['exception'] = array_merge($exceptionInfo, $debugInfo);
     } else {
         $this->json['exception'] = $exceptionInfo;
     }
 }
Пример #4
0
 /**
  * Determine context, format and uuid of the raw request
  *
  * @param Request $request A Request instance
  * @param int     $type    The type of the request (for Symfony compatibility, not implemented)
  *
  * @return Response A Response instance
  */
 public function handleRaw(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
 {
     // workaround for https://github.com/symfony/symfony/issues/13617
     $pathInfo = $request->server->has('PATH_INFO') ? $request->server->get('PATH_INFO') : '';
     if (0 === strlen($pathInfo)) {
         $pathInfo = $request->getPathInfo();
     }
     $format = pathinfo($pathInfo, PATHINFO_EXTENSION);
     if (!array_key_exists($format, self::$viewMapping)) {
         if (empty($pathInfo)) {
             throw new \Exception('Missing or invalid PATH_INFO');
         } elseif (empty($this->format)) {
             throw new \Exception('Missing format');
         } else {
             throw new \Exception('Unknown format: \'' . $this->format . '\'');
         }
     }
     // initialize debugging
     if (($debugLevel = $request->query->get('debug')) || ($debugLevel = Util\Configuration::read('debug'))) {
         if ($debugLevel > 0 && !Util\Debug::isActivated()) {
             new Util\Debug($debugLevel, $this->em);
         }
     } else {
         // make sure static debug instance is removed
         Util\Debug::deactivate();
     }
     $class = self::$viewMapping[$format];
     $this->view = new $class($request, $format);
     $path = explode('/', substr($pathInfo, 1, strrpos($pathInfo, '.') - 1));
     list($context, $uuid) = array_merge($path, array(null));
     // verify route
     if (!array_key_exists($context, self::$controllerMapping)) {
         throw new \Exception(empty($context) ? 'Missing context' : 'Unknown context: \'' . $context . '\'');
     }
     return $this->handler($request, $context, $uuid);
 }
Пример #5
0
 /**
  * Add exception to output queue
  *
  * @param \Exception $exception
  * @param boolean $debug
  */
 protected function addException(\Exception $exception)
 {
     $exceptionType = explode('\\', get_class($exception));
     $xmlException = $this->xmlDoc->createElement('exception');
     $xmlException->setAttribute('code', $exception->getCode());
     $xmlException->setAttribute('type', end($exceptionType));
     $xmlException->appendChild($this->xmlDoc->createElement('message', $exception->getMessage()));
     if (Util\Debug::isActivated()) {
         $xmlException->appendChild($this->xmlDoc->createElement('file', $exception->getFile()));
         $xmlException->appendChild($this->xmlDoc->createElement('line', $exception->getLine()));
         $xmlException->appendChild($this->convertTrace($exception->getTrace()));
     }
     $this->xmlRoot->appendChild($xmlException);
 }
Пример #6
0
 /**
  * Render response and send it to the client
  */
 public function send()
 {
     if (Util\Debug::isActivated()) {
         $this->add(Util\Debug::getInstance());
     }
     $this->render();
     $this->response->send();
 }
Пример #7
0
 /**
  * Core data aggregation
  *
  * @param  int 	  $channel_id  id of channel to perform aggregation on
  * @param  string $interpreter interpreter class name
  * @param  string $mode        aggregation mode (full, delta)
  * @param  string $level       aggregation level (day...)
  * @param  int 	  $period      delta days to aggregate
  * @return int    number of rows
  */
 protected function aggregateChannel($channel_id, $interpreter, $mode, $level, $period)
 {
     $format = self::getAggregationDateFormat($level);
     $type = self::getAggregationLevelTypeValue($level);
     $weighed_avg = $interpreter == 'Volkszaehler\\Interpreter\\SensorInterpreter';
     $sqlParameters = array($type);
     $sql = 'REPLACE INTO aggregate (channel_id, type, timestamp, value, count) ';
     if ($weighed_avg) {
         // get interpreter's aggregation function
         $aggregationFunction = $interpreter::groupExprSQL('agg.value');
         // SQL query similar to MySQLOptimizer group mode
         $sql .= 'SELECT channel_id, ? AS type, ' . 'MAX(agg.timestamp) AS timestamp, ' . 'COALESCE( ' . 'SUM(agg.val_by_time) / (MAX(agg.timestamp) - MIN(agg.prev_timestamp)), ' . $aggregationFunction . ') AS value, ' . 'COUNT(agg.value) AS count ' . 'FROM ( ' . 'SELECT channel_id, timestamp, value, ' . 'value * (timestamp - @prev_timestamp) AS val_by_time, ' . 'GREATEST(0, IF(@prev_timestamp = NULL, NULL, @prev_timestamp)) AS prev_timestamp, ' . '@prev_timestamp := timestamp ' . 'FROM data ' . 'CROSS JOIN (SELECT @prev_timestamp := NULL) AS vars ' . 'WHERE ';
     } else {
         // get interpreter's aggregation function
         $aggregationFunction = $interpreter::groupExprSQL('value');
         $sql .= 'SELECT channel_id, ? AS type, MAX(timestamp) AS timestamp, ' . $aggregationFunction . ' AS value, COUNT(timestamp) AS count ' . 'FROM data WHERE ';
     }
     // selected channel only
     if ($channel_id) {
         $sqlParameters[] = $channel_id;
         $sql .= 'channel_id = ? ';
     }
     // since last aggregation only
     if ($mode == 'delta') {
         if ($channel_id) {
             // selected channel
             $sqlTimestamp = 'SELECT UNIX_TIMESTAMP(DATE_ADD(' . 'FROM_UNIXTIME(MAX(timestamp) / 1000, ' . $format . '), ' . 'INTERVAL 1 ' . $level . ')) * 1000 ' . 'FROM aggregate ' . 'WHERE type = ? AND channel_id = ?';
             if ($ts = $this->conn->fetchColumn($sqlTimestamp, array($type, $channel_id), 0)) {
                 $sqlParameters[] = $ts;
                 $sql .= 'AND timestamp >= ? ';
             }
         } else {
             // all channels
             $sqlParameters[] = $type;
             $sql .= 'AND timestamp >= IFNULL((' . 'SELECT UNIX_TIMESTAMP(DATE_ADD(' . 'FROM_UNIXTIME(MAX(timestamp) / 1000, ' . $format . '), ' . 'INTERVAL 1 ' . $level . ')) * 1000 ' . 'FROM aggregate ' . 'WHERE type = ? AND aggregate.channel_id = data.channel_id ' . '), 0) ';
         }
     }
     // selected number of periods only
     if ($period) {
         $sql .= 'AND timestamp >= (SELECT UNIX_TIMESTAMP(DATE_SUB(DATE_FORMAT(NOW(), ' . $format . '), INTERVAL ? ' . $level . ')) * 1000) ';
         $sqlParameters[] = $period;
     }
     // up to before current period
     $sql .= 'AND timestamp < UNIX_TIMESTAMP(DATE_FORMAT(NOW(), ' . $format . ')) * 1000 ';
     if ($weighed_avg) {
         // close inner table
         $sql .= 'ORDER BY timestamp ' . ') AS agg ';
     }
     $sql .= 'GROUP BY channel_id, ' . Interpreter\Interpreter::buildGroupBySQL($level);
     if (Util\Debug::isActivated()) {
         echo Util\Debug::getParametrizedQuery($sql, $sqlParameters) . "\n";
     }
     $rows = $this->conn->executeUpdate($sql, $sqlParameters);
     return $rows;
 }