Skip to content

Commit

Permalink
refactor(events-incident-history-improvements): removed deprecated co…
Browse files Browse the repository at this point in the history
…de, fixed code styling, implemented requested changes
  • Loading branch information
ncosta-ic committed Apr 18, 2024
1 parent a0f5800 commit cbffbef
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 94 deletions.
34 changes: 6 additions & 28 deletions application/controllers/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Icinga\Module\Notifications\Controllers;

use Icinga\Exception\MissingParameterException;
use Icinga\Module\Notifications\Common\Auth;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
Expand All @@ -16,31 +15,21 @@
use ipl\Html\Form;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\SearchControls;
use ipl\Web\Widget\Tabs;

class IncidentController extends CompatController
{
use Auth;
use SearchControls;

/** @var int $id Incident identifier used by incident-dependent actions */
protected $id;

/**
* @throws MissingParameterException
*/
public function init()
{
$id = $this->params->shiftRequired('incident');
$this->id = $id;
}

public function indexAction(): void
{
$this->addTitleTab(t('Incident'));

$id = $this->params->getRequired('id');

$query = Incident::on(Database::get())
->with(['object', 'object.source'])
->filter(Filter::equal('incident.id', $this->id));
->filter(Filter::equal('incident.id', $id));

$this->applyRestrictions($query);

Expand All @@ -50,7 +39,7 @@ public function indexAction(): void
$this->httpNotFound($this->translate('Incident not found'));
}

$this->addControl((new IncidentList($query->execute()))->setNoSubjectLink());
$this->addControl((new IncidentList($query))->setNoSubjectLink());

$this->controls->addAttributes(['class' => 'incident-detail']);

Expand All @@ -69,17 +58,6 @@ public function indexAction(): void
);
}

$this->setTitle(t('Incident'));
$this->getTabs()->activate('incident');
$this->addContent(new IncidentDetail($incident));
}

public function getTabs(): Tabs
{
return parent::getTabs()
->add('incident', [
'label' => $this->translate('Incident'),
'url' => Links::incident($this->id)
]);
}
}
2 changes: 1 addition & 1 deletion library/Notifications/Common/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function incidents(): Url

public static function incident(int $id): Url
{
return Url::fromPath('notifications/incident', ['incident' => $id]);
return Url::fromPath('notifications/incident', ['id' => $id]);
}

public static function contact(int $id): Url
Expand Down
12 changes: 3 additions & 9 deletions library/Notifications/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
* @property ?string $message
* @property ?string $username
*
* @property Model<Objects> | Query<Objects> $object
* @property Model<IncidentHistory> | Query<IncidentHistory> $incident_history
* @property Model<Incident> | Query<Incident> $incident
* @property Model | Query $object
* @property Model | Query $incident_history
* @property Model | Query $incident
*/
class Event extends Model
{
Expand All @@ -51,9 +51,6 @@ public function getColumns(): array
];
}

/**
* @return array<string, string>
*/
public function getColumnDefinitions(): array
{
return [
Expand All @@ -66,9 +63,6 @@ public function getColumnDefinitions(): array
];
}

/**
* @return array<string>
*/
public function getSearchColumns(): array
{
return ['object.name'];
Expand Down
23 changes: 7 additions & 16 deletions library/Notifications/Model/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
* @property ?DateTime $recovered_at
* @property string $severity
*
* @property Model<Objects> | Query<Objects> $object
* @property Model<Event> | Query<Event> $event
* @property Model<Contact> | Query<Contact> $contact
* @property Model<IncidentContact> | Query<IncidentContact> $incident_contact
* @property Model<IncidentHistory> | Query<IncidentContact> $incident_history
* @property Model<Rule> | Query<Rule> $rule
* @property Model<RuleEscalation> | Query<RuleEscalation> $rule_escalation
* @property Model | Query $object
* @property Model | Query $event
* @property Model | Query $contact
* @property Model | Query $incident_contact
* @property Model | Query $incident_history
* @property Model | Query $rule
* @property Model | Query $rule_escalation
*/
class Incident extends Model
{
Expand All @@ -51,9 +51,6 @@ public function getColumns(): array
];
}

/**
* @return array<string, string>
*/
public function getColumnDefinitions(): array
{
return [
Expand All @@ -64,17 +61,11 @@ public function getColumnDefinitions(): array
];
}

/**
* @return array<string>
*/
public function getSearchColumns(): array
{
return ['object.name'];
}

/**
* @return array<string>
*/
public function getDefaultSort(): array
{
return ['incident.severity desc, incident.started_at'];
Expand Down
25 changes: 8 additions & 17 deletions library/Notifications/Model/IncidentHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,21 @@
* @property DateTime $time
* @property string $type
* @property ?int $contact_id
* @property ?int $schedule_id
* @property ?int $contactgroup_id
* @property ?int $channel_id
* @property ?int $caused_by_incident_history_id
* @property ?string $new_severity
* @property ?string $old_severity
* @property ?string $new_recipient_role
* @property ?string $old_recipient_role
* @property ?string $message
*
* @property Model<Incident> | Query<Incident> $incident
* @property Model<Event> | Query<Event> $event
* @property Model<Contact> | Query<Event> $contact
* @property Model<Contactgroup> | Query<Contactgroup> $contactgroup
* @property Model<Schedule> | Query<Schedule> $schedule
* @property Model<Rule> | Query<Rule> $rule
* @property Model<RuleEscalation> | Query<RuleEscalation> $rule_escalation
* @property Model<Channel> | Query<Channel> $channel
* @property Model | Query $incident
* @property Model | Query $event
* @property Model | Query $contact
* @property Model | Query $contactgroup
* @property Model | Query $schedule
* @property Model | Query $rule
* @property Model | Query $rule_escalation
* @property Model | Query $channel
*/
class IncidentHistory extends Model
{
Expand Down Expand Up @@ -74,9 +71,6 @@ public function getColumns(): array
];
}

/**
* @return array<string, string>
*/
public function getColumnDefinitions(): array
{
return [
Expand All @@ -102,9 +96,6 @@ public function createBehaviors(Behaviors $behaviors): void
$behaviors->add(new MillisecondTimestamp(['time']));
}

/**
* @return array<string>
*/
public function getDefaultSort(): array
{
return ['incident_history.time desc'];
Expand Down
22 changes: 7 additions & 15 deletions library/Notifications/Model/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
* @property string $id
* @property int $source_id
* @property string $name
* @property string $host
* @property ?string $service
* @property ?string $url
*
* @property Model<Event> | Query<Event> $event
* @property Model<Incident> | Query<Incident> $incident
* @property Model<ObjectIdTag> | Query<ObjectIdTag> $object_id_tag
* @property Model<Tag> | Query<Tag> $tag
* @property Model<ObjectExtraTag> | Query<ObjectExtraTag> $object_extra_tag
* @property Model<ExtraTag> | Query<ExtraTag> $extra_tag
* @property Model<Source> | Query<Source> $source
* @property Model | Query $event
* @property Model | Query $incident
* @property Model | Query $object_id_tag
* @property Model | Query $tag
* @property Model | Query $object_extra_tag
* @property Model | Query $extra_tag
* @property Model | Query $source
*/
class Objects extends Model
{
Expand All @@ -51,17 +49,11 @@ public function getColumns(): array
];
}

/**
* @return array<string>
*/
public function getSearchColumns(): array
{
return ['object_id_tag.tag', 'object_id_tag.value'];
}

/**
* @return string
*/
public function getDefaultSort(): string
{
return 'object.name';
Expand Down
2 changes: 1 addition & 1 deletion library/Notifications/Widget/Detail/EventDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function createSource(): array

$elements[] = Html::tag('h2', t('Source'));

/** @var ?Objects $object */
/** @var Objects $object */
$object = $this->event->object;
if ($object) {
/** @var Source $source */
Expand Down
1 change: 0 additions & 1 deletion library/Notifications/Widget/Detail/IncidentDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ protected function createHistory()
'schedule',
'channel'
])
->execute()
)
];
}
Expand Down
9 changes: 3 additions & 6 deletions library/Notifications/Widget/ItemList/EventListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\I18n\Translation;
use ipl\Stdlib\Str;
use ipl\Web\Common\BaseListItem;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
Expand Down Expand Up @@ -117,13 +118,9 @@ protected function assembleTitle(BaseHtmlElement $title): void
$msg = null;
if ($this->item->severity === null) {
$description = strtolower(trim($this->item->message . '')) ?: '';
/*
* TODO(nc): strpos can be replaced with str_starts_with() once the minimal supported PHP version reaches
* 8.0
*/
if (strpos($description, 'incident reached age') === 0) {
if (Str::startsWith($description, 'incident reached age')) {
$msg = $this->translate('exceeded time constraint');
} elseif (strpos($description, 'incident reevaluation') === 0) {
} elseif (Str::startsWith($description, 'incident reevaluation')) {
$msg = $this->translate('was reevaluated at daemon startup');
} else {
$msg = $this->translate('was acknowledged');
Expand Down
16 changes: 16 additions & 0 deletions library/Notifications/Widget/ItemList/IncidentHistoryListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,29 @@ protected function assembleVisual(BaseHtmlElement $visual): void
case 'opened':
case 'incident_severity_changed':
$content = new Icon($iconName, ['class' => 'severity-' . $this->item->new_severity]);

break;
case 'closed':
$content = (new Icon($iconName, ['class' => 'type-' . $this->item->type]))->setStyle('fa-regular');

break;
case 'notified':
case 'recipient_role_changed':
$style = 'fa-regular';
if ($iconName === Icons::UNSUBSCRIBED || $iconName === Icons::UNMANAGE) {
$style = 'fa-solid';
}

$content = (new Icon($iconName))->setStyle($style);

break;
case 'rule_matched':
$content = new Icon($iconName, ['class' => 'type-' . $this->item->type]);

break;
case 'escalation_triggered':
$content = (new Icon($iconName))->setStyle('fa-solid');

break;
default:
$content = new Icon('');
Expand Down Expand Up @@ -114,9 +120,11 @@ protected function buildMessage(): string
t('Incident opened at severity %s'),
Event::mapSeverity($this->item->new_severity)
);

break;
case 'closed':
$message = t('Incident closed');

break;
case "notified":
if ($this->item->contactgroup_id) {
Expand All @@ -140,13 +148,15 @@ protected function buildMessage(): string
$this->item->channel->type
);
}

break;
case 'incident_severity_changed':
$message = sprintf(
t('Incident severity changed from %s to %s'),
Event::mapSeverity($this->item->old_severity),
Event::mapSeverity($this->item->new_severity)
);

break;
case 'recipient_role_changed':
$newRole = $this->item->new_recipient_role;
Expand Down Expand Up @@ -201,18 +211,24 @@ protected function buildMessage(): string
break;
case 'rule_matched':
$message = sprintf(t('Rule %s matched on this incident'), $this->item->rule->name);

break;
case 'escalation_triggered':
$message = sprintf(
t('Rule %s reached escalation %s'),
$this->item->rule->name,
$this->item->rule_escalation->name
);

break;
default:
$message = '';
}

if ($this->item->message) {
$message = $message === '' ? $this->item->message : $message . ': ' . $this->item->message;
}

return $message;
}

Expand Down

0 comments on commit cbffbef

Please sign in to comment.