summaryrefslogtreecommitdiffstats
path: root/library/Graphite/Web/Widget/IcingadbGraphs.php
blob: e038e92d243f26066fe5afc8e42db48548843f44 (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
<?php

/* Icinga Graphite Web | (c) 2022 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Graphite\Web\Widget;

use Icinga\Data\Filter\Filter;
use Icinga\Module\Graphite\Web\Widget\Graphs\Icingadb\IcingadbHost;
use Icinga\Module\Graphite\Web\Widget\Graphs\Icingadb\IcingadbService;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Widget\EmptyState;
use Icinga\Module\Icingadb\Model\Host;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Orm\ResultSet;
use ipl\Stdlib\BaseFilter;
use ipl\Web\Filter\QueryString;
use ipl\Web\Widget\Link;

/**
* Class for creating graphs of icingadb objects
*/
class IcingadbGraphs extends BaseHtmlElement
{
    use BaseFilter;

    protected $defaultAttributes = ['class' => 'grid'];

    /** @var Iterable */
    protected $objects;

    protected $tag = 'div';

    /**
     * Create a new Graph item
     *
     * @param ResultSet $objects
     */
    public function __construct(ResultSet $objects)
    {
        $this->objects = $objects;
    }

    protected function assemble()
    {
        if (! $this->objects->hasResult()) {
            $this->add(new EmptyState(t('No items found.')));
        }

        foreach ($this->objects as $object) {
            $this->add($this->createGridItem($object));
        }

        $document = new HtmlDocument();
        $document->addHtml(Html::tag('div', ['class' => 'graphite-graph-color-registry']), $this);
        $this->prependWrapper($document);
    }

    protected function createGridItem($object)
    {
        if ($object instanceof Host) {
            $graph = new IcingadbHost($object);
            $hostObj = $object;
        } else {
            $graph = new IcingadbService($object);
            $hostObj = $object->host;
        }

        $hostUrl = Links::host($hostObj);

        if ($this->hasBaseFilter()) {
            $hostUrl->addFilter(Filter::fromQueryString(QueryString::render($this->getBaseFilter())));
        }

        $hostLink =  new Link(
            $graph->createHostTitle(),
            $hostUrl,
            ['data-base-target' => '_next']
        );

        $serviceLink = null;
        if ($graph->getObjectType() === 'service') {
            $serviceUrl = Links::service($object, $hostObj);

            if ($this->hasBaseFilter()) {
                $serviceUrl->addFilter(Filter::fromQueryString(QueryString::render($this->getBaseFilter())));
            }

            $serviceLink = new Link(
                $graph->createServiceTitle(),
                $serviceUrl,
                ['data-base-target' => '_next']
            );
        }

        $gridItem = Html::tag('div', ['class' => 'grid-item']);
        $header = Html::tag('h2');

        $header->add([$hostLink, $serviceLink]);
        $gridItem->add($header);

        return $gridItem->add(HtmlString::create($graph->setPreloadDummy()->handleRequest()));
    }
}