Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Feb 9, 2023
1 parent d14c732 commit 837e50f
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 25 deletions.
5 changes: 5 additions & 0 deletions library/Icingadb/Model/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ public function createRelations(Relations $relations)
$relations->belongsToMany('hostgroup', Hostgroup::class)
->through(HostgroupMember::class);

$relations->hasMany('sla_history_state', SlaHistoryState::class)
->setJoinType('LEFT');
$relations->hasMany('sla_history_downtime', SlaHistoryDowntime::class)
->setJoinType('LEFT');

$relations->hasOne('state', HostState::class)->setJoinType('LEFT');
$relations->hasMany('comment', Comment::class)->setJoinType('LEFT');
$relations->hasMany('downtime', Downtime::class)->setJoinType('LEFT');
Expand Down
108 changes: 108 additions & 0 deletions library/Icingadb/Model/HostSlaHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Icinga\Module\Icingadb\Model;

use ipl\Orm\Relations;
use ipl\Orm\UnionModel;
use ipl\Sql\Expression;

class HostSlaHistory extends UnionModel
{
public function getTableName()
{
return 'host';
}

public function getKeyName()
{
return 'host_id';
}

public function getColumns()
{
return [
'host_id',
'display_name',
'event_time',
'event_type',
'event_priority',
'hard_state',
'previous_hard_state'
];
}

public function getUnions()
{
return [
[
Host::class,
[
'sla_history_downtime'
],
[
'display_name' => 'host.display_name',
'event_priority' => new Expression('1'),
'event_time' => 'sla_history_downtime.downtime_start',
'event_type' => new Expression('\'downtime_start\''),
'hard_state' => new Expression('NULL'),
'host_id' => 'id',
'previous_hard_state' => new Expression('NULL'),
]
],
[
Host::class,
[
'sla_history_downtime'
],
[
'display_name' => 'host.display_name',
'event_priority' => new Expression('2'),
'event_time' => 'sla_history_downtime.downtime_end',
'event_type' => new Expression('\'downtime_end\''),
'hard_state' => new Expression('NULL'),
'host_id' => 'id',
'previous_hard_state' => new Expression('NULL'),
]
],
[
Host::class,
[
'sla_history_state'
],
[
'display_name' => 'display_name',
'event_priority' => new Expression('0'),
'event_time' => 'sla_history_state.event_time',
'event_type' => new Expression('\'state_change\''),
'hard_state' => 'sla_history_state.hard_state',
'host_id' => 'id',
'previous_hard_state' => 'sla_history_state.previous_hard_state',
]
],
// Fake result!!
[
Host::class,
[],
[
'display_name' => 'host.display_name',
'event_priority' => new Expression('3'),
'event_time' => new Expression('NULL'),
'event_type' => new Expression('\'end\''),
'hard_state' => new Expression('NULL'),
'host_id' => 'id',
'previous_hard_state' => new Expression('NULL'),
]
]
];
}

public function getDefaultSort()
{
return ['display_name', 'event_time', 'event_priority'];
}

public function createRelations(Relations $relations)
{
(new Host())->createRelations($relations);
}
}
59 changes: 59 additions & 0 deletions library/Icingadb/Model/SlaHistoryDowntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Icinga\Module\Icingadb\Model;

use Icinga\Module\Icingadb\Model\Behavior\Timestamp;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Relations;

class SlaHistoryDowntime extends Model
{
public function getTableName()
{
return 'sla_history_downtime';
}

public function getKeyName()
{
return 'id';
}

public function getColumns()
{
return [
'environment_id',
'endpoint_id',
'host_id',
'service_id',
'downtime_id',
'object_type',
'downtime_start',
'downtime_end'
];
}

public function createBehaviors(Behaviors $behaviors)
{
$behaviors->add(new Binary([
'id',
'environment_id',
'endpoint_id',
'host_id',
'service_id',
'downtime_id'
]));

$behaviors->add(new Timestamp([
'downtime_start',
'downtime_end'
]));
}

public function createRelations(Relations $relations)
{
$relations->belongsTo('host', Host::class);
$relations->belongsTo('service', Service::class);
}
}
60 changes: 60 additions & 0 deletions library/Icingadb/Model/SlaHistoryState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Icinga\Module\Icingadb\Model;

use Icinga\Module\Icingadb\Model\Behavior\Timestamp;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Relations;

class SlaHistoryState extends Model
{
public function getTableName()
{
return 'sla_history_state';
}

public function getKeyName()
{
return 'id';
}

public function getColumns()
{
return [
'environment_id',
'endpoint_id',
'host_id',
'service_id',
'object_type',
'event_time',
'hard_state',
'previous_hard_state'
];
}

public function createBehaviors(Behaviors $behaviors)
{
$behaviors->add(new Binary([
'id',
'environment_id',
'endpoint_id',
'host_id',
'service_id'
]));

$behaviors->add(new Timestamp(['event_time']));
}

public function createRelations(Relations $relations)
{
$relations->belongsTo('host', Host::class);
$relations->belongsTo('service', Service::class);
}

public function getDefaultSort()
{
return ['event_time'];
}
}
68 changes: 51 additions & 17 deletions library/Icingadb/ProvidedHook/Reporting/HostSlaReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace Icinga\Module\Icingadb\ProvidedHook\Reporting;

use Icinga\Application\Icinga;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\HostSlaHistory;
use Icinga\Module\Reporting\ReportData;
use Icinga\Module\Reporting\ReportRow;
use Icinga\Module\Reporting\Timerange;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
use ipl\Stdlib\Filter\Rule;

use function ipl\I18n\t;
Expand Down Expand Up @@ -46,23 +47,56 @@ protected function createReportRow($row)

protected function fetchSla(Timerange $timerange, Rule $filter = null)
{
$sla = Host::on($this->getDb())
->columns([
'display_name',
'sla' => new Expression(sprintf(
"get_sla_ok_percent(%s, NULL, '%s', '%s')",
'host.id',
$timerange->getStart()->format('Uv'),
$timerange->getEnd()->format('Uv')
))
]);

$this->applyRestrictions($sla);

if ($filter !== null) {
$sla->filter($filter);
$start = (int) $timerange->getStart()->format('U');
$end = (int) $timerange->getEnd()->format('U');

$query = HostSlaHistory::on($this->getDb());

$index = 0;
foreach ($query->getUnions() as $union) {
$filterAll = Filter::all();
if ($index >= 2) {
if ($index < 3) {
$filterAll
->add(Filter::unlike('sla_history_state.service_id', '*'))
->add(Filter::greaterThan('sla_history_state.event_time', $start))
->add(Filter::lessThan('sla_history_state.event_time', $end));
} else {
$union->columns(array_merge($union->getColumns(), ['event_time' => new Expression($end * 1000)]));
}
} else {
$filterAll
->add(Filter::unlike('sla_history_downtime.service_id', '*'))
->add(Filter::lessThan('sla_history_downtime.downtime_start', $end))
->add(Filter::greaterThanOrEqual('sla_history_downtime.downtime_end', $start));

if ($index === 1) {
$filterAll->add(Filter::lessThan('sla_history_downtime.downtime_end', $end));
} else {
$union->columns(
array_merge(
$union->getColumns(),
[
'event_time' => new Expression(
sprintf('GREATEST(host_sla_history_downtime.downtime_start, %s)', $start * 1000)
)
]
)
);
}
}

++$index;

$union->filter($filterAll);

if ($filter !== null) {
$union->filter($filter);
}
}

return $sla;
$this->applyRestrictions($query);

return $query;
}
}
Loading

0 comments on commit 837e50f

Please sign in to comment.