@@ -118,39 +118,29 @@ export const createRumToolHandlers = (
118118 const sessions = new Map < string , Set < string > > ( )
119119
120120 for ( const event of response . data ) {
121- if ( event . attributes ?. attributes ) {
122- // Get the group value (default to 'unknown' if not found)
123- let groupValue = 'unknown'
121+ if ( ! event . attributes ?. attributes ) {
122+ continue
123+ }
124124
125- // Parse the groupBy path (e.g., 'application.id')
126- let current = event . attributes . attributes
127- const groupPath = groupBy . split ( '.' ) as Array <
128- keyof typeof event . attributes . attributes
129- >
130- let foundPath = true
131-
132- for ( const pathPart of groupPath ) {
133- if ( current [ pathPart ] !== undefined ) {
134- current = current [ pathPart ] as v2 . RUMEventAttributes
135- } else {
136- foundPath = false
137- break
138- }
139- }
125+ // Parse the groupBy path (e.g., 'application.id')
126+ const groupPath = groupBy . split ( '.' ) as Array <
127+ keyof typeof event . attributes . attributes
128+ >
140129
141- if ( foundPath ) {
142- groupValue = String ( current )
143- }
130+ const result = getValueByPath (
131+ event . attributes . attributes ,
132+ groupPath . map ( ( path ) => String ( path ) ) ,
133+ )
134+ const groupValue = result . found ? String ( result . value ) : 'unknown'
144135
145- // Get or create the session set for this group
146- if ( ! sessions . has ( groupValue ) ) {
147- sessions . set ( groupValue , new Set < string > ( ) )
148- }
136+ // Get or create the session set for this group
137+ if ( ! sessions . has ( groupValue ) ) {
138+ sessions . set ( groupValue , new Set < string > ( ) )
139+ }
149140
150- // Add the session ID to the set if it exists
151- if ( event . attributes . attributes . session ?. id ) {
152- sessions . get ( groupValue ) ?. add ( event . attributes . attributes . session . id )
153- }
141+ // Add the session ID to the set if it exists
142+ if ( event . attributes . attributes . session ?. id ) {
143+ sessions . get ( groupValue ) ?. add ( event . attributes . attributes . session . id )
154144 }
155145 }
156146
@@ -189,34 +179,38 @@ export const createRumToolHandlers = (
189179 }
190180
191181 // Extract and calculate performance metrics
192- const metrics : Record < string , number [ ] > = { }
193- metricNames . forEach ( ( name ) => {
194- metrics [ name ] = [ ]
195- } )
182+ const metrics : Record < string , number [ ] > = metricNames . reduce (
183+ ( acc , name ) => {
184+ acc [ name ] = [ ]
185+ return acc
186+ } ,
187+ { } as Record < string , number [ ] > ,
188+ )
196189
197190 for ( const event of response . data ) {
198- if ( event . attributes ?. attributes ) {
199- // Collect each requested metric if it exists
200- for ( const metricName of metricNames ) {
201- // Handle nested properties like 'view.load_time'
202- const parts = metricName . split ( '.' ) as Array <
203- keyof typeof event . attributes . attributes
204- >
205- let value = event . attributes . attributes
206-
207- for ( let i = 0 ; i < parts . length ; i ++ ) {
208- if ( value && value [ parts [ i ] ] !== undefined ) {
209- value = value [ parts [ i ] ]
210- } else {
211- value = { }
212- break
213- }
214- }
191+ if ( ! event . attributes ?. attributes ) {
192+ continue
193+ }
215194
216- // If we found a numeric value, add it to the metrics
217- if ( typeof value === 'number' ) {
218- metrics [ metricName ] . push ( value )
219- }
195+ // Collect each requested metric if it exists
196+ for ( const metricName of metricNames ) {
197+ // Handle nested properties like 'view.load_time'
198+ const metricNameParts = metricName . split ( '.' ) as Array <
199+ keyof typeof event . attributes . attributes
200+ >
201+
202+ if ( event . attributes . attributes == null ) {
203+ continue
204+ }
205+
206+ const value = metricNameParts . reduce (
207+ ( acc , part ) => ( acc ? acc [ part ] : undefined ) ,
208+ event . attributes . attributes ,
209+ )
210+
211+ // If we found a numeric value, add it to the metrics
212+ if ( typeof value === 'number' ) {
213+ metrics [ metricName ] . push ( value )
220214 }
221215 }
222216 }
@@ -225,21 +219,26 @@ export const createRumToolHandlers = (
225219 const results : Record <
226220 string ,
227221 { avg : number ; min : number ; max : number ; count : number }
228- > = { }
229-
230- for ( const [ name , values ] of Object . entries ( metrics ) ) {
231- if ( values . length > 0 ) {
232- const sum = values . reduce ( ( a , b ) => a + b , 0 )
233- results [ name ] = {
234- avg : sum / values . length ,
235- min : Math . min ( ...values ) ,
236- max : Math . max ( ...values ) ,
237- count : values . length ,
222+ > = Object . entries ( metrics ) . reduce (
223+ ( acc , [ name , values ] ) => {
224+ if ( values . length > 0 ) {
225+ const sum = values . reduce ( ( a , b ) => a + b , 0 )
226+ acc [ name ] = {
227+ avg : sum / values . length ,
228+ min : Math . min ( ...values ) ,
229+ max : Math . max ( ...values ) ,
230+ count : values . length ,
231+ }
232+ } else {
233+ acc [ name ] = { avg : 0 , min : 0 , max : 0 , count : 0 }
238234 }
239- } else {
240- results [ name ] = { avg : 0 , min : 0 , max : 0 , count : 0 }
241- }
242- }
235+ return acc
236+ } ,
237+ { } as Record <
238+ string ,
239+ { avg : number ; min : number ; max : number ; count : number }
240+ > ,
241+ )
243242
244243 return {
245244 content : [
@@ -276,3 +275,27 @@ export const createRumToolHandlers = (
276275 }
277276 } ,
278277} )
278+
279+ // Get the group value using a recursive function approach
280+ const getValueByPath = (
281+ obj : Record < string , unknown > ,
282+ path : string [ ] ,
283+ index = 0 ,
284+ ) : { value : unknown ; found : boolean } => {
285+ if ( index >= path . length ) {
286+ return { value : obj , found : true }
287+ }
288+
289+ const key = path [ index ]
290+ const typedObj = obj as Record < string , unknown >
291+
292+ if ( typedObj [ key ] === undefined ) {
293+ return { value : null , found : false }
294+ }
295+
296+ return getValueByPath (
297+ typedObj [ key ] as Record < string , unknown > ,
298+ path ,
299+ index + 1 ,
300+ )
301+ }
0 commit comments