Skip to content

Commit

Permalink
Dependency schema (#1049)
Browse files Browse the repository at this point in the history
Models for the new schema additions described here:
Icinga/icingadb#347 (comment)
  • Loading branch information
nilmerg authored Sep 5, 2024
2 parents 334aaf8 + 3171bf5 commit ec1d3d7
Show file tree
Hide file tree
Showing 10 changed files with 619 additions and 0 deletions.
95 changes: 95 additions & 0 deletions library/Icingadb/Model/Dependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model;

use Icinga\Module\Icingadb\Model\Behavior\Bitmask;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behavior\BoolCast;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;

/**
* Dependency model.
*
* @property string $id
* @property string $name
* @property string $display_name
* @property ?string $redundancy_group_id
* @property bool $disable_checks
* @property bool $disable_notifications
* @property bool $ignore_soft_states
* @property ?string $timeperiod_id
* @property string[] $states
*
* @property (?Timeperiod)|Query $timeperiod
* @property (?RedundancyGroup)|Query $redundancy_group
* @property (?DependencyState)|Query $state
* @property DependencyEdge|Query $edge
*/
class Dependency extends Model
{
public function getTableName(): string
{
return 'dependency';
}

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

public function getColumns(): array
{
return [
'name',
'display_name',
'redundancy_group_id',
'disable_checks',
'disable_notifications',
'ignore_soft_states',
'timeperiod_id',
'states'
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'id',
'redundancy_group_id',
'timeperiod_id'
]));
$behaviors->add(new BoolCast([
'disable_checks',
'disable_notifications',
'ignore_soft_states'
]));
$behaviors->add(new Bitmask([
'states' => [
'ok' => 1,
'warning' => 2,
'critical' => 4,
'unknown' => 8,
'up' => 16,
'down' => 32
],
]));
}

public function createRelations(Relations $relations): void
{
$relations->belongsTo('timeperiod', Timeperiod::class)
->setJoinType('LEFT');
$relations->belongsTo('redundancy_group', RedundancyGroup::class)
->setJoinType('LEFT');

$relations->hasOne('state', DependencyState::class)
->setJoinType('LEFT');

$relations->hasOne('edge', DependencyEdge::class);
}
}
63 changes: 63 additions & 0 deletions library/Icingadb/Model/DependencyEdge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model;

use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;

/**
* Dependency edge model.
*
* @property string $to_node_id
* @property string $from_node_id
* @property ?string $dependency_id
*
* @property DependencyNode|Query $from
* @property DependencyNode|Query $to
* @property (?Dependency)|Query $dependency
*/
class DependencyEdge extends Model
{
public function getTableName(): string
{
return 'dependency_edge';
}

public function getKeyName(): array
{
return ['to_node_id', 'from_node_id'];
}

public function getColumns(): array
{
return [
'to_node_id',
'from_node_id',
'dependency_id'
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'to_node_id',
'from_node_id',
'dependency_id'
]));
}

public function createRelations(Relations $relations): void
{
$relations->belongsTo('from', DependencyNode::class)
->setCandidateKey('from_node_id');
$relations->belongsTo('to', DependencyNode::class)
->setCandidateKey('to_node_id');
$relations->belongsTo('dependency', Dependency::class)
->setJoinType('LEFT');
}
}
88 changes: 88 additions & 0 deletions library/Icingadb/Model/DependencyNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model;

use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;

/**
* Dependency node model.
*
* @property string $id
* @property ?string $host_id
* @property ?string $service_id
* @property ?string $redundancy_group_id
*
* @property (?Host)|Query $host
* @property (?Service)|Query $service
* @property (?RedundancyGroup)|Query $redundancy_group
* @property (?DependencyEdge)|Query $from
* @property (?DependencyEdge)|Query $to
* @property (?DependencyNode)|Query $child
* @property (?DependencyNode)|Query $parent
*/
class DependencyNode extends Model
{
public function getTableName(): string
{
return 'dependency_node';
}

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

public function getColumns(): array
{
return [
'id',
'host_id',
'service_id',
'redundancy_group_id'
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'id',
'host_id',
'service_id',
'redundancy_group_id'
]));
}

public function createRelations(Relations $relations): void
{
$relations->belongsTo('host', Host::class)
->setJoinType('LEFT');
$relations->belongsTo('service', Service::class)
->setJoinType('LEFT');
$relations->belongsTo('redundancy_group', RedundancyGroup::class)
->setJoinType('LEFT');

$relations->hasMany('from', DependencyEdge::class)
->setForeignKey('from_node_id')
->setJoinType('LEFT');
$relations->hasMany('to', DependencyEdge::class)
->setForeignKey('to_node_id')
->setJoinType('LEFT');

$relations->belongsToMany('child', self::class)
->through(DependencyEdge::class)
->setForeignKey('to_node_id')
->setTargetForeignKey('from_node_id')
->setJoinType('LEFT');
$relations->belongsToMany('parent', self::class)
->through(DependencyEdge::class)
->setForeignKey('from_node_id')
->setTargetForeignKey('to_node_id')
->setJoinType('LEFT');
}
}
58 changes: 58 additions & 0 deletions library/Icingadb/Model/DependencyState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model;

use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behavior\BoolCast;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;

/**
* Dependency state model.
*
* @property string $id
* @property string $dependency_id
* @property bool $failed
*
* @property Dependency|Query $dependency
*/
class DependencyState extends Model
{
public function getTableName(): string
{
return 'dependency_state';
}

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

public function getColumns(): array
{
return [
'dependency_id',
'failed'
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'id',
'dependency_id'
]));
$behaviors->add(new BoolCast([
'failed'
]));
}

public function createRelations(Relations $relations): void
{
$relations->belongsTo('dependency', Dependency::class);
}
}
4 changes: 4 additions & 0 deletions library/Icingadb/Model/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public function createBehaviors(Behaviors $behaviors)
]));

$behaviors->add(new ReRoute([
'child' => 'dependency_node.child',
'parent' => 'dependency_node.parent',
'servicegroup' => 'service.servicegroup',
'user' => 'notification.user',
'usergroup' => 'notification.usergroup'
Expand Down Expand Up @@ -235,6 +237,8 @@ public function createDefaults(Defaults $defaults)

public function createRelations(Relations $relations)
{
$relations->belongsTo('dependency_node', DependencyNode::class)
->setJoinType('LEFT');
$relations->belongsTo('environment', Environment::class);
$relations->belongsTo('eventcommand', Eventcommand::class);
$relations->belongsTo('checkcommand', Checkcommand::class);
Expand Down
67 changes: 67 additions & 0 deletions library/Icingadb/Model/RedundancyGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model;

use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behavior\BoolCast;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;

/**
* Redundancy group model.
*
* @property string $id
* @property string $name
* @property string $display_name
*
* @property (?RedundancyGroupState)|Query $state
* @property Dependency|Query $dependency
*/
class RedundancyGroup extends Model
{
public function getTableName(): string
{
return 'redundancy_group';
}

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

public function getColumns(): array
{
return [
'name',
'display_name'
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'id'
]));
$behaviors->add(new ReRoute([
'child' => 'dependency_node.child',
'parent' => 'dependency_node.parent'
]));
}

public function createRelations(Relations $relations): void
{
$relations->belongsTo('dependency_node', DependencyNode::class)
->setForeignKey('redundancy_group_id')
->setCandidateKey('id');

$relations->hasOne('state', RedundancyGroupState::class)
->setJoinType('LEFT');

$relations->hasMany('dependency', Dependency::class);
}
}
Loading

0 comments on commit ec1d3d7

Please sign in to comment.