/** * Constructor * * @param HTTP\Request $request * @param HTTP\Response $response * @param string $format one of png, jpeg, gif */ public function __construct(HTTP\Request $request, HTTP\Response $response, $format = 'png') { parent::__construct($request, $response); // to enabled jpgraphs graphical exception handler restore_exception_handler(); if ($this->request->getParameter('width')) { $this->width = $this->request->getParameter('width'); } if ($this->request->getParameter('height')) { $this->height = $this->request->getParameter('height'); } $this->colors = Util\Configuration::read('colors'); $this->graph = new \Graph($this->width, $this->height); $this->graph->img->SetImgFormat($format); // Specify what scale we want to use, $this->graph->SetScale('datlin'); $this->graph->legend->SetPos(0.03, 0.06); $this->graph->legend->SetShadow(FALSE); $this->graph->legend->SetFrameWeight(1); $this->graph->SetMarginColor('white'); $this->graph->SetYDeltaDist(65); $this->graph->yaxis->SetTitlemargin(36); $this->graph->SetTickDensity(TICKD_DENSE, TICKD_SPARSE); $this->graph->xaxis->SetFont(FF_ARIAL); $this->graph->xaxis->SetLabelAngle(45); $this->graph->xaxis->SetLabelFormatCallback(function ($label) { return date('j.n.y G:i', $label); }); $this->graph->img->SetAntiAliasing(function_exists('imageantialias')); }
/** * Processes the request * * Example: http://sub.domain.local/middleware.php/channel/550e8400-e29b-11d4-a716-446655440000/data.json?operation=edit&title=New Title */ public function run() { $operation = self::getOperation($this->view->request); $context = explode('/', substr($this->pathInfo, 1, strrpos($this->pathInfo, '.') - 1)); // parse pathinfo if (!array_key_exists($context[0], self::$controllerMapping)) { if (empty($context[0])) { throw new \Exception('Missing context'); } else { throw new \Exception('Unknown context: \'' . $context[0] . '\''); } } $class = self::$controllerMapping[$context[0]]; $controller = new $class($this->view, $this->em); $result = $controller->run($operation, array_slice($context, 1)); $this->view->add($result); }
/** * Constructor * * @param Symfony\Component\HttpFoundation\Request $request * @param string $format one of png, jpeg, gif */ public function __construct(Request $request, $format = 'png') { parent::__construct($request); $this->response->headers->set('Content-Type', 'image/' . $format); // load JpGraph // NOTE: JpGraph installs its own graphical error handler \JpGraph\JpGraph::load(); \JpGraph\JpGraph::module('date'); \JpGraph\JpGraph::module('line'); if ($this->request->query->has('width')) { $this->width = $this->request->query->get('width'); } if ($this->request->query->has('height')) { $this->height = $this->request->query->get('height'); } $this->colors = Util\Configuration::read('colors'); $this->graph = new \Graph($this->width, $this->height); // disable JpGraph default handler restore_exception_handler(); $this->graph->img->SetImgFormat($format); // Specify what scale we want to use, $this->graph->SetScale('datlin'); $this->graph->legend->SetPos(0.03, 0.06); $this->graph->legend->SetShadow(FALSE); $this->graph->legend->SetFrameWeight(1); $this->graph->SetMarginColor('white'); $this->graph->SetYDeltaDist(65); $this->graph->yaxis->SetTitlemargin(36); $this->graph->SetTickDensity(TICKD_DENSE, TICKD_SPARSE); $this->graph->xaxis->SetFont(FF_ARIAL); $this->graph->xaxis->SetLabelAngle(45); $this->graph->xaxis->SetLabelFormatCallback(function ($label) { return date('j.n.y G:i', $label); }); if (function_exists('imageantialias')) { $this->graph->img->SetAntiAliasing(true); } }
/** * Add an array of objects to the output */ protected function addArray($data, &$refNode) { if (is_null($refNode)) { $refNode = array(); } foreach ($data as $index => $value) { if (is_array($value)) { $this->addArray($value, $refNode[$index]); } elseif ($value instanceof Model\Entity) { $refNode[$index] = self::convertEntity($value); } elseif ($value instanceof Interpreter\Interpreter) { // special case: interpreters are always added to the root node if (!isset($this->json['data'])) { $this->json['data'] = array(); } $this->json['data'][] = $value; } elseif (is_numeric($value)) { $refNode[$index] = View::formatNumber($value); } else { $refNode[$index] = $value; } } }
/** * 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; } } }
/** * Add data to output queue * * @param $interpreter */ protected function addData($interpreter) { $xmlDoc = $this->xmlDoc; $xmlData = $this->xmlDoc->createElement('data'); $xmlTuples = $this->xmlDoc->createElement('tuples'); $data = $interpreter->processData(function ($tuple) use($xmlDoc, $xmlTuples) { $xmlTuple = $xmlDoc->createElement('tuple'); $xmlTuple->setAttribute('timestamp', $tuple[0]); $xmlTuple->setAttribute('value', View::formatNumber($tuple[1])); $xmlTuple->setAttribute('count', $tuple[2]); $xmlTuples->appendChild($xmlTuple); return $tuple; }); $from = $interpreter->getFrom(); $to = $interpreter->getTo(); $min = $interpreter->getMin(); $max = $interpreter->getMax(); $average = $interpreter->getAverage(); $consumption = $interpreter->getConsumption(); $xmlData->appendChild($this->xmlDoc->createElement('uuid', $interpreter->getEntity()->getUuid())); if (isset($from)) { $xmlData->appendChild($this->xmlDoc->createElement('from', $from)); } if (isset($to)) { $xmlData->appendChild($this->xmlDoc->createElement('to', $to)); } if (isset($min)) { $xmlMin = $this->xmlDoc->createElement('min'); $xmlMin->setAttribute('timestamp', $min[0]); $xmlMin->setAttribute('value', $min[1]); $xmlData->appendChild($xmlMin); } if (isset($max)) { $xmlMax = $this->xmlDoc->createElement('max'); $xmlMax->setAttribute('timestamp', $max[0]); $xmlMax->setAttribute('value', $max[1]); $xmlData->appendChild($xmlMax); } if (isset($average)) { $xmlData->appendChild($this->xmlDoc->createElement('average', View::formatNumber($average))); } if (isset($consumption)) { $xmlData->appendChild($this->xmlDoc->createElement('consumption', View::formatNumber($consumption))); } $xmlData->appendChild($this->xmlDoc->createElement('rows', $interpreter->getRowCount())); if (($interpreter->getTupleCount() > 0 || is_null($interpreter->getTupleCount())) && count($data) > 0) { $xmlData->appendChild($xmlTuples); } $this->xmlRoot->appendChild($xmlData); }
/** * Add data to output queue * * @param $interpreter * @param boolean $children */ protected function addData($interpreter, $children = false) { $xmlDoc = $this->xmlDoc; $xmlData = $children ? $this->xmlDoc->createElement('data') : $this->obtainElementById('data'); $xmlTuples = $this->xmlDoc->createElement('tuples'); // iterate through PDO resultset foreach ($interpreter as $tuple) { $xmlTuple = $xmlDoc->createElement('tuple'); $xmlTuple->setAttribute('timestamp', $tuple[0]); $xmlTuple->setAttribute('value', View::formatNumber($tuple[1])); $xmlTuple->setAttribute('count', $tuple[2]); $xmlTuples->appendChild($xmlTuple); } $from = $interpreter->getFrom(); $to = $interpreter->getTo(); $min = $interpreter->getMin(); $max = $interpreter->getMax(); $average = $interpreter->getAverage(); $consumption = $interpreter->getConsumption(); $xmlData->appendChild($this->xmlDoc->createElement('uuid', $interpreter->getEntity()->getUuid())); if (isset($from)) { $xmlData->appendChild($this->xmlDoc->createElement('from', $from)); } if (isset($to)) { $xmlData->appendChild($this->xmlDoc->createElement('to', $to)); } if (isset($min)) { $xmlMin = $this->xmlDoc->createElement('min'); $xmlMin->setAttribute('timestamp', $min[0]); $xmlMin->setAttribute('value', $min[1]); $xmlData->appendChild($xmlMin); } if (isset($max)) { $xmlMax = $this->xmlDoc->createElement('max'); $xmlMax->setAttribute('timestamp', $max[0]); $xmlMax->setAttribute('value', $max[1]); $xmlData->appendChild($xmlMax); } if (isset($average)) { $xmlData->appendChild($this->xmlDoc->createElement('average', View::formatNumber($average))); } if (isset($consumption)) { $xmlData->appendChild($this->xmlDoc->createElement('consumption', View::formatNumber($consumption))); } $xmlData->appendChild($this->xmlDoc->createElement('rows', $interpreter->getRowCount())); if (($interpreter->getTupleCount() > 0 || is_null($interpreter->getTupleCount())) && count($xmlTuples) > 0) { $xmlData->appendChild($xmlTuples); } if ($children) { $xmlChildren = $this->obtainElementById('children'); $xmlChildren->appendChild($xmlData); $xmlParentData = $this->obtainElementById('data'); $xmlParentData->appendChild($xmlChildren); $this->xmlRoot->appendChild($xmlParentData); } else { $this->xmlRoot->appendChild($xmlData); } }
protected function addArray($data, &$refNode) { if (is_null($refNode)) { $refNode = array(); } foreach ($data as $index => $value) { if ($value instanceof Util\JSON || is_array($value)) { $this->addArray($value, $refNode[$index]); } elseif ($value instanceof Model\Entity) { $refNode[$index] = self::convertEntity($value); } elseif (is_numeric($value)) { $refNode[$index] = View::formatNumber($value); } else { $refNode[$index] = $value; } } }
/** * 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; } }