Skip to content

Commit

Permalink
WIP: Optimize the trait and related code
Browse files Browse the repository at this point in the history
WIP: DetailAssembler trail

(generelize part 1)

WIP

wip
  • Loading branch information
sukhwinder33445 committed Nov 11, 2024
1 parent 6582257 commit b451308
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 55 deletions.
146 changes: 146 additions & 0 deletions application/controllers/RedundancyGroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

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

namespace Icinga\Module\Icingadb\Controllers;

use Icinga\Exception\NotFoundError;
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\ItemList\DependencyNodeList;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Stdlib\Filter;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;

class RedundancyGroupController extends Controller
{
/** @var string */
protected $groupId;

/** @var RedundancyGroup */
protected $group;

public function init(): void
{
$this->addTitleTab(t('Redundancy Group'));

$this->groupId = $this->params->shiftRequired('id');

Check failure on line 34 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Module\Icingadb\Controllers\RedundancyGroupController::$groupId (string) does not accept mixed.

Check failure on line 34 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Icingadb\Controllers\RedundancyGroupController::$groupId (string) does not accept mixed.

$this->group = RedundancyGroup::on($this->getDb())

Check failure on line 36 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Property Icinga\Module\Icingadb\Controllers\RedundancyGroupController::$group (Icinga\Module\Icingadb\Model\RedundancyGroup) does not accept ipl\Orm\Model|null.

Check failure on line 36 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Property Icinga\Module\Icingadb\Controllers\RedundancyGroupController::$group (Icinga\Module\Icingadb\Model\RedundancyGroup) does not accept ipl\Orm\Model|null.
->with(['state'])
->filter(Filter::equal('id', $this->groupId))

Check failure on line 38 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.

Check failure on line 38 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #2 $value of static method ipl\Stdlib\Filter::equal() expects array|bool|float|int|string, mixed given.
->first();

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

public function indexAction()

Check failure on line 46 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Method Icinga\Module\Icingadb\Controllers\RedundancyGroupController::indexAction() has no return type specified.

Check failure on line 46 in application/controllers/RedundancyGroupController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Method Icinga\Module\Icingadb\Controllers\RedundancyGroupController::indexAction() has no return type specified.
{
$membersQuery = DependencyNode::on($this->getDb())
->with([
'host',
'host.state',
'service',
'service.state',
'service.host',
'service.host.state'
])
->filter(Filter::equal('child.redundancy_group.id', $this->groupId));

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

$searchBar = $this->createSearchBar($membersQuery, [
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam(),
'id'
]);
$searchBar->getSuggestionUrl()->addParams(['id' => $this->groupId]);

if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}

$membersQuery->filter($filter);

yield $this->export($membersQuery);

$this->addControl(new HtmlElement('div', null, Text::create($this->group->display_name)));

$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
$this->addControl($searchBar);

$this->addContent(
(new DependencyNodeList($membersQuery->execute()))
->setViewMode($viewModeSwitcher->getViewMode())
);

$this->addFooter(
new DependencyNodeStatistics(
RedundancyGroupSummary::on($this->getDb())
->filter(Filter::equal('id', $this->groupId))
->first()
)
);

$this->setAutorefreshInterval(10);
}

public function completeAction(): void
{
$suggestions = (new ObjectSuggestions())
->setModel(DependencyNode::class)
->setBaseFilter(Filter::equal('child.redundancy_group.id', $this->groupId))
->forRequest($this->getServerRequest());

$this->getDocument()->add($suggestions);
}

public function searchEditorAction(): void
{
$editor = $this->createSearchEditor(DependencyNode::on($this->getDb()), [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM,
'id'
]);

if ($editor->getSuggestionUrl()) {
$editor->getSuggestionUrl()->addParams(['id' => $this->groupId]);
}

$this->getDocument()->add($editor);
$this->setTitle(t('Adjust Filter'));
}
}
56 changes: 55 additions & 1 deletion library/Icingadb/Model/DependencyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\Relations;
use ipl\Sql\Expression;

/**
* Dependency node model.
Expand All @@ -18,6 +19,10 @@
* @property ?string $host_id
* @property ?string $service_id
* @property ?string $redundancy_group_id
* @property ?string $name
* @property ?int $severity
* @property ?int $state
* @property ?int $last_state_change
*
* @property (?Host)|Query $host
* @property (?Service)|Query $service
Expand All @@ -43,10 +48,59 @@ public function getColumns(): array
'id',
'host_id',
'service_id',
'redundancy_group_id'
'redundancy_group_id',
'name' => new Expression(
'CASE WHEN %s IS NOT NULL THEN %s WHEN %s IS NOT NULL THEN %s ELSE %s END',
['service_id', 'service.display_name', 'host_id', 'host.display_name', 'redundancy_group.display_name']
),
'severity' => new Expression(
'CASE WHEN %s IS NOT NULL THEN %s WHEN %s IS NOT NULL THEN %s ELSE %s END',
[
'service_id',
'service.state.severity',
'host_id',
'host.state.severity',
'redundancy_group.state.failed'
]
),
'state' => new Expression(
'CASE WHEN %s IS NOT NULL THEN %s WHEN %s IS NOT NULL THEN %s ELSE %s END',
[
'service_id',
'service.state.soft_state',
'host_id',
'host.state.soft_state',
'redundancy_group.state.failed'
]
),
'last_state_change' => new Expression(
'CASE WHEN %s IS NOT NULL THEN %s WHEN %s IS NOT NULL THEN %s ELSE %s END',
[
'service_id',
'service.state.last_state_change',
'host_id',
'host.state.last_state_change',
'redundancy_group.state.last_state_change'
]
)
];
}

public function getSearchColumns(): array
{
return [
'host.name_ci',
'service.name_ci',
'redundancy_group.display_name'
];
}

public function getDefaultSort(): string
{//TODO: this breaks some host/service detail view, having problematic_parent
return 'id';
return 'severity desc, last_state_change desc';
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
Expand Down
7 changes: 7 additions & 0 deletions library/Icingadb/Model/RedundancyGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function getColumns(): array
];
}

public function getColumnDefinitions(): array
{
return [
'display_name' => t('Redundancy Display Name')
];
}

public function createBehaviors(Behaviors $behaviors): void
{
$behaviors->add(new Binary([
Expand Down
Loading

0 comments on commit b451308

Please sign in to comment.