Completed
Pull Request — master (#257)
by
unknown
01:18
created

Xhgui_Controller_Run::deleteSubmit()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 2
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 deleteForm()
112
    {
113
        $request = $this->app->request();
114
        $id = $request->get('id');
115
        if (!is_string($id) || !strlen($id)) {
116
            throw new Exception('The "id" parameter is required.');
117
        }
118
119
        // Get details
120
        $result = $this->profiles->get($id);
121
122
        $this->_template = 'runs/delete-form.twig';
123
        $this->set(array(
124
            'run_id' => $id,
125
            'result' => $result,
126
        ));
127
    }
128
129
    public function deleteSubmit()
130
    {
131
        $request = $this->app->request();
132
        $id = $request->post('id');
133
        // Don't call profilers->delete() unless $id is set,
134
        // otherwise it will turn the null into a MongoId and return "Sucessful".
135
        if (!is_string($id) || !strlen($id)) {
136
            // Form checks this already,
137
            // only reachable by handcrafted or malformed requests.
138
            throw new Exception('The "id" parameter is required.');
139
        }
140
141
        // Delete the profile run.
142
        $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...
143
144
        $this->app->flash('success', 'Deleted profile ' . $id);
145
146
        $this->app->redirect($this->app->urlFor('home'));
147
    }
148
149
    public function deleteAllForm()
150
    {
151
        $this->_template = 'runs/delete-all-form.twig';
152
    }
153
154
    public function deleteAllSubmit()
155
    {
156
        $request = $this->app->request();
0 ignored issues
show
Unused Code introduced by
$request 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...
157
158
        // Delete all profile runs.
159
        $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...
160
161
        $this->app->flash('success', 'Deleted all profiles');
162
163
        $this->app->redirect($this->app->urlFor('home'));
164
    }
165
166
    public function url()
167
    {
168
        $request = $this->app->request();
169
        $pagination = array(
170
            'sort' => $request->get('sort'),
171
            'direction' => $request->get('direction'),
172
            'page' => $request->get('page'),
173
            'perPage' => $this->app->config('page.limit'),
174
        );
175
176
        $search = array();
177
        $keys = array('date_start', 'date_end', 'limit', 'limit_custom');
178
        foreach ($keys as $key) {
179
            $search[$key] = $request->get($key);
180
        }
181
182
        $runs = $this->profiles->getForUrl(
183
            $request->get('url'),
184
            $pagination,
185
            $search
186
        );
187
188
        if (isset($search['limit_custom']) && strlen($search['limit_custom']) > 0 && $search['limit_custom'][0] == 'P') {
189
            $search['limit'] = $search['limit_custom'];
190
        }
191
192
        $chartData = $this->profiles->getPercentileForUrl(
193
            90,
194
            $request->get('url'),
195
            $search
196
        );
197
198
        $paging = array(
199
            'total_pages' => $runs['totalPages'],
200
            'sort' => $pagination['sort'],
201
            'page' => $runs['page'],
202
            'direction' => $runs['direction']
203
        );
204
205
        $this->_template = 'runs/url.twig';
206
        $this->set(array(
207
            'paging' => $paging,
208
            'base_url' => 'url.view',
209
            'runs' => $runs['results'],
210
            'url' => $request->get('url'),
211
            'chart_data' => $chartData,
212
            'date_format' => $this->app->config('date.format'),
213
            'search' => array_merge($search, array('url' => $request->get('url'))),
214
        ));
215
    }
216
217
    public function compare()
218
    {
219
        $request = $this->app->request();
220
221
        $baseRun = $headRun = $candidates = $comparison = null;
222
        $paging = array();
223
224
        if ($request->get('base')) {
225
            $baseRun = $this->profiles->get($request->get('base'));
226
        }
227
228
        if ($baseRun && !$request->get('head')) {
229
            $pagination = array(
230
                'direction' => $request->get('direction'),
231
                'sort' => $request->get('sort'),
232
                'page' => $request->get('page'),
233
                'perPage' => $this->app->config('page.limit'),
234
            );
235
            $candidates = $this->profiles->getForUrl(
236
                $baseRun->getMeta('simple_url'),
237
                $pagination
238
            );
239
240
            $paging = array(
241
                'total_pages' => $candidates['totalPages'],
242
                'sort' => $pagination['sort'],
243
                'page' => $candidates['page'],
244
                'direction' => $candidates['direction']
245
            );
246
        }
247
248
        if ($request->get('head')) {
249
            $headRun = $this->profiles->get($request->get('head'));
250
        }
251
252
        if ($baseRun && $headRun) {
253
            $comparison = $baseRun->compare($headRun);
254
        }
255
256
        $this->_template = 'runs/compare.twig';
257
        $this->set(array(
258
            'base_url' => 'run.compare',
259
            'base_run' => $baseRun,
260
            'head_run' => $headRun,
261
            'candidates' => $candidates,
262
            'url_params' => $request->get(),
263
            'date_format' => $this->app->config('date.format'),
264
            'comparison' => $comparison,
265
            'paging' => $paging,
266
            'search' => array(
267
                'base' => $request->get('base'),
268
                'head' => $request->get('head'),
269
            )
270
        ));
271
    }
272
273
    public function symbol()
274
    {
275
        $request = $this->app->request();
276
        $id = $request->get('id');
277
        $symbol = $request->get('symbol');
278
279
        $profile = $this->profiles->get($id);
280
        $profile->calculateSelf();
281
        list($parents, $current, $children) = $profile->getRelatives($symbol);
282
283
        $this->_template = 'runs/symbol.twig';
284
        $this->set(array(
285
            'symbol' => $symbol,
286
            'id' => $id,
287
            'main' => $profile->get('main()'),
288
            'parents' => $parents,
289
            'current' => $current,
290
            'children' => $children,
291
        ));
292
    }
293
294
    public function symbolShort()
295
    {
296
        $request = $this->app->request();
297
        $id = $request->get('id');
298
        $threshold = $request->get('threshold');
299
        $symbol = $request->get('symbol');
300
        $metric = $request->get('metric');
301
302
        $profile = $this->profiles->get($id);
303
        $profile->calculateSelf();
304
        list($parents, $current, $children) = $profile->getRelatives($symbol, $metric, $threshold);
305
306
        $this->_template = 'runs/symbol-short.twig';
307
        $this->set(array(
308
            'symbol' => $symbol,
309
            'id' => $id,
310
            'main' => $profile->get('main()'),
311
            'parents' => $parents,
312
            'current' => $current,
313
            'children' => $children,
314
        ));
315
    }
316
317
    public function callgraph()
318
    {
319
        $request = $this->app->request();
320
        $profile = $this->profiles->get($request->get('id'));
321
322
        $this->_template = 'runs/callgraph.twig';
323
        $this->set(array(
324
            'profile' => $profile,
325
            'date_format' => $this->app->config('date.format'),
326
        ));
327
    }
328
329 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...
330
    {
331
        $request = $this->app->request();
332
        $response = $this->app->response();
333
        $profile = $this->profiles->get($request->get('id'));
334
        $metric = $request->get('metric') ?: 'wt';
335
        $threshold = (float)$request->get('threshold') ?: 0.01;
336
        $callgraph = $profile->getCallgraph($metric, $threshold);
337
338
        $response['Content-Type'] = 'application/json';
339
        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...
340
    }
341
342 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...
343
    {
344
        $request = $this->app->request();
345
        $response = $this->app->response();
346
        $profile = $this->profiles->get($request->get('id'));
347
        $metric = $request->get('metric') ?: 'wt';
348
        $threshold = (float)$request->get('threshold') ?: 0.01;
349
        $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...
350
351
        $response['Content-Type'] = 'application/json';
352
        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...
353
    }
354
}
355