Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlaReport: Add checkbox to optionally show critical SLA of hosts/serv… #680

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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([
Expand Down
3 changes: 2 additions & 1 deletion library/Icingadb/ProvidedHook/Reporting/ServiceSlaReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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([
Expand Down
40 changes: 35 additions & 5 deletions library/Icingadb/ProvidedHook/Reporting/SlaReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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)
{
Expand All @@ -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':
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down