Skip to content

Commit

Permalink
RedundancygroupController:: add some optimizations
Browse files Browse the repository at this point in the history
Use Translation trait's translate() method
  • Loading branch information
sukhwinder33445 committed Nov 14, 2024
1 parent baf553a commit 0d3afba
Showing 1 changed file with 50 additions and 49 deletions.
99 changes: 50 additions & 49 deletions application/controllers/RedundancygroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use Icinga\Exception\NotFoundError;
use Icinga\Module\Icingadb\Common\CommandActions;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Data\DependencyNodes;
use Icinga\Module\Icingadb\Model\DependencyNode;
use Icinga\Module\Icingadb\Model\RedundancyGroup;
use Icinga\Module\Icingadb\Model\RedundancyGroupSummary;
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Widget\DependencyNodeStatistics;
use Icinga\Module\Icingadb\Widget\Detail\MultiselectQuickActions;
use Icinga\Module\Icingadb\Widget\Detail\RedundancyGroupDetail;
use Icinga\Module\Icingadb\Widget\ItemList\DependencyNodeList;
Expand Down Expand Up @@ -46,34 +44,34 @@ public function init(): void
}

$this->groupId = $groupId;
}

/**
* Load the redundancy group
*/
protected function loadGroup(): void
{
$query = RedundancyGroup::on($this->getDb())
->with(['state'])
->filter(Filter::equal('id', $this->groupId));

$this->applyRestrictions($query);
$this->filter($query);

$this->group = $query->first();

if ($this->group === null) {
throw new NotFoundError(t('Redundancy Group not found'));
throw new NotFoundError($this->translate('Redundancy Group not found'));
}

$this->setTitleTab($this->getRequest()->getActionName());
$this->setTitle($this->group->display_name);

$this->addControl(new HtmlElement('div', null, Text::create($this->group->display_name)));
$this->addFooter(
new DependencyNodeStatistics(
RedundancyGroupSummary::on($this->getDb())
->filter(Filter::equal('id', $this->groupId))
->first()
)
);
}

public function indexAction(): void
{
$this->loadGroup();
$summary = RedundancyGroupSummary::on($this->getDb())
->filter(Filter::equal('id', $this->groupId));

Expand All @@ -90,25 +88,19 @@ public function indexAction(): void
$this->addContent(new RedundancyGroupDetail($this->group));
}

public function membersAction(): \Generator
public function membersAction(): void
{
$this->loadGroup();
$nodesQuery = $this->fetchNodes(true);

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($nodesQuery);
$sortControl = $this->createSortControl(
$nodesQuery,
[
'name' => t('Name'),
'severity desc, last_state_change desc' => t('Severity'),
'state' => t('Current State'),
'last_state_change desc' => t('Last State Change')
]
);
$sortControl = $this->createSortControl($nodesQuery);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);

$searchBar = $this->createSearchBar($nodesQuery,
Links::redundancyGroupMembers($this->group),
$searchBar = $this->createSearchBar(
$nodesQuery,
Url::fromPath('icingadb/redundancygroup/members', ['id' => $this->groupId]),
[
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
Expand All @@ -131,8 +123,6 @@ public function membersAction(): \Generator

$nodesQuery->filter($filter);

yield $this->export($nodesQuery);

$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
Expand All @@ -151,26 +141,19 @@ public function membersAction(): \Generator
$this->setAutorefreshInterval(10);
}

public function childrenAction()
public function childrenAction(): void
{
$this->loadGroup();
$nodesQuery = $this->fetchNodes();

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($nodesQuery);
$sortControl = $this->createSortControl(
$nodesQuery,
[
'name' => t('Name'),
'severity desc, last_state_change desc' => t('Severity'),
'state' => t('Current State'),
'last_state_change desc' => t('Last State Change')
]
);
$sortControl = $this->createSortControl($nodesQuery);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);

$searchBar = $this->createSearchBar(
$nodesQuery,
Links::redundancyGroupChildren($this->group),
Url::fromPath('icingadb/redundancygroup/children', ['id' => $this->groupId]),
[
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
Expand All @@ -196,8 +179,6 @@ public function childrenAction()

$nodesQuery->filter($filter);

yield $this->export($nodesQuery);

$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
Expand Down Expand Up @@ -233,10 +214,11 @@ public function searchEditorAction(): void
{
$isChildrenTab = $this->params->shift('isChildrenTab');
$redirectUrl = $isChildrenTab
? Links::redundancyGroupChildren($this->group)
: Links::redundancyGroupMembers($this->group);
? Url::fromPath('icingadb/redundancygroup/children', ['id' => $this->groupId])
: Url::fromPath('icingadb/redundancygroup/members', ['id' => $this->groupId]);

$editor = $this->createSearchEditor(DependencyNode::on($this->getDb()),
$editor = $this->createSearchEditor(
DependencyNode::on($this->getDb()),
$redirectUrl,
[
LimitControl::DEFAULT_LIMIT_PARAM,
Expand All @@ -251,23 +233,23 @@ public function searchEditorAction(): void
}

$this->getDocument()->add($editor);
$this->setTitle(t('Adjust Filter'));
$this->setTitle($this->translate('Adjust Filter'));
}

protected function createTabs(): Tabs
{
$tabs = $this->getTabs()
->add('index', [
'label' => t('Redundancy Group'),
'url' => Links::redundancyGroup($this->group)
'label' => $this->translate('Redundancy Group'),
'url' => Url::fromPath('icingadb/redundancygroup', ['id' => $this->groupId])
])
->add('members', [
'label' => t('Members'),
'url' => Links::redundancyGroupMembers($this->group)
'label' => $this->translate('Members'),
'url' => Url::fromPath('icingadb/redundancygroup/members', ['id' => $this->groupId])
])
->add('children', [
'label' => t('Children'),
'url' => Links::redundancyGroupChildren($this->group)
'label' => $this->translate('Children'),
'url' => Url::fromPath('icingadb/redundancygroup/children', ['id' => $this->groupId])
]);

return $tabs;
Expand All @@ -282,6 +264,25 @@ protected function setTitleTab(string $name): void
}
}

public function createSortControl(Query $query, array $columns = null): SortControl
{
$sortRules = [
'host.display_name, service.display_name, redundancy_group.display_name' => $this->translate('Name'),
'service.state.severity desc, service.state.last_state_change desc, '
. 'host.state.severity desc, host.state.last_state_change desc, '
. 'redundancy_group.state.failed desc, redundancy_group.state.last_state_change desc' => $this->translate(
'Severity'
),
'service.state.soft_state, host.state.soft_state, redundancy_group.state.failed' => $this->translate(
'Current State'
),
'service.state.last_state_change desc, host.state.last_state_change desc, '
. 'redundancy_group.state.last_state_change desc' => $this->translate('Last State Change')
];

return parent::createSortControl($query, $sortRules);
}

/**
* Fetch the nodes for the current group
*
Expand Down Expand Up @@ -333,7 +334,7 @@ protected function fetchCommandTargets()

protected function getCommandTargetsUrl(): Url
{
return Links::redundancyGroup($this->group);
return Url::fromPath('icingadb/redundancygroup', ['id' => $this->groupId]);
}

public function processCheckresultAction(): void
Expand Down

0 comments on commit 0d3afba

Please sign in to comment.