From 6d9f348a9a4cec1192ad0d394eab78b579aae5ea Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 13 Feb 2023 11:08:15 +0100 Subject: [PATCH] Handle `unknown` sla results correctly --- .../Reporting/Common/ReportData.php | 8 +++++++ .../Reporting/Common/SlaReportUtils.php | 3 ++- .../ProvidedHook/Reporting/HostSlaReport.php | 6 +----- .../Reporting/ServiceSlaReport.php | 2 +- .../ProvidedHook/Reporting/SlaReport.php | 21 +++++++++++++++---- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/library/Icingadb/ProvidedHook/Reporting/Common/ReportData.php b/library/Icingadb/ProvidedHook/Reporting/Common/ReportData.php index fe0d7fe64..deb2c14ff 100644 --- a/library/Icingadb/ProvidedHook/Reporting/Common/ReportData.php +++ b/library/Icingadb/ProvidedHook/Reporting/Common/ReportData.php @@ -24,10 +24,18 @@ public function getAverages() $problemTime += $timeline->getProblemTime(); } + if ($totalTime <= 0) { + continue; + } + ++$count; $totals += 100 * ($totalTime - $problemTime) / $totalTime; } + if ($count === 0) { + return [null]; + } + return [$totals / $count]; } } diff --git a/library/Icingadb/ProvidedHook/Reporting/Common/SlaReportUtils.php b/library/Icingadb/ProvidedHook/Reporting/Common/SlaReportUtils.php index 65ac00c51..285b0813b 100644 --- a/library/Icingadb/ProvidedHook/Reporting/Common/SlaReportUtils.php +++ b/library/Icingadb/ProvidedHook/Reporting/Common/SlaReportUtils.php @@ -322,7 +322,8 @@ protected function fetchInitialHardState(DateTime $start, string $hostId, string $serviceFilter, Filter::greaterThan('event_time', $start) ) - ); + ) + ->limit(1); $hardState = $hardState->first(); diff --git a/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php b/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php index e04061e5b..31d470951 100644 --- a/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php +++ b/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php @@ -31,12 +31,8 @@ protected function createReportData() protected function createReportRow($row) { - if ($row->sla === null) { - return null; - } - return (new ReportRow()) ->setDimensions([$row->display_name]) - ->setValues([(float) $row->sla]); + ->setValues([$row->sla]); } } diff --git a/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php b/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php index d4cb3f141..dbb59e146 100644 --- a/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php +++ b/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php @@ -33,6 +33,6 @@ protected function createReportRow($row) { return (new ReportRow()) ->setDimensions([$row->host_display_name, $row->display_name]) - ->setValues([(float) $row->sla]); + ->setValues([$row->sla]); } } diff --git a/library/Icingadb/ProvidedHook/Reporting/SlaReport.php b/library/Icingadb/ProvidedHook/Reporting/SlaReport.php index 69e432e95..c2e2616da 100644 --- a/library/Icingadb/ProvidedHook/Reporting/SlaReport.php +++ b/library/Icingadb/ProvidedHook/Reporting/SlaReport.php @@ -119,20 +119,29 @@ public function getHtml(Timerange $timerange, array $config = null) // We only have one metric $sla = $row->getValues()[0]; - if ($sla < $threshold) { + if ($sla === null) { + $slaClass = 'unknown'; + } elseif ($sla < $threshold) { $slaClass = 'nok'; } else { $slaClass = 'ok'; } - $cells[] = Html::tag('td', ['class' => "sla-column $slaClass"], round($sla, $precision)); + $cells[] = Html::tag( + 'td', + ['class' => "sla-column $slaClass"], + $sla === null ? t('N/A') : round($sla, $precision) + ); $tableRows[] = Html::tag('tr', null, $cells); } // We only have one average $average = $data->getAverages()[0]; - if ($average < $threshold) { + + if ($average === null) { + $slaClass = 'unknown'; + } elseif ($average < $threshold) { $slaClass = 'nok'; } else { $slaClass = 'ok'; @@ -144,7 +153,11 @@ public function getHtml(Timerange $timerange, array $config = null) $tableRows[] = Html::tag('tr', null, [ Html::tag('td', ['colspan' => count($data->getDimensions())], $total), - Html::tag('td', ['class' => "sla-column $slaClass"], round($average, $precision)) + Html::tag( + 'td', + ['class' => "sla-column $slaClass"], + $average === null ? t('N/A') : round($average, $precision) + ) ]); $table = Html::tag(