diff --git a/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php b/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php index d9c4f4f13..0de719fb3 100644 --- a/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php +++ b/library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php @@ -9,6 +9,7 @@ use Icinga\Module\Reporting\ReportData; use Icinga\Module\Reporting\ReportRow; use Icinga\Module\Reporting\Timerange; +use ipl\Orm\Query; use ipl\Sql\Expression; use ipl\Stdlib\Filter\Rule; @@ -44,7 +45,7 @@ protected function createReportRow($row) ->setValues([(float) $row->sla]); } - protected function fetchSla(Timerange $timerange, Rule $filter = null) + protected function fetchSla(Timerange $timerange, Rule $filter = null): Query { $sla = Host::on($this->getDb()) ->columns([ diff --git a/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php b/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php index 46a068437..6a8b78dd9 100644 --- a/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php +++ b/library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php @@ -9,6 +9,7 @@ use Icinga\Module\Reporting\ReportData; use Icinga\Module\Reporting\ReportRow; use Icinga\Module\Reporting\Timerange; +use ipl\Orm\Query; use ipl\Sql\Expression; use ipl\Stdlib\Filter\Rule; @@ -44,7 +45,7 @@ protected function createReportRow($row) ->setValues([(float) $row->sla]); } - protected function fetchSla(Timerange $timerange, Rule $filter = null) + protected function fetchSla(Timerange $timerange, Rule $filter = null): Query { $sla = Service::on($this->getDb()) ->columns([ diff --git a/library/Icingadb/ProvidedHook/Reporting/SlaReport.php b/library/Icingadb/ProvidedHook/Reporting/SlaReport.php index b5898fd83..ef7d7cc63 100644 --- a/library/Icingadb/ProvidedHook/Reporting/SlaReport.php +++ b/library/Icingadb/ProvidedHook/Reporting/SlaReport.php @@ -6,6 +6,7 @@ use DateInterval; use DatePeriod; +use Generator; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Common\Database; use Icinga\Module\Icingadb\Widget\EmptyState; @@ -15,6 +16,8 @@ use Icinga\Module\Reporting\Timerange; use ipl\Html\Form; use ipl\Html\Html; +use ipl\Orm\Query; +use ipl\Sql\Expression; use ipl\Stdlib\Filter\Rule; use ipl\Web\Filter\QueryString; @@ -57,9 +60,9 @@ abstract protected function createReportRow($row); * @param Timerange $timerange * @param Rule|null $filter * - * @return iterable + * @return Query */ - abstract protected function fetchSla(Timerange $timerange, Rule $filter = null); + abstract protected function fetchSla(Timerange $timerange, Rule $filter = null): Query; protected function fetchReportData(Timerange $timerange, array $config = null) { @@ -69,6 +72,27 @@ protected function fetchReportData(Timerange $timerange, array $config = null) $filter = trim((string) $config['filter']) ?: '*'; $filter = $filter !== '*' ? QueryString::parse($filter) : null; + $yieldSla = function (Timerange $timerange, Rule $filter = null) use ($config): Generator { + $sla = $this->fetchSla($timerange, $filter); + + if ($config['only-violation'] === '1') { + $threshold = $config['threshold'] ?? static::DEFAULT_THRESHOLD; + + $sla->assembleSelect(); + $sla->getSelectBase()->where(new Expression( + '(%s) < %F', + [$sla->getColumns()['sla']->getStatement(), $threshold] + )); + //$sla->filter(Filter::lessThan('sla', $threshold)); + //$sla->getSelectBase()->where(['sla < ?' => $threshold]) requires to wrap Model again and + // order by sla after + } + + foreach ($sla as $row) { + yield $row; + } + }; + if (isset($config['breakdown']) && $config['breakdown'] !== 'none') { switch ($config['breakdown']) { case 'day': @@ -96,7 +120,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null) $rd->setDimensions($dimensions); foreach ($this->yieldTimerange($timerange, $interval, $boundary) as list($start, $end)) { - foreach ($this->fetchSla(new Timerange($start, $end), $filter) as $row) { + foreach ($yieldSla(new Timerange($start, $end), $filter) as $row) { $row = $this->createReportRow($row); if ($row === null) { @@ -111,7 +135,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null) } } } else { - foreach ($this->fetchSla($timerange, $filter) as $row) { + foreach ($yieldSla($timerange, $filter) as $row) { $rows[] = $this->createReportRow($row); } } @@ -129,7 +153,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null) * @param string|null $boundary English text datetime description for calculating bounds to get * calendar days, weeks or months instead of relative times according to interval * - * @return \Generator + * @return Generator */ protected function yieldTimerange(Timerange $timerange, DateInterval $interval, $boundary = null) { @@ -188,6 +212,12 @@ public function initConfigForm(Form $form) 'min' => '1', 'max' => '12' ]); + + $form->addElement('checkbox', 'only-violation', [ + 'label' => t('Show only critical SLA'), + 'checkedValue' => '1', + 'uncheckedValue' => '0' + ]); } public function getData(Timerange $timerange, array $config = null)