summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/BasketSnapshotTable.php
blob: 08f808ab094253dc06236ac942c54b126e172722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

namespace Icinga\Module\Director\Web\Table;

use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Icinga\Date\DateFormatter;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\DirectorObject\Automation\Basket;
use RuntimeException;

class BasketSnapshotTable extends ZfQueryBasedTable
{
    use DbHelper;

    protected $searchColumns = [
        'basket_name',
        'summary'
    ];

    /** @var Basket */
    protected $basket;

    public function setBasket(Basket $basket)
    {
        $this->basket = $basket;
        $this->searchColumns = [];

        return $this;
    }

    public function renderRow($row)
    {
        $this->splitByDay($row->ts_create_seconds);
        $link = $this->linkToSnapshot($this->renderSummary($row->summary), $row);

        if ($this->basket === null) {
            $columns = [
                [
                    new Link(
                        Html::tag('strong', $row->basket_name),
                        'director/basket',
                        ['name' => $row->basket_name]
                    ),
                    Html::tag('br'),
                    $link,
                ],
                DateFormatter::formatTime($row->ts_create / 1000),
            ];
        } else {
            $columns = [
                $link,
                DateFormatter::formatTime($row->ts_create / 1000),
            ];
        }
        return $this::row($columns);
    }

    protected function renderSummary($summary)
    {
        $summary = Json::decode($summary);
        if ($summary === null) {
            return '-';
        }
        $result = [];
        if (! is_object($summary) && ! is_array($summary)) {
            throw new RuntimeException(sprintf(
                'Got invalid basket summary: %s ',
                var_export($summary, 1)
            ));
        }

        foreach ($summary as $type => $count) {
            $result[] = sprintf(
                '%dx %s',
                $count,
                $type
            );
        }

        if (empty($result)) {
            return '-';
        }

        return implode(', ', $result);
    }

    protected function linkToSnapshot($caption, $row)
    {
        return new Link($caption, 'director/basket/snapshot', [
            'checksum' => bin2hex($this->wantBinaryValue($row->content_checksum)),
            'ts'       => $row->ts_create,
            'name'     => $row->basket_name,
        ]);
    }

    public function prepareQuery()
    {
        $query = $this->db()->select()->from([
            'b' => 'director_basket'
        ], [
            'b.uuid',
            'b.basket_name',
            'bs.ts_create',
            'ts_create_seconds' => '(bs.ts_create / 1000)',
            'bs.content_checksum',
            'bc.summary',
        ])->join(
            ['bs' => 'director_basket_snapshot'],
            'bs.basket_uuid = b.uuid',
            []
        )->join(
            ['bc' => 'director_basket_content'],
            'bc.checksum = bs.content_checksum',
            []
        )->order('bs.ts_create DESC');

        if ($this->basket !== null) {
            $query->where('b.uuid = ?', $this->quoteBinary($this->basket->get('uuid')));
        }

        return $query;
    }
}