Completed
Pull Request — master (#333)
by Elan
01:50 queued 35s
created

WaterfallController::index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace XHGui\Controller;
4
5
use Slim\App;
6
use Slim\Http\Request;
7
use Slim\Http\Response;
8
use Slim\Slim as App;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Slim\Slim as App because the name is already in use
Loading history...
9
use XHGui\AbstractController;
10
use XHGui\Profile;
11
use XHGui\Searcher\SearcherInterface;
12
13
class WaterfallController extends AbstractController
14
{
15
    /**
16
     * @var SearcherInterface
17
     */
18
    protected $searcher;
19
20
    public function __construct(App $app, SearcherInterface $searcher)
21
    {
22
        parent::__construct($app);
23
        $this->searcher = $searcher;
24
    }
25
26
    public function index()
27
    {
28
        $request = $this->app->request();
29
        $search = [];
30
        $keys = ['remote_addr', 'request_start', 'request_end'];
31
        foreach ($keys as $key) {
32
            if ($request->get($key)) {
33
                $search[$key] = trim($request->get($key));
34
            }
35
        }
36
        $result = $this->searcher->getAll([
37
            'sort' => 'time',
38
            'direction' => 'asc',
39
            'conditions' => $search,
40
            'projection' => true,
41
        ]);
42
43
        $paging = [
44
            'total_pages' => $result['totalPages'],
45
            'page' => $result['page'],
46
            'sort' => 'asc',
47
            'direction' => $result['direction'],
48
        ];
49
50
        $this->_template = 'waterfall/list.twig';
51
        $this->set([
52
            'runs' => $result['results'],
53
            'search' => $search,
54
            'paging' => $paging,
55
            'base_url' => 'waterfall.list',
56
        ]);
57
    }
58
59
    public function query(Request $request, Response $response)
60
    {
61
        $search = [];
62
        $keys = ['remote_addr', 'request_start', 'request_end'];
63
        foreach ($keys as $key) {
64
            $search[$key] = $request->get($key);
65
        }
66
        $result = $this->searcher->getAll([
67
            'sort' => 'time',
68
            'direction' => 'asc',
69
            'conditions' => $search,
70
            'projection' => true,
71
        ]);
72
        $datas = [];
73
        /** @var Profile $r */
74
        foreach ($result['results'] as $r) {
75
            $duration = $r->get('main()', 'wt');
76
            $start = $r->getMeta('SERVER.REQUEST_TIME_FLOAT');
77
            $title = $r->getMeta('url');
78
            $datas[] = [
79
                'id' => $r->getId(),
80
                'title' => $title,
81
                'start' => $start * 1000,
82
                'duration' => $duration / 1000, // Convert to correct scale
83
            ];
84
        }
85
        $response->body(json_encode($datas));
86
        $response['Content-Type'] = 'application/json';
87
    }
88
}
89