Skip to content

Commit 6c00c92

Browse files
committed
Handle unknown sla results correctly
1 parent 7950d89 commit 6c00c92

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ public function getAverages()
2424
$problemTime += $timeline->getProblemTime();
2525
}
2626

27+
if ($totalTime <= 0) {
28+
continue;
29+
}
30+
2731
++$count;
2832
$totals += 100 * ($totalTime - $problemTime) / $totalTime;
2933
}
3034

35+
if ($count === 0) {
36+
return [null];
37+
}
38+
3139
return [$totals / $count];
3240
}
3341
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,15 @@ protected function fetchReportData(DateTime $reportStart, DateTime $reportEnd, a
179179
}
180180

181181
foreach ($reports as $name => $timeline) {
182-
$rd->addTimeline($name, $timeline);
183182
$row = (object) [];
184-
$row->sla = $timeline->getResult();
185183
$row->display_name = $name;
184+
$row->sla = $timeline->getResult();
185+
if ($row->sla === null) {
186+
// No data available
187+
continue;
188+
}
186189

190+
$rd->addTimeline($name, $timeline);
187191
if (strpos($name, static::$hostServiceSeparator) !== false) {
188192
list($host, $service) = Str::trimSplit($name, static::$hostServiceSeparator);
189193
$row->display_name = $service;
@@ -317,7 +321,8 @@ protected function fetchInitialHardState(DateTime $start, string $hostId, string
317321
$serviceFilter,
318322
Filter::greaterThan('event_time', $start)
319323
)
320-
);
324+
)
325+
->limit(1);
321326

322327
$hardState = $hardState->first();
323328

library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ protected function createReportData()
3131

3232
protected function createReportRow($row)
3333
{
34-
if ($row->sla === null) {
35-
return null;
36-
}
37-
3834
return (new ReportRow())
3935
->setDimensions([$row->display_name])
40-
->setValues([(float) $row->sla]);
36+
->setValues([$row->sla]);
4137
}
4238
}

library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ protected function createReportRow($row)
3333
{
3434
return (new ReportRow())
3535
->setDimensions([$row->host_display_name, $row->display_name])
36-
->setValues([(float) $row->sla]);
36+
->setValues([$row->sla]);
3737
}
3838
}

library/Icingadb/ProvidedHook/Reporting/SlaReport.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,29 @@ public function getHtml(Timerange $timerange, array $config = null)
119119
// We only have one metric
120120
$sla = $row->getValues()[0];
121121

122-
if ($sla < $threshold) {
122+
if ($sla === null) {
123+
$slaClass = 'unknown';
124+
} elseif ($sla < $threshold) {
123125
$slaClass = 'nok';
124126
} else {
125127
$slaClass = 'ok';
126128
}
127129

128-
$cells[] = Html::tag('td', ['class' => "sla-column $slaClass"], round($sla, $precision));
130+
$cells[] = Html::tag(
131+
'td',
132+
['class' => "sla-column $slaClass"],
133+
$sla === null ? t('N/A') : round($sla, $precision)
134+
);
129135

130136
$tableRows[] = Html::tag('tr', null, $cells);
131137
}
132138

133139
// We only have one average
134140
$average = $data->getAverages()[0];
135-
if ($average < $threshold) {
141+
142+
if ($average === null) {
143+
$slaClass = 'unknown';
144+
} elseif ($average < $threshold) {
136145
$slaClass = 'nok';
137146
} else {
138147
$slaClass = 'ok';
@@ -144,7 +153,11 @@ public function getHtml(Timerange $timerange, array $config = null)
144153

145154
$tableRows[] = Html::tag('tr', null, [
146155
Html::tag('td', ['colspan' => count($data->getDimensions())], $total),
147-
Html::tag('td', ['class' => "sla-column $slaClass"], round($average, $precision))
156+
Html::tag(
157+
'td',
158+
['class' => "sla-column $slaClass"],
159+
$average === null ? t('N/A') : round($average, $precision)
160+
)
148161
]);
149162

150163
$table = Html::tag(

0 commit comments

Comments
 (0)