Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
130 changes: 130 additions & 0 deletions packages/analytics/analytics-utilities/src/timeframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ class CurrentMonth extends Timeframe {
}
}

class CurrentYear extends Timeframe {
rawStart(tz?: string): Date {
let firstOfTheYear = new Date(this.tzAdjustedDate(tz).getFullYear(), 0, 1)

if (tz) {
firstOfTheYear = adjustForTz(firstOfTheYear, tz)
}

return firstOfTheYear
}

maximumTimeframeLength() {
return 60 * 60 * 24 * 366
}
}

class PreviousWeek extends Timeframe {
rawEnd(tz?: string): Date {
// `startOfWeek` isn't aware of timezones, so the resulting "start of month" time is in the local timezone.
Expand Down Expand Up @@ -259,6 +275,28 @@ class PreviousMonth extends Timeframe {
}
}

class PreviousYear extends Timeframe {
rawEnd(tz?: string): Date {
let thisYear = new Date(this.tzAdjustedDate(tz).getFullYear(), 0, 1)

if (tz) {
thisYear = adjustForTz(thisYear, tz)
}

return thisYear
}

rawStart(tz?: string): Date {
let lastYear = new Date(this.tzAdjustedDate(tz).getFullYear() - 1, 0, 1)

if (tz) {
lastYear = adjustForTz(lastYear, tz)
}

return lastYear
}
}

// These TimePeriod definitions request a default granularity and can be adjusted
//
// Using <string, any> as a temp workaround for TimePeriods.get() potentially returning `undefined` lint issue.
Expand Down Expand Up @@ -371,6 +409,51 @@ export const TimePeriods = new Map<string, Timeframe>([
allowedGranularitiesOverride: ['hourly', 'twoHourly', 'twelveHourly', 'daily', 'weekly'],
}),
],
[
TimeframeKeys.NINETY_DAY,
new Timeframe({
key: TimeframeKeys.NINETY_DAY,
display: 'Last 90 days',
timeframeText: '90 days',
timeframeLength: () => 60 * 60 * 24 * 90,
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: true,
fineGrainedDefaultGranularity: 'daily',
allowedTiers: ['trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['hourly', 'twoHourly', 'twelveHourly', 'daily', 'weekly'],
}),
],
[
TimeframeKeys.ONE_HUNDRED_EIGHTY_DAY,
new Timeframe({
key: TimeframeKeys.ONE_HUNDRED_EIGHTY_DAY,
display: 'Last 180 days',
timeframeText: '180 days',
timeframeLength: () => 60 * 60 * 24 * 180,
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: true,
fineGrainedDefaultGranularity: 'daily',
allowedTiers: ['trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['hourly', 'twoHourly', 'twelveHourly', 'daily', 'weekly'],
}),
],
[
TimeframeKeys.ONE_YEAR,
new Timeframe({
key: TimeframeKeys.ONE_YEAR,
display: 'Last 365 days',
timeframeText: '365 days',
timeframeLength: () => 60 * 60 * 24 * 365,
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: true,
fineGrainedDefaultGranularity: 'daily',
allowedTiers: ['trial', 'plus', 'enterprise'],
allowedGranularitiesOverride: ['hourly', 'twoHourly', 'twelveHourly', 'daily', 'weekly'],
}),
],
[
TimeframeKeys.CURRENT_WEEK,
new CurrentWeek({
Expand Down Expand Up @@ -411,6 +494,25 @@ export const TimePeriods = new Map<string, Timeframe>([
allowedTiers: ['plus', 'enterprise'],
}),
],
[
TimeframeKeys.CURRENT_YEAR,
new CurrentYear({
key: TimeframeKeys.CURRENT_YEAR,
display: 'This year',
timeframeText: 'Year',
timeframeLength: () => {
// Jan 1 -> now
const firstOfTheYear = new Date(new Date().getFullYear(), 0, 1)
const end = startOfDay(addDays(new Date(), 1))

return (end.getTime() - firstOfTheYear.getTime()) / 1000
},
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: false,
allowedTiers: ['plus', 'enterprise'],
}),
],
[
TimeframeKeys.PREVIOUS_WEEK,
new PreviousWeek({
Expand Down Expand Up @@ -453,6 +555,29 @@ export const TimePeriods = new Map<string, Timeframe>([
allowedTiers: ['plus', 'enterprise'],
}),
],
[
TimeframeKeys.PREVIOUS_YEAR,
new PreviousYear({
key: TimeframeKeys.PREVIOUS_YEAR,
display: 'Previous year',
timeframeText: 'Year',
timeframeLength: () => {
// Not all years have the same number of days (leap years).
const end = new Date(new Date().getFullYear(), 0, 1)
const start = new Date(new Date().getFullYear() - 1, 0, 1)
let offset = 0
if (end.getTimezoneOffset() !== start.getTimezoneOffset()) {
offset = dstOffsetHours(end, start)
}

return 60 * 60 * 24 * (365 + (start.getFullYear() % 4 === 0 ? 1 : 0)) + hoursToSeconds(offset)
},
defaultResponseGranularity: 'daily',
dataGranularity: 'daily',
isRelative: false,
allowedTiers: ['plus', 'enterprise'],
}),
],
])

export function datePickerSelectionToTimeframe(datePickerSelection: DatePickerSelection): Timeframe {
Expand Down Expand Up @@ -523,8 +648,13 @@ export const TIMEFRAME_LOOKUP: Record<string, TimeframeKeys> = {
'24h': TimeframeKeys.ONE_DAY,
'7d': TimeframeKeys.SEVEN_DAY,
'30d': TimeframeKeys.THIRTY_DAY,
'90d': TimeframeKeys.NINETY_DAY,
'180d': TimeframeKeys.ONE_HUNDRED_EIGHTY_DAY,
'365d': TimeframeKeys.ONE_YEAR,
current_week: TimeframeKeys.CURRENT_WEEK,
current_month: TimeframeKeys.CURRENT_MONTH,
current_year: TimeframeKeys.CURRENT_YEAR,
previous_week: TimeframeKeys.PREVIOUS_WEEK,
previous_month: TimeframeKeys.PREVIOUS_MONTH,
previous_year: TimeframeKeys.PREVIOUS_YEAR,
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export const relativeTimeRangeValuesV4 = [
'30d',
'current_month',
'previous_month',
'90d',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to add these here? Adding these would mess up some of our validations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for ts:
Screenshot 2025-10-16 at 11 14 40 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer that I use assertions?

'180d',
'365d',
'current_year',
'previous_year',
] as const

export type RelativeTimeRangeValuesV4 = typeof relativeTimeRangeValuesV4[number]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ export enum TimeframeKeys {
ONE_DAY = '24h',
SEVEN_DAY = '7d',
THIRTY_DAY = '30d',
NINETY_DAY = '90d',
ONE_HUNDRED_EIGHTY_DAY = '180d',
ONE_YEAR = '365d',
CURRENT_WEEK = 'current_week',
CURRENT_MONTH = 'current_month',
CURRENT_QUARTER = 'current_quarter',
CURRENT_YEAR = 'current_year',
PREVIOUS_WEEK = 'previous_week',
PREVIOUS_MONTH = 'previous_month',
PREVIOUS_QUARTER = 'previous_quarter',
PREVIOUS_YEAR = 'previous_year',
}
Loading