Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

feat: add last 24 hours and last 3 months

Carl-Gerhard Lindesvärd064983401a14
diff --git a/apps/start/src/components/chat/chat-context-widget.tsx b/apps/start/src/components/chat/chat-context-widget.tsx
index adefe5826..eb3be33b5 100644
--- a/apps/start/src/components/chat/chat-context-widget.tsx
+++ b/apps/start/src/components/chat/chat-context-widget.tsx
@@ -1,5 +1,6 @@
import { usePageContextValue } from '@/contexts/page-context';
import { cn } from '@/utils/cn';
+import { timeWindows } from '@openpanel/constants';
import {
Building2Icon,
GanttChartIcon,
@@ -123,32 +124,5 @@ const PAGE_META: Record<
};
function formatRange(range: string): string {
- switch (range) {
- case '30min':
- return 'Last 30 min';
- case 'lastHour':
- return 'Last hour';
- case 'today':
- return 'Today';
- case 'yesterday':
- return 'Yesterday';
- case '7d':
- return 'Last 7 days';
- case '30d':
- return 'Last 30 days';
- case '6m':
- return 'Last 6 months';
- case '12m':
- return 'Last 12 months';
- case 'monthToDate':
- return 'Month to date';
- case 'lastMonth':
- return 'Last month';
- case 'yearToDate':
- return 'Year to date';
- case 'lastYear':
- return 'Last year';
- default:
- return range;
- }
+ return timeWindows[range as keyof typeof timeWindows]?.label ?? range;
}
diff --git a/apps/start/src/components/chat/tool-results/chat-ui-apply-result.tsx b/apps/start/src/components/chat/tool-results/chat-ui-apply-result.tsx
index 1f221cb46..935a260b2 100644
--- a/apps/start/src/components/chat/tool-results/chat-ui-apply-result.tsx
+++ b/apps/start/src/components/chat/tool-results/chat-ui-apply-result.tsx
@@ -1,3 +1,4 @@
+import { timeWindows } from '@openpanel/constants';
import { CheckIcon } from 'lucide-react';
import type { ReactNode } from 'react';
import type { ToolResultProps } from './types';
@@ -80,34 +81,5 @@ function AppliedChip({ children }: { children: ReactNode }) {
}
function formatRange(range: string): string {
- switch (range) {
- case '30min':
- return 'Last 30 min';
- case 'lastHour':
- return 'Last hour';
- case 'today':
- return 'Today';
- case 'yesterday':
- return 'Yesterday';
- case '7d':
- return 'Last 7 days';
- case '30d':
- return 'Last 30 days';
- case '6m':
- return 'Last 6 months';
- case '12m':
- return 'Last 12 months';
- case 'monthToDate':
- return 'Month to date';
- case 'lastMonth':
- return 'Last month';
- case 'yearToDate':
- return 'Year to date';
- case 'lastYear':
- return 'Last year';
- case 'custom':
- return 'Custom range';
- default:
- return range;
- }
+ return timeWindows[range as keyof typeof timeWindows]?.label ?? range;
}
diff --git a/apps/start/src/components/report/ReportInterval.tsx b/apps/start/src/components/report/ReportInterval.tsx
index 216892df6..27d78945c 100644
--- a/apps/start/src/components/report/ReportInterval.tsx
+++ b/apps/start/src/components/report/ReportInterval.tsx
@@ -76,13 +76,18 @@ export function ReportInterval({
disabled:
range === 'today' ||
range === 'lastHour' ||
+ range === 'last24h' ||
range === '30min' ||
range === '7d',
},
{
value: 'month',
label: 'Month',
- disabled: range === 'today' || range === 'lastHour' || range === '30min',
+ disabled:
+ range === 'today' ||
+ range === 'lastHour' ||
+ range === 'last24h' ||
+ range === '30min',
},
];
diff --git a/apps/start/src/components/time-window-picker.tsx b/apps/start/src/components/time-window-picker.tsx
index a8bb0035f..f487154ac 100644
--- a/apps/start/src/components/time-window-picker.tsx
+++ b/apps/start/src/components/time-window-picker.tsx
@@ -138,6 +138,12 @@ export function TimeWindowPicker({
{timeWindows.lastHour.shortcut}
</DropdownMenuShortcut>
</DropdownMenuItem>
+ <DropdownMenuItem onClick={() => onChange(timeWindows.last24h.key)}>
+ {timeWindows.last24h.label}
+ <DropdownMenuShortcut>
+ {timeWindows.last24h.shortcut}
+ </DropdownMenuShortcut>
+ </DropdownMenuItem>
<DropdownMenuItem onClick={() => onChange(timeWindows.today.key)}>
{timeWindows.today.label}
<DropdownMenuShortcut>
@@ -167,6 +173,12 @@ export function TimeWindowPicker({
{timeWindows['30d'].shortcut}
</DropdownMenuShortcut>
</DropdownMenuItem>
+ <DropdownMenuItem onClick={() => onChange(timeWindows['3m'].key)}>
+ {timeWindows['3m'].label}
+ <DropdownMenuShortcut>
+ {timeWindows['3m'].shortcut}
+ </DropdownMenuShortcut>
+ </DropdownMenuItem>
<DropdownMenuItem onClick={() => onChange(timeWindows['6m'].key)}>
{timeWindows['6m'].label}
<DropdownMenuShortcut>
diff --git a/packages/constants/index.ts b/packages/constants/index.ts
index 911297512..10ae31cee 100644
--- a/packages/constants/index.ts
+++ b/packages/constants/index.ts
@@ -16,6 +16,11 @@ export const timeWindows = {
label: 'Last hour',
shortcut: 'H',
},
+ last24h: {
+ key: 'last24h',
+ label: 'Last 24 hours',
+ shortcut: '4',
+ },
today: {
key: 'today',
label: 'Today',
@@ -36,6 +41,11 @@ export const timeWindows = {
label: 'Last 30 days',
shortcut: 'T',
},
+ '3m': {
+ key: '3m',
+ label: 'Last 3 months',
+ shortcut: '3',
+ },
'6m': {
key: '6m',
label: 'Last 6 months',
@@ -179,15 +189,6 @@ export const alphabetIds = [
'Z',
] as const;
-export const deprecated_timeRanges = {
- '1h': '1h',
- '24h': '24h',
- '14d': '14d',
- '1m': '1m',
- '3m': '3m',
- '1y': '1y',
-};
-
export const metrics = {
count: 'count',
sum: 'sum',
@@ -207,6 +208,7 @@ export function isHourIntervalEnabledByRange(range: keyof typeof timeWindows) {
isMinuteIntervalEnabledByRange(range) ||
range === 'today' ||
range === 'yesterday' ||
+ range === 'last24h' ||
range === '7d'
);
}
@@ -217,12 +219,13 @@ export function getDefaultIntervalByRange(
if (range === '30min' || range === 'lastHour') {
return 'minute';
}
- if (range === 'today' || range === 'yesterday') {
+ if (range === 'today' || range === 'yesterday' || range === 'last24h') {
return 'hour';
}
if (
range === '7d' ||
range === '30d' ||
+ range === '3m' ||
range === 'lastMonth' ||
range === 'monthToDate'
) {
diff --git a/packages/db/code-migrations/17-migrate-deprecated-ranges.ts b/packages/db/code-migrations/17-migrate-deprecated-ranges.ts
new file mode 100644
index 000000000..fd9e8db09
--- /dev/null
+++ b/packages/db/code-migrations/17-migrate-deprecated-ranges.ts
@@ -0,0 +1,14 @@
+import { db } from '../index';
+import { printBoxMessage } from './helpers';
+
+export async function up() {
+ printBoxMessage('🔄 Migrating deprecated report.range values', []);
+
+ const updated = await db.$executeRaw`
+ UPDATE reports
+ SET range = '30d'
+ WHERE range IN ('1h', '24h', '14d', '1m', '3m', '1y')
+ `;
+
+ printBoxMessage('✅ Migration Complete', [`Updated: ${updated} reports`]);
+}
diff --git a/packages/db/src/services/date.service.ts b/packages/db/src/services/date.service.ts
index 7bb5433aa..19fcd7232 100644
--- a/packages/db/src/services/date.service.ts
+++ b/packages/db/src/services/date.service.ts
@@ -31,6 +31,23 @@ export function getDatesFromRange(range: IChartRange, timezone: string) {
};
}
+ if (range === 'last24h') {
+ const startDate = DateTime.now()
+ .minus({ hour: 24 })
+ .startOf('minute')
+ .setZone(timezone)
+ .toFormat('yyyy-MM-dd HH:mm:ss');
+ const endDate = DateTime.now()
+ .setZone(timezone)
+ .endOf('minute')
+ .toFormat('yyyy-MM-dd HH:mm:ss');
+
+ return {
+ startDate,
+ endDate,
+ };
+ }
+
if (range === 'today') {
const startDate = DateTime.now()
.setZone(timezone)
@@ -82,6 +99,24 @@ export function getDatesFromRange(range: IChartRange, timezone: string) {
};
}
+ if (range === '3m') {
+ const startDate = DateTime.now()
+ .minus({ month: 3 })
+ .setZone(timezone)
+ .startOf('day')
+ .toFormat('yyyy-MM-dd HH:mm:ss');
+ const endDate = DateTime.now()
+ .setZone(timezone)
+ .endOf('day')
+ .plus({ millisecond: 1 })
+ .toFormat('yyyy-MM-dd HH:mm:ss');
+
+ return {
+ startDate,
+ endDate,
+ };
+ }
+
if (range === '6m') {
const startDate = DateTime.now()
.minus({ month: 6 })
diff --git a/packages/db/src/services/reports.service.ts b/packages/db/src/services/reports.service.ts
index 57dc5cf3a..f25fb6b4e 100644
--- a/packages/db/src/services/reports.service.ts
+++ b/packages/db/src/services/reports.service.ts
@@ -1,8 +1,4 @@
-import {
- alphabetIds,
- deprecated_timeRanges,
- lineTypes,
-} from '@openpanel/constants';
+import { alphabetIds, lineTypes } from '@openpanel/constants';
import type {
IChartBreakdown,
IChartEventFilter,
@@ -82,10 +78,7 @@ export function transformReport(
series:
(report.events as IChartEventItem[]).map(transformReportEventItem) ?? [],
breakdowns: report.breakdowns as IChartBreakdown[],
- range:
- report.range in deprecated_timeRanges
- ? '30d'
- : (report.range as IChartRange),
+ range: report.range as IChartRange,
previous: report.previous ?? false,
formula: report.formula ?? undefined,
metric: report.metric ?? 'sum',
diff --git a/packages/trpc/src/routers/overview.ts b/packages/trpc/src/routers/overview.ts
index e4329c6db..c1a7af5ec 100644
--- a/packages/trpc/src/routers/overview.ts
+++ b/packages/trpc/src/routers/overview.ts
@@ -42,6 +42,7 @@ const cacher = cacheMiddleware((input, opts) => {
case '30min':
case 'today':
case 'lastHour':
+ case 'last24h':
return 1;
default:
return 1;