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

Handle missing results more gracefully #1005

Merged
merged 4 commits into from
Jul 25, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/Icingadb/Common/TicketLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public function createTicketLinks($text): string
return $tickets->createLinks($text);
}

return $text;
return $text ?? '';
}
}
2 changes: 1 addition & 1 deletion library/Icingadb/Compat/CompatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function fromModel(Model $object)
*/
public function getName(): string
{
return $this->object->name;
return $this->object->name ?? '';
}

public function fetch(): bool
Expand Down
2 changes: 1 addition & 1 deletion library/Icingadb/Redis/VolatileStateResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function applyRedisUpdates($rows)
}
}

if ($type === 'service' && $row->host instanceof Host) {
if ($type === 'service' && $row->host instanceof Host && isset($row->host->id)) {
$hostStates[bin2hex($row->host->id)] = $row->host->state;
if (empty($hostStateKeys)) {
$hostStateKeys = $row->host->state->getColumns();
Expand Down
2 changes: 1 addition & 1 deletion library/Icingadb/Widget/Detail/CheckStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ protected function getCheckInterval(): int
return $this->object->check_interval;
}

$delay = ($this->object->state->execution_time + $this->object->state->latency) / 1000 + 5;
$delay = ($this->object->state->execution_time + $this->object->state->latency / 1000) + 5;
$interval = $this->object->state->next_check->getTimestamp()
- $this->object->state->last_update->getTimestamp();

Expand Down
50 changes: 36 additions & 14 deletions library/Icingadb/Widget/Detail/EventDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ protected function assembleNotificationEvent(NotificationHistory $notification)
new HtmlElement('h2', null, Text::create(t('Event Info'))),
new HorizontalKeyValue(
t('Sent On'),
DateFormatter::formatDateTime($notification->send_time->getTimestamp())
isset($notification->send_time)
? DateFormatter::formatDateTime($notification->send_time->getTimestamp())
: new EmptyState(t('n. a.'))
)
];

Expand Down Expand Up @@ -249,7 +251,9 @@ protected function assembleStateChangeEvent(StateHistory $stateChange)
new HtmlElement('h2', null, Text::create(t('Event Info'))),
new HorizontalKeyValue(
t('Occurred On'),
DateFormatter::formatDateTime($stateChange->event_time->getTimestamp())
isset($stateChange->event_time)
? DateFormatter::formatDateTime($stateChange->event_time->getTimestamp())
: new EmptyState(t('n. a.'))
),
new HorizontalKeyValue(t('Scheduling Source'), $stateChange->scheduling_source),
new HorizontalKeyValue(t('Check Source'), $stateChange->check_source)
Expand Down Expand Up @@ -341,29 +345,41 @@ protected function assembleDowntimeEvent(DowntimeHistory $downtime)
));
$eventInfo[] = new HorizontalKeyValue(
t('Entered On'),
DateFormatter::formatDateTime($downtime->entry_time->getTimestamp())
isset($downtime->entry_time)
? DateFormatter::formatDateTime($downtime->entry_time->getTimestamp())
: new EmptyState(t('n. a.'))
);
$eventInfo[] = new HorizontalKeyValue(t('Author'), [new Icon('user'), $downtime->author]);
// TODO: The following should be presented in a specific widget (maybe just like the downtime card)
$eventInfo[] = new HorizontalKeyValue(
t('Triggered On'),
DateFormatter::formatDateTime($downtime->trigger_time->getTimestamp())
isset($downtime->trigger_time)
? DateFormatter::formatDateTime($downtime->trigger_time->getTimestamp())
: new EmptyState(t('n. a.'))
);
$eventInfo[] = new HorizontalKeyValue(
t('Scheduled Start'),
DateFormatter::formatDateTime($downtime->scheduled_start_time->getTimestamp())
isset($downtime->scheduled_start_time)
? DateFormatter::formatDateTime($downtime->scheduled_start_time->getTimestamp())
: new EmptyState(t('n. a.'))
);
$eventInfo[] = new HorizontalKeyValue(
t('Actual Start'),
DateFormatter::formatDateTime($downtime->start_time->getTimestamp())
isset($downtime->start_time)
? DateFormatter::formatDateTime($downtime->start_time->getTimestamp())
: new EmptyState(t('n. a.'))
);
$eventInfo[] = new HorizontalKeyValue(
t('Scheduled End'),
DateFormatter::formatDateTime($downtime->scheduled_end_time->getTimestamp())
isset($downtime->scheduled_end_time)
? DateFormatter::formatDateTime($downtime->scheduled_end_time->getTimestamp())
: new EmptyState(t('n. a.'))
);
$eventInfo[] = new HorizontalKeyValue(
t('Actual End'),
DateFormatter::formatDateTime($downtime->end_time->getTimestamp())
isset($downtime->end_time)
? DateFormatter::formatDateTime($downtime->end_time->getTimestamp())
: new EmptyState(t('n. a.'))
);

if ($downtime->is_flexible) {
Expand Down Expand Up @@ -419,7 +435,9 @@ protected function assembleCommentEvent(CommentHistory $comment)
));
$eventInfo[] = new HorizontalKeyValue(
t('Entered On'),
DateFormatter::formatDateTime($comment->entry_time->getTimestamp())
isset($comment->entry_time)
? DateFormatter::formatDateTime($comment->entry_time->getTimestamp())
: new EmptyState(t('n. a.'))
);
$eventInfo[] = new HorizontalKeyValue(t('Author'), [new Icon('user'), $comment->author]);
$eventInfo[] = new HorizontalKeyValue(
Expand Down Expand Up @@ -487,7 +505,9 @@ protected function assembleFlappingEvent(FlappingHistory $flapping)
)),
new HorizontalKeyValue(
t('Started on'),
DateFormatter::formatDateTime($flapping->start_time->getTimestamp())
isset($flapping->start_time)
? DateFormatter::formatDateTime($flapping->start_time->getTimestamp())
: new EmptyState(t('n. a.'))
)
];
if ($this->event->event_type === 'flapping_start') {
Expand Down Expand Up @@ -529,7 +549,9 @@ protected function assembleAcknowledgeEvent(AcknowledgementHistory $acknowledgem
new HtmlElement('h2', null, Text::create(t('Event Info'))),
new HorizontalKeyValue(
t('Set on'),
DateFormatter::formatDateTime($acknowledgement->set_time->getTimestamp())
isset($acknowledgement->set_time)
? DateFormatter::formatDateTime($acknowledgement->set_time->getTimestamp())
: new EmptyState(t('n. a.'))
),
new HorizontalKeyValue(t('Author'), $acknowledgement->author
? [new Icon('user'), $acknowledgement->author]
Expand All @@ -555,8 +577,8 @@ protected function assembleAcknowledgeEvent(AcknowledgementHistory $acknowledgem
$eventInfo[] = new HorizontalKeyValue(
t('Expires On'),
$acknowledgement->expire_time
? DateFormatter::formatDateTime($acknowledgement->expire_time->getTimestamp())
: new EmptyState(t('Never'))
? DateFormatter::formatDateTime($acknowledgement->expire_time->getTimestamp())
: new EmptyState(t('Never'))
);
$eventInfo[] = new HorizontalKeyValue(t('Sticky'), isset($acknowledgement->is_sticky)
? ($acknowledgement->is_sticky ? t('Yes') : t('No'))
Expand All @@ -568,7 +590,7 @@ protected function assembleAcknowledgeEvent(AcknowledgementHistory $acknowledgem
$eventInfo[] = new HorizontalKeyValue(
t('Cleared on'),
DateFormatter::formatDateTime(
$acknowledgement->clear_time
$acknowledgement->clear_time !== null
? $acknowledgement->clear_time->getTimestamp()
: $this->event->event_time->getTimestamp()
)
Expand Down
4 changes: 3 additions & 1 deletion library/Icingadb/Widget/Detail/ObjectDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ protected function createPrintHeader()

$info[] = new HorizontalKeyValue(
t('Last State Change'),
DateFormatter::formatDateTime($this->object->state->last_state_change->getTimestamp())
isset($this->object->state->last_state_change)
? DateFormatter::formatDateTime($this->object->state->last_state_change)
: new EmptyState(t('n. a.'))
);

return [
Expand Down
8 changes: 7 additions & 1 deletion library/Icingadb/Widget/Detail/ServiceMetaInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Icinga\Date\DateFormatter;
use Icinga\Module\Icingadb\Model\Service;
use ipl\I18n\Translation;
use ipl\Web\Widget\EmptyState;
use ipl\Web\Widget\VerticalKeyValue;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
Expand All @@ -15,6 +17,8 @@

class ServiceMetaInfo extends BaseHtmlElement
{
use Translation;

protected $tag = 'div';

protected $defaultAttributes = ['class' => 'object-meta-info'];
Expand All @@ -33,7 +37,9 @@ protected function assemble()
new VerticalKeyValue('service.name', $this->service->name),
new VerticalKeyValue(
'last_state_change',
DateFormatter::formatDateTime($this->service->state->last_state_change->getTimestamp())
isset($this->service->state->last_state_change)
? DateFormatter::formatDateTime($this->service->state->last_state_change->getTimestamp())
: (new EmptyState($this->translate('n. a.')))->setTag('span')
)
);

Expand Down
14 changes: 8 additions & 6 deletions library/Icingadb/Widget/ItemList/BaseHistoryListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ protected function assembleCaption(BaseHtmlElement $caption): void
t('State Change Rate: %.2f%%; End Threshold: %.2f%%; Flapping for %s'),
$this->item->flapping->percent_state_change_end,
$this->item->flapping->flapping_threshold_low,
DateFormatter::formatDuration(
$this->item->flapping->end_time->getTimestamp()
- $this->item->flapping->start_time->getTimestamp()
)
isset($this->item->flapping->end_time)
? DateFormatter::formatDuration(
$this->item->flapping->end_time->getTimestamp()
- $this->item->flapping->start_time->getTimestamp()
)
: t('n. a.')
))
->getAttributes()
->add('class', 'plugin-output');
Expand Down Expand Up @@ -345,10 +347,10 @@ protected function assembleTitle(BaseHtmlElement $title): void

break;
case 'notification':
$subjectLabel = sprintf(
$subjectLabel = isset($this->item->notification->type) ? sprintf(
NotificationListItem::phraseForType($this->item->notification->type),
ucfirst($this->item->object_type)
);
) : $this->item->event_type;

break;
case 'state_change':
Expand Down
12 changes: 10 additions & 2 deletions library/Icingadb/Widget/ItemTable/StateRowItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ protected function assembleCell(BaseHtmlElement $cell, string $path, $value)
case $path === 'state.last_update':
case $path === 'state.last_state_change':
$column = substr($path, 6);
$cell->addHtml(new TimeSince($this->item->state->$column->getTimestamp()));
$cell->addHtml(
isset($this->item->state->$column)
? new TimeSince($this->item->state->$column->getTimestamp())
: new EmptyState(t('n. a.'))
);
break;
case $path === 'state.next_check':
case $path === 'state.next_update':
$column = substr($path, 6);
$cell->addHtml(new TimeUntil($this->item->state->$column->getTimestamp()));
$cell->addHtml(
isset($this->item->state->$column)
? new TimeUntil($this->item->state->$column->getTimestamp())
: new EmptyState(t('n. a.'))
);
break;
case $path === 'state.performance_data':
case $path === 'state.normalized_performance_data':
Expand Down
20 changes: 10 additions & 10 deletions phpstan-baseline-standard.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4487,7 +4487,7 @@ parameters:

-
message: "#^Cannot access property \\$host on mixed\\.$#"
count: 4
count: 2
path: library/Icingadb/Redis/VolatileStateResults.php

-
Expand Down Expand Up @@ -6960,6 +6960,11 @@ parameters:
count: 1
path: library/Icingadb/Widget/ItemList/BaseHistoryListItem.php

-
message: "#^Access to an undefined property object\\:\\:\\$start_time\\.$#"
count: 1
path: library/Icingadb/Widget/ItemList/BaseHistoryListItem.php

-
message: "#^Access to an undefined property object\\:\\:\\$text\\.$#"
count: 1
Expand Down Expand Up @@ -7070,11 +7075,6 @@ parameters:
count: 3
path: library/Icingadb/Widget/ItemList/BaseHistoryListItem.php

-
message: "#^Cannot access property \\$start_time on mixed\\.$#"
count: 1
path: library/Icingadb/Widget/ItemList/BaseHistoryListItem.php

-
message: "#^Cannot access property \\$state_type on mixed\\.$#"
count: 3
Expand Down Expand Up @@ -7542,22 +7542,22 @@ parameters:

-
message: "#^Cannot access property \\$last_state_change on mixed\\.$#"
count: 1
count: 2
path: library/Icingadb/Widget/ItemTable/StateRowItem.php

-
message: "#^Cannot access property \\$last_update on mixed\\.$#"
count: 1
count: 2
path: library/Icingadb/Widget/ItemTable/StateRowItem.php

-
message: "#^Cannot access property \\$next_check on mixed\\.$#"
count: 1
count: 2
path: library/Icingadb/Widget/ItemTable/StateRowItem.php

-
message: "#^Cannot access property \\$next_update on mixed\\.$#"
count: 1
count: 2
path: library/Icingadb/Widget/ItemTable/StateRowItem.php

-
Expand Down