summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/CustomvarVariantsTable.php
blob: 80fca7050213296894f128133b30d424b244ff64 (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 Icinga\Module\Director\Db;
use Icinga\Module\Director\PlainObjectRenderer;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Zend_Db_Adapter_Abstract as ZfDbAdapter;
use Zend_Db_Select as ZfDbSelect;

class CustomvarVariantsTable extends ZfQueryBasedTable
{
    protected $searchColumns = ['varvalue'];

    protected $varName;

    public static function create(Db $db, $varName)
    {
        $table = new static($db);
        $table->varName = $varName;
        $table->getAttributes()->set('class', 'common-table');
        return $table;
    }

    public function renderRow($row)
    {
        if ($row->format === 'json') {
            $value = PlainObjectRenderer::render(json_decode($row->varvalue));
        } else {
            $value = $row->varvalue;
        }
        $tr = $this::row([
            /* new Link(
                $value,
                'director/customvar/value',
                ['name' => $row->varvalue]
            )*/
            $value
        ]);

        foreach ($this->getObjectTypes() as $type) {
            $cnt = (int) $row->{"cnt_$type"};
            if ($cnt === 0) {
                $cnt = '-';
            }
            $tr->add($this::td($cnt));
        }

        return $tr;
    }

    public function getColumnsToBeRendered()
    {
        return array(
            $this->translate('Variable Value'),
            $this->translate('Commands'),
            $this->translate('Hosts'),
            $this->translate('Services'),
            $this->translate('Service Sets'),
            $this->translate('Notifications'),
            $this->translate('Users'),
        );
    }

    protected function getObjectTypes()
    {
        return ['command', 'host', 'service', 'service_set', 'notification', 'user'];
    }

    public function prepareQuery()
    {
        $db = $this->db();
        $varsColumns = ['varvalue' => 'v.varvalue'];
        $varsTypes = $this->getObjectTypes();
        foreach ($varsTypes as $type) {
            $varsColumns["cnt_$type"] = '(0)';
        }
        $varsQueries = [];
        foreach ($varsTypes as $type) {
            $varsQueries[] = $this->makeVarSub($type, $varsColumns, $db);
        }

        $union = $db->select()->union($varsQueries, ZfDbSelect::SQL_UNION_ALL);

        $columns = [
            'varvalue' => 'u.varvalue',
            'format'   => 'u.format',
        ];
        foreach ($varsTypes as $column) {
            $columns["cnt_$column"] = "SUM(u.cnt_$column)";
        }
        return $db->select()->from(['u' => $union], $columns)
            ->group('u.varvalue')->group('u.format')
            ->order('u.varvalue ASC')
            ->order('u.format ASC')
            ->limit(100);
    }

    /**
     * @param string $type
     * @param array $columns
     * @param ZfDbAdapter $db
     * @return ZfDbSelect
     */
    protected function makeVarSub($type, array $columns, ZfDbAdapter $db)
    {
        $columns["cnt_$type"] = 'COUNT(*)';
        $columns['format'] = 'v.format';
        return $db->select()->from(
            ['v' => "icinga_${type}_var"],
            $columns
        )->join(
            ['o' => "icinga_${type}"],
            "o.id = v.${type}_id",
            []
        )->where(
            'v.varname = ?',
            $this->varName
        )->where(
            'o.object_type != ?',
            'external_object'
        )->group('varvalue')->group('v.format');
    }
}