Skip to content

Commit

Permalink
fix(dynamic-sampling): Loop over paginated results
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurKnaus committed Nov 6, 2024
1 parent 3e360f4 commit 9119044
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {Client} from 'sentry/api';
import type {Organization} from 'sentry/types/organization';
import parseLinkHeader from 'sentry/utils/parseLinkHeader';
import {
type ApiQueryKey,
setApiQueryData,
useApiQuery,
useMutation,
useQuery,
useQueryClient,
} from 'sentry/utils/queryClient';
import type RequestError from 'sentry/utils/requestError/requestError';
Expand All @@ -20,12 +22,50 @@ function getEndpoint(organization: Organization) {
}

function getQueryKey(organization: Organization): ApiQueryKey {
return [getEndpoint(organization), {query: {perPage: 10000}}];
return [getEndpoint(organization)];
}

/**
* Fetches all sampling rates for the organization by looping through
* the paginated results.
*/
const fetchAllSamplingRates = async (
api: Client,
organization: Organization
): Promise<SamplingProjectRate[]> => {
const endpoint = getEndpoint(organization);
let cursor: string | null = '';
let result: SamplingProjectRate[] = [];

while (cursor !== null) {
const [data, _, response] = await api.requestPromise(endpoint, {
method: 'GET',
includeAllArgs: true,
query: {cursor},
});

result = result.concat(data);

cursor = null;
const linkHeader = response?.getResponseHeader('Link');

if (linkHeader) {
const links = parseLinkHeader(linkHeader);
cursor = (links.next.results && links.next.cursor) || null;
}
}

return result;
};

export function useGetSamplingProjectRates() {
const api = useApi();
const organization = useOrganization();
return useApiQuery<SamplingProjectRate[]>(getQueryKey(organization), {staleTime: 0});
return useQuery<SamplingProjectRate[]>({
queryKey: getQueryKey(organization),
queryFn: () => fetchAllSamplingRates(api, organization),
staleTime: 0,
});
}

export function useUpdateSamplingProjectRates() {
Expand Down

0 comments on commit 9119044

Please sign in to comment.