-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref(distributions): Refactor fetching flags and their suspect scores #89962
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
static/app/views/issueDetails/groupFeatureFlags/useGroupFlagDrawerData.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import {useMemo} from 'react'; | ||
|
|
||
| import {OrderBy, SortBy} from 'sentry/components/events/featureFlags/utils'; | ||
| import type {Group} from 'sentry/types/group'; | ||
| import useGroupFeatureFlags from 'sentry/views/issueDetails/groupFeatureFlags/useGroupFeatureFlags'; | ||
| import {useGroupSuspectFlagScores} from 'sentry/views/issueDetails/groupFeatureFlags/useGroupSuspectFlagScores'; | ||
| import type {GroupTag} from 'sentry/views/issueDetails/groupTags/useGroupTags'; | ||
|
|
||
| interface SuspectGroupTag extends GroupTag { | ||
| suspect: { | ||
| baselinePercent: undefined | number; | ||
| score: undefined | number; | ||
| }; | ||
| } | ||
|
|
||
| interface Props { | ||
| environments: string[]; | ||
| group: Group; | ||
| orderBy: OrderBy; | ||
| search: string; | ||
| sortBy: SortBy; | ||
| } | ||
|
|
||
| interface Response { | ||
| allGroupFlagCount: number; | ||
| displayFlags: SuspectGroupTag[]; | ||
| isError: boolean; | ||
| isPending: boolean; | ||
| refetch: () => void; | ||
| } | ||
|
|
||
| export default function useGroupFlagDrawerData({ | ||
| environments, | ||
| group, | ||
| orderBy, | ||
| search, | ||
| sortBy, | ||
| }: Props): Response { | ||
| const isSuspectEnabled = sortBy === SortBy.SUSPICION; | ||
|
|
||
| // Fetch the base flag data | ||
| const { | ||
| data: groupFlags = [], | ||
| isError: isFlagsError, | ||
| isPending: isFlagsPending, | ||
| refetch: refetchFlags, | ||
| } = useGroupFeatureFlags({ | ||
| groupId: group.id, | ||
| environment: environments, | ||
| }); | ||
|
|
||
| // Fetch the suspect data, if we need it for this render | ||
| const { | ||
| data: suspectScores, | ||
| isError: isSuspectError, | ||
| isPending: isSuspectPending, | ||
| refetch: refetchScores, | ||
| } = useGroupSuspectFlagScores({ | ||
| groupId: group.id, | ||
| environment: environments.length ? environments : undefined, | ||
| enabled: isSuspectEnabled, | ||
| }); | ||
|
|
||
| // Combine the flag and suspect data into SuspectGroupTag objects | ||
| const allFlagsWithScores = useMemo(() => { | ||
| const suspectScoresMap = suspectScores | ||
| ? Object.fromEntries(suspectScores.data.map(score => [score.flag, score])) | ||
| : {}; | ||
|
|
||
| return groupFlags.map<SuspectGroupTag>(flag => ({ | ||
| ...flag, | ||
| suspect: { | ||
| baselinePercent: suspectScoresMap[flag.key]?.baseline_percent, | ||
| score: suspectScoresMap[flag.key]?.score, | ||
| }, | ||
| })); | ||
| }, [groupFlags, suspectScores]); | ||
|
|
||
| // Flatten all the tag values together into a big string. | ||
| // A perf improvement: here we iterate over all tags&values once, (N*M) then | ||
| // later only iterate through each tag (N) as the search term changes. | ||
| const tagValues = useMemo( | ||
| () => | ||
| groupFlags.reduce<Record<string, string>>((valueMap, flag) => { | ||
| valueMap[flag.key] = flag.topValues | ||
| .map(tv => tv.value) | ||
| .join(' ') | ||
| .toLowerCase(); | ||
| return valueMap; | ||
| }, {}), | ||
| [groupFlags] | ||
| ); | ||
|
|
||
| const filteredFlags = useMemo(() => { | ||
| const searchLower = search.toLowerCase(); | ||
| return allFlagsWithScores.filter(flag => { | ||
| return ( | ||
| flag.name.includes(searchLower) || | ||
| flag.key.includes(searchLower) || | ||
| tagValues[flag.key]?.includes(searchLower) | ||
| ); | ||
| }); | ||
| }, [allFlagsWithScores, search, tagValues]); | ||
|
|
||
| const displayFlags = useMemo(() => { | ||
| if (sortBy === SortBy.ALPHABETICAL) { | ||
| const sorted = filteredFlags.toSorted((a, b) => a.key.localeCompare(b.key)); | ||
| return orderBy === OrderBy.A_TO_Z ? sorted : sorted.reverse(); | ||
| } | ||
| if (sortBy === SortBy.SUSPICION) { | ||
| return filteredFlags.toSorted( | ||
| (a, b) => (b.suspect.score ?? 0) - (a.suspect.score ?? 0) | ||
| ); | ||
| } | ||
| return filteredFlags; | ||
| }, [filteredFlags, orderBy, sortBy]); | ||
|
|
||
| return { | ||
| allGroupFlagCount: allFlagsWithScores.length, | ||
| displayFlags, | ||
| isError: isSuspectEnabled ? isFlagsError || isSuspectError : isFlagsError, | ||
| isPending: isSuspectEnabled ? isFlagsPending || isSuspectPending : isFlagsPending, | ||
| refetch: () => { | ||
| refetchFlags(); | ||
| refetchScores(); | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.