Completed
Pull Request — master (#238)
by
unknown
03:20
created

Xhgui_Controller_Run::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
     * @var Xhgui_Profiles
9
     */
10
    private $profiles;
11
12
    /**
13
     * @var Xhgui_WatchFunctions
14
     */
15
    private $watches;
16
17
    public function __construct(Slim $app, Xhgui_Profiles $profiles, Xhgui_WatchFunctions $watches)
18
    {
19
        $this->app = $app;
20
        $this->profiles = $profiles;
21
        $this->watches = $watches;
22
    }
23
24
    public function index()
25
    {
26
        $request = $this->app->request();
27
28
        $search = array();
29
        $keys = array('date_start', 'date_end', 'url');
30
        foreach ($keys as $key) {
31
            if ($request->get($key)) {
32
                $search[$key] = $request->get($key);
33
            }
34
        }
35
        $sort = $request->get('sort');
36
37
        $result = $this->profiles->getAll(array(
38
            'sort' => $sort,
39
            'page' => $request->get('page'),
40
            'direction' => $request->get('direction'),
41
            'perPage' => $this->app->config('page.limit'),
42
            'conditions' => $search,
43
            'projection' => true,
44
        ));
45
46
        $title = 'Recent runs';
47
        $titleMap = array(
48
            'wt' => 'Longest wall time',
49
            'cpu' => 'Most CPU time',
50
            'mu' => 'Highest memory use',
51
        );
52
        if (isset($titleMap[$sort])) {
53
            $title = $titleMap[$sort];
54
        }
55
56
        $paging = array(
57
            'total_pages' => $result['totalPages'],
58
            'page' => $result['page'],
59
            'sort' => $sort,
60
            'direction' => $result['direction']
61
        );
62
63
        $this->_template = 'runs/list.twig';
64
        $this->set(array(
65
            'paging' => $paging,
66
            'base_url' => 'home',
67
            'runs' => $result['results'],
68
            'date_format' => $this->app->config('date.format'),
69
            'search' => $search,
70
            'has_search' => strlen(implode('', $search)) > 0,
71
            'title' => $title
72
        ));
73
    }
74
75
    public function view()
76
    {
77
        $request = $this->app->request();
78
        $detailCount = $this->app->config('detail.count');
79
        $result = $this->profiles->get($request->get('id'));
80
81
        $result->calculateSelf();
82
83
        // Self wall time graph
84
        $timeChart = $result->extractDimension('ewt', $detailCount);
85
86
        // Memory Block
87
        $memoryChart = $result->extractDimension('emu', $detailCount);
88
89
        // Watched Functions Block
90
        $watchedFunctions = array();
91
        foreach ($this->watches->getAll() as $watch) {
92
            $matches = $result->getWatched($watch['name']);
93
            if ($matches) {
94
                $watchedFunctions = array_merge($watchedFunctions, $matches);
95
            }
96
        }
97
98
        $profile = $result->sort('ewt', $result->getProfile());
99
100
        $this->_template = 'runs/view.twig';
101
        $this->set(array(
102
            'profile' => $profile,
103
            'result' => $result,
104
            'wall_time' => $timeChart,
105
            'memory' => $memoryChart,
106
            'watches' => $watchedFunctions,
107
            'date_format' => $this->app->config('date.format'),
108
        ));
109
    }
110
111
    public function delete()
112
    {
113
        $request = $this->app->request();
114
        $id = $request->get('id');
115
116
        $delete = $this->profiles->delete($id);
0 ignored issues
show
Unused Code introduced by
$delete is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
117
118
        $this->_template = 'runs/delete.twig';
119
        $this->set(array(
120
          'id' => $id,
121
        ));
122
    }
123
124
    public function url()
125
    {
126
        $request = $this->app->request();
127
        $pagination = array(
128
            'sort' => $request->get('sort'),
129
            'direction' => $request->get('direction'),
130
            'page' => $request->get('page'),
131
            'perPage' => $this->app->config('page.limit'),
132
        );
133
134
        $search = array();
135
        $keys = array('date_start', 'date_end', 'limit', 'limit_custom');
136
        foreach ($keys as $key) {
137
            $search[$key] = $request->get($key);
138
        }
139
140
        $runs = $this->profiles->getForUrl(
141
            $request->get('url'),
142
            $pagination,
143
            $search
144
        );
145
146
        if (isset($search['limit_custom']) && strlen($search['limit_custom']) > 0 && $search['limit_custom'][0] == 'P') {
147
            $search['limit'] = $search['limit_custom'];
148
        }
149
150
        $chartData = $this->profiles->getPercentileForUrl(
151
            90,
152
            $request->get('url'),
153
            $search
154
        );
155
156
        $paging = array(
157
            'total_pages' => $runs['totalPages'],
158
            'sort' => $pagination['sort'],
159
            'page' => $runs['page'],
160
            'direction' => $runs['direction']
161
        );
162
163
        $this->_template = 'runs/url.twig';
164
        $this->set(array(
165
            'paging' => $paging,
166
            'base_url' => 'url.view',
167
            'runs' => $runs['results'],
168
            'url' => $request->get('url'),
169
            'chart_data' => $chartData,
170
            'date_format' => $this->app->config('date.format'),
171
            'search' => array_merge($search, array('url' => $request->get('url'))),
172
        ));
173
    }
174
175
    public function compare()
176
    {
177
        $request = $this->app->request();
178
179
        $baseRun = $headRun = $candidates = $comparison = null;
180
        $paging = array();
181
182
        if ($request->get('base')) {
183
            $baseRun = $this->profiles->get($request->get('base'));
184
        }
185
186
        if ($baseRun && !$request->get('head')) {
187
            $pagination = array(
188
                'direction' => $request->get('direction'),
189
                'sort' => $request->get('sort'),
190
                'page' => $request->get('page'),
191
                'perPage' => $this->app->config('page.limit'),
192
            );
193
            $candidates = $this->profiles->getForUrl(
194
                $baseRun->getMeta('simple_url'),
195
                $pagination
196
            );
197
198
            $paging = array(
199
                'total_pages' => $candidates['totalPages'],
200
                'sort' => $pagination['sort'],
201
                'page' => $candidates['page'],
202
                'direction' => $candidates['direction']
203
            );
204
        }
205
206
        if ($request->get('head')) {
207
            $headRun = $this->profiles->get($request->get('head'));
208
        }
209
210
        if ($baseRun && $headRun) {
211
            $comparison = $baseRun->compare($headRun);
212
        }
213
214
        $this->_template = 'runs/compare.twig';
215
        $this->set(array(
216
            'base_url' => 'run.compare',
217
            'base_run' => $baseRun,
218
            'head_run' => $headRun,
219
            'candidates' => $candidates,
220
            'url_params' => $request->get(),
221
            'date_format' => $this->app->config('date.format'),
222
            'comparison' => $comparison,
223
            'paging' => $paging,
224
            'search' => array(
225
                'base' => $request->get('base'),
226
                'head' => $request->get('head'),
227
            )
228
        ));
229
    }
230
231
    public function symbol()
232
    {
233
        $request = $this->app->request();
234
        $id = $request->get('id');
235
        $symbol = $request->get('symbol');
236
237
        $profile = $this->profiles->get($id);
238
        $profile->calculateSelf();
239
        list($parents, $current, $children) = $profile->getRelatives($symbol);
240
241
        $this->_template = 'runs/symbol.twig';
242
        $this->set(array(
243
            'symbol' => $symbol,
244
            'id' => $id,
245
            'main' => $profile->get('main()'),
246
            'parents' => $parents,
247
            'current' => $current,
248
            'children' => $children,
249
        ));
250
    }
251
252
    public function symbolShort()
253
    {
254
        $request = $this->app->request();
255
        $id = $request->get('id');
256
        $threshold = $request->get('threshold');
257
        $symbol = $request->get('symbol');
258
        $metric = $request->get('metric');
259
260
        $profile = $this->profiles->get($id);
261
        $profile->calculateSelf();
262
        list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);
263
264
        $this->_template = 'runs/symbol-short.twig';
265
        $this->set(array(
266
            'symbol' => $symbol,
267
            'id' => $id,
268
            'main' => $profile->get('main()'),
269
            'parents' => $parents,
270
            'current' => $current,
271
            'children' => $children,
272
        ));
273
    }
274
275 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...
276
    {
277
        $request = $this->app->request();
278
        $profile = $this->profiles->get($request->get('id'));
279
280
        $this->_template = 'runs/callgraph.twig';
281
        $this->set(array(
282
            'profile' => $profile,
283
            'date_format' => $this->app->config('date.format'),
284
        ));
285
    }
286
287 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...
288
    {
289
        $request = $this->app->request();
290
        $response = $this->app->response();
291
        $profile = $this->profiles->get($request->get('id'));
292
        $metric = $request->get('metric') ?: 'wt';
293
        $threshold = (float)$request->get('threshold') ?: 0.01;
294
        $callgraph = $profile->getCallgraph($metric, $threshold);
295
296
        $response['Content-Type'] = 'application/json';
297
        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...
298
    }
299
300 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...
301
    {
302
        $request = $this->app->request();
303
        $profile = $this->profiles->get($request->get('id'));
304
305
        $this->_template = 'runs/flamegraph.twig';
306
        $this->set(array(
307
            'profile' => $profile,
308
            'date_format' => $this->app->config('date.format'),
309
        ));
310
    }
311
312 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...
313
    {
314
        $request = $this->app->request();
315
        $response = $this->app->response();
316
        $profile = $this->profiles->get($request->get('id'));
317
        $metric = $request->get('metric') ?: 'wt';
318
        $threshold = (float)$request->get('threshold') ?: 0.01;
319
        $flamegraph = $profile->getFlamegraph($metric, $threshold);
320
321
        $response['Content-Type'] = 'application/json';
322
        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...
323
    }
324
325 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...
326
    {
327
        $request = $this->app->request();
328
        $response = $this->app->response();
329
        $profile = $this->profiles->get($request->get('id'));
330
        $metric = $request->get('metric') ?: 'wt';
331
        $threshold = (float)$request->get('threshold') ?: 0.01;
332
        $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...
333
334
        $response['Content-Type'] = 'application/json';
335
        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...
336
    }
337
}
338