Completed
Push — master ( 8e1e89...bfefcf )
by Mark
10s
created

Xhgui_Controller_Run::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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