Skip to content

Commit 96ed8f7

Browse files
committed
WIP
1 parent d14c732 commit 96ed8f7

File tree

6 files changed

+400
-23
lines changed

6 files changed

+400
-23
lines changed

library/Icingadb/Model/Host.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public function createRelations(Relations $relations)
222222
$relations->belongsToMany('hostgroup', Hostgroup::class)
223223
->through(HostgroupMember::class);
224224

225+
$relations->hasMany('sla_history_state', SlaHistoryState::class)
226+
->setJoinType('LEFT');
227+
$relations->hasMany('sla_history_downtime', SlaHistoryDowntime::class)
228+
->setJoinType('LEFT');
229+
225230
$relations->hasOne('state', HostState::class)->setJoinType('LEFT');
226231
$relations->hasMany('comment', Comment::class)->setJoinType('LEFT');
227232
$relations->hasMany('downtime', Downtime::class)->setJoinType('LEFT');
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Icinga\Module\Icingadb\Model;
4+
5+
use ipl\Orm\Relations;
6+
use ipl\Orm\UnionModel;
7+
use ipl\Sql\Expression;
8+
9+
class HostSlaHistorySummary extends UnionModel
10+
{
11+
public function getTableName()
12+
{
13+
return 'host';
14+
}
15+
16+
public function getKeyName()
17+
{
18+
return 'host_id';
19+
}
20+
21+
public function getColumns()
22+
{
23+
return [
24+
'host_id',
25+
'display_name',
26+
'event_time',
27+
'event_type',
28+
'event_priority',
29+
'hard_state',
30+
'previous_hard_state'
31+
];
32+
}
33+
34+
public function getUnions()
35+
{
36+
return [
37+
[
38+
Host::class,
39+
[
40+
'sla_history_downtime'
41+
],
42+
[
43+
'display_name' => 'host.display_name',
44+
'event_priority' => new Expression('1'),
45+
'event_time' => 'sla_history_downtime.downtime_start',
46+
'event_type' => new Expression('\'downtime_start\''),
47+
'hard_state' => new Expression('NULL'),
48+
'previous_hard_state' => new Expression('NULL'),
49+
'host_id' => 'id'
50+
]
51+
],
52+
[
53+
Host::class,
54+
[
55+
'sla_history_downtime'
56+
],
57+
[
58+
'display_name' => 'host.display_name',
59+
'event_priority' => new Expression('2'),
60+
'event_time' => 'sla_history_downtime.downtime_end',
61+
'event_type' => new Expression('\'downtime_end\''),
62+
'hard_state' => new Expression('NULL'),
63+
'previous_hard_state' => new Expression('NULL'),
64+
'host_id' => 'id'
65+
]
66+
],
67+
[
68+
Host::class,
69+
[
70+
'sla_history_state'
71+
],
72+
[
73+
'display_name' => 'display_name',
74+
'event_priority' => new Expression('0'),
75+
'event_time' => 'sla_history_state.event_time',
76+
'event_type' => new Expression('\'state_change\''),
77+
'hard_state' => 'sla_history_state.hard_state',
78+
'previous_hard_state' => 'sla_history_state.previous_hard_state',
79+
'host_id' => 'id'
80+
]
81+
],
82+
// Fake result!!
83+
[
84+
Host::class,
85+
[],
86+
[
87+
'display_name' => 'host.display_name',
88+
'event_priority' => new Expression('3'),
89+
'event_time' => new Expression('NULL'),
90+
'event_type' => new Expression('\'end\''),
91+
'hard_state' => new Expression('NULL'),
92+
'previous_hard_state' => new Expression('NULL'),
93+
'host_id' => 'id'
94+
]
95+
]
96+
];
97+
}
98+
99+
public function getDefaultSort()
100+
{
101+
return ['display_name', 'event_time', 'event_priority'];
102+
}
103+
104+
public function createRelations(Relations $relations)
105+
{
106+
(new Host())->createRelations($relations);
107+
}
108+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Icinga\Module\Icingadb\Model;
4+
5+
use Icinga\Module\Icingadb\Model\Behavior\Timestamp;
6+
use ipl\Orm\Behavior\Binary;
7+
use ipl\Orm\Behaviors;
8+
use ipl\Orm\Model;
9+
use ipl\Orm\Relations;
10+
11+
class SlaHistoryDowntime extends Model
12+
{
13+
public function getTableName()
14+
{
15+
return 'sla_history_downtime';
16+
}
17+
18+
public function getKeyName()
19+
{
20+
return 'id';
21+
}
22+
23+
public function getColumns()
24+
{
25+
return [
26+
'environment_id',
27+
'endpoint_id',
28+
'host_id',
29+
'service_id',
30+
'downtime_id',
31+
'object_type',
32+
'downtime_start',
33+
'downtime_end'
34+
];
35+
}
36+
37+
public function createBehaviors(Behaviors $behaviors)
38+
{
39+
$behaviors->add(new Binary([
40+
'id',
41+
'environment_id',
42+
'endpoint_id',
43+
'host_id',
44+
'service_id',
45+
'downtime_id'
46+
]));
47+
48+
$behaviors->add(new Timestamp([
49+
'downtime_start',
50+
'downtime_end'
51+
]));
52+
}
53+
54+
public function createRelations(Relations $relations)
55+
{
56+
$relations->belongsTo('host', Host::class);
57+
$relations->belongsTo('service', Service::class);
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Icinga\Module\Icingadb\Model;
4+
5+
use Icinga\Module\Icingadb\Model\Behavior\Timestamp;
6+
use ipl\Orm\Behavior\Binary;
7+
use ipl\Orm\Behaviors;
8+
use ipl\Orm\Model;
9+
use ipl\Orm\Relations;
10+
11+
class SlaHistoryState extends Model
12+
{
13+
public function getTableName()
14+
{
15+
return 'sla_history_state';
16+
}
17+
18+
public function getKeyName()
19+
{
20+
return 'id';
21+
}
22+
23+
public function getColumns()
24+
{
25+
return [
26+
'environment_id',
27+
'endpoint_id',
28+
'host_id',
29+
'service_id',
30+
'object_type',
31+
'event_time',
32+
'hard_state',
33+
'previous_hard_state'
34+
];
35+
}
36+
37+
public function createBehaviors(Behaviors $behaviors)
38+
{
39+
$behaviors->add(new Binary([
40+
'id',
41+
'environment_id',
42+
'endpoint_id',
43+
'host_id',
44+
'service_id'
45+
]));
46+
47+
$behaviors->add(new Timestamp(['event_time']));
48+
}
49+
50+
public function createRelations(Relations $relations)
51+
{
52+
$relations->belongsTo('host', Host::class);
53+
$relations->belongsTo('service', Service::class);
54+
}
55+
56+
public function getDefaultSort()
57+
{
58+
return ['event_time'];
59+
}
60+
}

library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace Icinga\Module\Icingadb\ProvidedHook\Reporting;
66

77
use Icinga\Application\Icinga;
8-
use Icinga\Module\Icingadb\Model\Host;
8+
use Icinga\Module\Icingadb\Model\HostSlaHistorySummary;
99
use Icinga\Module\Reporting\ReportData;
1010
use Icinga\Module\Reporting\ReportRow;
1111
use Icinga\Module\Reporting\Timerange;
1212
use ipl\Sql\Expression;
13+
use ipl\Stdlib\Filter;
1314
use ipl\Stdlib\Filter\Rule;
1415

1516
use function ipl\I18n\t;
@@ -46,23 +47,52 @@ protected function createReportRow($row)
4647

4748
protected function fetchSla(Timerange $timerange, Rule $filter = null)
4849
{
49-
$sla = Host::on($this->getDb())
50-
->columns([
51-
'display_name',
52-
'sla' => new Expression(sprintf(
53-
"get_sla_ok_percent(%s, NULL, '%s', '%s')",
54-
'host.id',
55-
$timerange->getStart()->format('Uv'),
56-
$timerange->getEnd()->format('Uv')
57-
))
58-
]);
59-
60-
$this->applyRestrictions($sla);
61-
62-
if ($filter !== null) {
63-
$sla->filter($filter);
50+
$start = (int) $timerange->getStart()->format('U');
51+
$end = (int) $timerange->getEnd()->format('U');
52+
53+
$query = HostSlaHistorySummary::on($this->getDb());
54+
55+
$index = 0;
56+
foreach ($query->getUnions() as $union) {
57+
$filterAll = Filter::all();
58+
if ($index >= 2) {
59+
if ($index < 3) {
60+
$filterAll
61+
->add(Filter::unlike('sla_history_state.service_id', '*'))
62+
->add(Filter::greaterThan('sla_history_state.event_time', $start))
63+
->add(Filter::lessThan('sla_history_state.event_time', $end));
64+
} else {
65+
$union->columns(array_merge($union->getColumns(), ['event_time' => new Expression($end)]));
66+
}
67+
} else {
68+
$filterAll
69+
->add(Filter::unlike('sla_history_downtime.service_id', '*'))
70+
->add(Filter::lessThan('sla_history_downtime.downtime_start', $end))
71+
->add(Filter::greaterThanOrEqual('sla_history_downtime.downtime_end', $start));
72+
73+
if ($index === 1) {
74+
$filterAll->add(Filter::lessThan('sla_history_downtime.downtime_end', $end));
75+
} else {
76+
$union->columns(
77+
array_merge(
78+
$union->getColumns(),
79+
['event_time' => new Expression(sprintf('GREATEST(host_sla_history_downtime.downtime_start, %s)', $start))]
80+
)
81+
);
82+
}
83+
}
84+
85+
++$index;
86+
87+
$union->filter($filterAll);
88+
89+
if ($filter !== null) {
90+
$union->filter($filter);
91+
}
6492
}
6593

66-
return $sla;
94+
$this->applyRestrictions($query);
95+
96+
return $query;
6797
}
6898
}

0 commit comments

Comments
 (0)