/**
  * Add data to output queue
  *
  * @param Interpreter\InterpreterInterface $interpreter
  * @param boolean $children
  * @todo  Aggregate first is assumed- this deviates from json view behaviour
  */
 protected function addData(Interpreter\Interpreter $interpreter)
 {
     $this->response->headers->set('Content-Disposition', 'attachment; ' . 'filename="' . strtolower($interpreter->getEntity()->getProperty('title')) . '.csv" ');
     echo PHP_EOL;
     // UUID delimiter
     echo '# uuid:' . CSV::DELIMITER . $interpreter->getEntity()->getUuid() . PHP_EOL;
     echo '# title:' . CSV::DELIMITER . $interpreter->getEntity()->getProperty('title') . PHP_EOL;
     if ($interpreter instanceof Interpreter\AggregatorInterpreter) {
         // min/ max etc are not populated if $children->processData hasn't been called
         return;
     }
     $data = array();
     // iterate through PDO resultset
     foreach ($interpreter as $tuple) {
         $data[] = $tuple;
     }
     $min = $interpreter->getMin();
     $max = $interpreter->getMax();
     $average = $interpreter->getAverage();
     $consumption = $interpreter->getConsumption();
     $from = $this->formatTimestamp($interpreter->getFrom());
     $to = $this->formatTimestamp($interpreter->getTo());
     if (isset($from)) {
         echo '# from:' . CSV::DELIMITER . $from . PHP_EOL;
     }
     if (isset($to)) {
         echo '# to:' . CSV::DELIMITER . $to . PHP_EOL;
     }
     if (isset($min)) {
         echo '# min:' . CSV::DELIMITER . $this->formatTimestamp($min[0]) . CSV::DELIMITER . ' => ' . CSV::DELIMITER . View::formatNumber($min[1]) . PHP_EOL;
     }
     if (isset($max)) {
         echo '# max:' . CSV::DELIMITER . $this->formatTimestamp($max[0]) . CSV::DELIMITER . ' => ' . CSV::DELIMITER . View::formatNumber($max[1]) . PHP_EOL;
     }
     if (isset($average)) {
         echo '# average:' . CSV::DELIMITER . View::formatNumber($average) . PHP_EOL;
     }
     if (isset($consumption)) {
         echo '# consumption:' . CSV::DELIMITER . View::formatNumber($consumption) . PHP_EOL;
     }
     echo '# rows:' . CSV::DELIMITER . $interpreter->getRowCount() . PHP_EOL;
     if (isset($data)) {
         // Aggregators don't return data
         foreach ($data as $tuple) {
             echo $this->formatTimestamp($tuple[0]) . CSV::DELIMITER . View::formatNumber($tuple[1]) . CSV::DELIMITER . $tuple[2] . PHP_EOL;
         }
     }
 }
Exemple #2
0
 /**
  * Render Interpreter output
  *
  * See comments regarding StreamedResponse at renderDeferred()
  */
 protected function renderInterpreter(Interpreter\Interpreter $interpreter)
 {
     $this->content .= '{"tuples":[';
     // start with iterating through PDO result set to populate interpreter header data
     foreach ($interpreter as $key => $tuple) {
         // render buffered content- likely no exception after loop iteration has started
         $this->renderContent();
         if ($key) {
             echo ',';
         }
         echo '[' . $tuple[0] . ',' . View::formatNumber($tuple[1]) . ',' . $tuple[2] . ']';
     }
     // render buffered content if not rendered inside foreach loop due to Interpreter empty
     $this->renderContent();
     $from = 0 + $interpreter->getFrom();
     $to = 0 + $interpreter->getTo();
     $min = $interpreter->getMin();
     $max = $interpreter->getMax();
     $average = $interpreter->getAverage();
     $consumption = $interpreter->getConsumption();
     $header = array();
     $header['uuid'] = $interpreter->getEntity()->getUuid();
     if (isset($from)) {
         $header['from'] = $from;
     }
     if (isset($to)) {
         $header['to'] = $to;
     }
     if (isset($min)) {
         $header['min'] = $min;
     }
     if (isset($max)) {
         $header['max'] = $max;
     }
     if (isset($average)) {
         $header['average'] = View::formatNumber($average);
     }
     if (isset($consumption)) {
         $header['consumption'] = View::formatNumber($consumption);
     }
     $header['rows'] = $interpreter->getRowCount();
     echo '],' . substr(json_encode($header), 1, -1) . '}';
 }
Exemple #3
0
 /**
  * Add data to output queue
  *
  * @param Interpreter\InterpreterInterface $interpreter
  */
 protected function addData(Interpreter\Interpreter $interpreter)
 {
     $this->response->setHeader('Content-Disposition', 'attachment; ' . 'filename="' . strtolower($interpreter->getEntity()->getProperty('title')) . '.csv" ' . 'creation-date="' . date(DATE_RFC2822, $interpreter->getTo() / 1000) . '"');
     $tuples = $interpreter->processData(function ($tuple) {
         return array($tuple[0], View::formatNumber($tuple[1]), $tuple[2]);
     });
     $min = $interpreter->getMin();
     $max = $interpreter->getMax();
     $average = $interpreter->getAverage();
     $consumption = $interpreter->getConsumption();
     $from = $interpreter->getFrom();
     $to = $interpreter->getTo();
     echo '# uuid: ' . $interpreter->getEntity()->getUuid() . PHP_EOL;
     if (isset($from)) {
         echo '# from: ' . $from . PHP_EOL;
     }
     if (isset($to)) {
         echo '# to: ' . $to . PHP_EOL;
     }
     if (isset($min)) {
         echo '# min: ' . $min[0] . ' => ' . $min[1] . PHP_EOL;
     }
     if (isset($max)) {
         echo '# max: ' . $max[0] . ' => ' . $max[1] . PHP_EOL;
     }
     if (isset($average)) {
         echo '# average: ' . View::formatNumber($average) . PHP_EOL;
     }
     if (isset($consumption)) {
         echo '# consumption: ' . View::formatNumber($consumption) . PHP_EOL;
     }
     echo '# rows: ' . $interpreter->getRowCount() . PHP_EOL;
     foreach ($tuples as $tuple) {
         echo implode(CSV::DELIMITER, $tuple) . PHP_EOL;
     }
 }