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

Display downtime icon independent from object state #772

Merged
merged 2 commits into from
Jun 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions library/Icingadb/Model/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Icinga\Module\Icingadb\Model;

use Icinga\Module\Icingadb\Common\Icons;
use Icinga\Module\Icingadb\Model\Behavior\BoolCast;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behavior\MillisecondTimestamp;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
use ipl\Web\Widget\Icon;

/**
* Base class for the {@link HostState} and {@link ServiceState} models providing common columns.
Expand Down Expand Up @@ -78,4 +80,38 @@ public function createBehaviors(Behaviors $behaviors)
'last_comment_id'
]));
}

/**
* Get the state icon
*
* @return Icon|null
*/
public function getIcon(): ?Icon
{
$icon = null;
switch (true) {
case $this->is_acknowledged:
$icon = new Icon(Icons::IS_ACKNOWLEDGED);
break;
case $this->in_downtime:
$icon = new Icon(
Icons::IN_DOWNTIME,
['title' => sprintf(
'%s (%s)',
strtoupper($this->getStateTextTranslated()),
$this->is_handled ? t('handled by Downtime') : t('in Downtime')
)]
);

break;
case $this->is_flapping:
$icon = new Icon(Icons::IS_FLAPPING);
break;
case $this->is_handled:
$icon = new Icon(Icons::HOST_DOWN);
break;
}

return $icon;
}
}
9 changes: 2 additions & 7 deletions library/Icingadb/Widget/ItemList/HostDetailHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Icinga\Module\Icingadb\Widget\ItemList;

use Icinga\Module\Icingadb\Common\HostStates;
use Icinga\Module\Icingadb\Widget\CheckAttempt;
use Icinga\Module\Icingadb\Widget\StateChange;
use ipl\Html\BaseHtmlElement;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;

class HostDetailHeader extends HostListItemMinimal
Expand Down Expand Up @@ -51,11 +49,8 @@ protected function assembleVisual(BaseHtmlElement $visual)
}
}

if ($this->state->is_handled) {
$currentStateBall = $stateChange->ensureAssembled()->getContent()[1];
$currentStateBall->addHtml(new Icon($this->getHandledIcon()));
$currentStateBall->getAttributes()->add('class', 'handled');
}
$stateChange->setIcon($this->state->getIcon());
$stateChange->setHandled($this->state->is_handled);

$visual->addHtml($stateChange);
}
Expand Down
9 changes: 2 additions & 7 deletions library/Icingadb/Widget/ItemList/ServiceDetailHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Icinga\Module\Icingadb\Widget\ItemList;

use Icinga\Module\Icingadb\Common\ServiceStates;
use Icinga\Module\Icingadb\Widget\CheckAttempt;
use Icinga\Module\Icingadb\Widget\StateChange;
use ipl\Html\BaseHtmlElement;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;

class ServiceDetailHeader extends ServiceListItemMinimal
Expand Down Expand Up @@ -51,11 +49,8 @@ protected function assembleVisual(BaseHtmlElement $visual)
}
}

if ($this->state->is_handled) {
$currentStateBall = $stateChange->ensureAssembled()->getContent()[1];
$currentStateBall->addHtml(new Icon($this->getHandledIcon()));
$currentStateBall->getAttributes()->add('class', 'handled');
}
$stateChange->setIcon($this->state->getIcon());
$stateChange->setHandled($this->state->is_handled);

$visual->addHtml($stateChange);
}
Expand Down
19 changes: 1 addition & 18 deletions library/Icingadb/Widget/ItemList/StateListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ protected function assembleTitle(BaseHtmlElement $title)
protected function assembleVisual(BaseHtmlElement $visual)
{
$stateBall = new StateBall($this->state->getStateText(), $this->getStateBallSize());

$stateBall->add($this->state->getIcon());
if ($this->state->is_handled) {
$stateBall->addHtml(new Icon($this->getHandledIcon()));
$stateBall->getAttributes()->add('class', 'handled');
} elseif ($this->state->getStateText() === 'pending' && $this->state->in_downtime) {
$stateBall->addHtml(new Icon($this->getHandledIcon()));
}

$visual->addHtml($stateBall);
Expand All @@ -104,20 +101,6 @@ protected function createTimestamp()
}
}

protected function getHandledIcon(): string
{
switch (true) {
case $this->state->is_acknowledged:
return Icons::IS_ACKNOWLEDGED;
case $this->state->in_downtime:
return Icons::IN_DOWNTIME;
case $this->state->is_flapping:
return Icons::IS_FLAPPING;
default:
return Icons::HOST_DOWN;
}
}

protected function assemble()
{
if ($this->state->is_overdue) {
Expand Down
16 changes: 1 addition & 15 deletions library/Icingadb/Widget/ItemTable/StateRowItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,12 @@ abstract class StateRowItem extends BaseRowItem
/** @var StateItemTable */
protected $list;

protected function getHandledIcon(): string
{
switch (true) {
case $this->item->state->in_downtime:
return Icons::IN_DOWNTIME;
case $this->item->state->is_acknowledged:
return Icons::IS_ACKNOWLEDGED;
case $this->item->state->is_flapping:
return Icons::IS_FLAPPING;
default:
return Icons::HOST_DOWN;
}
}

protected function assembleVisual(BaseHtmlElement $visual)
{
$stateBall = new StateBall($this->item->state->getStateText(), StateBall::SIZE_LARGE);
$stateBall->add($this->item->state->getIcon());

if ($this->item->state->is_handled) {
$stateBall->addHtml(new Icon($this->getHandledIcon()));
$stateBall->getAttributes()->add('class', 'handled');
}

Expand Down
51 changes: 43 additions & 8 deletions library/Icingadb/Widget/StateChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Icingadb\Widget;

use ipl\Html\BaseHtmlElement;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;

class StateChange extends BaseHtmlElement
Expand All @@ -21,6 +22,12 @@ class StateChange extends BaseHtmlElement

protected $tag = 'div';

/** @var ?Icon Current state ball icon */
protected $icon;

/** @var bool Whether the state is handled */
protected $isHandled = false;

public function __construct(string $state, string $previousState)
{
$this->previousState = $previousState;
Expand Down Expand Up @@ -55,20 +62,48 @@ public function setCurrentStateBallSize(string $size): self
return $this;
}

/**
* Set the current state ball icon
*
* @param $icon
*
* @return $this
*/
public function setIcon($icon): self
{
$this->icon = $icon;

return $this;
}

/**
* Set whether the current state is handled
*
* @return $this
*/
public function setHandled($isHandled = true): self
{
$this->isHandled = $isHandled;

return $this;
}

protected function assemble()
{
$currentStateBall = (new StateBall($this->state, $this->currentStateBallSize))
->add($this->icon);

if ($this->isHandled) {
$currentStateBall->getAttributes()->add('class', 'handled');
}

$previousStateBall = new StateBall($this->previousState, $this->previousStateBallSize);
if ($this->isRightBiggerThanLeft()) {
$this->getAttributes()->add('class', 'reversed-state-balls');

$this->addHtml(
new StateBall($this->state, $this->currentStateBallSize),
new StateBall($this->previousState, $this->previousStateBallSize)
);
$this->addHtml($currentStateBall, $previousStateBall);
} else {
$this->addHtml(
new StateBall($this->previousState, $this->previousStateBallSize),
new StateBall($this->state, $this->currentStateBallSize)
);
$this->addHtml($previousStateBall, $currentStateBall);
}
}

Expand Down