-
Notifications
You must be signed in to change notification settings - Fork 82
/
Text.php
131 lines (113 loc) · 3.89 KB
/
Text.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @copyright Copyright (c) 2011-2018, The volkszaehler.org project
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License version 3
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\View;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Volkszaehler\Util;
use Volkszaehler\Interpreter;
/**
* Plain text view
*
* @author Andreas Götz <cpuidle@gmx.de>
*/
class Text extends CSV {
/**
* Constructor
*/
public function __construct(Request $request) {
// avoid calling parent::__construct($request);
$this->request = $request;
$this->response = new Response();
$this->response->headers->set('Content-Type', 'text/plain');
ob_start();
}
/**
* 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;
}
}
/**
* Add debugging information include queries and messages to output queue
*
* @param Util\Debug $debug
*/
protected function addDebug(Util\Debug $debug) {
echo PHP_EOL;
echo "database:\t" . Util\Configuration::read('db.driver') . PHP_EOL;
echo "time:\t" . $debug->getExecutionTime() . PHP_EOL;
if ($uptime = Util\Debug::getUptime()) echo "uptime:\t" . $uptime*1000 . PHP_EOL;
if ($load = Util\Debug::getLoadAvg()) echo "load:\t" . implode(', ', $load) . PHP_EOL;
if ($commit = Util\Debug::getCurrentCommit()) echo "commit-hash:\t" . $commit . PHP_EOL;
if ($version = phpversion()) echo "php-version:\t" . $version . PHP_EOL;
foreach ($debug->getMessages() as $message) {
echo "message:\t" . $message['message'] . PHP_EOL; // TODO add more information
}
echo "sql:\t" . PHP_EOL;
foreach ($debug->getQueries() as $query) {
$parameters = isset($query['params']) ? $query['params'] : [];
echo Util\Debug::getParametrizedQuery($query['sql'], $parameters) . PHP_EOL;
}
}
/**
* 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) {
// echo "uuid:' . $interpreter->getEntity()->getUuid() . PHP_EOL;
// echo "title:' . $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;
}
// get unit
$unit = $interpreter->getEntity()->getDefinition();
$unit = isset($unit->unit) ? $unit->unit : '';
if (sizeof($data) == 0 ||
$this->request->query->has('tuples') && $this->request->query->get('tuples') == 1
) {
$val = $interpreter->getConsumption();
$unit .= 'h';
}
else {
$val = $data[sizeof($data)-1];
}
$val = is_array($val) ? $val[1] : $val;
echo $val . ' ' . $unit;
}
}
?>