Skip to content

Commit

Permalink
feat(analytics): specific defaults for granularities [MA-3507] (#1849)
Browse files Browse the repository at this point in the history
Allow specifying default values for fine-grained relative times.
  • Loading branch information
adorack authored Dec 12, 2024
1 parent 15150c1 commit e724aec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

})
Expand Down Expand Up @@ -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'))
Expand Down
5 changes: 1 addition & 4 deletions packages/analytics/analytics-utilities/src/queryTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions packages/analytics/analytics-utilities/src/timeframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -272,6 +275,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'minutely',
dataGranularity: 'minutely',
isRelative: true,
fineGrainedDefaultGranularity: 'thirtySecondly',
allowedTiers: ['free', 'trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['tenSecondly', 'thirtySecondly', 'minutely'],
}),
Expand All @@ -286,6 +290,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'minutely',
dataGranularity: 'minutely',
isRelative: true,
fineGrainedDefaultGranularity: 'minutely',
allowedTiers: ['free', 'trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['tenSecondly', 'thirtySecondly', 'minutely', 'fiveMinutely', 'tenMinutely'],
}),
Expand All @@ -300,6 +305,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'hourly',
dataGranularity: 'hourly',
isRelative: true,
fineGrainedDefaultGranularity: 'fiveMinutely',
allowedTiers: ['free', 'trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['thirtySecondly', 'minutely', 'fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly'],
}),
Expand All @@ -314,6 +320,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'hourly',
dataGranularity: 'hourly',
isRelative: true,
fineGrainedDefaultGranularity: 'tenMinutely',
allowedTiers: ['free', 'trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['minutely', 'fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly'],
}),
Expand All @@ -328,6 +335,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'hourly',
dataGranularity: 'hourly',
isRelative: true,
fineGrainedDefaultGranularity: 'thirtyMinutely',
allowedTiers: ['free', 'trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly'],
}),
Expand All @@ -342,6 +350,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: true,
fineGrainedDefaultGranularity: 'twoHourly',
allowedTiers: ['trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['thirtyMinutely', 'hourly', 'twoHourly', 'twelveHourly', 'daily'],
}),
Expand All @@ -356,6 +365,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: true,
fineGrainedDefaultGranularity: 'twelveHourly',
allowedTiers: ['trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['hourly', 'twoHourly', 'twelveHourly', 'daily', 'weekly'],
}),
Expand All @@ -376,6 +386,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: false,
fineGrainedDefaultGranularity: 'twoHourly',
allowedTiers: ['plus', 'enterprise'],
allowedGranularitiesOverride: ['thirtyMinutely', 'hourly', 'twoHourly', 'twelveHourly', 'daily'],
}),
Expand Down Expand Up @@ -409,6 +420,7 @@ export const TimePeriods = new Map<string, Timeframe>([
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: false,
fineGrainedDefaultGranularity: 'twoHourly',
allowedTiers: ['plus', 'enterprise'],
allowedGranularitiesOverride: ['thirtyMinutely', 'hourly', 'twoHourly', 'twelveHourly', 'daily'],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface TimeframeOptions {
startCustom?: Date
endCustom?: Date
allowedGranularitiesOverride?: GranularityValues[]
fineGrainedDefaultGranularity?: GranularityValues
}

0 comments on commit e724aec

Please sign in to comment.