Completed
Pull Request — master (#230)
by
unknown
07:02
created

Xhgui_Controller_Run::compare()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 7.8235
c 0
b 0
f 0
cc 7
eloc 37
nc 16
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
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...
4
{
5
    /**
6
     * @var \Slim\Slim
7
     */
8
    protected $_app;
9
10
    /**
11
     * @var Xhgui_Profiles
12
     */
13
    private $_profiles;
14
15
    /**
16
     * @var Xhgui_WatchFunctions
17
     */
18
    private $_watches;
19
20
    public function __construct($app, $profiles, $watches)
21
    {
22
        $this->_app = $app;
23
        $this->_profiles = $profiles;
24
        $this->_watches = $watches;
25
    }
26
27
    public function index()
28
    {
29
        $request = $this->_app->request();
30
31
        $search = array();
32
        $keys = array('date_start', 'date_end', 'url');
33
        foreach ($keys as $key) {
34
            if ($request->get($key)) {
35
                $search[$key] = $request->get($key);
36
            }
37
        }
38
        $sort = $request->get('sort');
39
40
        $result = $this->_profiles->getAll(array(
41
            'sort' => $sort,
42
            'page' => $request->get('page'),
43
            'direction' => $request->get('direction'),
44
            'perPage' => $this->_app->config('page.limit'),
45
            'conditions' => $search,
46
            'projection' => true,
47
        ));
48
49
        $title = 'Recent runs';
50
        $titleMap = array(
51
            'wt' => 'Longest wall time',
52
            'cpu' => 'Most CPU time',
53
            'mu' => 'Highest memory use',
54
        );
55
        if (isset($titleMap[$sort])) {
56
            $title = $titleMap[$sort];
57
        }
58
59
        $paging = array(
60
            'total_pages' => $result['totalPages'],
61
            'page' => $result['page'],
62
            'sort' => $sort,
63
            'direction' => $result['direction']
64
        );
65
66
        $this->_template = 'runs/list.twig';
67
        $this->set(array(
68
            'paging' => $paging,
69
            'base_url' => 'home',
70
            'runs' => $result['results'],
71
            'date_format' => $this->_app->config('date.format'),
72
            'search' => $search,
73
            'has_search' => strlen(implode('', $search)) > 0,
74
            'title' => $title
75
        ));
76
    }
77
78
    public function view()
79
    {
80
        $request = $this->_app->request();
81
        $detailCount = $this->_app->config('detail.count');
82
        $result = $this->_profiles->get($request->get('id'));
83
84
        $result->calculateSelf();
85
86
        // Self wall time graph
87
        $timeChart = $result->extractDimension('ewt', $detailCount);
88
89
        // Memory Block
90
        $memoryChart = $result->extractDimension('emu', $detailCount);
91
92
        // Watched Functions Block
93
        $watchedFunctions = array();
94
        foreach ($this->_watches->getAll() as $watch) {
95
            $matches = $result->getWatched($watch['name']);
96
            if ($matches) {
97
                $watchedFunctions = array_merge($watchedFunctions, $matches);
98
            }
99
        }
100
101
        $profile = $result->sort('ewt', $result->getProfile());
102
103
        $this->_template = 'runs/view.twig';
104
        $this->set(array(
105
            'profile' => $profile,
106
            'result' => $result,
107
            'wall_time' => $timeChart,
108
            'memory' => $memoryChart,
109
            'watches' => $watchedFunctions,
110
            'date_format' => $this->_app->config('date.format'),
111
        ));
112
    }
113
114
    public function url()
115
    {
116
        $request = $this->_app->request();
117
        $pagination = array(
118
            'sort' => $request->get('sort'),
119
            'direction' => $request->get('direction'),
120
            'page' => $request->get('page'),
121
            'perPage' => $this->_app->config('page.limit'),
122
        );
123
124
        $search = array();
125
        $keys = array('date_start', 'date_end', 'limit', 'limit_custom');
126
        foreach ($keys as $key) {
127
            $search[$key] = $request->get($key);
128
        }
129
130
        $runs = $this->_profiles->getForUrl(
131
            $request->get('url'),
132
            $pagination,
133
            $search
134
        );
135
136
        if (isset($search['limit_custom']) && strlen($search['limit_custom']) > 0 && $search['limit_custom'][0] == 'P') {
137
            $search['limit'] = $search['limit_custom'];
138
        }
139
140
        $chartData = $this->_profiles->getPercentileForUrl(
141
            90,
142
            $request->get('url'),
143
            $search
144
        );
145
146
        $paging = array(
147
            'total_pages' => $runs['totalPages'],
148
            'sort' => $pagination['sort'],
149
            'page' => $runs['page'],
150
            'direction' => $runs['direction']
151
        );
152
153
        $this->_template = 'runs/url.twig';
154
        $this->set(array(
155
            'paging' => $paging,
156
            'base_url' => 'url.view',
157
            'runs' => $runs['results'],
158
            'url' => $request->get('url'),
159
            'chart_data' => $chartData,
160
            'date_format' => $this->_app->config('date.format'),
161
            'search' => array_merge($search, array('url' => $request->get('url'))),
162
        ));
163
    }
164
165
    public function compare()
166
    {
167
        $request = $this->_app->request();
168
169
        $baseRun = $headRun = $candidates = $comparison = null;
170
        $paging = array();
171
172
        if ($request->get('base')) {
173
            $baseRun = $this->_profiles->get($request->get('base'));
174
        }
175
176
        if ($baseRun && !$request->get('head')) {
177
            $pagination = array(
178
                'direction' => $request->get('direction'),
179
                'sort' => $request->get('sort'),
180
                'page' => $request->get('page'),
181
                'perPage' => $this->_app->config('page.limit'),
182
            );
183
            $candidates = $this->_profiles->getForUrl(
184
                $baseRun->getMeta('simple_url'),
185
                $pagination
186
            );
187
188
            $paging = array(
189
                'total_pages' => $candidates['totalPages'],
190
                'sort' => $pagination['sort'],
191
                'page' => $candidates['page'],
192
                'direction' => $candidates['direction']
193
            );
194
        }
195
196
        if ($request->get('head')) {
197
            $headRun = $this->_profiles->get($request->get('head'));
198
        }
199
200
        if ($baseRun && $headRun) {
201
            $comparison = $baseRun->compare($headRun);
202
        }
203
204
        $this->_template = 'runs/compare.twig';
205
        $this->set(array(
206
            'base_url' => 'run.compare',
207
            'base_run' => $baseRun,
208
            'head_run' => $headRun,
209
            'candidates' => $candidates,
210
            'url_params' => $request->get(),
211
            'date_format' => $this->_app->config('date.format'),
212
            'comparison' => $comparison,
213
            'paging' => $paging,
214
            'search' => array(
215
                'base' => $request->get('base'),
216
                'head' => $request->get('head'),
217
            )
218
        ));
219
    }
220
221
    public function symbol()
222
    {
223
        $request = $this->_app->request();
224
        $id = $request->get('id');
225
        $symbol = $request->get('symbol');
226
227
        $profile = $this->_profiles->get($id);
228
        $profile->calculateSelf();
229
        list($parents, $current, $children) = $profile->getRelatives($symbol);
230
231
        $this->_template = 'runs/symbol.twig';
232
        $this->set(array(
233
            'symbol' => $symbol,
234
            'id' => $id,
235
            'main' => $profile->get('main()'),
236
            'parents' => $parents,
237
            'current' => $current,
238
            'children' => $children,
239
        ));
240
    }
241
242
    public function symbolShort()
243
    {
244
        $request = $this->_app->request();
245
        $id = $request->get('id');
246
        $threshold = $request->get('threshold');
247
        $symbol = $request->get('symbol');
248
        $metric = $request->get('metric');
249
250
        $profile = $this->_profiles->get($id);
251
        $profile->calculateSelf();
252
        list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);
253
254
        $this->_template = 'runs/symbol-short.twig';
255
        $this->set(array(
256
            'symbol' => $symbol,
257
            'id' => $id,
258
            'main' => $profile->get('main()'),
259
            'parents' => $parents,
260
            'current' => $current,
261
            'children' => $children,
262
        ));
263
    }
264
265 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...
266
    {
267
        $request = $this->_app->request();
268
        $profile = $this->_profiles->get($request->get('id'));
269
270
        $this->_template = 'runs/callgraph.twig';
271
        $this->set(array(
272
            'profile' => $profile,
273
            'date_format' => $this->_app->config('date.format'),
274
        ));
275
    }
276
277 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...
278
    {
279
        $request = $this->_app->request();
280
        $response = $this->_app->response();
281
        $profile = $this->_profiles->get($request->get('id'));
282
        $metric = $request->get('metric') ?: 'wt';
283
        $threshold = (float)$request->get('threshold') ?: 0.01;
284
        $callgraph = $profile->getCallgraph($metric, $threshold);
285
286
        $response['Content-Type'] = 'application/json';
287
        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...
288
    }
289
290 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...
291
    {
292
        $request = $this->_app->request();
293
        $profile = $this->_profiles->get($request->get('id'));
294
295
        $this->_template = 'runs/flamegraph.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 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...
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
        $flamegraph = $profile->getFlamegraph($metric, $threshold);
310
311
        $response['Content-Type'] = 'application/json';
312
        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...
313
    }
314
315 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...
316
    {
317
        $request = $this->_app->request();
318
        $response = $this->_app->response();
319
        $profile = $this->_profiles->get($request->get('id'));
320
        $metric = $request->get('metric') ?: 'wt';
321
        $threshold = (float)$request->get('threshold') ?: 0.01;
322
        $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...
323
324
        $response['Content-Type'] = 'application/json';
325
        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...
326
    }
327
328
    public function import()
329
    {
330
        $request = $this->_app->request();
331
332
        $data = json_decode($request->getBody(), true);
333
334
        $container = Xhgui_ServiceContainer::instance();
335
        /* @var Xhgui_Saver_Mongo $saver */
336
        $saver = $container['saverMongo'];
337
338
        $saver->save($data);
339
    }
340
}
341