Add goal notifications and collector health
Test and publish / test (push) Successful in 1m40s
Test and publish / publish (push) Successful in 3m45s

This commit is contained in:
2026-10-23 20:45:51 +01:00
parent 667091efb0
commit 453cef40af
8 changed files with 375 additions and 3 deletions
+12
View File
@@ -48,6 +48,18 @@ export interface NotificationSettings {
retention_days: number; updated_at?: string;
}
export interface CollectorHealth {
id: number; name: string; kind: string;
last_seen: string | null; age_s: number | null;
status: "healthy" | "stale" | "never_seen" | "revoked";
activity_count: number; sample_count: number;
}
export interface GoalNotificationResult {
date: string; evaluated: number; at_risk: number; sent: number;
configured: boolean; errors: string[];
}
export function dur(s: number): string {
s = Math.round(s);
const h = Math.floor(s / 3600), m = Math.floor(s % 3600 / 60);
+2
View File
@@ -104,6 +104,8 @@ li { margin:6px 0; }
.data-actions { margin:10px 0 14px; }
.privacy-tools { border-top:1px solid var(--line); margin-top:16px; padding-top:12px; }
.privacy-tools h3 { margin-top:0; }
.notification-check { border-top:1px solid var(--line); margin-top:16px; padding-top:4px; }
.notification-check h3 { margin-bottom:2px; }
.report-list { border-top:1px solid var(--line); margin-top:16px; padding-top:12px; }
.report { border-bottom:1px solid var(--line); padding:8px 0; }
.report:last-child { border-bottom:0; }
+72 -2
View File
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, reactive, ref, watch } from "vue";
import { useRoute } from "vue-router";
import { api, type AdminGoal, type Device, type NotificationSettings, type Report } from "../api";
import { api, type AdminGoal, type CollectorHealth, type Device, type GoalNotificationResult, type NotificationSettings, type Report } from "../api";
import CodeBlock from "../components/CodeBlock.vue";
import Collector from "../components/Collector.vue";
import KeyBox from "../components/KeyBox.vue";
@@ -25,6 +25,13 @@ const reports = ref<Report[]>([]);
const notifications = reactive<NotificationSettings>({ ntfy_server: "https://ntfy.sh", ntfy_topic: "", enabled: false, retention_days: 0 });
const purgeBefore = ref(new Date(Date.now() - 90 * 86400000).toISOString().slice(0, 10));
const privacyBusy = ref(false);
const collectorHealth = ref<CollectorHealth[]>([]);
const collectorHealthLoaded = ref(false);
const collectorHealthBusy = ref(false);
const collectorHealthError = ref("");
const notificationCheckBusy = ref(false);
const notificationCheck = ref<GoalNotificationResult | null>(null);
const notificationCheckError = ref("");
// Which collector sections are open: the browser's by default, and the one a
// #section in the URL names.
@@ -93,16 +100,50 @@ async function loadGoals() {
}
async function loadAdminData() {
try {
const [notification, reportList] = await Promise.all([
const [notification, reportList, health] = await Promise.all([
api<{ settings: NotificationSettings }>("GET", "notifications/status"),
api<{ reports: Report[] }>("GET", "reports"),
api<{ collectors: CollectorHealth[] }>("GET", "health/collectors"),
]);
Object.assign(notifications, notification.settings);
reports.value = reportList.reports;
collectorHealth.value = health.collectors;
collectorHealthLoaded.value = true;
} catch (e) { status.value = { msg: (e as Error).message, cls: "err" }; }
}
onMounted(() => { loadDevices(); loadGoals(); loadAdminData(); });
function healthStatus(health: CollectorHealth) {
return health.status === "healthy" ? { label: "Healthy", cls: "good" }
: health.status === "stale" ? { label: "Stale", cls: "warn" }
: health.status === "never_seen" ? { label: "Never seen", cls: "warn" }
: { label: "Revoked", cls: "bad" };
}
function healthAge(seconds: number | null) {
return seconds === null ? "never" : ago(new Date(Date.now() - seconds * 1000).toISOString());
}
async function refreshCollectorHealth() {
collectorHealthBusy.value = true;
collectorHealthError.value = "";
try {
collectorHealth.value = (await api<{ collectors: CollectorHealth[] }>("GET", "health/collectors")).collectors;
collectorHealthLoaded.value = true;
} catch (e) { collectorHealthError.value = (e as Error).message; }
finally { collectorHealthBusy.value = false; }
}
async function checkGoalNotifications() {
notificationCheckBusy.value = true;
notificationCheckError.value = "";
notificationCheck.value = null;
try {
notificationCheck.value = await api<GoalNotificationResult>("POST", "notifications/goals/check");
} catch (e) { notificationCheckError.value = (e as Error).message; }
finally { notificationCheckBusy.value = false; }
}
// A readable name that isn't taken yet (names stay taken after revoking).
function suggestName(kind: string) {
let stem = ({ location: "iphone", manual: "script" } as Record<string, string>)[kind] || kind;
@@ -399,6 +440,14 @@ docker exec activity-tracker tracker goal list
<label>Retention days <span class="muted">(0 = keep forever)</span><input v-model.number="notifications.retention_days" type="number" min="0"></label>
<button class="primary" :disabled="privacyBusy">Save settings</button>
</form>
<div class="notification-check">
<div class="row spread"><div><h3>Check goal risks</h3><p class="muted small-text">Evaluate today's goals now and send notifications for goals behind pace.</p></div><button class="primary" :disabled="notificationCheckBusy" @click="checkGoalNotifications">{{ notificationCheckBusy ? "Checking…" : "Check and send" }}</button></div>
<p v-if="notificationCheckError" class="small-text err" role="alert">{{ notificationCheckError }}</p>
<p v-else-if="notificationCheck" class="small-text ok" role="status" aria-live="polite">
Checked {{ notificationCheck.evaluated }} goal{{ notificationCheck.evaluated === 1 ? "" : "s" }}: {{ notificationCheck.at_risk }} at risk, {{ notificationCheck.sent }} notification{{ notificationCheck.sent === 1 ? "" : "s" }} sent{{ notificationCheck.configured ? "" : " (notifications are not configured)" }}.
<span v-if="notificationCheck.errors.length" class="err"> {{ notificationCheck.errors.join(" ") }}</span>
</p>
</div>
<div class="privacy-tools">
<h3>Delete old records</h3>
<p class="muted small-text">This permanently deletes activities, samples, and reports before the selected date.</p>
@@ -415,4 +464,25 @@ docker exec activity-tracker tracker goal list
</article>
</div>
</section>
<section class="card" aria-labelledby="collector-health-title">
<div class="row spread"><h2 id="collector-health-title">Collector health</h2><div class="row"><span class="badge">Admin only</span><button class="small" :disabled="collectorHealthBusy" @click="refreshCollectorHealth">{{ collectorHealthBusy ? "Refreshing…" : "Refresh" }}</button></div></div>
<p class="muted small-text">Last upload and all-time ingestion counts for each collector.</p>
<p v-if="collectorHealthError" class="small-text err" role="alert">{{ collectorHealthError }}</p>
<div class="tablewrap"><table>
<caption class="sr-only">Collector health and ingestion counts</caption>
<thead><tr><th scope="col">Name</th><th scope="col">Health</th><th scope="col">Last upload</th><th scope="col">Activities</th><th scope="col">Samples</th></tr></thead>
<tbody v-if="!collectorHealthLoaded"><tr><td colspan="5" class="muted">Loading…</td></tr></tbody>
<tbody v-else>
<tr v-for="collector in collectorHealth" :key="collector.id">
<td class="label">{{ collector.name }} <span class="badge">{{ collector.kind }}</span></td>
<td><span class="status-dot" :class="healthStatus(collector).cls"></span>{{ healthStatus(collector).label }}</td>
<td class="time">{{ healthAge(collector.age_s) }}</td>
<td class="num">{{ collector.activity_count }}</td>
<td class="num">{{ collector.sample_count }}</td>
</tr>
<tr v-if="!collectorHealth.length"><td colspan="5" class="muted">No collectors registered.</td></tr>
</tbody>
</table></div>
</section>
</template>