-
Notifications
You must be signed in to change notification settings - Fork 0
/
propel-panel.php
195 lines (163 loc) · 6.4 KB
/
propel-panel.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Addons\Propel\Diagnostics;
use Nette\Diagnostics\Debugger;
use \Propel;
class PropelPanel extends \Nette\Object implements \Nette\Diagnostics\IBarPanel, \BasicLogger {
/** @var int logged time */
public $totalTime = 0;
/** @var int */
public $maxPriority = 0;
/** @var array */
public $queries = array();
/** @var array */
public $priorities = array();
const name = 'propel';
public function emergency($m) {
$this->log($m, Propel::LOG_EMERG);
}
public function alert($m) {
$this->log($m, Propel::LOG_ALERT);
}
public function crit($m) {
$this->log($m, Propel::LOG_CRIT);
}
public function err($m) {
$this->log($m, Propel::LOG_ERR);
}
public function warning($m) {
$this->log($m, Propel::LOG_WARNING);
}
public function notice($m) {
$this->log($m, Propel::LOG_NOTICE);
}
public function info($m) {
$this->log($m, Propel::LOG_INFO);
}
public function debug($m) {
$this->log($m, Propel::LOG_DEBUG);
}
public function log($message, $priority = null) {
Debugger::timer(self::name);
$this->queries[] = array($message, $this->priorityToText($priority));
# set priority count
isset($this->priorities[$priority]) ? null : $this->priorities[$priority] = 0;
$this->priorities[$priority] ++;
# set max priority
if (!isset($this->maxPriority) || $priority > $this->maxPriority)
$this->maxPriority = $priority;
$keys = array_keys($this->queries);
$key = end($keys);
$this->queries[$key][2] = $queryTime = Debugger::timer(self::name);
$this->totalTime += $queryTime;
}
private function priorityToText($priority) {
switch ($priority) {
case Propel::LOG_EMERG:
return '<span style="color: red">Emergency</span>';
break;
case Propel::LOG_ALERT:
return '<span style="color: red">Alert</span>';
break;
case Propel::LOG_CRIT:
return '<span style="color: red">Critical</span>';
break;
case Propel::LOG_ERR:
return '<span style="color: red">Error</span>';
break;
case Propel::LOG_WARNING:
return '<span style="color: orange">Warning</span>';
break;
case Propel::LOG_NOTICE:
return '<span style="color: green">Notice</span>';
break;
case Propel::LOG_INFO:
return '<span style="color: blue">Info</span>';
break;
case Propel::LOG_DEBUG:
return '<span style="color: grey">Debug</span>';
break;
}
}
public function getTab() {
return '<span title="Propel">'
. '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEYSURBVBgZBcHPio5hGAfg6/2+R980k6wmJgsJ5U/ZOAqbSc2GnXOwUg7BESgLUeIQ1GSjLFnMwsKGGg1qxJRmPM97/1zXFAAAAEADdlfZzr26miup2svnelq7d2aYgt3rebl585wN6+K3I1/9fJe7O/uIePP2SypJkiRJ0vMhr55FLCA3zgIAOK9uQ4MS361ZOSX+OrTvkgINSjS/HIvhjxNNFGgQsbSmabohKDNoUGLohsls6BaiQIMSs2FYmnXdUsygQYmumy3Nhi6igwalDEOJEjPKP7CA2aFNK8Bkyy3fdNCg7r9/fW3jgpVJbDmy5+PB2IYp4MXFelQ7izPrhkPHB+P5/PjhD5gCgCenx+VR/dODEwD+A3T7nqbxwf1HAAAAAElFTkSuQmCC" />'
. count($this->queries) . ' queries'
. ($this->totalTime ? ' / ' . sprintf('%0.1f', $this->totalTime * 1000) . 'ms' : '')
. (isset($this->maxPriority) ? ' / ' . $this->priorityToText($this->maxPriority) : '')
. '</span>';
}
/**
* @param array
* @return string
*/
protected function processQuery(array $query) {
$s = '';
list($sql, $priority, $time) = $query;
$s .= '<tr><td>' . sprintf('%0.3f', $time * 1000);
$s .= '</td><td class="nette-PropelPanel-sql">' . \Nette\Database\Helpers::dumpSql($sql);
$s .= '</td><td>' . $this->priorityToText($priority) . '</tr>';
return $s;
}
protected function renderStyles() {
return '<style> #nette-debug td.nette-PropelPanel-sql { background: white !important }
#nette-debug .nette-PropelPanel-source { color: #BBB !important }
#nette-debug nette-PropelPanel tr table { margin: 8px 0; max-height: 150px; overflow:auto } </style>';
}
/**
* @param array
* @return string
*/
protected function processPriorities(array $priorities) {
if (empty($priorities))
return '';
$s = '<table><tr>';
foreach ($priorities as $priority => $count) {
$s .= '<td>' . $this->priorityToText($priority) . '</td><td>' . $count . '</td>';
}
$s .= '</tr></table><br/>';
return $s;
}
/**
* @param \PDOException
* @return array
*/
public function renderException($e) {
if ($e instanceof \PDOException && count($this->queries)) {
$s = '<table><tr><th>Time ms</th><th>SQL</th><th>Type</th></tr>';
$s .= $this->processQuery(end($this->queries));
$s .= '</table>';
return array(
'tab' => 'SQL',
'panel' => $this->renderStyles() . '<div class="nette-inner nette-PropelPanel">' . $s . '</div>',
);
} else {
\Nette\Database\Diagnostics\ConnectionPanel::renderException($e);
}
}
public function getPanel() {
$s = '';
foreach ($this->queries as $query) {
$s .= $this->processQuery($query);
}
return empty($this->queries) ? '' :
$this->renderStyles() .
'<h1>Queries: ' . count($this->queries) . ($this->totalTime ? ', time: ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : '') . '</h1>' .
$this->processPriorities($this->priorities) .
'<div class="nette-inner nette-PropelPanel">
<table>
<tr><th>Time ms</th><th>SQL</th><th>Type</th></tr>' . $s . '
</table>
</div>';
}
/**
* @return ConnectionPanel
*/
public static function register() {
$panel = new static;
if (Debugger::$bar) {
Debugger::$bar->addPanel($panel);
}
Debugger::$blueScreen->addPanel(array($panel, 'renderException'));
return $panel;
}
}