1- import { useInfiniteQuery } from '@tanstack/react-query'
21import {
32 ColumnFiltersState ,
43 getCoreRowModel ,
@@ -30,6 +29,9 @@ import { LiveRow } from 'components/ui/DataTable/LiveRow'
3029import { DataTableProvider } from 'components/ui/DataTable/providers/DataTableProvider'
3130import { RefreshButton } from 'components/ui/DataTable/RefreshButton'
3231import { TimelineChart } from 'components/ui/DataTable/TimelineChart'
32+ import { useUnifiedLogsChartQuery } from 'data/logs/unified-logs-chart-query'
33+ import { useUnifiedLogsCountQuery } from 'data/logs/unified-logs-count-query'
34+ import { useUnifiedLogsInfiniteQuery } from 'data/logs/unified-logs-infinite-query'
3335import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
3436import {
3537 ChartConfig ,
@@ -46,10 +48,10 @@ import {
4648import { COLUMNS } from './components/Columns'
4749import { MemoizedDataTableSheetContent } from './components/DataTableSheetContent'
4850import { FunctionLogsTab } from './components/FunctionLogsTab'
49- import { dataOptions , useChartData } from './QueryOptions'
5051import { CHART_CONFIG , SEARCH_PARAMS_PARSER } from './UnifiedLogs.constants'
5152import { filterFields as defaultFilterFields , sheetFields } from './UnifiedLogs.fields'
5253import { useLiveMode , useResetFocus } from './UnifiedLogs.hooks'
54+ import { QuerySearchParamsType } from './UnifiedLogs.types'
5355import { getFacetedUniqueValues , getLevelRowClassName , logEventBus } from './UnifiedLogs.utils'
5456
5557// Debug mode flag - set to true to enable detailed logs
@@ -84,29 +86,47 @@ export const UnifiedLogs = () => {
8486 [ ]
8587 )
8688
87- // [Joshen] This needs to move to the data folder to follow our proper RQ structure
88- const { data, isFetching, isLoading, fetchNextPage, hasNextPage, fetchPreviousPage, refetch } =
89- // @ts -ignore
90- useInfiniteQuery ( dataOptions ( search , projectRef ?? '' ) )
89+ // Create a stable query key object by removing nulls/undefined, uuid, and live
90+ // Mainly to prevent the react queries from unnecessarily re-fetching
91+ const searchParameters = Object . entries ( search ) . reduce (
92+ ( acc , [ key , value ] ) => {
93+ if ( ! [ 'uuid' , 'live' ] . includes ( key ) && value !== null && value !== undefined ) {
94+ acc [ key ] = value
95+ }
96+ return acc
97+ } ,
98+ { } as Record < string , any >
99+ ) as QuerySearchParamsType
100+
101+ const {
102+ data : unifiedLogsData ,
103+ isLoading,
104+ isFetching,
105+ hasNextPage,
106+ refetch,
107+ fetchNextPage,
108+ fetchPreviousPage,
109+ } = useUnifiedLogsInfiniteQuery ( { projectRef, search : searchParameters } )
110+ const { data : counts } = useUnifiedLogsCountQuery ( { projectRef, search : searchParameters } )
111+ const { data : unifiedLogsChart = [ ] } = useUnifiedLogsChartQuery ( {
112+ projectRef,
113+ search : searchParameters ,
114+ } )
91115
92116 const flatData = useMemo ( ( ) => {
93- return data ?. pages ?. flatMap ( ( page ) => page . data ?? [ ] ) ?? [ ]
94- } , [ data ?. pages ] )
117+ return unifiedLogsData ?. pages ?. flatMap ( ( page ) => page . data ?? [ ] ) ?? [ ]
118+ } , [ unifiedLogsData ?. pages ] )
95119 const liveMode = useLiveMode ( flatData )
96120
97- // Add the chart data query for the entire time period
98- const { data : chartDataResult } = useChartData ( search , projectRef ?? '' )
99-
100121 // REMINDER: meta data is always the same for all pages as filters do not change(!)
101- const lastPage = data ?. pages ?. [ data ?. pages . length - 1 ]
122+ const lastPage = unifiedLogsData ?. pages ?. [ unifiedLogsData ?. pages . length - 1 ]
123+
102124 // Use the totalCount from chartDataResult which gives us the actual count of logs in the time period
103125 // instead of the hardcoded 10000 value
104- const totalDBRowCount = chartDataResult ?. totalCount || lastPage ?. meta ?. totalRowCount
126+ const totalDBRowCount = counts ?. totalRowCount
105127 const filterDBRowCount = lastPage ?. meta ?. filterRowCount
106- const metadata = lastPage ?. meta ?. metadata
107- // Use chart data from the separate query if available, fallback to the default
108- const chartData = chartDataResult ?. chartData || lastPage ?. meta ?. chartData
109- const facets = lastPage ?. meta ?. facets
128+
129+ const facets = counts ?. facets
110130 const totalFetched = flatData ?. length
111131
112132 // Create a filtered version of the chart config based on selected levels
@@ -152,18 +172,13 @@ export const UnifiedLogs = () => {
152172 getFacetedRowModel : getFacetedRowModel ( ) ,
153173 getFacetedUniqueValues : getTTableFacetedUniqueValues ( ) ,
154174 getFacetedMinMaxValues : getTTableFacetedMinMaxValues ( ) ,
155- // Here, manually override the filter function for the level column
156- // to prevent client-side filtering since it's already filtered on the server
157- // Always return true to pass all level values
158- // columnFilterFns: { level: () => true },
159- // debugAll: process.env.NEXT_PUBLIC_TABLE_DEBUG === 'true',
160175 } )
161176
162177 const selectedRow = useMemo ( ( ) => {
163178 if ( ( isLoading || isFetching ) && ! flatData . length ) return
164179 const selectedRowKey = Object . keys ( rowSelection ) ?. [ 0 ]
165180 return table . getCoreRowModel ( ) . flatRows . find ( ( row ) => row . id === selectedRowKey )
166- } , [ rowSelection , table , isLoading , isFetching , flatData ] )
181+ } , [ rowSelection , flatData ] )
167182
168183 const selectedRowKey = Object . keys ( rowSelection ) ?. [ 0 ]
169184
@@ -247,7 +262,6 @@ export const UnifiedLogs = () => {
247262 // eslint-disable-next-line react-hooks/exhaustive-deps
248263 } , [ sorting ] )
249264
250- // TODO: can only share uuid within the first batch
251265 useEffect ( ( ) => {
252266 if ( isLoading || isFetching ) return
253267 if ( Object . keys ( rowSelection ) ?. length && ! selectedRow ) {
@@ -283,7 +297,6 @@ export const UnifiedLogs = () => {
283297 enableColumnOrdering = { true }
284298 isLoading = { isFetching || isLoading }
285299 getFacetedUniqueValues = { getFacetedUniqueValues ( facets ) }
286- // getFacetedMinMaxValues={getFacetedMinMaxValues(facets)}
287300 >
288301 < DataTableSideBarLayout topBarHeight = { topBarHeight } >
289302 < FilterSideBar />
@@ -303,7 +316,7 @@ export const UnifiedLogs = () => {
303316 ] }
304317 />
305318 < TimelineChart
306- data = { chartData ?? [ ] }
319+ data = { unifiedLogsChart }
307320 className = "-mb-2"
308321 columnId = "timestamp"
309322 chartConfig = { filteredChartConfig }
@@ -394,8 +407,7 @@ export const UnifiedLogs = () => {
394407 totalRows : totalDBRowCount ?? 0 ,
395408 filterRows : filterDBRowCount ?? 0 ,
396409 totalRowsFetched : totalFetched ?? 0 ,
397- currentPercentiles : metadata ?. currentPercentiles ?? ( { } as any ) ,
398- ...metadata ,
410+ currentPercentiles : { } as any ,
399411 } }
400412 />
401413 </ TabsContent >
0 commit comments