Skip to content

Commit e1a180b

Browse files
committed
WIP
1 parent 6c00c92 commit e1a180b

File tree

4 files changed

+108
-85
lines changed

4 files changed

+108
-85
lines changed

library/Icingadb/Model/HostSlaHistory.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace Icinga\Module\Icingadb\Model;
66

7+
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaTimeline;
78
use ipl\Orm\Relations;
89
use ipl\Orm\UnionModel;
10+
use ipl\Sql\Connection;
911
use ipl\Sql\Expression;
12+
use ipl\Stdlib\Filter;
1013

1114
class HostSlaHistory extends UnionModel
1215
{
@@ -27,12 +30,24 @@ public function getColumns()
2730
'display_name',
2831
'event_time',
2932
'event_type',
30-
'event_priority',
3133
'hard_state',
3234
'previous_hard_state'
3335
];
3436
}
3537

38+
public static function on(Connection $db)
39+
{
40+
$query = parent::on($db);
41+
$unions = $query->getUnions();
42+
43+
$downtimeFilter = Filter::unlike('sla_history_downtime.service_id', '*');
44+
$unions[0]->filter($downtimeFilter);
45+
$unions[1]->filter($downtimeFilter);
46+
$unions[2]->filter(Filter::unlike('sla_history_state.service_id', '*'));
47+
48+
return $query;
49+
}
50+
3651
public function getUnions()
3752
{
3853
return [
@@ -43,9 +58,8 @@ public function getUnions()
4358
],
4459
[
4560
'display_name' => 'host.display_name',
46-
'event_priority' => new Expression('1'),
4761
'event_time' => 'sla_history_downtime.downtime_start',
48-
'event_type' => new Expression('\'downtime_start\''),
62+
'event_type' => new Expression(SlaTimeline::DOWNTIME_START),
4963
'hard_state' => new Expression('NULL'),
5064
'host_id' => 'id',
5165
'previous_hard_state' => new Expression('NULL'),
@@ -58,9 +72,8 @@ public function getUnions()
5872
],
5973
[
6074
'display_name' => 'host.display_name',
61-
'event_priority' => new Expression('2'),
6275
'event_time' => 'sla_history_downtime.downtime_end',
63-
'event_type' => new Expression('\'downtime_end\''),
76+
'event_type' => new Expression(SlaTimeline::DOWNTIME_END),
6477
'hard_state' => new Expression('NULL'),
6578
'host_id' => 'id',
6679
'previous_hard_state' => new Expression('NULL'),
@@ -73,9 +86,8 @@ public function getUnions()
7386
],
7487
[
7588
'display_name' => 'display_name',
76-
'event_priority' => new Expression('0'),
7789
'event_time' => 'sla_history_state.event_time',
78-
'event_type' => new Expression('\'state_change\''),
90+
'event_type' => new Expression(SlaTimeline::STATE_CHANGE),
7991
'hard_state' => 'sla_history_state.hard_state',
8092
'host_id' => 'id',
8193
'previous_hard_state' => 'sla_history_state.previous_hard_state',
@@ -87,9 +99,8 @@ public function getUnions()
8799
[],
88100
[
89101
'display_name' => 'host.display_name',
90-
'event_priority' => new Expression('3'),
91102
'event_time' => new Expression('NULL'),
92-
'event_type' => new Expression('\'end\''),
103+
'event_type' => new Expression(SlaTimeline::END_RESULT),
93104
'hard_state' => new Expression('NULL'),
94105
'host_id' => 'id',
95106
'previous_hard_state' => new Expression('NULL'),
@@ -100,7 +111,7 @@ public function getUnions()
100111

101112
public function getDefaultSort()
102113
{
103-
return ['display_name', 'event_time', 'event_priority'];
114+
return ['display_name', 'event_time', 'event_type'];
104115
}
105116

106117
public function createRelations(Relations $relations)

library/Icingadb/Model/ServiceSlaHistory.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Icinga\Module\Icingadb\Model;
66

7+
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaTimeline;
78
use ipl\Orm\Relations;
89
use ipl\Orm\UnionModel;
910
use ipl\Sql\Expression;
@@ -29,7 +30,6 @@ public function getColumns()
2930
'host_display_name',
3031
'event_time',
3132
'event_type',
32-
'event_priority',
3333
'hard_state',
3434
'previous_hard_state'
3535
];
@@ -47,9 +47,8 @@ public function getUnions()
4747
[
4848
'display_name' => 'display_name',
4949
'host_display_name' => 'host.display_name',
50-
'event_priority' => new Expression('1'),
5150
'event_time' => 'sla_history_downtime.downtime_start',
52-
'event_type' => new Expression('\'downtime_start\''),
51+
'event_type' => new Expression(SlaTimeline::DOWNTIME_START),
5352
'hard_state' => new Expression('NULL'),
5453
'host_id' => 'host.id',
5554
'service_id' => 'id',
@@ -65,9 +64,8 @@ public function getUnions()
6564
[
6665
'display_name' => 'display_name',
6766
'host_display_name' => 'host.display_name',
68-
'event_priority' => new Expression('2'),
6967
'event_time' => 'sla_history_downtime.downtime_end',
70-
'event_type' => new Expression('\'downtime_end\''),
68+
'event_type' => new Expression(SlaTimeline::DOWNTIME_END),
7169
'hard_state' => new Expression('NULL'),
7270
'host_id' => 'host.id',
7371
'service_id' => 'id',
@@ -83,9 +81,8 @@ public function getUnions()
8381
[
8482
'display_name' => 'display_name',
8583
'host_display_name' => 'host.display_name',
86-
'event_priority' => new Expression('0'),
8784
'event_time' => 'sla_history_state.event_time',
88-
'event_type' => new Expression('\'state_change\''),
85+
'event_type' => new Expression(SlaTimeline::STATE_CHANGE),
8986
'hard_state' => 'sla_history_state.hard_state',
9087
'host_id' => 'host.id',
9188
'service_id' => 'id',
@@ -101,9 +98,8 @@ public function getUnions()
10198
[
10299
'display_name' => 'display_name',
103100
'host_display_name' => 'host.display_name',
104-
'event_priority' => new Expression('3'),
105101
'event_time' => new Expression('NULL'),
106-
'event_type' => new Expression('\'end\''),
102+
'event_type' => new Expression(SlaTimeline::END_RESULT),
107103
'hard_state' => new Expression('NULL'),
108104
'host_id' => 'host.id',
109105
'service_id' => 'id',
@@ -115,7 +111,7 @@ public function getUnions()
115111

116112
public function getDefaultSort()
117113
{
118-
return ['host_display_name', 'display_name', 'event_time', 'event_priority'];
114+
return ['host_display_name', 'display_name', 'event_time', 'event_type'];
119115
}
120116

121117
public function createRelations(Relations $relations)

library/Icingadb/ProvidedHook/Reporting/Common/SlaReportUtils.php

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,61 +48,49 @@ protected function fetchSla(DateTime $start, DateTime $end, Rule $filter = null)
4848
$query = ServiceSlaHistory::on($this->getDb());
4949
}
5050

51-
$index = 0;
52-
foreach ($query->getUnions() as $union) {
53-
$filterAll = Filter::all();
54-
if ($index >= 2) {
55-
if ($index < 3) {
56-
if ($this->getReportType() === 'host') {
57-
$filterAll->add(Filter::unlike('sla_history_state.service_id', '*'));
58-
}
59-
60-
$filterAll
61-
->add(Filter::greaterThan('sla_history_state.event_time', $start))
62-
->add(Filter::lessThan('sla_history_state.event_time', $end));
63-
} else {
64-
$union->columns(
65-
array_merge($union->getColumns(), [
66-
'event_time' => new Expression($end->format('Uv'))
67-
])
68-
);
69-
}
70-
} else {
71-
if ($this->getReportType() === 'host') {
72-
$filterAll->add(Filter::unlike('sla_history_downtime.service_id', '*'));
73-
}
74-
75-
$filterAll
76-
->add(Filter::lessThan('sla_history_downtime.downtime_start', $end))
77-
->add(Filter::greaterThanOrEqual('sla_history_downtime.downtime_end', $start));
78-
79-
if ($index === 1) {
80-
$filterAll->add(Filter::lessThan('sla_history_downtime.downtime_end', $end));
81-
} else {
82-
$union->columns(
83-
array_merge(
84-
$union->getColumns(),
85-
[
86-
'event_time' => new Expression(
87-
sprintf(
88-
'GREATEST(%s_sla_history_downtime.downtime_start, %s)',
89-
$this->getReportType(),
90-
$start->format('Uv')
91-
)
92-
)
93-
]
51+
$unions = $query->getUnions();
52+
$slaDowntimeFilter = Filter::all(
53+
Filter::lessThan('sla_history_downtime.downtime_start', $end),
54+
Filter::greaterThanOrEqual('sla_history_downtime.downtime_end', $start)
55+
);
56+
57+
$unions[0]
58+
->filter($slaDowntimeFilter)
59+
->columns(
60+
array_merge(
61+
$unions[0]->getColumns(),
62+
[
63+
'event_time' => new Expression(
64+
sprintf(
65+
'GREATEST(%s_sla_history_downtime.downtime_start, %s)',
66+
$this->getReportType(),
67+
$start->format('Uv')
68+
)
9469
)
95-
);
96-
}
97-
}
98-
99-
++$index;
100-
101-
$union->filter($filterAll);
102-
103-
if ($filter !== null) {
104-
$union->filter($filter);
105-
}
70+
]
71+
)
72+
);
73+
74+
$unions[1]
75+
->filter($slaDowntimeFilter)
76+
->filter(Filter::lessThan('sla_history_downtime.downtime_end', $end));
77+
78+
$unions[2]->filter(Filter::all(
79+
Filter::greaterThan('sla_history_state.event_time', $start),
80+
Filter::lessThan('sla_history_state.event_time', $end)
81+
));
82+
83+
$unions[3]->columns(
84+
array_merge($unions[3]->getColumns(), [
85+
'event_time' => new Expression($end->format('Uv'))
86+
])
87+
);
88+
89+
if ($filter !== null) {
90+
$unions[0]->filter($filter);
91+
$unions[1]->filter($filter);
92+
$unions[2]->filter($filter);
93+
$unions[3]->filter($filter);
10694
}
10795

10896
if (method_exists($this, 'applyRestrictions')) {
@@ -158,7 +146,7 @@ protected function fetchReportData(DateTime $reportStart, DateTime $reportEnd, a
158146
if (isset($reports[$key])) {
159147
$timeline = $reports[$key];
160148
} else {
161-
$timeline = new SlaTimeline($start, $end);
149+
$timeline = new SlaTimeline($start, $end, $this->getReportType());
162150
$serviceId = null;
163151
if (! $this instanceof HostSlaReport) {
164152
$serviceId = $row->service_id;
@@ -212,7 +200,7 @@ protected function fetchReportData(DateTime $reportStart, DateTime $reportEnd, a
212200
if ($rd->hasTimelines($key)) {
213201
$timeline = current($rd->getTimelines($key));
214202
} else {
215-
$timeline = new SlaTimeline($reportStart, $reportEnd);
203+
$timeline = new SlaTimeline($reportStart, $reportEnd, $this->getReportType());
216204
$serviceId = null;
217205
if (! $isHostQuery) {
218206
$serviceId = $row->service_id;
@@ -231,7 +219,7 @@ protected function fetchReportData(DateTime $reportStart, DateTime $reportEnd, a
231219

232220
$rd->setTimeline($key, $timeline);
233221

234-
if ($row->event_type === 'end') {
222+
if ($row->event_type === SlaTimeline::END_RESULT) {
235223
$row->sla = $timeline->getResult();
236224
$rows[] = $this->createReportRow($row);
237225
}

library/Icingadb/ProvidedHook/Reporting/Common/SlaTimeline.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
*/
1313
class SlaTimeline implements Countable
1414
{
15+
/** @var int Sla history event type state change */
16+
public const STATE_CHANGE = 0;
17+
18+
/** @var int Sla history event type downtime start */
19+
public const DOWNTIME_START = 1;
20+
21+
/** @var int Sla history event type downtime end */
22+
public const DOWNTIME_END = 2;
23+
24+
/** @var int End of this timeline events */
25+
public const END_RESULT = 3;
26+
1527
/** @var DateTimeInterface Start time of the generated report timeframe */
1628
protected $start;
1729

@@ -39,10 +51,13 @@ class SlaTimeline implements Countable
3951
/** @var int The initial hard state of this timeline */
4052
protected $initialHardState = 0;
4153

42-
public function __construct(DateTimeInterface $start, DateTimeInterface $end)
54+
protected $objectType;
55+
56+
public function __construct(DateTimeInterface $start, DateTimeInterface $end, string $objectType)
4357
{
4458
$this->start = clone $start;
4559
$this->end = clone $end;
60+
$this->objectType = $objectType;
4661
}
4762

4863
/**
@@ -78,18 +93,31 @@ public function getResult(): ?float
7893
$time = $this->time[$i];
7994
$state = $this->state[$i];
8095

81-
if ($this->previousState[$i] === 99 || ($lastHardState === 99 && $event !== 'state_change')) {
96+
if ($this->previousState[$i] === 99 || ($lastHardState === 99 && $event !== static::STATE_CHANGE)) {
8297
$totalTime -= $time - $lastEventTime;
83-
} elseif ($lastHardState > 0 && $lastHardState !== 99 && $activeDowntimes === 0) {
98+
} elseif (
99+
(
100+
(
101+
$this->objectType === 'host'
102+
&& $lastHardState > 0
103+
)
104+
|| (
105+
$this->objectType === 'service'
106+
&& $lastHardState > 1
107+
)
108+
)
109+
&& $lastHardState !== 99
110+
&& $activeDowntimes === 0
111+
) {
84112
$problemTime += $time - $lastEventTime;
85113
}
86114

87115
$lastEventTime = $time;
88-
if ($event === 'state_change') {
116+
if ($event === static::STATE_CHANGE) {
89117
$lastHardState = $state;
90-
} elseif ($event === 'downtime_start') {
118+
} elseif ($event === static::DOWNTIME_START) {
91119
++$activeDowntimes;
92-
} elseif ($event === 'downtime_end') {
120+
} elseif ($event === static::DOWNTIME_END) {
93121
--$activeDowntimes;
94122
}
95123
}
@@ -121,11 +149,11 @@ public function addTime(int $time): self
121149
/**
122150
* Add event type to this timeline
123151
*
124-
* @param string $event
152+
* @param int $event
125153
*
126154
* @return $this
127155
*/
128-
public function addEvent(string $event): self
156+
public function addEvent(int $event): self
129157
{
130158
$this->event[] = $event;
131159

0 commit comments

Comments
 (0)