Completed
Push — master ( c564cb...34935e )
by Mark
02:40
created

Xhgui_Controller_Run::import()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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