From 993fbce82a822e19f6252345a580271aba94d20b Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 22 Jul 2011 12:28:58 +0200 Subject: [PATCH] updated view system --- lib/View/CSV.php | 68 ++++++++++++++++++++++++++++++++++++----------- lib/View/JSON.php | 12 ++++++--- lib/View/XML.php | 32 ++++++++++++++++------ 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/lib/View/CSV.php b/lib/View/CSV.php index f504d66..83b73f4 100644 --- a/lib/View/CSV.php +++ b/lib/View/CSV.php @@ -46,11 +46,10 @@ class CSV extends View { public function __construct(HTTP\Request $request, HTTP\Response $response) { parent::__construct($request, $response); - echo 'source: volkszaehler.org' . PHP_EOL; - echo 'version: ' . VZ_VERSION . PHP_EOL; + echo '# source: volkszaehler.org' . PHP_EOL; + echo '# version: ' . VZ_VERSION . PHP_EOL; $this->response->setHeader('Content-type', 'text/csv'); - $this->response->setHeader('Content-Disposition', 'attachment; filename="data.csv"'); } /** @@ -79,17 +78,17 @@ class CSV extends View { * @param Util\Debug $debug */ protected function addDebug(Util\Debug $debug) { - echo 'time: ' . $debug->getExecutionTime() . PHP_EOL; - echo 'database: ' . Util\Configuration::read('db.driver') . PHP_EOL; + echo '# time: ' . $debug->getExecutionTime() . PHP_EOL; + echo '# database: ' . Util\Configuration::read('db.driver') . PHP_EOL; foreach ($debug->getMessages() as $message) { - echo 'message: ' . $message['message'] . PHP_EOL; // TODO add more information + echo '# message: ' . $message['message'] . PHP_EOL; // TODO add more information } foreach ($debug->getQueries() as $query) { - echo 'query: ' . $query['sql'] . PHP_EOL; + echo '# query: ' . $query['sql'] . PHP_EOL; if (isset($query['parameters'])) { - echo "\tparameters: " . implode(', ', $query['parameters']) . PHP_EOL; + echo "# \tparameters: " . implode(', ', $query['parameters']) . PHP_EOL; } } } @@ -101,11 +100,11 @@ class CSV extends View { * @param boolean $debug */ protected function addException(\Exception $exception) { - echo get_class($exception) . '[' . $exception->getCode() . ']' . ':' . $exception->getMessage() . PHP_EOL; + echo get_class($exception) . '# [' . $exception->getCode() . ']' . ':' . $exception->getMessage() . PHP_EOL; if (Util\Debug::isActivated()) { - echo "\tfile: " . $exception->getFile() . PHP_EOL; - echo "\tline: " . $exception->getLine() . PHP_EOL; + echo "#\tfile: " . $exception->getFile() . PHP_EOL; + echo "#\tline: " . $exception->getLine() . PHP_EOL; } } @@ -115,19 +114,56 @@ class CSV extends View { * @param Interpreter\InterpreterInterface $interpreter */ protected function addData(Interpreter\Interpreter $interpreter) { - //$this->response->setHeader('Content-Disposition', 'attachment; filename="' . strtolower($interpreter->getEntity()->getProperty('title')) . '.csv"'); // TODO add time? + $this->response->setHeader( + 'Content-Disposition', + 'attachment; ' . + 'filename="' . strtolower($interpreter->getEntity()->getProperty('title')) . '.csv" ' . + 'creation-date="' . date(DATE_RFC2822, $interpreter->getTo()/1000). '"' + ); $tuples = $interpreter->processData( - $this->request->getParameter('tuples'), - $this->request->getParameter('group'), function($tuple) { - echo implode(CSV::DELIMITER, array( + return array( $tuple[0], View::formatNumber($tuple[1]), $tuple[2] - )) . PHP_EOL; + ); } ); + + $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; + } } /** diff --git a/lib/View/JSON.php b/lib/View/JSON.php index b21f631..fda900b 100644 --- a/lib/View/JSON.php +++ b/lib/View/JSON.php @@ -192,24 +192,28 @@ class JSON extends View { $this->json['data']['uuid'] = $interpreter->getEntity()->getUuid(); if (isset($from)) $this->json['data']['from'] = $from; + if (isset($to)) $this->json['data']['to'] = $to; + if (isset($min)) $this->json['data']['min'] = $min; + if (isset($max)) $this->json['data']['max'] = $max; + if (isset($average)) $this->json['data']['average'] = View::formatNumber($average); + if (isset($consumption)) $this->json['data']['consumption'] = View::formatNumber($consumption); - $this->json['data']['count'] = count($data); + + $this->json['data']['rows'] = $interpreter->getRowCount(); + if (($interpreter->getTupleCount() > 0 || is_null($interpreter->getTupleCount())) && count($data) > 0) $this->json['data']['tuples'] = $data; } - /** - * - */ protected function addArray($data, &$refNode) { if (is_null($refNode)) { $refNode = array(); diff --git a/lib/View/XML.php b/lib/View/XML.php index c7798c3..26139ca 100644 --- a/lib/View/XML.php +++ b/lib/View/XML.php @@ -237,31 +237,47 @@ class XML extends View { $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(); - $from = $interpreter->getFrom(); - $to = $interpreter->getTo(); - $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)) - $xmlData->appendChild($this->xmlDoc->createElement('min', $min)); - if (isset($max)) - $xmlData->appendChild($this->xmlDoc->createElement('max', $max)); + + 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('count', count($data))); + + $xmlData->appendChild($this->xmlDoc->createElement('rows', $interpreter->getRowCount())); + if (($interpreter->getTupleCount() > 0 || is_null($interpreter->getTupleCount())) && count($data) > 0) $xmlData->appendChild($xmlTuples);