File tree Expand file tree Collapse file tree 1 file changed +6
-5
lines changed
server/api/electricty_tracker/[slug] Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments