WaterfallController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 31 3
A query() 0 29 3
1
<?php
2
3
namespace XHGui\Controller;
4
5
use Slim\Http\Request;
6
use Slim\Http\Response;
7
use Slim\Slim as App;
8
use XHGui\AbstractController;
9
use XHGui\Options\SearchOptions;
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(): void
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(new SearchOptions([
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->render('waterfall/list.twig', [
51
            'runs' => $result['results'],
52
            'search' => $search,
53
            'paging' => $paging,
54
            'base_url' => 'waterfall.list',
55
        ]);
56
    }
57
58
    public function query(Request $request, Response $response): void
59
    {
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(new SearchOptions([
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