icingadb-web/library/Icingadb/Model/RedundancyGroup.php
Sukhwinder Dhillon eb7179ffd8 RedundancyGroupSummary: Use DepenedecyNode as base to apply restrictions correctly
* RedundancyGroupSummary:
  - Columns are same as in DependencyNodeSummary. Column `redundancy_group.state.failed` excluded because not required here.
  - Don't sort by default. Sorting the summary is not necessary and even fails for PostgreSQL as it would require adding the column to the GROUP BY clause.
  - Introduce new method for(), to easily get the summary query for given group id. Adjust filter. The summary is always for parent (member).
2025-06-17 15:08:48 +02:00

98 lines
2.4 KiB
PHP

<?php
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Icingadb\Model;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Backend;
use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Defaults;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;
/**
* Redundancy group model.
*
* @property string $id
* @property string $environment_id
* @property string $display_name
*
* @property (?RedundancyGroupState)|Query $state
* @property DependencyEdge|Query $from
* @property DependencyEdge|Query $to
*
* @property RedundancyGroupSummary $summary
*/
class RedundancyGroup extends Model
{
use Auth;
public function getTableName(): string
{
return 'redundancy_group';
}
public function getKeyName(): string
{
return 'id';
}
public function getColumns(): array
{
return [
'environment_id',
'display_name'
];
}
public function getColumnDefinitions(): array
{
return [
'display_name' => t('Redundancy Group Display Name')
];
}
public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
'id',
'environment_id'
]));
$behaviors->add(new ReRoute([
'child' => 'to.from',
'parent' => 'from.to'
]));
}
public function createRelations(Relations $relations): void
{
$relations->hasOne('state', RedundancyGroupState::class)
->setJoinType('LEFT');
$relations->hasOne('dependency_node', DependencyNode::class)->setJoinType('LEFT');
$relations->belongsToMany('from', DependencyEdge::class)
->setTargetCandidateKey('from_node_id')
->setTargetForeignKey('id')
->through(DependencyNode::class);
$relations->belongsToMany('to', DependencyEdge::class)
->setTargetCandidateKey('to_node_id')
->setTargetForeignKey('id')
->through(DependencyNode::class);
}
public function createDefaults(Defaults $defaults): void
{
$defaults->add('summary', function (RedundancyGroup $group) {
$summary = RedundancyGroupSummary::for($group->id, Backend::getDb());
$this->applyRestrictions($summary);
return $summary->first();
});
}
}