-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventListItem.php
176 lines (153 loc) · 5.62 KB
/
EventListItem.php
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
<?php
/* Icinga Notifications Web | (c) 2023 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Notifications\Widget\ItemList;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Model\Event;
use Icinga\Module\Notifications\Model\Incident;
use Icinga\Module\Notifications\Model\Objects;
use Icinga\Module\Notifications\Model\Source;
use Icinga\Module\Notifications\Widget\IconBall;
use Icinga\Module\Notifications\Widget\SourceIcon;
use InvalidArgumentException;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Stdlib\Str;
use ipl\Web\Common\BaseListItem;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\TimeAgo;
/**
* Event item of an event list. Represents one database row.
*/
class EventListItem extends BaseListItem
{
/** @var Event The associated list item */
protected $item;
/** @var Incident The related incident */
protected $incident;
/** @var EventList The list where the item is part of */
protected $list;
protected function init(): void
{
if (! $this->item->incident instanceof Incident) {
throw new InvalidArgumentException('Incidents must be loaded with the event');
} else {
$this->incident = $this->item->incident;
}
if (! $this->list->getNoSubjectLink()) {
$this->getAttributes()
->set('data-action-item', true);
}
$this->getAttributes()
->set('data-icinga-detail-filter', Links::event($this->item->id)->getQueryString());
}
protected function assembleVisual(BaseHtmlElement $visual): void
{
$content = null;
$severity = $this->item->severity;
$class = 'severity-' . $severity;
if ($this->item->type === 'internal') {
/*
* TODO(nc): Add proper handling of internal events once
* https://github.com/Icinga/icinga-notifications/issues/162 gets sorted out
*/
$content = new IconBall('square-up-right', 'fa-solid');
} else {
switch ($severity) {
case 'ok':
$content = (new Icon('heart', ['class' => $class]))->setStyle('fa-regular');
break;
case 'crit':
$content = new Icon('circle-exclamation', ['class' => $class]);
break;
case 'warning':
$content = new Icon('exclamation-triangle', ['class' => $class]);
break;
case 'err':
$content = (new Icon('circle-xmark', ['class' => $class]))->setStyle('fa-regular');
break;
case 'debug':
$content = new Icon('bug-slash');
break;
case 'info':
$content = new Icon('info');
break;
case 'alert':
$content = new Icon('bell');
break;
case 'emerg':
$content = new Icon('tower-broadcast');
break;
case 'notice':
$content = new Icon('envelope');
break;
}
}
if ($content) {
$visual->addHtml($content);
}
}
protected function assembleTitle(BaseHtmlElement $title): void
{
if ($this->incident->id !== null) {
$title->addHtml(Html::tag('span', [], sprintf('#%d:', $this->incident->id)));
}
if (! $this->list->getNoSubjectLink()) {
$content = new Link(null, Links::event($this->item->id));
} else {
$content = new HtmlElement('span');
}
/** @var Objects $obj */
$obj = $this->item->object;
$name = $obj->getName();
$content->addAttributes($name->getAttributes());
$content->addFrom($name);
if ($this->item->severity === null) {
$description = strtolower(trim($this->item->message ?? ''));
if (Str::startsWith($description, 'incident reached age')) {
$msg = t('exceeded time constraint');
} elseif (Str::startsWith($description, 'incident reevaluation')) {
$msg = t('was reevaluated at daemon startup');
} else {
$msg = t('was acknowledged');
}
} elseif ($this->item->severity === 'ok') {
$msg = t('recovered');
} else {
$msg = t('ran into a problem');
}
$title->addHtml($content);
$title->addHtml(Html::tag('span', ['class' => 'state'], $msg));
}
protected function assembleCaption(BaseHtmlElement $caption): void
{
$caption->add($this->item->message);
}
protected function assembleHeader(BaseHtmlElement $header): void
{
$content = [];
if ($this->item->type !== 'internal') {
/** @var Objects $object */
$object = $this->item->object;
/** @var Source $source */
$source = $object->source;
$content[] = (new SourceIcon(SourceIcon::SIZE_BIG))->addHtml($source->getIcon());
}
$content[] = new TimeAgo($this->item->time->getTimestamp());
$header->add($this->createTitle());
$header->add(
Html::tag(
'span',
['class' => 'meta'],
$content
)
);
}
protected function assembleMain(BaseHtmlElement $main): void
{
$main->add($this->createHeader());
$main->add($this->createCaption());
$main->add($this->createFooter());
}
}