Completed
Pull Request — master (#331)
by Elan
01:21
created

WaterfallController::query()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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