1111import { useMemo } from 'react'
1212import { useRouter } from 'next/router'
1313
14- import { useDatabaseSelectorStateSnapshot } from 'state/database-selector'
1514import type { AnalyticsInterval , DataPoint } from 'data/analytics/constants'
1615import { useAuthLogsReport } from 'data/reports/auth-report-query'
1716import type { ChartData } from 'components/ui/Charts/Charts.types'
1817import type { MultiAttribute } from 'components/ui/Charts/ComposedChart.utils'
19- import { useAttributeQueries } from 'components/ui/Charts/LogChartHandler'
2018import { useEdgeFunctionReport } from 'data/reports/edgefn-query'
21- import { useInfraMonitoringQuery } from 'data/analytics/infra-monitoring-query'
22- import type { InfraMonitoringAttribute } from 'data/analytics/infra-monitoring-query'
2319
2420export const useChartData = ( {
2521 attributes,
@@ -42,10 +38,8 @@ export const useChartData = ({
4238} ) => {
4339 const router = useRouter ( )
4440 const { ref } = router . query
45- const state = useDatabaseSelectorStateSnapshot ( )
4641
4742 const logsAttributes = attributes . filter ( ( attr ) => attr . provider === 'logs' )
48- const nonLogsAttributes = attributes . filter ( ( attr ) => attr . provider !== 'logs' )
4943
5044 const isEdgeFunctionRoute = router . asPath . includes ( '/reports/edge-functions' )
5145
@@ -76,66 +70,15 @@ export const useChartData = ({
7670 functionIds,
7771 } )
7872
79- const {
80- data : infraData ,
81- error : infraError ,
82- isLoading : isInfraLoading ,
83- } = useInfraMonitoringQuery (
84- {
85- projectRef : ref as string ,
86- attribute : nonLogsAttributes [ 0 ] ?. attribute as InfraMonitoringAttribute ,
87- interval : interval as AnalyticsInterval ,
88- startDate,
89- endDate,
90- databaseIdentifier : state . selectedDatabaseId ,
91- } ,
92- { enabled : enabled && nonLogsAttributes . length > 0 }
93- )
94-
9573 const logsData = isEdgeFunctionRoute ? edgeFunctionData : authData
9674 const logsChartAttributes = isEdgeFunctionRoute
9775 ? edgeFunctionChartAttributes
9876 : authChartAttributes
9977 const isLogsLoading = isEdgeFunctionRoute ? isEdgeFunctionLoading : isAuthLoading
10078
101- const chartAttributes = useMemo (
102- ( ) => nonLogsAttributes . concat ( logsChartAttributes || [ ] ) ,
103- [ nonLogsAttributes , logsChartAttributes ]
104- )
105-
106- const databaseIdentifier = state . selectedDatabaseId
107-
108- // Use the custom hook at the top level of the component
109- const attributeQueries = useAttributeQueries (
110- attributes ,
111- ref ,
112- startDate ,
113- endDate ,
114- interval as AnalyticsInterval ,
115- databaseIdentifier ,
116- data ,
117- true
118- )
119-
120- // Combine all the data into a single dataset
12179 const combinedData = useMemo ( ( ) => {
12280 if ( data ) return data
12381
124- const regularAttributeQueries = attributeQueries . filter (
125- ( q ) => q . data ?. provider !== 'logs' && q . data ?. provider !== 'reference-line'
126- )
127- const isLoading =
128- ( logsAttributes . length > 0 && isLogsLoading ) ||
129- regularAttributeQueries . some ( ( query : any ) => query . isLoading )
130- if ( isLoading ) {
131- return undefined
132- }
133-
134- const hasError = regularAttributeQueries . some ( ( query : any ) => ! query . data )
135- if ( hasError ) {
136- return undefined
137- }
138-
13982 // Get all unique timestamps from all datasets
14083 const timestamps = new Set < string > ( )
14184 if ( logsData ) {
@@ -145,17 +88,6 @@ export const useChartData = ({
14588 }
14689 } )
14790 }
148- regularAttributeQueries . forEach ( ( query : any ) => {
149- query . data ?. data ?. forEach ( ( point : any ) => {
150- if ( point ?. period_start ) {
151- timestamps . add ( point . period_start )
152- }
153- } )
154- } )
155-
156- const referenceLineQueries = attributeQueries . filter (
157- ( q ) => q . data ?. provider === 'reference-line'
158- )
15991
16092 // Combine data points for each timestamp
16193 const combined = Array . from ( timestamps )
@@ -166,44 +98,20 @@ export const useChartData = ({
16698 const logPoint = logsData ?. find ( ( p : any ) => p . period_start === timestamp ) || { }
16799 Object . assign ( point , logPoint )
168100
169- chartAttributes . forEach ( ( attr ) => {
170- if ( ! attr ) return
171- if ( attr . provider === 'logs' ) return
172- if ( attr . provider === 'reference-line' ) return
173- if ( attr . customValue !== undefined ) {
174- point [ attr . attribute ] = attr . customValue
175- return
176- }
177-
178- const query = regularAttributeQueries . find ( ( q ) => q . data ?. attribute === attr . attribute )
179- const matchingPoint = query ?. data ?. data ?. find ( ( p : any ) => p . period_start === timestamp )
180- point [ attr . attribute ] = matchingPoint ?. [ attr . attribute ] ?? 0
181- } )
182-
183- // Add reference line values for each timestamp
184- referenceLineQueries . forEach ( ( query : any ) => {
185- const attr = query . data . attribute
186- const value = query . data . total
187- point [ attr ] = value
188- } )
189-
190101 return point as DataPoint
191102 } )
192103
193104 return combined as DataPoint [ ]
194- } , [ data , attributeQueries , attributes , chartAttributes , isLogsLoading , logsData , logsAttributes ] )
105+ } , [ data , attributes , isLogsLoading , logsData , logsAttributes ] )
195106
196- const loading =
197- ( logsAttributes . length > 0 && isLogsLoading ) ||
198- attributeQueries . some ( ( query : any ) => query . isLoading )
107+ const loading = logsAttributes . length > 0 && isLogsLoading
199108
200109 // Calculate highlighted value based on the first attribute's data
201110 const _highlightedValue = useMemo ( ( ) => {
202111 if ( highlightedValue !== undefined ) return highlightedValue
203112
204113 const firstAttr = attributes [ 0 ]
205- const firstQuery = attributeQueries [ 0 ]
206- const firstData = firstQuery ?. data
114+ const firstData = logsChartAttributes . find ( ( p : any ) => p . attribute === firstAttr . attribute )
207115
208116 if ( ! firstData ) return undefined
209117
@@ -222,12 +130,11 @@ export const useChartData = ({
222130 : shouldHighlightTotalGroupedValue
223131 ? firstData . totalGrouped ?. [ firstAttr . attribute as keyof typeof firstData . totalGrouped ]
224132 : ( firstData . data ?. [ firstData . data ?. length - 1 ] as any ) ?. [ firstAttr . attribute ]
225- } , [ highlightedValue , attributes , attributeQueries ] )
133+ } , [ highlightedValue , attributes ] )
226134
227135 return {
228136 data : combinedData ,
229137 isLoading : loading ,
230- chartAttributes,
231138 highlightedValue : _highlightedValue ,
232139 }
233140}
0 commit comments