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

Add setmembers summyary line to TemplateUsageTable #2378

Merged
merged 5 commits into from
Sep 21, 2023
Merged
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
32 changes: 31 additions & 1 deletion library/Director/Db/IcingaObjectFilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree;
use InvalidArgumentException;
use Ramsey\Uuid\UuidInterface;
use RuntimeException;
use Zend_Db_Select as ZfSelect;

Expand Down Expand Up @@ -46,13 +47,42 @@ public static function filterByTemplate(
ZfSelect $query,
$template,
$tableAlias = 'o',
$inheritanceType = self::INHERIT_DIRECT
$inheritanceType = self::INHERIT_DIRECT,
UuidInterface $branchuuid = null
) {
$i = $tableAlias . 'i';
$o = $tableAlias;
$type = $template->getShortTableName();
$db = $template->getDb();
$id = static::wantId($template);

if ($branchuuid) {
if ($inheritanceType === self::INHERIT_DIRECT) {
return $query->where('imports LIKE \'%"' . $template->getObjectName() . '"%\'');
} elseif ($inheritanceType === self::INHERIT_INDIRECT
|| $inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT
) {
$tree = new TemplateTree($type, $template->getConnection());
$templateNames = $tree->getDescendantsFor($template);

if ($inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT) {
$templateNames[] = $template->getObjectName();
}

if (empty($templateNames)) {
$condition = '(1 = 0)';
} else {
$condition = 'imports LIKE \'%"' . array_pop($templateNames) . '"%\'';

foreach ($templateNames as $templateName) {
$condition .= " OR imports LIKE '%\"$templateName\"%'";
}
}

return $query->where($condition);
}
}

$sub = $db->select()->from(
array($i => "icinga_{$type}_inheritance"),
array('e' => '(1)')
Expand Down
2 changes: 1 addition & 1 deletion library/Director/Web/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function getTable()
*/
protected function getApplyRulesTable()
{
$table = new ApplyRulesTable($this->db());
$table = (new ApplyRulesTable($this->db()))->setBranch($this->getBranch());
$table->setType($this->getType())
->setBaseObjectUrl($this->getBaseObjectUrl());
$this->eventuallyFilterCommand($table);
Expand Down
27 changes: 26 additions & 1 deletion library/Director/Web/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
use Icinga\Module\Director\Web\Table\ApplyRulesTable;
use Icinga\Module\Director\Web\Table\ObjectsTable;
use Icinga\Module\Director\Web\Table\ObjectsTableSetMembers;
use Icinga\Module\Director\Web\Table\TemplatesTable;
use Icinga\Module\Director\Web\Table\TemplateUsageTable;
use Icinga\Module\Director\Web\Tabs\ObjectTabs;
Expand All @@ -24,6 +25,8 @@ abstract class TemplateController extends CompatController
{
use DirectorDb;

use BranchHelper;

/** @var IcingaObject */
protected $template;

Expand All @@ -42,7 +45,28 @@ public function objectsAction()

ObjectsTable::create($this->getType(), $this->db())
->setAuth($this->Auth())
->setBranch($this->getBranch())
->setBaseObjectUrl($this->getBaseObjectUrl())
->filterTemplate($template, $this->getInheritance())
->renderTo($this);
}

public function setmembersAction()
{
$template = $this->requireTemplate();
$plural = $this->getTranslatedPluralType();
$this
->addSingleTab($plural)
->setAutorefreshInterval(10)
->addTitle(
$this->translate('%s in service sets based on %s'),
$plural,
$template->getObjectName()
)->addBackToUsageLink($template);

ObjectsTableSetMembers::create($this->getType(), $this->db(), $this->Auth())
->setBaseObjectUrl($this->getBaseObjectUrl())
->setBranch($this->getBranch())
->filterTemplate($template, $this->getInheritance())
->renderTo($this);
}
Expand All @@ -61,6 +85,7 @@ public function applyrulesAction()

ApplyRulesTable::create($type, $this->db())
->setBaseObjectUrl($this->getBaseObjectUrl())
->setBranch($this->getBranch())
->filterTemplate($template, $this->params->get('inheritance', 'direct'))
->renderTo($this);
}
Expand Down Expand Up @@ -189,7 +214,7 @@ public function usageAction()

try {
$this->content()->add(
TemplateUsageTable::forTemplate($template)
TemplateUsageTable::forTemplate($template, $this->getBranch())
);
} catch (NestingError $e) {
$this->content()->add(Hint::error($e->getMessage()));
Expand Down
105 changes: 99 additions & 6 deletions library/Director/Web/Table/ApplyRulesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\DbSelectParenthesis;
use Icinga\Module\Director\Db\DbUtil;
use Icinga\Module\Director\Db\IcingaObjectFilterHelper;
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
Expand All @@ -20,6 +21,8 @@

class ApplyRulesTable extends ZfQueryBasedTable
{
use TableWithBranchSupport;

protected $searchColumns = [
'o.object_name',
'o.assign_filter',
Expand Down Expand Up @@ -94,10 +97,14 @@ public function renderRow($row)
// NOT (YET) static::td($this->createActionLinks($row))->setSeparator(' ')
]);

$classes = $this->getRowClasses($row);

if ($row->disabled === 'y') {
$tr->getAttributes()->add('class', 'disabled');
$classes[] = 'disabled';
}

$tr->getAttributes()->add('class', $classes);

return $tr;
}

Expand All @@ -117,7 +124,8 @@ public function filterTemplate(
$this->getQuery(),
$template,
'o',
$inheritance
$inheritance,
$this->branchUuid
);

return $this;
Expand Down Expand Up @@ -196,6 +204,15 @@ protected function applyRestrictions(ZfSelect $query)
return FilterRenderer::applyToQuery($filter, $query);
}

protected function getRowClasses($row)
{
// TODO: remove isset, to figure out where it is missing
if (isset($row->branch_uuid) && $row->branch_uuid !== null) {
return ['branch_modified'];
}
return [];
}


/**
* @return IcingaObject
Expand All @@ -216,6 +233,7 @@ public function prepareQuery()
'id' => 'o.id',
'uuid' => 'o.uuid',
'object_name' => 'o.object_name',
'object_type' => 'o.object_type',
'disabled' => 'o.disabled',
'assign_filter' => 'o.assign_filter',
'apply_for' => '(NULL)',
Expand All @@ -224,17 +242,92 @@ public function prepareQuery()
if ($table === 'icinga_service') {
$columns['apply_for'] = 'o.apply_for';
}

$conn = $this->connection();
$query = $this->db()->select()->from(
['o' => $table],
$columns
)->where(
"object_type = 'apply'"
)->order('o.object_name');

if ($this->type === 'service') {
$query->where('service_set_id IS NULL');
if ($this->branchUuid) {
$columns = $this->branchifyColumns($columns);
$columns['branch_uuid'] = 'bo.branch_uuid';
if ($conn->isPgsql()) {
$columns['imports'] = 'CONCAT(\'[\', ARRAY_TO_STRING(ARRAY_AGG'
. '(CONCAT(\'"\', sub_o.object_name, \'"\')), \',\'), \']\')';
} else {
$columns['imports'] = 'CONCAT(\'[\', '
. 'GROUP_CONCAT(CONCAT(\'"\', sub_o.object_name, \'"\')), \']\')';
}

$this->stripSearchColumnAliases();

$query->reset('columns');
$right = clone($query);

$query->columns($columns)
->joinLeft(
['oi' => $table . '_inheritance'],
'o.id = oi.' . $this->getType() . '_id',
[]
)->joinLeft(
['sub_o' => $table],
'sub_o.id = oi.parent_' . $this->getType() . '_id',
[]
)->group(['o.id', 'bo.uuid', 'bo.branch_uuid']);

$query->joinLeft(
['bo' => "branched_$table"],
// TODO: PgHexFunc
$this->db()->quoteInto(
'bo.uuid = o.uuid AND bo.branch_uuid = ?',
DbUtil::quoteBinaryLegacy($this->branchUuid->getBytes(), $this->db())
),
[]
)->where("(bo.branch_deleted IS NULL OR bo.branch_deleted = 'n')");

if ($this->type === 'service') {
$query->where('o.service_set_id IS NULL AND bo.service_set IS NULL');
}

$columns['imports'] = 'bo.imports';

$right->columns($columns)
->joinRight(
['bo' => "branched_$table"],
'bo.uuid = o.uuid',
[]
)
->where('o.uuid IS NULL')
->where('bo.branch_uuid = ?', $conn->quoteBinary($this->branchUuid->getBytes()));

$query = $this->db()->select()->union([
'l' => new DbSelectParenthesis($query),
'r' => new DbSelectParenthesis($right),
]);

$query = $this->db()->select()->from(['u' => $query]);
$query->order('object_name')->limit(100);
} else {
if ($this->type === 'service') {
$query->where('service_set_id IS NULL');
}
}

$query->where(
"object_type = 'apply'"
);

$this->applyRestrictions($query);

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

/**
* @return Db
*/
public function connection()
{
return parent::connection();
}
}
12 changes: 9 additions & 3 deletions library/Director/Web/Table/DependencyTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;

class DependencyTemplateUsageTable extends TemplateUsageTable
{
public function getTypes()
Expand All @@ -12,11 +14,15 @@ public function getTypes()
];
}

protected function getTypeSummaryDefinitions()
protected function getSummaryTables(string $templateType, Db $connection)
{
return [
'templates' => $this->getSummaryLine('template'),
'applyrules' => $this->getSummaryLine('apply'),
'templates' => TemplatesTable::create(
$templateType,
$connection
),
'applyrules' => ApplyRulesTable::create($templateType, $connection)
->setBranchUuid($this->branchUuid)
];
}
}
8 changes: 0 additions & 8 deletions library/Director/Web/Table/HostTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@ public function getTypes()
'objects' => $this->translate('Objects'),
];
}

protected function getTypeSummaryDefinitions()
{
return [
'templates' => $this->getSummaryLine('template'),
'objects' => $this->getSummaryLine('object'),
];
}
}
4 changes: 4 additions & 0 deletions library/Director/Web/Table/IcingaServiceSetServiceTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ protected function getServiceLink($row)
];
$url = 'director/host/servicesetservice';
} else {
if (is_resource($row->uuid)) {
$row->uuid =stream_get_contents($row->uuid);
}

$params = [
'uuid' => Uuid::fromBytes($row->uuid)->toString(),
];
Expand Down
12 changes: 9 additions & 3 deletions library/Director/Web/Table/NotificationTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;

class NotificationTemplateUsageTable extends TemplateUsageTable
{
public function getTypes()
Expand All @@ -12,11 +14,15 @@ public function getTypes()
];
}

protected function getTypeSummaryDefinitions()
protected function getSummaryTables(string $templateType, Db $connection)
{
return [
'templates' => $this->getSummaryLine('template'),
'applyrules' => $this->getSummaryLine('apply', 'o.host_id IS NULL'),
'templates' => TemplatesTable::create(
$templateType,
$connection
),
'applyrules' => ApplyRulesTable::create($templateType, $connection)
->setBranchUuid($this->branchUuid)
];
}
}
7 changes: 4 additions & 3 deletions library/Director/Web/Table/ObjectSetTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ protected function prepareQuery()
['bo' => "branched_icinga_{$type}"],
"bo.{$type}_set = bos.object_name",
[]
);
)->group(['bo.object_name', 'o.object_name']);
$query->joinLeft(
['bo' => "branched_icinga_{$type}"],
"bo.{$type}_set = bos.object_name",
[]
);
)->group(['bo.object_name', 'o.object_name']);
$this->queries = [
$query,
$right
Expand All @@ -185,8 +185,9 @@ protected function prepareQuery()
->group('object_type')
->group('assign_filter')
->group('description')
->group('service_object_name')
->group('count_services');
};
}
} else {
// Disabled for now, check for correctness:
// $query->joinLeft(
Expand Down
Loading