Skip to content

Commit cbe2181

Browse files
add sort
1 parent 80f084c commit cbe2181

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

server/api/electricty_tracker/[slug]/history.get.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@ export default defineEventHandler(async (event) => {
8686
for (const ping of timestamps) {
8787
if (!prevPing) {
8888
if (currentState === 'offline') {
89-
// Transition to online at first ping
9089
history.push({ status: 'offline', start: currentStart, end: ping })
9190
currentState = 'online'
9291
currentStart = ping
9392
} else {
94-
// Already online; keep session from week start
95-
// no interval push here
9693
}
9794
prevPing = ping
9895
continue
@@ -113,10 +110,14 @@ export default defineEventHandler(async (event) => {
113110
prevPing = ping
114111
}
115112

116-
// Close the current interval to now (end null for current state)
117113
history.push({ status: currentState, start: currentStart, end: null })
118114

119-
// Determine current.since from the last interval
115+
history.sort((a, b) => {
116+
const aEnd = a.end ? new Date(a.end).getTime() : Infinity
117+
const bEnd = b.end ? new Date(b.end).getTime() : Infinity
118+
return bEnd - aEnd
119+
})
120+
120121
const lastInterval = history[history.length - 1]
121122
current.since = lastInterval.start
122123

0 commit comments

Comments
 (0)