@@ -55,10 +55,10 @@ const calcAverage = (data: LogsResponseWithHeaders | undefined) => {
55
55
if ( ! data || ! Array . isArray ( data ?. records ) ) return 0 ;
56
56
57
57
const { fields, records } = data ;
58
- if ( _ . isEmpty ( records ) || ! _ . includes ( fields , 'log_count ' ) ) return 0 ;
58
+ if ( _ . isEmpty ( records ) || ! _ . includes ( fields , 'count ' ) ) return 0 ;
59
59
60
60
const total = records . reduce ( ( acc , d ) => {
61
- return acc + _ . toNumber ( d . log_count ) || 0 ;
61
+ return acc + _ . toNumber ( d . count ) || 0 ;
62
62
} , 0 ) ;
63
63
return parseInt ( Math . abs ( total / records . length ) . toFixed ( 0 ) ) ;
64
64
} ;
@@ -74,64 +74,58 @@ type GraphTickItem = {
74
74
75
75
type LogRecord = {
76
76
counts_timestamp : string ;
77
- log_count : number ;
77
+ count : number ;
78
78
} ;
79
79
80
80
// date_bin removes tz info
81
81
// filling data with empty values where there is no rec
82
- const parseGraphData = (
83
- data : LogsResponseWithHeaders | undefined ,
84
- avg : number ,
85
- startTime : Date ,
86
- endTime : Date ,
87
- interval : number ,
88
- ) : GraphTickItem [ ] => {
82
+ const parseGraphData = ( data : LogsResponseWithHeaders | undefined , avg : number , interval : number ) : GraphTickItem [ ] => {
89
83
if ( ! data || ! Array . isArray ( data ?. records ) ) return [ ] ;
90
84
91
85
const { fields, records } = data ;
92
- if ( _ . isEmpty ( records ) || ! _ . includes ( fields , 'log_count' ) || ! _ . includes ( fields , 'counts_timestamp' ) ) return [ ] ;
86
+ if (
87
+ _ . isEmpty ( records ) ||
88
+ ! _ . includes ( fields , 'count' ) ||
89
+ ! _ . includes ( fields , 'start_time' ) ||
90
+ ! _ . includes ( fields , 'end_time' )
91
+ )
92
+ return [ ] ;
93
93
94
94
const compactType = getCompactType ( interval ) ;
95
- const ticksCount = interval < 10 * 60 * 1000 ? interval / ( 60 * 1000 ) : interval < 60 * 60 * 1000 ? 10 : 60 ;
96
- const intervalDuration = ( endTime . getTime ( ) - startTime . getTime ( ) ) / ticksCount ;
97
95
98
96
const isValidRecord = ( record : any ) : record is LogRecord => {
99
- return typeof record . counts_timestamp === 'string' && typeof record . log_count === 'number' ;
97
+ return (
98
+ typeof record . start_time === 'string' &&
99
+ typeof record . end_time === 'string' &&
100
+ typeof record . count === 'number' &&
101
+ record . start_time &&
102
+ record . end_time
103
+ ) ;
100
104
} ;
101
105
102
- const allTimestamps = Array . from (
103
- { length : ticksCount } ,
104
- ( _ , index ) => new Date ( startTime . getTime ( ) + index * intervalDuration ) ,
105
- ) ;
106
-
107
- const parsedData = allTimestamps . map ( ( ts ) => {
108
- const countData = records . find ( ( d ) => {
109
- if ( ! isValidRecord ( d ) ) return false ;
110
- const recordTime = new Date ( d . counts_timestamp ) . getTime ( ) ;
111
- const tsTime = ts . getTime ( ) ;
112
- return Math . abs ( recordTime - tsTime ) < intervalDuration / 2 ;
113
- } ) ;
114
-
115
- const defaultOpts = {
116
- events : 0 ,
117
- minute : ts ,
118
- aboveAvgPercent : 0 ,
119
- compactType,
120
- startTime : dayjs ( ts ) ,
121
- endTime : dayjs ( new Date ( ts . getTime ( ) + intervalDuration ) ) ,
122
- } ;
106
+ // Filter valid records and sort them by start timestamp
107
+ const validRecords = records
108
+ . filter ( isValidRecord )
109
+ . map ( ( record ) => ( {
110
+ ...record ,
111
+ startDate : record . start_time ? new Date ( record . start_time ) : new Date ( ) ,
112
+ endDate : record . end_time ? new Date ( record . end_time ) : new Date ( ) ,
113
+ } ) )
114
+ . sort ( ( a , b ) => a . startDate . getTime ( ) - b . startDate . getTime ( ) ) ;
123
115
124
- if ( ! countData || ! isValidRecord ( countData ) ) {
125
- return defaultOpts ;
126
- }
116
+ if ( validRecords . length === 0 ) return [ ] ;
127
117
128
- const aboveAvgCount = countData . log_count - avg ;
118
+ const parsedData = validRecords . map ( ( record : any ) => {
119
+ const aboveAvgCount = record . count - avg ;
129
120
const aboveAvgPercent = avg > 0 ? parseInt ( ( ( aboveAvgCount / avg ) * 100 ) . toFixed ( 2 ) ) : 0 ;
130
121
131
122
return {
132
- ... defaultOpts ,
133
- events : countData . log_count ,
123
+ events : record . count ,
124
+ minute : record . startDate ,
134
125
aboveAvgPercent,
126
+ compactType,
127
+ startTime : dayjs ( record . startDate ) ,
128
+ endTime : dayjs ( record . endDate ) ,
135
129
} ;
136
130
} ) ;
137
131
@@ -179,16 +173,13 @@ const EventTimeLineGraph = () => {
179
173
useEffect ( ( ) => {
180
174
if ( ! localStream || localStream . length === 0 || ! firstEventAt ) return ;
181
175
182
- const adjustedStartTime = dayjs ( startTime ) . startOf ( 'minute' ) ;
183
- const adjustedEndTime = dayjs ( endTime ) . startOf ( 'minute' ) ;
184
-
185
176
const totalMinutes = interval / ( 1000 * 60 ) ;
186
177
const numBins = Math . trunc ( totalMinutes < 10 ? totalMinutes : totalMinutes < 60 ? 10 : 60 ) ;
187
178
188
179
const logsQuery = {
189
180
stream : localStream ,
190
- startTime : adjustedStartTime . toISOString ( ) ,
191
- endTime : adjustedEndTime . add ( 1 , 'minute' ) . toISOString ( ) ,
181
+ startTime : dayjs ( startTime ) . startOf ( 'minute' ) . toISOString ( ) ,
182
+ endTime : dayjs ( endTime ) . startOf ( 'minute' ) . toISOString ( ) ,
192
183
numBins,
193
184
} ;
194
185
@@ -205,7 +196,7 @@ const EventTimeLineGraph = () => {
205
196
const avgEventCount = useMemo ( ( ) => calcAverage ( fetchGraphDataMutation ?. data ) , [ fetchGraphDataMutation ?. data ] ) ;
206
197
const graphData = useMemo ( ( ) => {
207
198
if ( ! firstEventAt ) return null ;
208
- return parseGraphData ( fetchGraphDataMutation ?. data , avgEventCount , startTime , endTime , interval ) ;
199
+ return parseGraphData ( fetchGraphDataMutation ?. data , avgEventCount , interval ) ;
209
200
} , [ fetchGraphDataMutation ?. data , interval , firstEventAt ] ) ;
210
201
const hasData = Array . isArray ( graphData ) && graphData . length !== 0 ;
211
202
const [ , setAppStore ] = useAppStore ( ( _store ) => null ) ;
0 commit comments