Skip to content

Commit

Permalink
WIP: Optimize the trait and related code
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Oct 16, 2024
1 parent 263f16e commit bb57bd1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
50 changes: 36 additions & 14 deletions library/Icingadb/Widget/Detail/DetailAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Icinga\Module\Icingadb\Widget\Detail;

use Icinga\Module\Icingadb\Common\Icons;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\RedundancyGroup;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Icingadb\Util\PluginOutput;
use Icinga\Module\Icingadb\Widget\CheckAttempt;
use Icinga\Module\Icingadb\Widget\IconImage;
Expand All @@ -14,13 +17,21 @@
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Orm\Model;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\StateBall;
use ipl\Web\Widget\TimeSince;

trait DetailAssembler
{
/**
* Get the object
*
* @return Host|Service|RedundancyGroup
*/
abstract protected function getObject(): Model;

/**
* Create subject
*
Expand All @@ -30,13 +41,19 @@ abstract protected function createSubject();

protected function assembleCaption(BaseHtmlElement $caption): void
{
if ($this->state->soft_state === null && $this->state->output === null) {
if ($this->getObject() instanceof RedundancyGroup) {
// $state = $this->getObject()->state; worst member's state
} else {
$state = $this->getObject()->state;
}

if ($state->soft_state === null && $state->output === null) {
$caption->addHtml(Text::create($this->translate('Waiting for Icinga DB to synchronize the state.')));
} else {
if (empty($this->state->output)) {
if (empty($state->output)) {
$pluginOutput = new EmptyState($this->translate('Output unavailable.'));
} else {
$pluginOutput = new PluginOutputContainer(PluginOutput::fromObject($this->item));
$pluginOutput = new PluginOutputContainer(PluginOutput::fromObject($this->getObject()));
}

$caption->addHtml($pluginOutput);
Expand All @@ -45,13 +62,15 @@ protected function assembleCaption(BaseHtmlElement $caption): void

protected function assembleTitle(BaseHtmlElement $title): void
{
$state = $this->getObject()->state;

$title->addHtml(Html::sprintf(
$this->translate('%s is %s', '<hostname> is <state-text>'),
$this->createSubject(),
Html::tag('span', ['class' => 'state-text'], $this->state->getStateTextTranslated())
Html::tag('span', ['class' => 'state-text'], $state->getStateText())
));

if ($this->state->affects_children) {
if ($state->affects_children) {
$total = (int) $this->item->affected_children;

if ($total > 1000) {
Expand Down Expand Up @@ -84,29 +103,32 @@ protected function assembleTitle(BaseHtmlElement $title): void

protected function assembleVisual(BaseHtmlElement $visual): void
{
$stateBall = new StateBall($this->state->getStateText(), $this->getStateBallSize());
$stateBall->add($this->state->getIcon());
if ($this->state->is_problem && ($this->state->is_handled || ! $this->state->is_reachable)) {
$state = $this->getObject()->state;

$stateBall = new StateBall($state->getStateText(), $this->getStateBallSize());
$stateBall->add($state->getIcon());
if ($state->is_problem && ($state->is_handled || ! $state->is_reachable)) {
$stateBall->getAttributes()->add('class', 'handled');
}

$visual->addHtml($stateBall);
if ($this->state->state_type === 'soft') {
if ($state->state_type === 'soft') {
$visual->addHtml(
new CheckAttempt((int) $this->state->check_attempt, (int) $this->item->max_check_attempts)
new CheckAttempt((int) $state->check_attempt, (int) $this->item->max_check_attempts)
);
}
}

protected function createTimestamp(): ?BaseHtmlElement
{
$state = $this->getObject()->state;
$since = null;
if ($this->state->is_overdue) {
$since = new TimeSince($this->state->next_update->getTimestamp());
if ($state->is_overdue) {
$since = new TimeSince($state->next_update->getTimestamp());
$since->prepend($this->translate('Overdue') . ' ');
$since->prependHtml(new Icon(Icons::WARNING));
} elseif ($this->state->last_state_change !== null && $this->state->last_state_change->getTimestamp() > 0) {
$since = new TimeSince($this->state->last_state_change->getTimestamp());
} elseif ($state->last_state_change !== null && $state->last_state_change->getTimestamp() > 0) {
$since = new TimeSince($state->last_state_change->getTimestamp());
}

return $since;
Expand Down
5 changes: 5 additions & 0 deletions library/Icingadb/Widget/ItemList/BaseHostListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ abstract class BaseHostListItem extends StateListItem
use NoSubjectLink;
use DetailAssembler;

protected function getObject(): Host
{
return $this->item;
}

protected function createSubject()
{
if ($this->getNoSubjectLink()) {
Expand Down
6 changes: 5 additions & 1 deletion library/Icingadb/Widget/ItemList/BaseServiceListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Icingadb\Widget\Detail\DetailAssembler;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
Expand All @@ -28,6 +27,11 @@ abstract class BaseServiceListItem extends StateListItem
use NoSubjectLink;
use DetailAssembler;

protected function getObject(): Service
{
return $this->item;
}

protected function createSubject()
{
$service = $this->item->display_name;
Expand Down

0 comments on commit bb57bd1

Please sign in to comment.