-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(explore): Add hooks for date page filter props based on data cat… #103822
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import {useMemo, type ReactNode} from 'react'; | ||
|
|
||
| import type {SelectOptionWithKey} from 'sentry/components/core/compactSelect/types'; | ||
| import type {DatePageFilterProps} from 'sentry/components/organizations/datePageFilter'; | ||
| import {t} from 'sentry/locale'; | ||
| import {isEmptyObject} from 'sentry/utils/object/isEmptyObject'; | ||
| import type {MaxPickableDaysOptions} from 'sentry/utils/useMaxPickableDays'; | ||
|
|
||
| interface UseDatePageFilterPropsProps extends MaxPickableDaysOptions {} | ||
|
|
||
| export function useDatePageFilterProps({ | ||
| defaultPeriod, | ||
| maxPickableDays, | ||
| maxUpgradableDays, | ||
| upsellFooter, | ||
| }: UseDatePageFilterPropsProps): DatePageFilterProps { | ||
| return useMemo(() => { | ||
| // ensure the available relative options are always sorted | ||
| const availableRelativeOptions: Array<[number, string, ReactNode]> = [ | ||
| [1 / 24, '1h', t('Last hour')], | ||
| [1, '24h', t('Last 24 hours')], | ||
| [7, '7d', t('Last 7 days')], | ||
| [14, '14d', t('Last 14 days')], | ||
| [30, '30d', t('Last 30 days')], | ||
| [90, '90d', t('Last 90 days')], | ||
| ]; | ||
|
|
||
| // find the relative options that should be enabled based on the maxPickableDays | ||
| const pickableIndex = | ||
| availableRelativeOptions.findLastIndex(([days]) => days <= maxPickableDays) + 1; | ||
| const enabledOptions = Object.fromEntries( | ||
| availableRelativeOptions | ||
| .slice(0, pickableIndex) | ||
| .map(([_, period, label]) => [period, label]) | ||
| ); | ||
|
|
||
| // find the relative options that should be disabled based on the maxUpgradableDays | ||
| const upgradableIndex = | ||
| availableRelativeOptions.findLastIndex(([days]) => days <= maxUpgradableDays) + 1; | ||
| const disabledOptions = Object.fromEntries( | ||
| availableRelativeOptions | ||
| .slice(pickableIndex, upgradableIndex) | ||
| .map(([_, period, label]) => [period, label]) | ||
| ); | ||
|
|
||
| const isOptionDisabled = (option: SelectOptionWithKey<string>): boolean => { | ||
| return disabledOptions.hasOwnProperty(option.value); | ||
| }; | ||
|
|
||
| const menuFooter = isEmptyObject(disabledOptions) ? null : (upsellFooter ?? null); | ||
|
|
||
| return { | ||
| defaultPeriod, | ||
| isOptionDisabled, | ||
| maxPickableDays, | ||
| menuFooter, | ||
| relativeOptions: ({arbitraryOptions}) => ({ | ||
| ...arbitraryOptions, | ||
| ...enabledOptions, | ||
| ...disabledOptions, | ||
| }), | ||
| }; | ||
| }, [defaultPeriod, maxPickableDays, maxUpgradableDays, upsellFooter]); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import {OrganizationFixture} from 'sentry-fixture/organization'; | ||
|
|
||
| import {renderHook} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| import {DataCategory} from 'sentry/types/core'; | ||
|
|
||
| import {useMaxPickableDays} from './useMaxPickableDays'; | ||
|
|
||
| describe('useMaxPickableDays', () => { | ||
| it('returns 30/90 for spans without flag', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.SPANS], | ||
| organization: OrganizationFixture({features: []}), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: expect.any(Object), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 90/90 for spans with flag', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.SPANS], | ||
| organization: OrganizationFixture({features: ['visibility-explore-range-high']}), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| maxPickableDays: 90, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: expect.any(Object), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 30/30 days for tracemetrics', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.TRACE_METRICS], | ||
| organization: OrganizationFixture(), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| defaultPeriod: '24h', | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 30, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 30/30 days for logs', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [DataCategory.LOG_BYTE, DataCategory.LOG_ITEM], | ||
| organization: OrganizationFixture(), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| defaultPeriod: '24h', | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 30, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 30/90 for many without flag', () => { | ||
| const {result} = renderHook(() => | ||
| useMaxPickableDays({ | ||
| dataCategories: [ | ||
| DataCategory.SPANS, | ||
| DataCategory.SPANS_INDEXED, | ||
| DataCategory.TRACE_METRICS, | ||
| DataCategory.LOG_BYTE, | ||
| DataCategory.LOG_ITEM, | ||
| ], | ||
| organization: OrganizationFixture(), | ||
| }) | ||
| ); | ||
|
|
||
| expect(result.current).toEqual({ | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: expect.any(Object), | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import {useMemo, type ReactNode} from 'react'; | ||
|
|
||
| import HookOrDefault from 'sentry/components/hookOrDefault'; | ||
| import type {DatePageFilterProps} from 'sentry/components/organizations/datePageFilter'; | ||
| import {t} from 'sentry/locale'; | ||
| import {DataCategory} from 'sentry/types/core'; | ||
| import type {Organization} from 'sentry/types/organization'; | ||
|
|
||
| export interface MaxPickableDaysOptions { | ||
| /** | ||
| * The maximum number of days the user is allowed to pick on the date page filter | ||
| */ | ||
| maxPickableDays: NonNullable<DatePageFilterProps['maxPickableDays']>; | ||
| /** | ||
| * The maximum number of days the user can upgrade to on the date page filter | ||
| */ | ||
| maxUpgradableDays: NonNullable<DatePageFilterProps['maxPickableDays']>; | ||
| defaultPeriod?: DatePageFilterProps['defaultPeriod']; | ||
| upsellFooter?: ReactNode; | ||
| } | ||
|
|
||
| interface UseMaxPickableDaysProps { | ||
| dataCategories: readonly [DataCategory, ...DataCategory[]]; | ||
| organization: Organization; | ||
| } | ||
|
|
||
| export function useMaxPickableDays({ | ||
| dataCategories, | ||
| organization, | ||
| }: UseMaxPickableDaysProps): MaxPickableDaysOptions { | ||
| return useMemo(() => { | ||
| function getMaxPickableDaysFor(dataCategory: DataCategory) { | ||
| return getMaxPickableDays(dataCategory, organization); | ||
| } | ||
|
|
||
| return getBestMaxPickableDays(dataCategories, getMaxPickableDaysFor); | ||
| }, [dataCategories, organization]); | ||
| } | ||
|
|
||
| function getBestMaxPickableDays( | ||
| dataCategories: readonly [DataCategory, ...DataCategory[]], | ||
| getMaxPickableDaysFor: (dataCategory: DataCategory) => MaxPickableDaysOptions | ||
| ) { | ||
| let maxPickableDays: MaxPickableDaysOptions = getMaxPickableDaysFor(dataCategories[0]); | ||
|
|
||
| for (let i = 1; i < dataCategories.length; i++) { | ||
| const dataCategory = dataCategories[i]!; | ||
| const maxPickableDaysForDataCategory = getMaxPickableDaysFor(dataCategory); | ||
| maxPickableDays = max(maxPickableDays, maxPickableDaysForDataCategory); | ||
| } | ||
|
|
||
| return maxPickableDays; | ||
| } | ||
|
|
||
| function max( | ||
| a: MaxPickableDaysOptions, | ||
| b: MaxPickableDaysOptions | ||
| ): MaxPickableDaysOptions { | ||
| if (a.maxPickableDays < b.maxPickableDays) { | ||
| return b; | ||
| } | ||
|
|
||
| if (a.maxUpgradableDays < b.maxUpgradableDays) { | ||
| return b; | ||
| } | ||
|
|
||
| return a; | ||
| } | ||
Zylphrex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const DESCRIPTION = t('To query over longer time ranges, upgrade to Business'); | ||
|
|
||
| function getMaxPickableDays( | ||
| dataCategory: DataCategory, | ||
| organization: Organization | ||
| ): MaxPickableDaysOptions { | ||
| switch (dataCategory) { | ||
| case DataCategory.SPANS: | ||
| case DataCategory.SPANS_INDEXED: { | ||
| const maxPickableDays = organization.features.includes( | ||
| 'visibility-explore-range-high' | ||
| ) | ||
| ? 90 | ||
| : 30; | ||
| return { | ||
| maxPickableDays, | ||
| maxUpgradableDays: 90, | ||
| upsellFooter: <UpsellFooterHook description={DESCRIPTION} source="spans" />, | ||
| }; | ||
| } | ||
| case DataCategory.TRACE_METRICS: | ||
| case DataCategory.LOG_BYTE: | ||
| case DataCategory.LOG_ITEM: | ||
| return { | ||
| maxPickableDays: 30, | ||
| maxUpgradableDays: 30, | ||
| defaultPeriod: '24h', | ||
| }; | ||
|
Comment on lines
+90
to
+97
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. Bug: The 🔍 Detailed AnalysisThe 💡 Suggested FixModify the 🤖 Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| default: | ||
| throw new Error( | ||
| `Unsupported data category: ${dataCategory} for getMaxPickableDays` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const UpsellFooterHook = HookOrDefault({ | ||
| hookName: 'component:header-date-page-filter-upsell-footer', | ||
| defaultComponent: () => null, | ||
| }); | ||
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.
This hook will be replaced by an implementation that looks at the org's subscription.