-
Notifications
You must be signed in to change notification settings - Fork 340
/
RunController.php
350 lines (295 loc) · 10.4 KB
/
RunController.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace XHGui\Controller;
use Exception;
use Slim\App;
use XHGui\AbstractController;
use XHGui\Options\SearchOptions;
use XHGui\RequestProxy as Request;
use XHGui\Searcher\SearcherInterface;
class RunController extends AbstractController
{
/**
* HTTP GET attribute name for comma separated filters
*/
private const FILTER_ARGUMENT_NAME = 'filter';
/**
* @var SearcherInterface
*/
private $searcher;
public function __construct(App $app, SearcherInterface $searcher)
{
parent::__construct($app);
$this->searcher = $searcher;
}
public function index(Request $request): void
{
$search = [];
$keys = ['date_start', 'date_end', 'server_name', 'url'];
foreach ($keys as $key) {
if ($request->get($key)) {
$search[$key] = $request->get($key);
}
}
$sort = $request->get('sort');
$result = $this->searcher->getAll(new SearchOptions([
'sort' => $sort,
'page' => (int)$request->get('page', SearcherInterface::DEFAULT_PAGE),
'direction' => $request->get('direction'),
'perPage' => (int)$this->config('page.limit'),
'conditions' => $search,
'projection' => true,
]));
$serverNames = $this->searcher->getAllServerNames();
$title = 'Recent runs';
$titleMap = [
'wt' => 'Longest wall time',
'cpu' => 'Most CPU time',
'mu' => 'Highest memory use',
];
if (isset($titleMap[$sort])) {
$title = $titleMap[$sort];
}
$paging = [
'total_pages' => $result['totalPages'],
'page' => $result['page'],
'sort' => $sort,
'direction' => $result['direction'],
];
$this->render('runs/list.twig', [
'paging' => $paging,
'base_url' => 'home',
'runs' => $result['results'],
'search' => $search,
'has_search' => implode('', $search) !== '',
'title' => $title,
'server_names' => $serverNames,
]);
}
public function view(Request $request): void
{
$detailCount = $this->config('detail.count');
$result = $this->searcher->get($request->get('id'));
$result->calculateSelf();
// Self wall time graph
$timeChart = $result->extractDimension('ewt', $detailCount);
// Memory Block
$memoryChart = $result->extractDimension('emu', $detailCount);
// Watched Functions Block
$watchedFunctions = [];
foreach ($this->searcher->getAllWatches() as $watch) {
$matches = $result->getWatched($watch['name']);
if ($matches) {
$watchedFunctions = array_merge($watchedFunctions, $matches);
}
}
if (false !== $request->get(self::FILTER_ARGUMENT_NAME, false)) {
$profile = $result->sort('ewt', $result->filter($result->getProfile(), $this->getFilters($request)));
} else {
$profile = $result->sort('ewt', $result->getProfile());
}
$this->render('runs/view.twig', [
'profile' => $profile,
'result' => $result,
'wall_time' => $timeChart,
'memory' => $memoryChart,
'watches' => $watchedFunctions,
]);
}
/**
* @return array
*/
private function getFilters($request)
{
$filterString = $request->get(self::FILTER_ARGUMENT_NAME);
if (strlen($filterString) > 1 && $filterString !== 'true') {
$filters = array_map('trim', explode(',', $filterString));
} else {
$filters = $this->config('run.view.filter.names');
}
return $filters;
}
public function deleteForm(Request $request): void
{
$id = $request->get('id');
if (!is_string($id) || !strlen($id)) {
throw new Exception('The "id" parameter is required.');
}
// Get details
$result = $this->searcher->get($id);
$this->render('runs/delete-form.twig', [
'run_id' => $id,
'result' => $result,
]);
}
public function deleteSubmit(Request $request): void
{
$id = $request->post('id');
// Don't call profilers->delete() unless $id is set,
// otherwise it will turn the null into a MongoId and return "Successful".
if (!is_string($id) || !strlen($id)) {
// Form checks this already,
// only reachable by handcrafted or malformed requests.
throw new Exception('The "id" parameter is required.');
}
// Delete the profile run.
$this->searcher->delete($id);
$this->flashSuccess('Deleted profile ' . $id);
$this->redirectTo('home');
}
public function deleteAllForm(): void
{
$this->render('runs/delete-all-form.twig');
}
public function deleteAllSubmit(): void
{
// Delete all profile runs.
$this->searcher->truncate();
$this->flashSuccess('Deleted all profiles');
$this->redirectTo('home');
}
public function url(Request $request): void
{
$pagination = [
'sort' => $request->get('sort'),
'direction' => $request->get('direction'),
'page' => $request->get('page'),
'perPage' => $this->config('page.limit'),
];
$search = [];
$keys = ['date_start', 'date_end', 'limit', 'limit_custom', 'server_name'];
foreach ($keys as $key) {
$search[$key] = $request->get($key);
}
$runs = $this->searcher->getForUrl(
$request->get('url'),
$pagination,
$search
);
if (isset($search['limit_custom']) &&
strlen($search['limit_custom']) > 0 &&
$search['limit_custom'][0] === 'P'
) {
$search['limit'] = $search['limit_custom'];
}
$chartData = $this->searcher->getPercentileForUrl(
90,
$request->get('url'),
$search
);
$paging = [
'total_pages' => $runs['totalPages'],
'sort' => $pagination['sort'],
'page' => $runs['page'],
'direction' => $runs['direction'],
];
$this->render('runs/url.twig', [
'paging' => $paging,
'base_url' => 'url.view',
'runs' => $runs['results'],
'url' => $request->get('url'),
'chart_data' => $chartData,
'search' => array_merge($search, ['url' => $request->get('url')]),
]);
}
public function compare(Request $request): void
{
$baseRun = $headRun = $candidates = $comparison = null;
$paging = [];
if ($request->get('base')) {
$baseRun = $this->searcher->get($request->get('base'));
}
if ($baseRun && !$request->get('head')) {
$pagination = [
'direction' => $request->get('direction'),
'sort' => $request->get('sort'),
'page' => $request->get('page'),
'perPage' => $this->config('page.limit'),
];
$candidates = $this->searcher->getForUrl(
$baseRun->getMeta('simple_url'),
$pagination,
['server_name' => $baseRun->getMeta('SERVER.SERVER_NAME')]
);
$paging = [
'total_pages' => $candidates['totalPages'],
'sort' => $pagination['sort'],
'page' => $candidates['page'],
'direction' => $candidates['direction'],
];
}
if ($request->get('head')) {
$headRun = $this->searcher->get($request->get('head'));
}
if ($baseRun && $headRun) {
$comparison = $baseRun->compare($headRun);
}
$this->render('runs/compare.twig', [
'base_url' => 'run.compare',
'base_run' => $baseRun,
'head_run' => $headRun,
'candidates' => $candidates,
'url_params' => $request->getQueryParams(),
'comparison' => $comparison,
'paging' => $paging,
'search' => [
'base' => $request->get('base'),
'head' => $request->get('head'),
],
]);
}
public function symbol(Request $request): void
{
$id = $request->get('id');
$symbol = $request->get('symbol');
$profile = $this->searcher->get($id);
$profile->calculateSelf();
[$parents, $current, $children] = $profile->getRelatives($symbol);
$this->render('runs/symbol.twig', [
'symbol' => $symbol,
'id' => $id,
'main' => $profile->get('main()'),
'parents' => $parents,
'current' => $current,
'children' => $children,
]);
}
public function symbolShort(Request $request): void
{
$id = $request->get('id');
$threshold = $request->get('threshold');
$symbol = $request->get('symbol');
$metric = $request->get('metric');
$profile = $this->searcher->get($id);
$profile->calculateSelf();
[$parents, $current, $children] = $profile->getRelatives($symbol, $metric, $threshold);
$this->render('runs/symbol-short.twig', [
'symbol' => $symbol,
'id' => $id,
'main' => $profile->get('main()'),
'parents' => $parents,
'current' => $current,
'children' => $children,
]);
}
public function callgraph(Request $request): void
{
$profile = $this->searcher->get($request->get('id'));
$this->render('runs/callgraph.twig', [
'profile' => $profile,
]);
}
public function callgraphData(Request $request)
{
$profile = $this->searcher->get($request->get('id'));
$metric = $request->get('metric') ?: 'wt';
$threshold = (float)$request->get('threshold') ?: 0.01;
return $profile->getCallgraph($metric, $threshold);
}
public function callgraphDataDot(Request $request)
{
$profile = $this->searcher->get($request->get('id'));
$metric = $request->get('metric') ?: 'wt';
$threshold = (float)$request->get('threshold') ?: 0.01;
return $profile->getCallgraphNodes($metric, $threshold);
}
}