@@ -3,8 +3,9 @@ import QuestionMark from "../question_mark.svg?url";
33import QuestionMarkDark from "../question_mark_dark.svg?url" ;
44import { Settings , SettingsChangedEvent } from "../settings" ;
55import { registerInfoDialog } from "./info_dialog" ;
6- import { AppState } from "../service/state" ;
6+ import { AppState , CountEvent } from "../service/state" ;
77
8+ const UPDATE_PERIOD = 1000 ;
89const SECONDS_TO_MILLIS = 1000 ;
910const MINUTES_TO_SECONDS = 60 ;
1011const MINUTES_TO_MILLIS = MINUTES_TO_SECONDS * SECONDS_TO_MILLIS ;
@@ -39,7 +40,7 @@ class EventCounterData {
3940 }
4041}
4142
42- const events : [ Date , number ] [ ] = [ ] ;
43+ const events : CountEvent [ ] = [ ] ;
4344let prevData = new EventCounterData ( ) ;
4445let lastData = new EventCounterData ( ) ;
4546let lastUpdate = 0 ;
@@ -78,7 +79,7 @@ export function setupEventCounters(
7879
7980 setInterval ( ( ) => {
8081 updateCounters ( state . newEventsQueue ) ;
81- } , 1000 ) ;
82+ } , UPDATE_PERIOD ) ;
8283
8384 settings . addChangedListener ( ( event : CustomEvent < SettingsChangedEvent > ) => {
8485 // Reset counters when a filter is changed
@@ -203,34 +204,35 @@ function addElements(
203204 } ;
204205}
205206
206- function updateCounters ( newEventsQueue : number [ ] ) {
207+ function updateCounters ( newEventsQueue : CountEvent [ ] ) {
207208 const currentData = new EventCounterData ( ) ;
208209
209- const currentDate = new Date ( ) ;
210+ const currentTime = Date . now ( ) ;
210211 lastUpdate = performance . now ( ) ;
211- events . forEach ( ( [ date , count ] ) => {
212- currentData . total += count ;
213- if ( date . getTime ( ) + 5 * MINUTES_TO_MILLIS > currentDate . getTime ( ) ) {
214- currentData . last5min += count ;
215-
216- if ( date . getTime ( ) + 1 * MINUTES_TO_MILLIS > currentDate . getTime ( ) ) {
217- currentData . last1min += count ;
218-
219- if ( date . getTime ( ) + 10 * SECONDS_TO_MILLIS > currentDate . getTime ( ) ) {
220- currentData . last10s += count ;
212+ events . forEach ( ( { startTime, count } ) => {
213+ if ( startTime <= currentTime ) {
214+ currentData . total += count ;
215+ if ( startTime + 5 * MINUTES_TO_MILLIS > currentTime ) {
216+ currentData . last5min += count ;
217+
218+ if ( startTime + 1 * MINUTES_TO_MILLIS > currentTime ) {
219+ currentData . last1min += count ;
220+
221+ if ( startTime + 10 * SECONDS_TO_MILLIS > currentTime ) {
222+ currentData . last10s += count ;
223+ }
221224 }
222225 }
223226 }
224227 } ) ;
225228
226- const newSum = newEventsQueue
227- . splice ( 0 , newEventsQueue . length )
228- . reduce ( ( l : number , r : number , _index , _array ) => l + r , 0 ) ;
229- if ( newSum > 0 ) {
230- events . push ( [ new Date ( ) , newSum ] ) ;
229+ newEventsQueue . splice ( 0 , newEventsQueue . length ) . forEach ( ( c : CountEvent ) => {
230+ events . push ( c ) ;
231231
232- currentData . addToAll ( newSum ) ;
233- }
232+ if ( c . startTime <= currentTime ) {
233+ currentData . addToAll ( c . count ) ;
234+ }
235+ } ) ;
234236
235237 prevData = lastData ;
236238 lastData = currentData ;
@@ -240,8 +242,8 @@ function updateCountersUI(
240242 timestamp : DOMHighResTimeStamp ,
241243 elements : EventCounterElements ,
242244) {
243- const delta = clamp ( timestamp - lastUpdate , 0 , 1000 ) ;
244- const factor = delta / 1000 ;
245+ const delta = clamp ( timestamp - lastUpdate , 0 , UPDATE_PERIOD ) ;
246+ const factor = delta / UPDATE_PERIOD ;
245247 const scaledValue = prevData . lerpTo ( lastData , factor ) ;
246248 for ( const [ element , value ] of [
247249 [ elements . total , scaledValue . total ] ,
0 commit comments