Skip to content

Commit 7e6ea63

Browse files
authored
Merge pull request #80 from govigilant/develop
Fix bug in outpost observer and uptime result aggregates
2 parents ef985b0 + 1540847 commit 7e6ea63

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

packages/uptime/src/Actions/AggregateResults.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,33 @@ public function aggregate(Monitor $monitor): void
1313
{
1414
$results = $monitor
1515
->results()
16-
->select(['id', 'country'])
16+
->select(['id', 'country', 'created_at'])
1717
->where('created_at', '<', now()->subHour())
1818
->get();
1919

2020
$groupedByCountry = $results->groupBy('country');
2121

2222
foreach ($groupedByCountry as $country => $countryResults) {
23-
$resultChunks = $countryResults->chunk(60);
23+
$groupedByHour = $countryResults->groupBy(function ($result) {
24+
return $result->created_at->startOfHour()->toDateTimeString();
25+
});
2426

25-
/** @var Collection $chunk */
26-
foreach ($resultChunks as $chunk) {
27-
$ids = $chunk->pluck('id');
27+
foreach ($groupedByHour as $hour => $hourResults) {
28+
$ids = $hourResults->pluck('id')->toArray();
2829

29-
$query = Result::query()
30-
->whereIn('id', $ids);
30+
$averageTotalTime = Result::query()
31+
->whereIn('id', $ids)
32+
->average('total_time');
3133

3234
ResultAggregate::query()->create([
3335
'monitor_id' => $monitor->id,
34-
'total_time' => $query->average('total_time'),
36+
'total_time' => $averageTotalTime,
3537
'country' => $country,
36-
'created_at' => now()->subHour(),
37-
'updated_at' => now()->subHour(),
38+
'created_at' => $hour,
39+
'updated_at' => $hour,
3840
]);
3941

40-
$query->delete();
42+
Result::query()->whereIn('id', $ids)->delete();
4143
}
4244
}
4345
}

packages/uptime/src/Observers/OutpostObserver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function updated(Outpost $outpost): void
1313
// If outpost becomes unavailable, clear it from monitors using it as closest outpost
1414
if ($outpost->status === OutpostStatus::Unavailable) {
1515
Monitor::query()
16+
->withoutGlobalScopes()
1617
->where('closest_outpost_id', $outpost->id)
1718
->update(['closest_outpost_id' => null]);
1819

@@ -27,6 +28,7 @@ public function deleted(Outpost $outpost): void
2728
{
2829
// Clear the closest_outpost_id for monitors using this outpost
2930
Monitor::query()
31+
->withoutGlobalScopes()
3032
->where('closest_outpost_id', $outpost->id)
3133
->update(['closest_outpost_id' => null]);
3234
}

0 commit comments

Comments
 (0)