Skip to content
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

feat(utilities): add new timeframe methods [MA-1960] #1165

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/analytics/analytics-utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kong-ui-public/analytics-utilities",
"version": "0.15.0",
"version": "1.0.0",
"type": "module",
"main": "./dist/vitals-utilities.umd.js",
"module": "./dist/vitals-utilities.es.js",
Expand Down
54 changes: 53 additions & 1 deletion packages/analytics/analytics-utilities/src/timeframes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'vitest'
import { GranularityKeys, TimeframeKeys } from './types'
import { TimePeriods } from './timeframes'
import { Timeframe, TimePeriods } from './timeframes'

const freeTierMax = TimePeriods.get(TimeframeKeys.ONE_DAY)?.timeframeLength() || 86400
const sevenDays = TimePeriods.get(TimeframeKeys.SEVEN_DAY)?.timeframeLength() || 86400 * 7
Expand Down Expand Up @@ -62,3 +62,55 @@ describe('allowedGranularities', () => {
.toEqual(new Set([GranularityKeys.DAILY, GranularityKeys.WEEKLY]))
})
})

describe('cacheKey', () => {
it('handles relative keys', () => {
expect(TimePeriods.get(TimeframeKeys.ONE_DAY)?.cacheKey()).toBe('24h')
expect(TimePeriods.get(TimeframeKeys.CURRENT_MONTH)?.cacheKey()).toBe('current_month')
})

it('handles absolute keys', () => {
expect((new Timeframe({
key: 'custom',
timeframeText: 'custom',
display: 'custom',
startCustom: new Date('2024-01-01T00:00:00Z'),
endCustom: new Date('2024-01-02T00:00:00Z'),
timeframeLength: () => 0,
defaultResponseGranularity: GranularityKeys.DAILY,
dataGranularity: GranularityKeys.DAILY,
isRelative: false,
allowedTiers: ['free', 'plus', 'enterprise'],
})).cacheKey()).toBe('2024-01-01T00:00:00.000Z-2024-01-02T00:00:00.000Z')
})
})

describe('v4Query', () => {
it('handles relative keys', () => {
expect(TimePeriods.get(TimeframeKeys.ONE_DAY)!.v4Query()).toEqual({
type: 'relative',
time_range: '24h',
})
})

it('handles custom timeframes', () => {
const customTime = new Timeframe({
key: 'custom',
timeframeText: 'custom',
display: 'custom',
startCustom: new Date('2024-01-01T00:00:00Z'),
endCustom: new Date('2024-02-01T00:00:00Z'),
timeframeLength: () => 0,
defaultResponseGranularity: GranularityKeys.DAILY,
dataGranularity: GranularityKeys.DAILY,
isRelative: false,
allowedTiers: ['free', 'plus', 'enterprise'],
})

expect(customTime.v4Query()).toEqual({
type: 'absolute',
start: new Date('2024-01-01T00:00:00Z'),
end: new Date('2024-02-01T00:00:00Z'),
})
})
})
28 changes: 27 additions & 1 deletion packages/analytics/analytics-utilities/src/timeframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
DatePickerSelection,
TimeframeOptions,
TimePeriod,
RelativeTimeRangeValuesV4, TimeRangeV4,
} from './types'
import { getTimezoneOffset, utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz'
import type { ITimeframe } from './types/timeframe'
Expand All @@ -30,7 +31,7 @@ const adjustForTz = (d: Date, tz: string) => {
export class Timeframe implements ITimeframe {
readonly timeframeText: string

readonly key: string
readonly key: RelativeTimeRangeValuesV4 | 'custom'

readonly display: string

Expand Down Expand Up @@ -121,6 +122,31 @@ export class Timeframe implements ITimeframe {
return allowedValues
}

cacheKey(): string {
if (this.key !== 'custom') {
// Right now, `key === custom` is our flag for whether we're dealing with an absolute or relative timeframe.
return this.key
}

return `${this.rawStart().toISOString()}-${this.rawEnd().toISOString()}`
}

v4Query(): TimeRangeV4 {
if (this.key === 'custom') {
// Right now, `key === custom` is our flag for whether we're dealing with an absolute or relative timeframe.
return {
type: 'absolute',
start: this.rawStart(),
end: this.rawEnd(),
}
}

return {
type: 'relative',
time_range: this.key,
}
}

protected tzAdjustedDate(tz?: string): Date {
if (!tz) {
return new Date()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface ExploreQuery {
dimensions?: QueryableExploreDimensions[]
filters?: ExploreFilter[]
granularity?: GranularityValues
time_range: TimeRangeV4
time_range?: TimeRangeV4
limit?: number
descending?: boolean
meta?: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { GranularityKeys } from './granularity-keys'
import type { RelativeTimeRangeValuesV4 } from './explore-v4'

export interface TimeframeOptions {
key: string
key: RelativeTimeRangeValuesV4 | 'custom'
timeframeText: string
display: string
defaultResponseGranularity: GranularityKeys
Expand Down
Loading