Skip to content

Commit f8b16e4

Browse files
committed
SlaReport: Add checkbox to optionally show critical SLA of hosts/services reports
1 parent 5948e9f commit f8b16e4

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Icinga\Module\Reporting\ReportData;
1010
use Icinga\Module\Reporting\ReportRow;
1111
use Icinga\Module\Reporting\Timerange;
12+
use ipl\Orm\Query;
1213
use ipl\Sql\Expression;
1314
use ipl\Stdlib\Filter\Rule;
1415

@@ -44,7 +45,7 @@ protected function createReportRow($row)
4445
->setValues([(float) $row->sla]);
4546
}
4647

47-
protected function fetchSla(Timerange $timerange, Rule $filter = null)
48+
protected function fetchSla(Timerange $timerange, Rule $filter = null): Query
4849
{
4950
$sla = Host::on($this->getDb())
5051
->columns([

library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Icinga\Module\Reporting\ReportData;
1010
use Icinga\Module\Reporting\ReportRow;
1111
use Icinga\Module\Reporting\Timerange;
12+
use ipl\Orm\Query;
1213
use ipl\Sql\Expression;
1314
use ipl\Stdlib\Filter\Rule;
1415

@@ -44,7 +45,7 @@ protected function createReportRow($row)
4445
->setValues([(float) $row->sla]);
4546
}
4647

47-
protected function fetchSla(Timerange $timerange, Rule $filter = null)
48+
protected function fetchSla(Timerange $timerange, Rule $filter = null): Query
4849
{
4950
$sla = Service::on($this->getDb())
5051
->columns([

library/Icingadb/ProvidedHook/Reporting/SlaReport.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateInterval;
88
use DatePeriod;
9+
use Generator;
910
use Icinga\Module\Icingadb\Common\Auth;
1011
use Icinga\Module\Icingadb\Common\Database;
1112
use Icinga\Module\Icingadb\Widget\EmptyState;
@@ -15,6 +16,8 @@
1516
use Icinga\Module\Reporting\Timerange;
1617
use ipl\Html\Form;
1718
use ipl\Html\Html;
19+
use ipl\Orm\Query;
20+
use ipl\Sql\Expression;
1821
use ipl\Stdlib\Filter\Rule;
1922
use ipl\Web\Filter\QueryString;
2023

@@ -57,9 +60,9 @@ abstract protected function createReportRow($row);
5760
* @param Timerange $timerange
5861
* @param Rule|null $filter
5962
*
60-
* @return iterable
63+
* @return Query
6164
*/
62-
abstract protected function fetchSla(Timerange $timerange, Rule $filter = null);
65+
abstract protected function fetchSla(Timerange $timerange, Rule $filter = null): Query;
6366

6467
protected function fetchReportData(Timerange $timerange, array $config = null)
6568
{
@@ -69,6 +72,26 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
6972
$filter = trim((string) $config['filter']) ?: '*';
7073
$filter = $filter !== '*' ? QueryString::parse($filter) : null;
7174

75+
$yieldSla = function (Timerange $timerange, Rule $filter = null) use ($config): Generator {
76+
$sla = $this->fetchSla($timerange, $filter);
77+
78+
if ($config['only-violation'] === '1') {
79+
$threshold = $config['threshold'] ?? static::DEFAULT_THRESHOLD;
80+
81+
$sla->assembleSelect();
82+
$sla->getSelectBase()->where(new Expression(
83+
'(%s) < %F', [$sla->getColumns()['sla']->getStatement(), $threshold]
84+
));
85+
//$sla->filter(Filter::lessThan('sla', $threshold));
86+
//$sla->getSelectBase()->where(['sla < ?' => $threshold]) requires to wrap Model again and
87+
// order by sla after
88+
}
89+
90+
foreach ($sla as $row) {
91+
yield $row;
92+
}
93+
};
94+
7295
if (isset($config['breakdown']) && $config['breakdown'] !== 'none') {
7396
switch ($config['breakdown']) {
7497
case 'day':
@@ -96,7 +119,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
96119
$rd->setDimensions($dimensions);
97120

98121
foreach ($this->yieldTimerange($timerange, $interval, $boundary) as list($start, $end)) {
99-
foreach ($this->fetchSla(new Timerange($start, $end), $filter) as $row) {
122+
foreach ($yieldSla(new Timerange($start, $end), $filter) as $row) {
100123
$row = $this->createReportRow($row);
101124

102125
if ($row === null) {
@@ -111,7 +134,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
111134
}
112135
}
113136
} else {
114-
foreach ($this->fetchSla($timerange, $filter) as $row) {
137+
foreach ($yieldSla($timerange, $filter) as $row) {
115138
$rows[] = $this->createReportRow($row);
116139
}
117140
}
@@ -129,7 +152,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
129152
* @param string|null $boundary English text datetime description for calculating bounds to get
130153
* calendar days, weeks or months instead of relative times according to interval
131154
*
132-
* @return \Generator
155+
* @return Generator
133156
*/
134157
protected function yieldTimerange(Timerange $timerange, DateInterval $interval, $boundary = null)
135158
{
@@ -188,6 +211,12 @@ public function initConfigForm(Form $form)
188211
'min' => '1',
189212
'max' => '12'
190213
]);
214+
215+
$form->addElement('checkbox', 'only-violation', [
216+
'label' => t('Show only critical SLA'),
217+
'checkedValue' => '1',
218+
'uncheckedValue' => '0'
219+
]);
191220
}
192221

193222
public function getData(Timerange $timerange, array $config = null)

0 commit comments

Comments
 (0)