Skip to content

Commit 383a5b0

Browse files
committed
Merge branch 'release/1.0.1'
2 parents 4278918 + 735e88b commit 383a5b0

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This plugin provides a [Oh Dear](https://ohdear.app) Application Health checker
66

77
## 🚦 Health Checks Overview
88

9-
This plugin performs the following health checks and provides a JSON feed on yourwebsite.com/application-health.json for Oh Dear. The response is cached for 10 minutes.
9+
This plugin performs the following health checks and provides a JSON feed on yourwebsite.com/application-health.json for Oh Dear. The response is cached for 5 minutes.
1010

1111
### ✅ Updates
1212
Checks if updates are available for Craft CMS and installed plugins.

src/controllers/HealthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HealthController extends Controller
1919
{
2020
protected array|int|bool $allowAnonymous = true;
2121
private const CACHE_KEY = 'ohdear-health-check-json';
22-
private const CACHE_DURATION = 600;
22+
private const CACHE_DURATION = 300;
2323

2424
public function actionCheck(): Response
2525
{

src/services/HealthCheckService.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,89 @@ private function addUpdateCheck(CheckResults $checkResults): void
4949
$meta = [];
5050
$updateCount = 0;
5151
$updateNames = [];
52+
$criticalUpdates = [];
53+
$oldestUpdateDate = null;
54+
$now = new DateTime();
5255

5356
if ($updates->cms->getHasReleases()) {
54-
$meta['Craft CMS'] = sprintf('%s => %s', Craft::$app->version, $updates->cms->getLatest()->version);
57+
$oldest = null;
58+
foreach ($updates->cms->releases as $release) {
59+
if ($oldest === null || $release->date < $oldest) {
60+
$oldest = $release->date;
61+
}
62+
}
63+
64+
$latest = $updates->cms->getLatest();
65+
$ageDays = $oldest ? $now->diff($oldest)->days : 'unknown';
66+
$meta['Craft CMS'] = sprintf('%s => %s (oldest update %s days ago)', Craft::$app->version, $latest->version, $ageDays);
5567
$updateNames[] = 'Craft CMS';
5668
$updateCount++;
69+
70+
if ($updates->cms->getHasCritical()) {
71+
$criticalUpdates[] = 'Craft CMS';
72+
}
73+
74+
if ($oldest && ($oldestUpdateDate === null || $oldest < $oldestUpdateDate)) {
75+
$oldestUpdateDate = $oldest;
76+
}
5777
}
5878

5979
foreach ($updates->plugins as $pluginHandle => $pluginUpdate) {
6080
if ($pluginUpdate->getHasReleases()) {
6181
try {
6282
$pluginInfo = Craft::$app->getPlugins()->getPluginInfo($pluginHandle);
6383
if ($pluginInfo['isInstalled']) {
64-
$meta[$pluginInfo['name']] = sprintf('%s => %s', $pluginInfo['version'], $pluginUpdate->getLatest()->version);
84+
$oldest = null;
85+
foreach ($pluginUpdate->releases as $release) {
86+
if ($oldest === null || $release->date < $oldest) {
87+
$oldest = $release->date;
88+
}
89+
}
90+
91+
$latest = $pluginUpdate->getLatest();
92+
$ageDays = $oldest ? $now->diff($oldest)->days : 'unknown';
93+
$meta[$pluginInfo['name']] = sprintf('%s => %s (oldest update %s days ago)', $pluginInfo['version'], $latest->version, $ageDays);
6594
$updateNames[] = $pluginInfo['name'];
6695
$updateCount++;
96+
97+
if ($pluginUpdate->getHasCritical()) {
98+
$criticalUpdates[] = $pluginInfo['name'];
99+
}
100+
101+
if ($oldest && ($oldestUpdateDate === null || $oldest < $oldestUpdateDate)) {
102+
$oldestUpdateDate = $oldest;
103+
}
67104
}
68105
} catch (\craft\errors\InvalidPluginException $e) {
69106
continue;
70107
}
71108
}
72109
}
73110

74-
$status = $updateCount === 0 ? CheckResult::STATUS_OK : CheckResult::STATUS_WARNING;
75-
$message = $updateCount === 0 ? 'Plugins and CMS are up to date' : implode(', ', $updateNames) . ' have updates available';
111+
$status = CheckResult::STATUS_OK;
112+
$message = $updateCount === 0
113+
? 'Plugins and CMS are up to date'
114+
: implode(', ', $updateNames) . ' have updates available';
115+
116+
$shortSummary = "{$updateCount} updates";
117+
118+
if (!empty($criticalUpdates)) {
119+
$status = CheckResult::STATUS_WARNING;
120+
$message = '🚨 ' . $message . ' (critical updates: ' . implode(', ', $criticalUpdates) . ')';
121+
$shortSummary = "🚨 {$updateCount} updates (critical)";
122+
} elseif ($oldestUpdateDate !== null) {
123+
$diff = $now->diff($oldestUpdateDate);
124+
if ($diff->days > 30) {
125+
$status = CheckResult::STATUS_WARNING;
126+
$message .= ' (oldest update is over 30 days old)';
127+
}
128+
}
76129

77130
$checkResults->addCheckResult(new CheckResult(
78131
name: 'Updates',
79132
label: 'Available Updates',
80133
notificationMessage: $message,
81-
shortSummary: "{$updateCount} updates",
134+
shortSummary: $shortSummary,
82135
status: $status,
83136
meta: $meta
84137
));

0 commit comments

Comments
 (0)