summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/Detail/ObjectStatistics.php
blob: 477bf5faa0e8eb824051d5de53e9f4eff779b11e (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
<?php

/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Widget\Detail;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\ValidHtml;
use ipl\Stdlib\BaseFilter;

abstract class ObjectStatistics extends BaseHtmlElement
{
    use BaseFilter;

    protected $tag = 'ul';

    protected $defaultAttributes = ['class' => 'object-statistics'];

    abstract protected function createDonut(): ValidHtml;

    abstract protected function createTotal(): ValidHtml;

    abstract protected function createBadges(): ValidHtml;

    /**
     * Shorten the given amount to 4 characters max
     *
     * @param int $amount
     *
     * @return string
     */
    protected function shortenAmount(int $amount): string
    {
        if ($amount < 10000) {
            return (string) $amount;
        }

        if ($amount < 999500) {
            return sprintf('%dk', round($amount / 1000.0));
        }

        if ($amount < 9959000) {
            return sprintf('%.1fM', $amount / 1000000.0);
        }

        // I think we can rule out amounts over 1 Billion
        return sprintf('%dM', $amount / 1000000.0);
    }

    protected function assemble()
    {
        $this->add([
            Html::tag('li', ['class' => 'object-statistics-graph'], $this->createDonut()),
            Html::tag('li', ['class' => ['object-statistics-total', 'text-center']], $this->createTotal()),
            Html::tag('li', $this->createBadges())
        ]);
    }
}