-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref(insights): Replace usage of <ReleaseSeries>
in <InsightsTimeSeriesWidget>
#86300
Changes from 1 commit
a28ede2
ca8cc8e
aa324ac
8f4edca
60d4d0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import {useEffect, useRef} from 'react'; | ||
|
||
import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse'; | ||
import type {PageFilters} from 'sentry/types/core'; | ||
import {useInfiniteApiQuery} from 'sentry/utils/queryClient'; | ||
import useOrganization from 'sentry/utils/useOrganization'; | ||
|
||
|
@@ -8,11 +9,28 @@ interface ReleaseMetaBasic { | |
version: string; | ||
} | ||
|
||
interface UseReleaseStatsParams { | ||
datetime: Parameters<typeof normalizeDateTimeParams>[0]; | ||
environments: readonly string[]; | ||
projects: readonly number[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to change this from using |
||
|
||
/** | ||
* Max number of pages to fetch. Default is 10 pages, which should be | ||
* sufficient to fetch "all" releases. | ||
*/ | ||
maxPages?: number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also decided to add this so we can control the # of pages to fetch and to limit the damage if something were to go wrong or if an org has more releases than we expect. For reference, Sentry fetches ~3 pages for a 90d window. |
||
} | ||
|
||
/** | ||
* Fetches *ALL* releases (e.g. all pages worth) | ||
* This is intended to fetch "all" releases, we have a default limit of | ||
* 10 pages (of 1000 results) to be slightly cautious. | ||
*/ | ||
export function useReleaseStats({datetime, environments, projects}: PageFilters) { | ||
export function useReleaseStats( | ||
{datetime, environments, projects, maxPages = 10}: UseReleaseStatsParams, | ||
queryOptions: {staleTime: number} = {staleTime: Infinity} | ||
) { | ||
const organization = useOrganization(); | ||
const currentNumberPages = useRef(0); | ||
|
||
const { | ||
isLoading, | ||
|
@@ -33,24 +51,26 @@ export function useReleaseStats({datetime, environments, projects}: PageFilters) | |
...normalizeDateTimeParams(datetime), | ||
}, | ||
}, | ||
// This is here to prevent a cache key conflict between normal queries and | ||
// "infinite" queries. Read more here: https://tkdodo.eu/blog/effective-react-query-keys#caching-data | ||
'load-all', | ||
], | ||
staleTime: Infinity, | ||
...queryOptions, | ||
enabled: currentNumberPages.current < maxPages, | ||
}); | ||
|
||
if (!isFetching && hasNextPage) { | ||
fetchNextPage(); | ||
} | ||
|
||
const releases = | ||
data?.pages.flatMap(([pageData]) => | ||
pageData.map(({date, version}) => ({timestamp: date, version})) | ||
) ?? []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this mapping to where it's needed |
||
useEffect(() => { | ||
if (!isFetching && hasNextPage && currentNumberPages.current + 1 < maxPages) { | ||
fetchNextPage(); | ||
currentNumberPages.current++; | ||
} | ||
}, [isFetching, hasNextPage, fetchNextPage, maxPages]); | ||
|
||
return { | ||
isLoading, | ||
isPending, | ||
isError, | ||
error, | ||
releases, | ||
releases: data?.pages.flatMap(([pageData]) => pageData), | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
ca8cc8e
(#86300)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the better fix would be to have
useInfiniteApiQuery
always append an additional’infinite’
key. This would avoid future conflicts if the same endpoint is used as query and infinite query. I think there’s only one other usage ofuseInfiniteApiQuery
so it shouldn’t be a huge impact.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, though it gets kind of messy because
queryKey
can be 1 or 2 elementsI suppose we can check the queryKey length and do something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that’s not too bad. What it would then take is to make an
InfiniteApiQueryKey
type that we would use infetchInfiniteQuery
.useInfiniteApiQuery
would still accept anApiQueryKey
, and we would do the transformation internally.I split up the types a bit to get there:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm, will do this in a followup