summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Widget/Detail/DowntimeCard.php
blob: 81f59dab7f9376d76dcff390bf17cde04a6796f8 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php

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

namespace Icinga\Module\Icingadb\Widget\Detail;

use Icinga\Module\Icingadb\Model\Downtime;
use ipl\Html\Attributes;
use ipl\Html\HtmlElement;
use ipl\Web\Compat\StyleWithNonce;
use ipl\Web\Widget\TimeAgo;
use ipl\Web\Widget\TimeUntil;
use ipl\Web\Widget\VerticalKeyValue;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;

class DowntimeCard extends BaseHtmlElement
{
    protected $downtime;

    protected $duration;

    protected $defaultAttributes = ['class' => 'progress-bar downtime-progress'];

    protected $tag = 'div';

    protected $start;

    protected $end;

    public function __construct(Downtime $downtime)
    {
        $this->downtime = $downtime;

        $this->start = $this->downtime->scheduled_start_time->getTimestamp();
        $this->end = $this->downtime->scheduled_end_time->getTimestamp();

        if ($this->downtime->end_time > $this->downtime->scheduled_end_time) {
            $this->duration = $this->downtime->end_time->getTimestamp() - $this->start;
        } else {
            $this->duration = $this->end - $this->start;
        }
    }

    protected function assemble()
    {
        $styleElement = (new StyleWithNonce())
            ->setModule('icingadb');

        $timeline = Html::tag('div', ['class' => 'downtime-timeline timeline']);
        $hPadding = 10;

        $above = Html::tag('ul', ['class' => 'above']);
        $below = Html::tag('ul', ['class' => 'below']);

        $markerStart = new HtmlElement('div', Attributes::create(['class' => ['marker' , 'left']]));
        $markerEnd = new HtmlElement('div', Attributes::create(['class' => ['marker', 'right']]));

        $timelineProgress = null;
        $flexProgress = null;
        $markerFlexStart = null;
        $markerFlexEnd = null;

        if ($this->end < time()) {
            $endTime = new TimeAgo($this->end);
        } else {
            $endTime = new TimeUntil($this->end);
        }

        if ($this->downtime->is_flexible && $this->downtime->is_in_effect) {
            $this->addAttributes(['class' => 'flexible in-effect']);

            $flexStartLeft = $hPadding + $this->calcRelativeLeft($this->downtime->start_time->getTimestamp());
            $flexEndLeft = $hPadding + $this->calcRelativeLeft($this->downtime->end_time->getTimestamp());

            $evade = false;
            if ($flexEndLeft - $flexStartLeft < 2) {
                $flexStartLeft -= 1;
                $flexEndLeft += 1;

                if ($flexEndLeft > $hPadding + $this->calcRelativeLeft($this->end)) {
                    $flexEndLeft = $hPadding + $this->calcRelativeLeft($this->end) - .5;
                    $flexStartLeft = $flexEndLeft - 2;
                }

                if ($flexStartLeft < $hPadding + $this->calcRelativeLeft($this->start)) {
                    $flexStartLeft = $hPadding + $this->calcRelativeLeft($this->start) + .5;
                    $flexEndLeft = $flexStartLeft + 2;
                }

                $evade = true;
            }

            $markerFlexStart = new HtmlElement('div', Attributes::create(['class' => ['highlighted', 'marker']]));
            $markerFlexEnd = new HtmlElement('div', Attributes::create(['class' => ['highlighted', 'marker']]));

            $styleElement
                ->addFor($markerFlexStart, ['left' => sprintf('%F%%', $flexStartLeft)])
                ->addFor($markerFlexEnd, ['left' => sprintf('%F%%', $flexEndLeft)]);

            $scheduledEndBubble = new HtmlElement(
                'li',
                null,
                new HtmlElement(
                    'div',
                    Attributes::create(['class' => ['bubble', 'upwards']]),
                    new VerticalKeyValue(t('Scheduled End'), $endTime)
                )
            );

            $timelineProgress = new HtmlElement('div', Attributes::create([
                'class' => ['progress', 'downtime-elapsed'],
                'data-animate-progress' => true,
                'data-start-time' => ((float) $this->downtime->start_time->format('U.u')),
                'data-end-time' => ((float) $this->downtime->end_time->format('U.u'))
            ]), new HtmlElement(
                'div',
                Attributes::create(['class' => 'bar']),
                new HtmlElement('div', Attributes::create(['class' => 'now']))
            ));

            $styleElement->addFor($timelineProgress, [
                'left'  => sprintf('%F%%', $flexStartLeft),
                'width' => sprintf('%F%%', $flexEndLeft - $flexStartLeft)
            ]);

            if (time() > $this->end) {
                $styleElement
                    ->addFor($markerEnd, [
                        'left' => sprintf('%F%%', $hPadding + $this->calcRelativeLeft($this->end))
                    ])
                    ->addFor($scheduledEndBubble, [
                        'left' => sprintf('%F%%', $hPadding + $this->calcRelativeLeft($this->end))
                    ]);
            } else {
                $scheduledEndBubble->getAttributes()
                    ->add('class', 'right');
            }

            $below->add([
                Html::tag(
                    'li',
                    ['class' => 'left'],
                    Html::tag(
                        'div',
                        ['class' => ['bubble', 'upwards']],
                        new VerticalKeyValue(t('Scheduled Start'), new TimeAgo($this->start))
                    )
                ),
                $scheduledEndBubble
            ]);

            $aboveStart = Html::tag('li', ['class' => 'positioned'], Html::tag(
                'div',
                ['class' => ['bubble', ($evade ? 'left-aligned' : null)]],
                new VerticalKeyValue(t('Start'), new TimeAgo($this->downtime->start_time->getTimestamp()))
            ));

            $aboveEnd = Html::tag('li', ['class' => 'positioned'], Html::tag(
                'div',
                ['class' => ['bubble', ($evade ? 'right-aligned' : null)]],
                new VerticalKeyValue(t('End'), new TimeUntil($this->downtime->end_time->getTimestamp()))
            ));

            $styleElement
                ->addFor($aboveStart, ['left' => sprintf('%F%%', $flexStartLeft)])
                ->addFor($aboveEnd, ['left' => sprintf('%F%%', $flexEndLeft)]);

            $above->add([$aboveStart, $aboveEnd, $styleElement]);
        } elseif ($this->downtime->is_flexible) {
            $this->addAttributes(['class' => 'flexible']);

            $below->add([
                Html::tag(
                    'li',
                    ['class' => 'left'],
                    Html::tag(
                        'div',
                        ['class' => ['bubble', 'upwards']],
                        new VerticalKeyValue(
                            t('Scheduled Start'),
                            time() > $this->start
                                ? new TimeAgo($this->start)
                                : new TimeUntil($this->start)
                        )
                    )
                ),
                Html::tag(
                    'li',
                    ['class' => 'right'],
                    Html::tag(
                        'div',
                        ['class' => ['bubble', 'upwards']],
                        new VerticalKeyValue(t('Scheduled End'), $endTime)
                    )
                )
            ]);

            $above = null;
        } else {
            if (time() >= $this->start) {
                $timelineProgress = new HtmlElement('div', Attributes::create([
                    'class' => ['progress', 'downtime-elapsed'],
                    'data-animate-progress' => true,
                    'data-start-time' => $this->start,
                    'data-end-time' => $this->end
                ]), new HtmlElement(
                    'div',
                    Attributes::create(['class' => 'bar']),
                    new HtmlElement('div', Attributes::create(['class' => 'now']))
                ));
            }

            $below->add([
                Html::tag(
                    'li',
                    ['class' => 'left'],
                    Html::tag(
                        'div',
                        ['class' => 'bubble upwards'],
                        new VerticalKeyValue(t('Start'), new TimeAgo($this->start))
                    )
                ),
                Html::tag(
                    'li',
                    ['class' => 'right'],
                    Html::tag(
                        'div',
                        ['class' => 'bubble upwards'],
                        new VerticalKeyValue(t('End'), new TimeUntil($this->end))
                    )
                )
            ]);

            $above = null;
        }

        $timeline->add([
            $timelineProgress,
            $flexProgress,
            $markerStart,
            $markerEnd,
            $markerFlexStart,
            $markerFlexEnd
        ]);

        $this->add([
            $above,
            $timeline,
            $below
        ]);
    }

    protected function calcRelativeLeft($value)
    {
        return round(($value - $this->start) / $this->duration * 80, 2);
    }
}