Completed
Pull Request — master (#237)
by
unknown
05:12 queued 02:15
created

Xhgui_Controller_Run::symbolShort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
use Slim\Slim;
4
5
class Xhgui_Controller_Run extends Xhgui_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    /**
8
     * HTTP GET attribute name for comma separated filters
9
     */
10
    const FILTER_ARGUMENT_NAME = 'filter';
11
12
    /**
13
     * @var Xhgui_Profiles
14
     */
15
    private $profiles;
16
17
    /**
18
     * @var Xhgui_WatchFunctions
19
     */
20
    private $watches;
21
22
    public function __construct(Slim $app, Xhgui_Profiles $profiles, Xhgui_WatchFunctions $watches)
23
    {
24
        $this->app = $app;
25
        $this->profiles = $profiles;
26
        $this->watches = $watches;
27
    }
28
29
    public function index()
30
    {
31
        $request = $this->app->request();
32
33
        $search = array();
34
        $keys = array('date_start', 'date_end', 'url');
35
        foreach ($keys as $key) {
36
            if ($request->get($key)) {
37
                $search[$key] = $request->get($key);
38
            }
39
        }
40
        $sort = $request->get('sort');
41
42
        $result = $this->profiles->getAll(array(
43
            'sort' => $sort,
44
            'page' => $request->get('page'),
45
            'direction' => $request->get('direction'),
46
            'perPage' => $this->app->config('page.limit'),
47
            'conditions' => $search,
48
            'projection' => true,
49
        ));
50
51
        $title = 'Recent runs';
52
        $titleMap = array(
53
            'wt' => 'Longest wall time',
54
            'cpu' => 'Most CPU time',
55
            'mu' => 'Highest memory use',
56
        );
57
        if (isset($titleMap[$sort])) {
58
            $title = $titleMap[$sort];
59
        }
60
61
        $paging = array(
62
            'total_pages' => $result['totalPages'],
63
            'page' => $result['page'],
64
            'sort' => $sort,
65
            'direction' => $result['direction']
66
        );
67
68
        $this->_template = 'runs/list.twig';
69
        $this->set(array(
70
            'paging' => $paging,
71
            'base_url' => 'home',
72
            'runs' => $result['results'],
73
            'date_format' => $this->app->config('date.format'),
74
            'search' => $search,
75
            'has_search' => strlen(implode('', $search)) > 0,
76
            'title' => $title
77
        ));
78
    }
79
80
    public function view()
81
    {
82
        $request = $this->app->request();
83
        $detailCount = $this->app->config('detail.count');
84
        $result = $this->profiles->get($request->get('id'));
85
86
        $result->calculateSelf();
87
88
        // Self wall time graph
89
        $timeChart = $result->extractDimension('ewt', $detailCount);
90
91
        // Memory Block
92
        $memoryChart = $result->extractDimension('emu', $detailCount);
93
94
        // Watched Functions Block
95
        $watchedFunctions = array();
96
        foreach ($this->watches->getAll() as $watch) {
97
            $matches = $result->getWatched($watch['name']);
98
            if ($matches) {
99
                $watchedFunctions = array_merge($watchedFunctions, $matches);
100
            }
101
        }
102
103
        if (false !== $request->get(self::FILTER_ARGUMENT_NAME, false)) {
104
            $profile = $result->sort('ewt', $result->filter($result->getProfile(), $this->getFilters()));
105
        } else {
106
            $profile = $result->sort('ewt', $result->getProfile());
107
        }
108
109
        $this->_template = 'runs/view.twig';
110
        $this->set(array(
111
            'profile' => $profile,
112
            'result' => $result,
113
            'wall_time' => $timeChart,
114
            'memory' => $memoryChart,
115
            'watches' => $watchedFunctions,
116
            'date_format' => $this->app->config('date.format'),
117
        ));
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    protected function getFilters()
124
    {
125
        $request = $this->app->request();
126
        $filterString = $request->get(self::FILTER_ARGUMENT_NAME);
127
        if (strlen($filterString)) {
128
            $filters = explode(',', $filterString);
129
        } else {
130
            $filters = $this->app->config('run.view.filter.names');
131
        }
132
133
        return $filters;
134
    }
135
136
    public function url()
137
    {
138
        $request = $this->app->request();
139
        $pagination = array(
140
            'sort' => $request->get('sort'),
141
            'direction' => $request->get('direction'),
142
            'page' => $request->get('page'),
143
            'perPage' => $this->app->config('page.limit'),
144
        );
145
146
        $search = array();
147
        $keys = array('date_start', 'date_end', 'limit', 'limit_custom');
148
        foreach ($keys as $key) {
149
            $search[$key] = $request->get($key);
150
        }
151
152
        $runs = $this->profiles->getForUrl(
153
            $request->get('url'),
154
            $pagination,
155
            $search
156
        );
157
158
        if (isset($search['limit_custom'])
159
            && strlen($search['limit_custom']) > 0
160
            && $search['limit_custom'][0] == 'P'
161
        ) {
162
            $search['limit'] = $search['limit_custom'];
163
        }
164
165
        $chartData = $this->profiles->getPercentileForUrl(
166
            90,
167
            $request->get('url'),
168
            $search
169
        );
170
171
        $paging = array(
172
            'total_pages' => $runs['totalPages'],
173
            'sort' => $pagination['sort'],
174
            'page' => $runs['page'],
175
            'direction' => $runs['direction']
176
        );
177
178
        $this->_template = 'runs/url.twig';
179
        $this->set(array(
180
            'paging' => $paging,
181
            'base_url' => 'url.view',
182
            'runs' => $runs['results'],
183
            'url' => $request->get('url'),
184
            'chart_data' => $chartData,
185
            'date_format' => $this->app->config('date.format'),
186
            'search' => array_merge($search, array('url' => $request->get('url'))),
187
        ));
188
    }
189
190
    public function compare()
191
    {
192
        $request = $this->app->request();
193
194
        $baseRun = $headRun = $candidates = $comparison = null;
195
        $paging = array();
196
197
        if ($request->get('base')) {
198
            $baseRun = $this->profiles->get($request->get('base'));
199
        }
200
201
        if ($baseRun && !$request->get('head')) {
202
            $pagination = array(
203
                'direction' => $request->get('direction'),
204
                'sort' => $request->get('sort'),
205
                'page' => $request->get('page'),
206
                'perPage' => $this->app->config('page.limit'),
207
            );
208
            $candidates = $this->profiles->getForUrl(
209
                $baseRun->getMeta('simple_url'),
210
                $pagination
211
            );
212
213
            $paging = array(
214
                'total_pages' => $candidates['totalPages'],
215
                'sort' => $pagination['sort'],
216
                'page' => $candidates['page'],
217
                'direction' => $candidates['direction']
218
            );
219
        }
220
221
        if ($request->get('head')) {
222
            $headRun = $this->profiles->get($request->get('head'));
223
        }
224
225
        if ($baseRun && $headRun) {
226
            $comparison = $baseRun->compare($headRun);
227
        }
228
229
        $this->_template = 'runs/compare.twig';
230
        $this->set(array(
231
            'base_url' => 'run.compare',
232
            'base_run' => $baseRun,
233
            'head_run' => $headRun,
234
            'candidates' => $candidates,
235
            'url_params' => $request->get(),
236
            'date_format' => $this->app->config('date.format'),
237
            'comparison' => $comparison,
238
            'paging' => $paging,
239
            'search' => array(
240
                'base' => $request->get('base'),
241
                'head' => $request->get('head'),
242
            )
243
        ));
244
    }
245
246
    public function symbol()
247
    {
248
        $request = $this->app->request();
249
        $id = $request->get('id');
250
        $symbol = $request->get('symbol');
251
252
        $profile = $this->profiles->get($id);
253
        $profile->calculateSelf();
254
        list($parents, $current, $children) = $profile->getRelatives($symbol);
255
256
        $this->_template = 'runs/symbol.twig';
257
        $this->set(array(
258
            'symbol' => $symbol,
259
            'id' => $id,
260
            'main' => $profile->get('main()'),
261
            'parents' => $parents,
262
            'current' => $current,
263
            'children' => $children,
264
        ));
265
    }
266
267
    public function symbolShort()
268
    {
269
        $request = $this->app->request();
270
        $id = $request->get('id');
271
        $threshold = $request->get('threshold');
272
        $symbol = $request->get('symbol');
273
        $metric = $request->get('metric');
274
275
        $profile = $this->profiles->get($id);
276
        $profile->calculateSelf();
277
        list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);
278
279
        $this->_template = 'runs/symbol-short.twig';
280
        $this->set(array(
281
            'symbol' => $symbol,
282
            'id' => $id,
283
            'main' => $profile->get('main()'),
284
            'parents' => $parents,
285
            'current' => $current,
286
            'children' => $children,
287
        ));
288
    }
289
290 View Code Duplication
    public function callgraph()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
291
    {
292
        $request = $this->app->request();
293
        $profile = $this->profiles->get($request->get('id'));
294
295
        $this->_template = 'runs/callgraph.twig';
296
        $this->set(array(
297
            'profile' => $profile,
298
            'date_format' => $this->app->config('date.format'),
299
        ));
300
    }
301
302 View Code Duplication
    public function callgraphData()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
    {
304
        $request = $this->app->request();
305
        $response = $this->app->response();
306
        $profile = $this->profiles->get($request->get('id'));
307
        $metric = $request->get('metric') ?: 'wt';
308
        $threshold = (float)$request->get('threshold') ?: 0.01;
309
        $callgraph = $profile->getCallgraph($metric, $threshold);
310
311
        $response['Content-Type'] = 'application/json';
312
        return $response->body(json_encode($callgraph));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
313
    }
314
315 View Code Duplication
    public function flamegraph()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
    {
317
        $request = $this->app->request();
318
        $profile = $this->profiles->get($request->get('id'));
319
320
        $this->_template = 'runs/flamegraph.twig';
321
        $this->set(array(
322
            'profile' => $profile,
323
            'date_format' => $this->app->config('date.format'),
324
        ));
325
    }
326
327 View Code Duplication
    public function flamegraphData()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
328
    {
329
        $request = $this->app->request();
330
        $response = $this->app->response();
331
        $profile = $this->profiles->get($request->get('id'));
332
        $metric = $request->get('metric') ?: 'wt';
333
        $threshold = (float)$request->get('threshold') ?: 0.01;
334
        $flamegraph = $profile->getFlamegraph($metric, $threshold);
335
336
        $response['Content-Type'] = 'application/json';
337
        return $response->body(json_encode($flamegraph));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
338
    }
339
340 View Code Duplication
    public function callgraphDataDot()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
    {
342
        $request = $this->app->request();
343
        $response = $this->app->response();
344
        $profile = $this->profiles->get($request->get('id'));
345
        $metric = $request->get('metric') ?: 'wt';
346
        $threshold = (float)$request->get('threshold') ?: 0.01;
347
        $callgraph = $profile->getCallgraphNodes($metric, $threshold);
0 ignored issues
show
Bug introduced by
The method getCallgraphNodes() does not exist on Xhgui_Profile. Did you maybe mean getCallgraph()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
348
349
        $response['Content-Type'] = 'application/json';
350
        return $response->body(json_encode($callgraph));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
351
    }
352
}
353