|
1 | 1 | import {useCallback, useEffect, useMemo, useState} from 'react'; |
2 | 2 | import {logger} from '@sentry/react'; |
| 3 | +import type {QueryClient} from '@tanstack/react-query'; |
3 | 4 |
|
4 | 5 | import {type ApiResult} from 'sentry/api'; |
5 | 6 | import {usePageFilters} from 'sentry/components/pageFilters/usePageFilters'; |
@@ -28,8 +29,10 @@ import {SAMPLING_MODE} from 'sentry/views/explore/hooks/useProgressiveQuery'; |
28 | 29 | import {useTraceItemDetails} from 'sentry/views/explore/hooks/useTraceItemDetails'; |
29 | 30 | import { |
30 | 31 | AlwaysPresentLogFields, |
| 32 | + LOCAL_LOG_ROWS_FOR_EXPANDED_INFINITE_PAGES, |
31 | 33 | MAX_LOG_INGEST_DELAY, |
32 | 34 | MAX_LOGS_INFINITE_QUERY_PAGES, |
| 35 | + MAX_LOGS_INFINITE_QUERY_PAGES_EXPANDED, |
33 | 36 | QUERY_PAGE_LIMIT, |
34 | 37 | QUERY_PAGE_LIMIT_WITH_AUTO_REFRESH, |
35 | 38 | } from 'sentry/views/explore/logs/constants'; |
@@ -402,6 +405,19 @@ type QueryKey = [ |
402 | 405 | 'infinite', |
403 | 406 | ]; |
404 | 407 |
|
| 408 | +/** |
| 409 | + * `maxPages` is evaluated before `useInfiniteQuery` returns `data`, so we base it on the |
| 410 | + * query cache (same snapshot React Query will use for this key). |
| 411 | + */ |
| 412 | +function maxPagesForLogsInfiniteQuery(client: QueryClient, queryKey: QueryKey): number { |
| 413 | + const cached = client.getQueryData<InfiniteData<ApiResult<EventsLogsResult>>>(queryKey); |
| 414 | + const rows = |
| 415 | + cached?.pages?.reduce((n, page) => n + (page[0]?.data?.length ?? 0), 0) ?? 0; |
| 416 | + return rows < LOCAL_LOG_ROWS_FOR_EXPANDED_INFINITE_PAGES |
| 417 | + ? MAX_LOGS_INFINITE_QUERY_PAGES |
| 418 | + : MAX_LOGS_INFINITE_QUERY_PAGES_EXPANDED; |
| 419 | +} |
| 420 | + |
405 | 421 | export function useInfiniteLogsQuery({ |
406 | 422 | disabled, |
407 | 423 | highFidelity, |
@@ -526,7 +542,7 @@ export function useInfiniteLogsQuery({ |
526 | 542 | initialPageParam, |
527 | 543 | enabled: !disabled, |
528 | 544 | staleTime: autoRefresh ? Infinity : getStaleTimeForEventView(other.eventView), |
529 | | - maxPages: MAX_LOGS_INFINITE_QUERY_PAGES, |
| 545 | + maxPages: maxPagesForLogsInfiniteQuery(queryClient, queryKeyWithInfinite), |
530 | 546 | refetchIntervalInBackground: true, // Don't refetch when tab is not visible |
531 | 547 | }); |
532 | 548 |
|
|
0 commit comments