From 993ef771c9873bc974cc842e593e5a7937f5c84f Mon Sep 17 00:00:00 2001 From: Rocky Fischer Date: Wed, 11 Dec 2024 16:50:35 -0500 Subject: [PATCH] feat(analytics): specific defaults for granularities [MA-3507] Allow specifying default values for fine-grained relative times. --- .../analytics-utilities/src/queryTime.spec.tz.ts | 8 ++++---- .../analytics/analytics-utilities/src/queryTime.ts | 5 +---- .../analytics/analytics-utilities/src/timeframes.ts | 12 ++++++++++++ .../src/types/timeframe-options.ts | 1 + 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts b/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts index 369180214e..0a9cc4fc7b 100644 --- a/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts +++ b/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts @@ -44,10 +44,10 @@ describe('granularity enforcement', () => { expect(new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), 'tenMinutely', undefined, undefined, true).granularitySeconds()).toBe(10 * 60) // Should not permit finer grains outside the allowed finer grains. - expect(new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), 'tenSecondly', undefined, undefined, true).granularitySeconds()).toBe(5 * 60) + expect(new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), 'tenSecondly', undefined, undefined, true).granularitySeconds()).toBe(30 * 60) // Should pick an appropriate default response granularity. - expect(new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), undefined, undefined, undefined, true).granularitySeconds()).toBe(5 * 60) + expect(new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), undefined, undefined, undefined, true).granularitySeconds()).toBe(30 * 60) }) }) @@ -559,14 +559,14 @@ runUtcTest('UTC: fine granularity', () => { }) it('handles fine granularity in rounding - timeseries, 5 minutely', () => { - const tsQuery = new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), undefined, undefined, undefined,true) + const tsQuery = new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.ONE_DAY), 'fiveMinutely', undefined, undefined,true) expect(tsQuery.endDate()).toEqual(new Date('2023-11-09T01:20:00Z')) expect(tsQuery.startDate()).toEqual(new Date('2023-11-08T01:20:00Z')) }) it('handles fine granularity in rounding - timeseries, hourly', () => { - const tsQuery = new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.THIRTY_DAY), undefined, undefined, undefined,true) + const tsQuery = new TimeseriesQueryTime(getTimePeriod(TimeframeKeys.THIRTY_DAY), 'hourly', undefined, undefined,true) expect(tsQuery.endDate()).toEqual(new Date('2023-11-09T02:00:00Z')) expect(tsQuery.startDate()).toEqual(new Date('2023-10-10T02:00:00Z')) diff --git a/packages/analytics/analytics-utilities/src/queryTime.ts b/packages/analytics/analytics-utilities/src/queryTime.ts index 1349c1c7cc..8c90ddc425 100644 --- a/packages/analytics/analytics-utilities/src/queryTime.ts +++ b/packages/analytics/analytics-utilities/src/queryTime.ts @@ -87,10 +87,7 @@ export class TimeseriesQueryTime extends BaseQueryTime { if (granularity && timeframe.allowedGranularities(fineGrain).has(granularity)) { this.granularity = granularity } else if (fineGrain) { - // TODO: when removing the feature flag, consider redefining `defaultResponseGranularity` - // in the timeframes constructor: it should probably handle this calculation on its own. - const finestGranularity = timeframe.allowedGranularities(fineGrain).keys().next().value - this.granularity = finestGranularity ?? timeframe.defaultResponseGranularity + this.granularity = timeframe.fineGrainedDefaultGranularity ?? timeframe.defaultResponseGranularity } else { this.granularity = timeframe.defaultResponseGranularity } diff --git a/packages/analytics/analytics-utilities/src/timeframes.ts b/packages/analytics/analytics-utilities/src/timeframes.ts index 28210ce5de..2704537f08 100644 --- a/packages/analytics/analytics-utilities/src/timeframes.ts +++ b/packages/analytics/analytics-utilities/src/timeframes.ts @@ -55,6 +55,8 @@ export class Timeframe implements ITimeframe { // the first time bucket. readonly isRelative: boolean + readonly fineGrainedDefaultGranularity?: GranularityValues + private _startCustom?: Date private _endCustom?: Date @@ -73,6 +75,7 @@ export class Timeframe implements ITimeframe { this._startCustom = opts.startCustom this._endCustom = opts.endCustom this._allowedGranularitiesOverride = opts.allowedGranularitiesOverride + this.fineGrainedDefaultGranularity = opts.fineGrainedDefaultGranularity } // rawEnd does not consider granularity and should not be used directly in queries. @@ -272,6 +275,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'minutely', dataGranularity: 'minutely', isRelative: true, + fineGrainedDefaultGranularity: 'thirtySecondly', allowedTiers: ['free', 'trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['tenSecondly', 'thirtySecondly', 'minutely'], }), @@ -286,6 +290,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'minutely', dataGranularity: 'minutely', isRelative: true, + fineGrainedDefaultGranularity: 'minutely', allowedTiers: ['free', 'trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['tenSecondly', 'thirtySecondly', 'minutely', 'fiveMinutely', 'tenMinutely'], }), @@ -300,6 +305,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'hourly', dataGranularity: 'hourly', isRelative: true, + fineGrainedDefaultGranularity: 'fiveMinutely', allowedTiers: ['free', 'trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['thirtySecondly', 'minutely', 'fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly'], }), @@ -314,6 +320,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'hourly', dataGranularity: 'hourly', isRelative: true, + fineGrainedDefaultGranularity: 'tenMinutely', allowedTiers: ['free', 'trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['minutely', 'fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly'], }), @@ -328,6 +335,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'hourly', dataGranularity: 'hourly', isRelative: true, + fineGrainedDefaultGranularity: 'thirtyMinutely', allowedTiers: ['free', 'trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly'], }), @@ -342,6 +350,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'daily', dataGranularity: 'daily', isRelative: true, + fineGrainedDefaultGranularity: 'twoHourly', allowedTiers: ['trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['thirtyMinutely', 'hourly', 'twoHourly', 'twelveHourly', 'daily'], }), @@ -356,6 +365,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'daily', dataGranularity: 'daily', isRelative: true, + fineGrainedDefaultGranularity: 'twelveHourly', allowedTiers: ['trial', 'plus', 'enterprise'], allowedGranularitiesOverride: ['hourly', 'twoHourly', 'twelveHourly', 'daily', 'weekly'], }), @@ -376,6 +386,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'daily', dataGranularity: 'daily', isRelative: false, + fineGrainedDefaultGranularity: 'twoHourly', allowedTiers: ['plus', 'enterprise'], allowedGranularitiesOverride: ['thirtyMinutely', 'hourly', 'twoHourly', 'twelveHourly', 'daily'], }), @@ -409,6 +420,7 @@ export const TimePeriods = new Map([ defaultResponseGranularity: 'daily', dataGranularity: 'daily', isRelative: false, + fineGrainedDefaultGranularity: 'twoHourly', allowedTiers: ['plus', 'enterprise'], allowedGranularitiesOverride: ['thirtyMinutely', 'hourly', 'twoHourly', 'twelveHourly', 'daily'], }), diff --git a/packages/analytics/analytics-utilities/src/types/timeframe-options.ts b/packages/analytics/analytics-utilities/src/types/timeframe-options.ts index 4333282568..47c2d8a20f 100644 --- a/packages/analytics/analytics-utilities/src/types/timeframe-options.ts +++ b/packages/analytics/analytics-utilities/src/types/timeframe-options.ts @@ -12,4 +12,5 @@ export interface TimeframeOptions { startCustom?: Date endCustom?: Date allowedGranularitiesOverride?: GranularityValues[] + fineGrainedDefaultGranularity?: GranularityValues }