Continue phase 2 device ops and dynamic QR lifecycle
This commit is contained in:
60
dist/shared/store/heartbeatStore.js
vendored
60
dist/shared/store/heartbeatStore.js
vendored
@ -100,3 +100,63 @@ export function deriveDeviceStatus(input) {
|
||||
}
|
||||
return "online";
|
||||
}
|
||||
export function deriveDeviceHealthSummary(input) {
|
||||
const now = Date.now();
|
||||
const lastSeen = Date.parse(input?.last_seen_at || "");
|
||||
const reasons = [];
|
||||
if (!Number.isFinite(lastSeen)) {
|
||||
return {
|
||||
status: "offline",
|
||||
score: 0,
|
||||
age_seconds: null,
|
||||
reasons: ["no_heartbeat"]
|
||||
};
|
||||
}
|
||||
const ageSeconds = Math.max(0, Math.floor((now - lastSeen) / 1000));
|
||||
const networkStrength = typeof input?.network_strength === "number" ? input.network_strength : null;
|
||||
const batteryLevel = typeof input?.battery_level === "number" ? input.battery_level : null;
|
||||
if (ageSeconds > 900) {
|
||||
reasons.push("offline_threshold_exceeded");
|
||||
}
|
||||
else if (ageSeconds > 90) {
|
||||
reasons.push("stale_threshold_exceeded");
|
||||
}
|
||||
if (typeof networkStrength === "number" && networkStrength < 40) {
|
||||
reasons.push("low_signal");
|
||||
}
|
||||
if (typeof batteryLevel === "number" && batteryLevel < 20) {
|
||||
reasons.push("low_battery");
|
||||
}
|
||||
let status = "online";
|
||||
if (reasons.includes("offline_threshold_exceeded")) {
|
||||
status = "offline";
|
||||
}
|
||||
else if (reasons.includes("stale_threshold_exceeded")) {
|
||||
status = "stale";
|
||||
}
|
||||
else if (reasons.includes("low_signal") || reasons.includes("low_battery")) {
|
||||
status = "degraded";
|
||||
}
|
||||
let score = 100;
|
||||
if (ageSeconds > 900) {
|
||||
score -= 80;
|
||||
}
|
||||
else if (ageSeconds > 90) {
|
||||
score -= 35;
|
||||
}
|
||||
else if (ageSeconds > 60) {
|
||||
score -= 10;
|
||||
}
|
||||
if (typeof networkStrength === "number") {
|
||||
score -= Math.max(0, 40 - networkStrength);
|
||||
}
|
||||
if (typeof batteryLevel === "number") {
|
||||
score -= Math.max(0, 20 - batteryLevel);
|
||||
}
|
||||
return {
|
||||
status,
|
||||
score: Math.max(0, Math.min(100, Math.round(score))),
|
||||
age_seconds: ageSeconds,
|
||||
reasons
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user