Skip to content

Commit

Permalink
fix1: resolve review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Sep 26, 2024
1 parent bd9e387 commit a1bbc36
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 141 deletions.
12 changes: 11 additions & 1 deletion library/Icingadb/Common/IcingaRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Generator;
use Icinga\Application\Config;
use Icinga\Application\Logger;
use ipl\Sql\Expression;
use Predis\Client as Redis;

class IcingaRedis
Expand Down Expand Up @@ -163,7 +164,16 @@ protected static function fetchState(string $key, array $ids, array $columns): G
foreach ($results as $i => $json) {
if ($json !== null) {
$data = json_decode($json, true);
$keyMap = array_fill_keys($columns, null);
$keyMap = [];

foreach ($columns as $alias => $column) {
if ($column instanceof Expression) {
$column = $alias;
}

$keyMap[] = $column;
}

unset($keyMap['is_overdue']); // Is calculated by Icinga DB, not Icinga 2, hence it's never in redis

// TODO: Remove once https://github.com/Icinga/icinga2/issues/9427 is fixed
Expand Down
18 changes: 9 additions & 9 deletions library/Icingadb/Common/StateBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Icinga\Module\Icingadb\Common;

use InvalidArgumentException;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Stdlib\BaseFilter;
use ipl\Stdlib\Filter;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\StateBadge;
Expand Down Expand Up @@ -76,10 +76,12 @@ protected function getBaseUrl(): ?Url
* @param string $state
*
* @return int
*
* @throws InvalidArgumentException if the given state is not valid
*/
protected function getStateInt(string $state): int
{
return 0;
throw new InvalidArgumentException('%s is not a valid state', $state);
}

/**
Expand Down Expand Up @@ -138,18 +140,17 @@ public function createLink($content, Filter\Rule $filter = null): Link
* Create a state bade
*
* @param string $state
* @param bool $createLink Create link for the badge if true
*
* @return ?BaseHtmlElement
*/
protected function createBadge(string $state, bool $createLink = true)
protected function createBadge(string $state): ?BaseHtmlElement
{
$key = $this->prefix . "_{$state}";

if (isset($this->item->$key) && $this->item->$key) {
$stateBadge = new StateBadge($this->item->$key, $state);

if ($createLink) {
if ($this->url !== null) {
$this->createLink(
$stateBadge,
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state))
Expand All @@ -166,11 +167,10 @@ protected function createBadge(string $state, bool $createLink = true)
* Create a state group
*
* @param string $state
* @param bool $createLink Create link for the badge if true
*
* @return ?BaseHtmlElement
*/
protected function createGroup(string $state, bool $createLink = true)
protected function createGroup(string $state): ?BaseHtmlElement
{
$content = [];
$handledKey = $this->prefix . "_{$state}_handled";
Expand All @@ -179,7 +179,7 @@ protected function createGroup(string $state, bool $createLink = true)
if (isset($this->item->$unhandledKey) && $this->item->$unhandledKey) {
$unhandledStateBadge = new StateBadge($this->item->$unhandledKey, $state);

if ($createLink) {
if ($this->url !== null) {
$unhandledStateBadge = $this->createLink(
$unhandledStateBadge,
Filter::all(
Expand All @@ -196,7 +196,7 @@ protected function createGroup(string $state, bool $createLink = true)
if (isset($this->item->$handledKey) && $this->item->$handledKey) {
$handledStateBadge = new StateBadge($this->item->$handledKey, $state, true);

if ($createLink) {
if ($this->url !== null) {
$handledStateBadge = $this->createLink(
$handledStateBadge,
Filter::all(
Expand Down
43 changes: 21 additions & 22 deletions library/Icingadb/Model/RedundancyGroupParentStateSummary.php
Original file line number Diff line number Diff line change
@@ -1,80 +1,79 @@
<?php

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

namespace Icinga\Module\Icingadb\Model;

use ipl\Sql\Connection;
use ipl\Sql\Expression;

/**
* @property int $parents_acknowledged
* @property int $parents_down_critical_handled
* @property int $parents_down_critical_unhandled
* @property int $parents_pending
* @property int $parents_problems_unacknowledged
* @property int $parents_total
* @property int $parents_up_ok
* @property int $parents_unknown_handled
* @property int $parents_unknown_unhandled
* @property int $parents_warning_handled
* @property int $parents_warning_unhandled
* @property int $objects_down_critical_handled
* @property int $objects_down_critical_unhandled
* @property int $objects_pending
* @property int $objects_problems_unacknowledged
* @property int $objects_total
* @property int $objects_up_ok
* @property int $objects_unknown_handled
* @property int $objects_unknown_unhandled
* @property int $objects_warning_handled
* @property int $objects_warning_unhandled
*/
class RedundancyGroupParentStateSummary extends RedundancyGroup
{
public function getSummaryColumns()
{
return [
'parents_problem_handled' => new Expression(
'objects_problem_handled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 1'
. ' AND (redundancy_group_from_to_host_state.is_handled = \'y\''
. ' OR redundancy_group_from_to_host_state.is_reachable = \'n\') THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.soft_state = 2'
. ' AND (redundancy_group_from_to_service_state.is_handled = \'y\''
. ' OR redundancy_group_from_to_service_state.is_reachable = \'n\') THEN 1 ELSE 0 END)'
),
'parents_problem_unhandled' => new Expression(
'objects_problem_unhandled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 1'
. ' AND redundancy_group_from_to_host_state.is_handled = \'n\''
. ' AND redundancy_group_from_to_host_state.is_reachable = \'y\' THEN 1 ELSE 0 END'
. ' + CASE WHEN redundancy_group_from_to_service_state.soft_state = 2'
. ' AND redundancy_group_from_to_service_state.is_handled = \'n\''
. ' AND redundancy_group_from_to_service_state.is_reachable = \'y\' THEN 1 ELSE 0 END)'
),
'parents_pending' => new Expression(
'objects_pending' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 99 THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.soft_state = 99 THEN 1 ELSE 0 END)'
),
'parents_problems_unacknowledged' => new Expression(
'objects_problems_unacknowledged' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.is_problem = \'y\''
. ' AND redundancy_group_from_to_host_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.is_problem = \'y\''
. ' AND redundancy_group_from_to_service_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END)'
),
'parents_total' => new Expression(
'objects_total' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host.id IS NOT NULL THEN 1 ELSE 0 END)'
. '+ SUM(CASE WHEN redundancy_group_from_to_service.id IS NOT NULL THEN 1 ELSE 0 END)'
),
'parents_ok' => new Expression(
'objects_ok' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 0 THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.soft_state = 0 THEN 1 ELSE 0 END)'
),
'parents_unknown_handled' => new Expression(
'objects_unknown_handled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_service_state.soft_state = 3'
. ' AND (redundancy_group_from_to_service_state.is_handled = \'y\''
. ' OR redundancy_group_from_to_service_state.is_reachable = \'n\') THEN 1 ELSE 0 END)'
),
'parents_unknown_unhandled' => new Expression(
'objects_unknown_unhandled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_service_state.soft_state = 3'
. ' AND redundancy_group_from_to_service_state.is_handled = \'n\''
. ' AND redundancy_group_from_to_service_state.is_reachable = \'y\' THEN 1 ELSE 0 END)'
),
'parents_warning_handled' => new Expression(
'objects_warning_handled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_service_state.soft_state = 1'
. ' AND (redundancy_group_from_to_service_state.is_handled = \'y\''
. ' OR redundancy_group_from_to_service_state.is_reachable = \'n\') THEN 1 ELSE 0 END)'
),
'parents_warning_unhandled' => new Expression(
'objects_warning_unhandled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_service_state.soft_state = 1'
. ' AND redundancy_group_from_to_service_state.is_handled = \'n\''
. ' AND redundancy_group_from_to_service_state.is_reachable = \'y\' THEN 1 ELSE 0 END)'
Expand Down
18 changes: 7 additions & 11 deletions library/Icingadb/Widget/Detail/ObjectDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Icinga\Module\Icingadb\Model\CustomvarFlat;
use Icinga\Module\Icingadb\Model\UnreachableParent;
use Icinga\Module\Icingadb\Web\Navigation\Action;
use Icinga\Module\Icingadb\Widget\ItemList\RootProblemList;
use Icinga\Module\Icingadb\Widget\ItemList\DependencyNodeList;
use Icinga\Module\Icingadb\Widget\MarkdownText;
use Icinga\Module\Icingadb\Common\ServiceLinks;
use Icinga\Module\Icingadb\Forms\Command\Object\ToggleObjectFeaturesForm;
Expand Down Expand Up @@ -443,7 +443,7 @@ protected function createPluginOutput(): array
'div',
[
'id' => 'check-output-' . $this->object->checkcommand_name,
'class' => 'collapsible',
'class' => ['collapsible', 'check-command-output'],
'data-visible-height' => 100
],
$pluginOutput
Expand Down Expand Up @@ -606,6 +606,10 @@ protected function fetchCustomVars()

protected function createRootProblems(): ?array
{
if ($this->object->state->is_reachable) {
return null;
}

$rootProblems = UnreachableParent::on($this->getDb(), $this->object)
->with([
'redundancy_group',
Expand All @@ -624,14 +628,6 @@ protected function createRootProblems(): ?array

$this->applyRestrictions($rootProblems);

if ($rootProblems->count() > 0) {
$rootProblemList = (new RootProblemList(
$rootProblems->execute()
));

return [HtmlElement::create('h2', null, t('Root Problems')), $rootProblemList];
}

return null;
return [HtmlElement::create('h2', null, t('Root Problems')), new DependencyNodeList($rootProblems)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Icinga\Module\Icingadb\Widget\ItemList;

use Icinga\Module\Icingadb\Model\DependencyNode;
use Icinga\Module\Icingadb\Model\UnreachableParent;

class RootProblemList extends StateList
class DependencyNodeList extends StateList
{
protected $defaultAttributes = ['class' => ['root-problem-list']];

Expand All @@ -22,7 +23,7 @@ protected function getItemClass(): string

protected function createListItem(object $data)
{
/** @var UnreachableParent $data */
/** @var UnreachableParent|DependencyNode $data */
if ($data->redundancy_group_id !== null) {
return new RedundancyGroupListItem($data->redundancy_group, $this);
} elseif ($data->service_id !== null) {
Expand Down
44 changes: 25 additions & 19 deletions library/Icingadb/Widget/ItemList/RedundancyGroupListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Icinga\Module\Icingadb\Model\RedundancyGroupParentStateSummary;
use Icinga\Module\Icingadb\Util\PluginOutput;
use Icinga\Module\Icingadb\Widget\PluginOutputContainer;
use Icinga\Module\Icingadb\Widget\RedundancyGroupStatistics;
use Icinga\Module\Icingadb\Widget\ObjectsStatistics;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Sql\Expression;
Expand All @@ -33,6 +33,8 @@ class RedundancyGroupListItem extends StateListItem
use Auth;
use Database;

protected $baseAttributes = ['class' => ['list-item', 'redundancy-group-list-item']];

protected function getStateBallSize(): string
{
return StateBall::SIZE_LARGE;
Expand Down Expand Up @@ -78,27 +80,27 @@ protected function assembleCaption(BaseHtmlElement $caption): void
$members = RedundancyGroup::on($this->getDb())
->columns([
'id' => 'id',
'parent_output' => new Expression(
'objects_output' => new Expression(
'CASE WHEN redundancy_group_from_to_host_state.output IS NULL'
. ' THEN redundancy_group_from_to_service_state.output'
. ' ELSE redundancy_group_from_to_host_state.output END'
),
'parent_long_output' => new Expression(
'objects_long_output' => new Expression(
'CASE WHEN redundancy_group_from_to_host_state.long_output IS NULL'
. ' THEN redundancy_group_from_to_service_state.long_output'
. ' ELSE redundancy_group_from_to_host_state.long_output END'
),
'parent_checkcommand_name' => new Expression(
'objects_checkcommand_name' => new Expression(
'CASE WHEN redundancy_group_from_to_host.checkcommand_name IS NULL'
. ' THEN redundancy_group_from_to_service.checkcommand_name'
. ' ELSE redundancy_group_from_to_host.checkcommand_name END'
),
'parent_last_state_change' => new Expression(
'objects_last_state_change' => new Expression(
'CASE WHEN redundancy_group_from_to_host_state.last_state_change IS NULL'
. ' THEN redundancy_group_from_to_service_state.last_state_change'
. ' ELSE redundancy_group_from_to_host_state.last_state_change END'
),
'parent_severity' => new Expression(
'objects_severity' => new Expression(
'CASE WHEN redundancy_group_from_to_host_state.severity IS NULL'
. ' THEN redundancy_group_from_to_service_state.severity'
. ' ELSE redundancy_group_from_to_host_state.severity END'
Expand All @@ -107,8 +109,8 @@ protected function assembleCaption(BaseHtmlElement $caption): void
->with($relations)
->filter($filter)
->orderBy([
'parent_severity',
'parent_last_state_change',
'objects_severity',
'objects_last_state_change',
], 'DESC');

$this->applyRestrictions($members);
Expand All @@ -118,28 +120,32 @@ protected function assembleCaption(BaseHtmlElement $caption): void

if($data) {
$caption->addHtml(new PluginOutputContainer(
(new PluginOutput($data->parent_output . "\n" .$data->parent_long_output))
->setCommandName($data->parent_checkcommand_name)
(new PluginOutput($data->objects_output . "\n" .$data->objects_long_output))
->setCommandName($data->objects_checkcommand_name)
));
}

$caption->addHtml(new RedundancyGroupStatistics($summary->first()));
$caption->addHtml(new ObjectsStatistics($summary->first()));
}

protected function assembleTitle(BaseHtmlElement $title): void
{
$subject = $this->createSubject();
if ($this->state->failed) {
$verb = 'has';
$stateTextElement = Html::sprintf(
t('%s has %s', '<hostname> has <state-text>'),
$subject,
Html::tag('span', ['class' => 'state-text'], 'FAILED')
);
} else {
$verb = 'is';
$stateTextElement = Html::sprintf(
t('%s is %s', '<hostname> is <state-text>'),
$subject,
Html::tag('span', ['class' => 'state-text'], 'OK')
);
}

$title->addHtml(Html::sprintf(
t('%s %s %s', '<hostname> has/is <state-text>'),
$this->createSubject(),
$verb,
Html::tag('span', ['class' => 'state-text'], $this->state->failed ? 'Failed' : 'OK')
));
$title->addHtml($stateTextElement);
}

protected function assemble(): void
Expand Down
Loading

0 comments on commit a1bbc36

Please sign in to comment.