Skip to content
Open
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
27 changes: 22 additions & 5 deletions src/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
use Cachet\Enums\SystemStatusEnum;
use Cachet\Models\Component;
use Cachet\Models\Incident;
use Cachet\Models\Schedule;
use Cachet\Models\Update;
use Cachet\Settings\AppSettings;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Date;

class Status
{
Expand Down Expand Up @@ -104,14 +108,27 @@ public function incidents(): object
}

/**
* Get the most recent update timestamp across enabled components.
* Get the most recent guest-visible activity timestamp across components,
* incidents, incident updates and schedules.
*/
public function lastUpdated(): ?CarbonInterface
{
return Component::query()
->enabled()
->latest('updated_at')
->first(['updated_at'])?->updated_at;
return collect([
Component::query()->enabled()->max('updated_at'),
Incident::query()->viewableBy(false)->max('updated_at'),
Schedule::query()->published()->max('updated_at'),
Update::query()
->where('updateable_type', Relation::getMorphAlias(Incident::class))
->whereIn('updateable_id', Incident::query()->viewableBy(false)->select('id'))
->max('updated_at'),
Update::query()
->where('updateable_type', Relation::getMorphAlias(Schedule::class))
->whereIn('updateable_id', Schedule::query()->published()->select('id'))
->max('updated_at'),
])
->filter()
->map(fn ($timestamp): CarbonInterface => Date::parse($timestamp))
->max();
}

/**
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,60 @@
->toEqual($component->updated_at);
});

it('considers incidents, incident updates and schedules when calculating the last updated timestamp', function () {
expect((new Status)->lastUpdated())->toBeNull();

Component::factory()->create([
'enabled' => true,
'updated_at' => now()->subWeek(),
]);
$incident = Incident::factory()->create([
'visible' => ResourceVisibilityEnum::guest,
'updated_at' => now()->subDay(),
]);

expect((new Status)->lastUpdated())->toEqual($incident->updated_at);

$update = Update::factory()->forIncident($incident)->create([
'status' => null,
'created_at' => now()->subHour(),
'updated_at' => now()->subHour(),
]);

expect((new Status)->lastUpdated())->toEqual($update->updated_at);

$schedule = Schedule::factory()->create([
'updated_at' => now()->subMinute(),
]);

expect((new Status)->lastUpdated())->toEqual($schedule->updated_at);
});

it('ignores activity hidden from guests when calculating the last updated timestamp', function () {
$component = Component::factory()->create([
'enabled' => true,
'updated_at' => now()->subWeek(),
]);
$hidden = Incident::factory()->create([
'visible' => ResourceVisibilityEnum::authenticated,
'updated_at' => now()->subMinute(),
]);
Update::factory()->forIncident($hidden)->create([
'status' => null,
'created_at' => now()->subMinute(),
'updated_at' => now()->subMinute(),
]);
Incident::factory()->scheduled()->create([
'visible' => ResourceVisibilityEnum::guest,
'updated_at' => now()->subMinute(),
]);
Schedule::factory()->scheduled()->create([
'updated_at' => now()->subMinute(),
]);

expect((new Status)->lastUpdated())->toEqual($component->updated_at);
});

it('excludes disabled components from component overview', function () {
Component::factory()->create([
'status' => ComponentStatusEnum::operational->value,
Expand Down
Loading