Completed
Push — master ( 0f10bf...9b4bb8 )
by Mark
01:27
created

Xhgui_Controller_Run::symbol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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