|
| 1 | +import { defineEventHandler, getRouterParam, createError, useNitroApp } from '#imports' |
| 2 | + |
| 3 | +type Status = 'online' | 'offline' |
| 4 | + |
| 5 | +const TEN_MIN_MS = 10 * 60 * 1000 |
| 6 | +const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000 |
| 7 | + |
| 8 | +export default defineEventHandler(async (event) => { |
| 9 | + const user = event.context.user |
| 10 | + if (!user) { |
| 11 | + throw createError({ statusCode: 401, statusMessage: 'Unauthorized' }) |
| 12 | + } |
| 13 | + |
| 14 | + const slug = getRouterParam(event, 'slug') |
| 15 | + if (!slug) { |
| 16 | + throw createError({ statusCode: 400, statusMessage: 'Missing slug' }) |
| 17 | + } |
| 18 | + |
| 19 | + const db = useNitroApp().db |
| 20 | + |
| 21 | + const tracker = await db.collection('electricity_trackers').findOne({ slug, userId: user.userId }) |
| 22 | + if (!tracker) { |
| 23 | + throw createError({ statusCode: 404, statusMessage: 'Tracker not found' }) |
| 24 | + } |
| 25 | + |
| 26 | + const now = new Date() |
| 27 | + const weekStart = new Date(now.getTime() - SEVEN_DAYS_MS) |
| 28 | + |
| 29 | + const preWindowPing = await db |
| 30 | + .collection('electricity_tracker_alive') |
| 31 | + .find({ deviceSlug: slug, timestamp: { $lt: weekStart } }) |
| 32 | + .sort({ timestamp: -1 }) |
| 33 | + .limit(1) |
| 34 | + .toArray() |
| 35 | + |
| 36 | + const pingsInWindow = await db |
| 37 | + .collection('electricity_tracker_alive') |
| 38 | + .find({ deviceSlug: slug, timestamp: { $gte: weekStart, $lte: now } }) |
| 39 | + .sort({ timestamp: 1 }) |
| 40 | + .toArray() |
| 41 | + |
| 42 | + const timestamps: Date[] = pingsInWindow.map((d: any) => d.timestamp as Date) |
| 43 | + const lastPingDoc = await db |
| 44 | + .collection('electricity_tracker_alive') |
| 45 | + .find({ deviceSlug: slug }) |
| 46 | + .sort({ timestamp: -1 }) |
| 47 | + .limit(1) |
| 48 | + .toArray() |
| 49 | + |
| 50 | + const lastAliveAt: Date | undefined = lastPingDoc.length ? (lastPingDoc[0].timestamp as Date) : undefined |
| 51 | + const current: { status: Status; lastAliveAt?: Date; since?: Date } = { |
| 52 | + status: lastAliveAt && now.getTime() - lastAliveAt.getTime() <= TEN_MIN_MS ? 'online' : 'offline', |
| 53 | + lastAliveAt, |
| 54 | + since: undefined, |
| 55 | + } |
| 56 | + |
| 57 | + type Interval = { status: Status; start: Date; end: Date | null } |
| 58 | + |
| 59 | + const history: Interval[] = [] |
| 60 | + |
| 61 | + let currentState: Status |
| 62 | + let currentStart: Date |
| 63 | + const firstInWindow = timestamps[0] |
| 64 | + |
| 65 | + if (firstInWindow) { |
| 66 | + const pre = preWindowPing[0]?.timestamp as Date | undefined |
| 67 | + const gapToStart = pre ? firstInWindow.getTime() - pre.getTime() : Infinity |
| 68 | + // If there was a ping shortly before the first in-window ping, consider the device online at week start |
| 69 | + if (pre && (weekStart.getTime() - pre.getTime() <= TEN_MIN_MS || gapToStart <= TEN_MIN_MS)) { |
| 70 | + currentState = 'online' |
| 71 | + } else { |
| 72 | + currentState = 'offline' |
| 73 | + } |
| 74 | + } else { |
| 75 | + // No pings in window |
| 76 | + const pre = preWindowPing[0]?.timestamp as Date | undefined |
| 77 | + if (pre && weekStart.getTime() - pre.getTime() <= TEN_MIN_MS) { |
| 78 | + currentState = 'online' |
| 79 | + } else { |
| 80 | + currentState = 'offline' |
| 81 | + } |
| 82 | + } |
| 83 | + currentStart = new Date(weekStart) |
| 84 | + |
| 85 | + let prevPing: Date | undefined |
| 86 | + for (const ping of timestamps) { |
| 87 | + if (!prevPing) { |
| 88 | + if (currentState === 'offline') { |
| 89 | + // Transition to online at first ping |
| 90 | + history.push({ status: 'offline', start: currentStart, end: ping }) |
| 91 | + currentState = 'online' |
| 92 | + currentStart = ping |
| 93 | + } else { |
| 94 | + // Already online; keep session from week start |
| 95 | + // no interval push here |
| 96 | + } |
| 97 | + prevPing = ping |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + const gap = ping.getTime() - prevPing.getTime() |
| 102 | + if (gap > TEN_MIN_MS) { |
| 103 | + if (currentState === 'online') { |
| 104 | + history.push({ status: 'online', start: currentStart, end: prevPing }) |
| 105 | + currentState = 'offline' |
| 106 | + currentStart = prevPing |
| 107 | + } |
| 108 | + // We are offline until this ping |
| 109 | + history.push({ status: 'offline', start: currentStart, end: ping }) |
| 110 | + currentState = 'online' |
| 111 | + currentStart = ping |
| 112 | + } |
| 113 | + prevPing = ping |
| 114 | + } |
| 115 | + |
| 116 | + // Close the current interval to now (end null for current state) |
| 117 | + history.push({ status: currentState, start: currentStart, end: null }) |
| 118 | + |
| 119 | + // Determine current.since from the last interval |
| 120 | + const lastInterval = history[history.length - 1] |
| 121 | + current.since = lastInterval.start |
| 122 | + |
| 123 | + return { |
| 124 | + tracker: { |
| 125 | + name: tracker.name, |
| 126 | + slug: tracker.slug, |
| 127 | + createdAt: tracker.createdAt, |
| 128 | + }, |
| 129 | + current: { |
| 130 | + status: current.status, |
| 131 | + lastAliveAt: current.lastAliveAt, |
| 132 | + since: current.since, |
| 133 | + }, |
| 134 | + history: history, |
| 135 | + } |
| 136 | +}) |
| 137 | + |
| 138 | + |
0 commit comments