Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

feat(analytics): refactor time parameters to use pastMinutesStart and… (#309)

Bill Yang1a5e4dfe3008
diff --git a/client/src/api/analytics/useGetGoals.ts b/client/src/api/analytics/useGetGoals.ts
index 461e97142..1e48558e2 100644
--- a/client/src/api/analytics/useGetGoals.ts
+++ b/client/src/api/analytics/useGetGoals.ts
@@ -45,7 +45,8 @@ export function useGetGoals({
sort = "createdAt",
order = "desc",
enabled = true,
- minutes,
+ pastMinutesStart,
+ pastMinutesEnd,
}: {
startDate?: string;
endDate?: string;
@@ -54,7 +55,8 @@ export function useGetGoals({
sort?: "goalId" | "name" | "goalType" | "createdAt";
order?: "asc" | "desc";
enabled?: boolean;
- minutes?: number;
+ pastMinutesStart?: number;
+ pastMinutesEnd?: number;
}) {
const { site, time } = useStore();
const filteredFilters = getFilteredFilters(GOALS_PAGE_FILTERS);
@@ -62,9 +64,13 @@ export function useGetGoals({
// If startDate and endDate are not provided, use time from store
let timeParams: Record<string, string> = {};
- if (minutes) {
- // If minutes is explicitly provided, use it
- timeParams = { minutes: minutes.toString(), timeZone };
+ if (pastMinutesStart !== undefined && pastMinutesEnd !== undefined) {
+ // If past minutes range is explicitly provided, use it
+ timeParams = {
+ pastMinutesStart: pastMinutesStart.toString(),
+ pastMinutesEnd: pastMinutesEnd.toString(),
+ timeZone,
+ };
} else if (!startDate || !endDate) {
// Otherwise get time parameters from the store's time
// This will handle last-24-hours mode automatically
@@ -104,14 +110,16 @@ export function useGetGoals({
* Hook to get goals data for the past X minutes
*/
export function useGetGoalsPastMinutes({
- minutes = 24 * 60,
+ pastMinutesStart = 24 * 60,
+ pastMinutesEnd = 0,
page = 1,
pageSize = 10,
sort = "createdAt",
order = "desc",
enabled = true,
}: {
- minutes?: number;
+ pastMinutesStart?: number;
+ pastMinutesEnd?: number;
page?: number;
pageSize?: number;
sort?: "goalId" | "name" | "goalType" | "createdAt";
@@ -119,7 +127,8 @@ export function useGetGoalsPastMinutes({
enabled?: boolean;
}) {
return useGetGoals({
- minutes,
+ pastMinutesStart,
+ pastMinutesEnd,
page,
pageSize,
sort,
diff --git a/client/src/api/analytics/useGetOverview.ts b/client/src/api/analytics/useGetOverview.ts
index 8000a3059..63b2933a8 100644
--- a/client/src/api/analytics/useGetOverview.ts
+++ b/client/src/api/analytics/useGetOverview.ts
@@ -51,27 +51,18 @@ export function useGetOverview({
}
export function useGetOverviewPastMinutes({
- pastMinutes,
pastMinutesStart,
pastMinutesEnd,
site,
}: {
- pastMinutes?: number;
- pastMinutesStart?: number;
- pastMinutesEnd?: number;
+ pastMinutesStart: number;
+ pastMinutesEnd: number;
site?: number | string;
}) {
return useQuery({
- queryKey: [
- "overview-past-minutes",
- pastMinutes,
- pastMinutesStart,
- pastMinutesEnd,
- site,
- ],
+ queryKey: ["overview-past-minutes", pastMinutesStart, pastMinutesEnd, site],
queryFn: () => {
return authedFetch(`${BACKEND_URL}/overview/${site}`, {
- pastMinutes,
timeZone,
pastMinutesStart,
pastMinutesEnd,
diff --git a/client/src/api/analytics/useGetOverviewBucketed.ts b/client/src/api/analytics/useGetOverviewBucketed.ts
index 99b033e2f..83058e706 100644
--- a/client/src/api/analytics/useGetOverviewBucketed.ts
+++ b/client/src/api/analytics/useGetOverviewBucketed.ts
@@ -74,7 +74,6 @@ export function useGetOverviewBucketed({
}
export function useGetOverviewBucketedPastMinutes({
- pastMinutes = 24 * 60,
pastMinutesStart,
pastMinutesEnd,
site,
@@ -83,9 +82,8 @@ export function useGetOverviewBucketedPastMinutes({
dynamicFilters = [],
props,
}: {
- pastMinutes?: number;
- pastMinutesStart?: number;
- pastMinutesEnd?: number;
+ pastMinutesStart: number;
+ pastMinutesEnd: number;
site: number | string;
bucket?: TimeBucket;
refetchInterval?: number;
@@ -96,44 +94,23 @@ export function useGetOverviewBucketedPastMinutes({
const combinedFilters = [...globalFilters, ...dynamicFilters];
- const useRange =
- pastMinutesStart !== undefined && pastMinutesEnd !== undefined;
-
return useQuery({
- queryKey: useRange
- ? [
- "overview-bucketed-past-minutes-range",
- pastMinutesStart,
- pastMinutesEnd,
- site,
- bucket,
- combinedFilters,
- ]
- : [
- "overview-bucketed-past-minutes",
- pastMinutes,
- site,
- bucket,
- combinedFilters,
- ],
+ queryKey: [
+ "overview-bucketed-past-minutes-range",
+ pastMinutesStart,
+ pastMinutesEnd,
+ site,
+ bucket,
+ combinedFilters,
+ ],
queryFn: () => {
- return authedFetch(
- `${BACKEND_URL}/overview-bucketed/${site}`,
- useRange
- ? {
- timeZone,
- bucket,
- pastMinutesStart,
- pastMinutesEnd,
- filters: combinedFilters,
- }
- : {
- timeZone,
- bucket,
- pastMinutes,
- filters: combinedFilters,
- }
- ).then((res) => res.json());
+ return authedFetch(`${BACKEND_URL}/overview-bucketed/${site}`, {
+ timeZone,
+ bucket,
+ pastMinutesStart,
+ pastMinutesEnd,
+ filters: combinedFilters,
+ }).then((res) => res.json());
},
refetchInterval,
placeholderData: (_, query: any) => {
@@ -159,14 +136,16 @@ export function useGetOverviewBucketedPastMinutes({
* Hook to get previous time period data for comparison with last-24-hours mode
*/
export function useGetOverviewBucketedPreviousPastMinutes({
- pastMinutes = 24 * 60,
+ pastMinutesStart,
+ pastMinutesEnd,
site,
bucket = "hour",
refetchInterval,
dynamicFilters = [],
props,
}: {
- pastMinutes?: number;
+ pastMinutesStart: number;
+ pastMinutesEnd: number;
site: number | string;
bucket?: TimeBucket;
refetchInterval?: number;
@@ -177,14 +156,11 @@ export function useGetOverviewBucketedPreviousPastMinutes({
const combinedFilters = [...globalFilters, ...dynamicFilters];
- const pastMinutesStartVal = pastMinutes * 2;
- const pastMinutesEndVal = pastMinutes;
-
return useQuery({
queryKey: [
"overview-bucketed-previous-past-minutes",
- pastMinutesStartVal,
- pastMinutesEndVal,
+ pastMinutesStart,
+ pastMinutesEnd,
site,
bucket,
combinedFilters,
@@ -193,8 +169,8 @@ export function useGetOverviewBucketedPreviousPastMinutes({
return authedFetch(`${BACKEND_URL}/overview-bucketed/${site}`, {
timeZone,
bucket,
- pastMinutesStart: pastMinutesStartVal,
- pastMinutesEnd: pastMinutesEndVal,
+ pastMinutesStart,
+ pastMinutesEnd,
filters: combinedFilters,
}).then((res) => res.json());
},
diff --git a/client/src/api/analytics/useGetPageTitles.ts b/client/src/api/analytics/useGetPageTitles.ts
index 0f1a44ffd..9be470526 100644
--- a/client/src/api/analytics/useGetPageTitles.ts
+++ b/client/src/api/analytics/useGetPageTitles.ts
@@ -49,7 +49,8 @@ export function useGetPageTitlesPaginated({
? {
// Past minutes approach for last-24-hours mode
timeZone: timeZone,
- minutes: 24 * 60, // 24 hours in minutes
+ pastMinutesStart: 24 * 60, // 24 hours ago
+ pastMinutesEnd: 0, // now
limit,
page,
filters: useFilters ? filters : undefined,
diff --git a/client/src/api/analytics/usePaginatedSingleCol.ts b/client/src/api/analytics/usePaginatedSingleCol.ts
index f84c52430..75e97e0d3 100644
--- a/client/src/api/analytics/usePaginatedSingleCol.ts
+++ b/client/src/api/analytics/usePaginatedSingleCol.ts
@@ -25,15 +25,30 @@ export function usePaginatedSingleCol({
}: UsePaginatedSingleColOptions): UseQueryResult<PaginatedResponse> {
const { time, site, filters } = useStore();
+ // Check if we're using last-24-hours mode
+ const isPast24HoursMode = time.mode === "last-24-hours";
+
// Determine the query parameters based on mode
- const queryParams = {
- ...getStartAndEndDate(time),
- timeZone: timeZone,
- parameter,
- limit,
- page,
- filters: useFilters ? filters : undefined,
- };
+ const queryParams = isPast24HoursMode
+ ? {
+ // Past minutes approach for last-24-hours mode
+ timeZone: timeZone,
+ pastMinutesStart: 24 * 60, // 24 hours ago
+ pastMinutesEnd: 0, // now
+ parameter,
+ limit,
+ page,
+ filters: useFilters ? filters : undefined,
+ }
+ : {
+ // Regular date-based approach
+ ...getStartAndEndDate(time),
+ timeZone: timeZone,
+ parameter,
+ limit,
+ page,
+ filters: useFilters ? filters : undefined,
+ };
return useQuery({
queryKey: [
@@ -43,8 +58,8 @@ export function usePaginatedSingleCol({
filters,
limit,
page,
- "paginated-single-col",
- ], // More specific key
+ isPast24HoursMode ? "past-minutes" : "date-range",
+ ],
queryFn: () => {
return authedFetch(`${BACKEND_URL}/single-col/${site}`, queryParams)
.then((res) => res.json())
diff --git a/client/src/api/analytics/useSingleCol.ts b/client/src/api/analytics/useSingleCol.ts
index 17f658290..3e6b894f7 100644
--- a/client/src/api/analytics/useSingleCol.ts
+++ b/client/src/api/analytics/useSingleCol.ts
@@ -37,11 +37,12 @@ export function useSingleCol({
// Determine the query parameters based on mode
const queryParams = isPast24HoursMode
? {
- // Past minutes approach
+ // Past minutes approach using pastMinutesStart/pastMinutesEnd
timeZone: timeZone,
parameter,
limit,
- minutes: periodTime === "previous" ? 48 * 60 : 24 * 60,
+ pastMinutesStart: periodTime === "previous" ? 48 * 60 : 24 * 60,
+ pastMinutesEnd: periodTime === "previous" ? 24 * 60 : 0,
filters: useFilters ? filters : undefined,
}
: {
@@ -88,22 +89,32 @@ export function useSingleCol({
export function useSingleColRealtime({
parameter,
limit = 1000,
- minutes = 30,
+ pastMinutesStart = 30,
+ pastMinutesEnd = 0,
}: {
parameter: FilterParameter;
limit?: number;
- minutes?: number;
+ pastMinutesStart?: number;
+ pastMinutesEnd?: number;
}): UseQueryResult<APIResponse<SingleColResponse[]>> {
const { site } = useStore();
return useQuery({
- queryKey: [parameter, site, limit, minutes, "realtime"],
+ queryKey: [
+ parameter,
+ site,
+ limit,
+ pastMinutesStart,
+ pastMinutesEnd,
+ "realtime",
+ ],
queryFn: () => {
return authedFetch(`${BACKEND_URL}/single-col/${site}`, {
timeZone,
parameter,
limit,
- minutes,
+ pastMinutesStart,
+ pastMinutesEnd,
}).then((res) => res.json());
},
staleTime: Infinity,
diff --git a/client/src/api/analytics/utils.ts b/client/src/api/analytics/utils.ts
index 48914b1df..81662494f 100644
--- a/client/src/api/analytics/utils.ts
+++ b/client/src/api/analytics/utils.ts
@@ -12,8 +12,9 @@ export function getQueryTimeParams(time: Time): string {
// Handle last-24-hours mode differently
if (time.mode === "last-24-hours") {
- // Use minutes parameter instead of date range
- params.append("minutes", "1440"); // 24 hours * 60 minutes
+ // Use pastMinutesStart/pastMinutesEnd parameters instead of date range
+ params.append("pastMinutesStart", "1440"); // 24 hours ago (24 * 60 minutes)
+ params.append("pastMinutesEnd", "0"); // now
params.append("timeZone", timeZone);
return params.toString();
}
diff --git a/client/src/app/[site]/main/components/MainSection/MainSection.tsx b/client/src/app/[site]/main/components/MainSection/MainSection.tsx
index 24d8f9709..3c3cae6cd 100644
--- a/client/src/app/[site]/main/components/MainSection/MainSection.tsx
+++ b/client/src/app/[site]/main/components/MainSection/MainSection.tsx
@@ -71,7 +71,8 @@ export function MainSection() {
isFetching: isPastMinutesFetching,
error: pastMinutesError,
} = useGetOverviewBucketedPastMinutes({
- pastMinutes: 24 * 60,
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
site,
bucket,
props: {
@@ -84,7 +85,8 @@ export function MainSection() {
isFetching: isPastMinutesPreviousFetching,
error: pastMinutesPreviousError,
} = useGetOverviewBucketedPreviousPastMinutes({
- pastMinutes: 24 * 60,
+ pastMinutesStart: 48 * 60,
+ pastMinutesEnd: 24 * 60,
site,
bucket,
props: {
diff --git a/client/src/app/[site]/main/components/MainSection/Overview.tsx b/client/src/app/[site]/main/components/MainSection/Overview.tsx
index aa3cb5737..53063a109 100644
--- a/client/src/app/[site]/main/components/MainSection/Overview.tsx
+++ b/client/src/app/[site]/main/components/MainSection/Overview.tsx
@@ -93,7 +93,8 @@ const Stat = ({
// Past minutes data for sparklines
const { data: pastMinutesData } = useGetOverviewBucketedPastMinutes({
- pastMinutes: 24 * 60,
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
site,
bucket,
props: {
@@ -199,7 +200,8 @@ export function Overview() {
error: pastMinutesOverviewError,
} = useGetOverviewPastMinutes({
site,
- pastMinutes: 24 * 60,
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
});
// Past minutes-based queries for previous period
diff --git a/client/src/app/[site]/main/components/sections/Weekdays.tsx b/client/src/app/[site]/main/components/sections/Weekdays.tsx
index 170394e49..45cfe3cb7 100644
--- a/client/src/app/[site]/main/components/sections/Weekdays.tsx
+++ b/client/src/app/[site]/main/components/sections/Weekdays.tsx
@@ -34,7 +34,7 @@ import {
shortDayNames,
longDayNames,
hourLabels,
- formatLocalTime
+ formatLocalTime,
} from "../../../../../lib/dateTimeUtils";
export function Weekdays() {
@@ -58,7 +58,8 @@ export function Weekdays() {
isFetching: isPast24HoursFetching,
error: past24HoursError,
} = useGetOverviewBucketedPastMinutes({
- pastMinutes: 24 * 60,
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
site,
bucket: "hour",
props: {
@@ -281,7 +282,8 @@ export function Weekdays() {
</TooltipTrigger>
<TooltipContent className="flex flex-col gap-1 p-2">
<div className="font-medium text-sm">
- {longDayNames[day]} {formatLocalTime(hour, 0)} - {formatLocalTime(hour, 59)}
+ {longDayNames[day]} {formatLocalTime(hour, 0)} -{" "}
+ {formatLocalTime(hour, 59)}
</div>
<div className="flex items-center gap-2">
<span className="font-semibold">
diff --git a/client/src/app/[site]/pages/components/PageListItem.tsx b/client/src/app/[site]/pages/components/PageListItem.tsx
index e396e90e4..01d17216a 100644
--- a/client/src/app/[site]/pages/components/PageListItem.tsx
+++ b/client/src/app/[site]/pages/components/PageListItem.tsx
@@ -55,10 +55,17 @@ export function PageListItem({
// Past minutes data for sparklines
const { data: pastMinutesData, isLoading: isLoadingPastMinutes } =
useGetOverviewBucketedPastMinutes({
- pastMinutes: 24 * 60,
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
site,
bucket,
- dynamicFilters: [pageSpecificFilter],
+ dynamicFilters: [
+ {
+ parameter: "pathname",
+ type: "equals",
+ value: [pageData.value],
+ },
+ ],
props: {
enabled: isPast24HoursMode,
},
diff --git a/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx b/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx
index a803e6ad4..e9f401674 100644
--- a/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx
+++ b/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx
@@ -9,10 +9,11 @@ export function RealtimeChart() {
const { site } = useStore();
const { data, isLoading } = useGetOverviewBucketedPastMinutes({
- pastMinutes: 30,
+ pastMinutesStart: 30,
+ pastMinutesEnd: 0,
site,
bucket: "minute",
- refetchInterval: 5000,
+ refetchInterval: 10000,
});
const chartData = useMemo(() => {
diff --git a/client/src/components/SiteCard.tsx b/client/src/components/SiteCard.tsx
index b8b6c9c03..3f5f2b044 100644
--- a/client/src/components/SiteCard.tsx
+++ b/client/src/components/SiteCard.tsx
@@ -1,7 +1,7 @@
import { ArrowRight } from "lucide-react";
import Link from "next/link";
-import { useGetOverviewWithInView } from "../api/analytics/useGetOverviewWithInView";
-import { useGetOverviewBucketedWithInView } from "../api/analytics/useGetOverviewBucketedWithInView";
+import { useGetOverviewPastMinutes } from "../api/analytics/useGetOverview";
+import { useGetOverviewBucketedPastMinutes } from "../api/analytics/useGetOverviewBucketed";
import { useInView } from "../hooks/useInView";
import { Favicon } from "./Favicon";
import { SiteSessionChart } from "./SiteSessionChart";
@@ -26,21 +26,24 @@ export function SiteCard({ siteId, domain }: SiteCardProps) {
// Track if we've ever loaded data successfully
const hasLoadedData = useRef(false);
- const { data, isLoading, isSuccess } = useGetOverviewBucketedWithInView({
- pastMinutes: 24 * 60,
+ const { data, isLoading, isSuccess } = useGetOverviewBucketedPastMinutes({
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
site: siteId,
bucket: "hour",
- isInView,
+ props: {
+ enabled: isInView,
+ },
});
const {
data: overviewData,
isLoading: isOverviewLoading,
isSuccess: isOverviewSuccess,
- } = useGetOverviewWithInView({
- pastMinutes: 24 * 60,
+ } = useGetOverviewPastMinutes({
site: siteId,
- isInView,
+ pastMinutesStart: 24 * 60,
+ pastMinutesEnd: 0,
});
// Update the hasLoadedData ref when data loads successfully
diff --git a/server/src/api/analytics/getEventNames.ts b/server/src/api/analytics/getEventNames.ts
index b2487166a..144dcb456 100644
--- a/server/src/api/analytics/getEventNames.ts
+++ b/server/src/api/analytics/getEventNames.ts
@@ -21,7 +21,8 @@ export interface GetEventNamesRequest {
endDate?: string;
timeZone: string;
filters?: string;
- minutes?: string;
+ pastMinutesStart?: string;
+ pastMinutesEnd?: string;
};
}
@@ -29,16 +30,29 @@ export async function getEventNames(
req: FastifyRequest<GetEventNamesRequest>,
res: FastifyReply
) {
- const { startDate, endDate, timeZone, filters, minutes } = req.query;
+ const {
+ startDate,
+ endDate,
+ timeZone,
+ filters,
+ pastMinutesStart,
+ pastMinutesEnd,
+ } = req.query;
const site = req.params.site;
const userHasAccessToSite = await getUserHasAccessToSitePublic(req, site);
if (!userHasAccessToSite) {
return res.status(403).send({ error: "Forbidden" });
}
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
const timeStatement = getTimeStatement(
- minutes
- ? { pastMinutes: Number(minutes) }
+ pastMinutesRange
+ ? { pastMinutesRange }
: { date: { startDate, endDate, timeZone } }
);
diff --git a/server/src/api/analytics/getEventProperties.ts b/server/src/api/analytics/getEventProperties.ts
index c402c049e..d2a9ffe41 100644
--- a/server/src/api/analytics/getEventProperties.ts
+++ b/server/src/api/analytics/getEventProperties.ts
@@ -23,7 +23,8 @@ export interface GetEventPropertiesRequest {
timeZone: string;
eventName: string;
filters?: string;
- minutes?: string;
+ pastMinutesStart?: string;
+ pastMinutesEnd?: string;
};
}
@@ -31,8 +32,15 @@ export async function getEventProperties(
req: FastifyRequest<GetEventPropertiesRequest>,
res: FastifyReply
) {
- const { startDate, endDate, timeZone, eventName, filters, minutes } =
- req.query;
+ const {
+ startDate,
+ endDate,
+ timeZone,
+ eventName,
+ filters,
+ pastMinutesStart,
+ pastMinutesEnd,
+ } = req.query;
const site = req.params.site;
const userHasAccessToSite = await getUserHasAccessToSitePublic(req, site);
if (!userHasAccessToSite) {
@@ -43,9 +51,15 @@ export async function getEventProperties(
return res.status(400).send({ error: "Event name is required" });
}
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
const timeStatement = getTimeStatement(
- minutes
- ? { pastMinutes: Number(minutes) }
+ pastMinutesRange
+ ? { pastMinutesRange }
: { date: { startDate, endDate, timeZone } }
);
@@ -80,9 +94,8 @@ export async function getEventProperties(
},
});
- const data = await processResults<GetEventPropertiesResponse[number]>(
- result
- );
+ const data =
+ await processResults<GetEventPropertiesResponse[number]>(result);
return res.send({ data });
} catch (error) {
console.error("Generated Query:", query);
diff --git a/server/src/api/analytics/getGoals.ts b/server/src/api/analytics/getGoals.ts
index fc20533fa..b012c35cd 100644
--- a/server/src/api/analytics/getGoals.ts
+++ b/server/src/api/analytics/getGoals.ts
@@ -48,7 +48,6 @@ export async function getGoals(
pageSize?: string;
sort?: string;
order?: "asc" | "desc";
- minutes?: string;
pastMinutesStart?: string;
pastMinutesEnd?: string;
};
@@ -65,7 +64,6 @@ export async function getGoals(
pageSize = "10",
sort = "createdAt",
order = "desc",
- minutes,
pastMinutesStart,
pastMinutesEnd,
} = request.query;
@@ -165,11 +163,9 @@ export async function getGoals(
// Set up time parameters
const timeParams = pastMinutesRange
? { pastMinutesRange }
- : minutes
- ? { pastMinutes: Number(minutes) }
: startDate || endDate
- ? { date: { startDate, endDate, timeZone } }
- : {};
+ ? { date: { startDate, endDate, timeZone } }
+ : {};
const timeStatement = getTimeStatement(timeParams);
@@ -292,9 +288,8 @@ export async function getGoals(
format: "JSONEachRow",
});
- const conversionData = await processResults<Record<string, number>>(
- conversionResult
- );
+ const conversionData =
+ await processResults<Record<string, number>>(conversionResult);
// If we didn't get any results, use zeros
const conversions = conversionData[0] || {};
diff --git a/server/src/api/analytics/getOverview.ts b/server/src/api/analytics/getOverview.ts
index 5db8e9d0b..33d87bb64 100644
--- a/server/src/api/analytics/getOverview.ts
+++ b/server/src/api/analytics/getOverview.ts
@@ -22,21 +22,17 @@ const getQuery = ({
endDate,
timeZone,
filters,
- pastMinutes,
pastMinutesRange,
}: {
startDate: string;
endDate: string;
timeZone: string;
filters: string;
- pastMinutes?: number;
pastMinutesRange?: { start: number; end: number };
}) => {
const timeParams = pastMinutesRange
? { pastMinutesRange }
- : pastMinutes
- ? { pastMinutes }
- : { date: { startDate, endDate, timeZone } };
+ : { date: { startDate, endDate, timeZone } };
const filterStatement = getFilterStatement(filters);
@@ -96,7 +92,6 @@ export interface GenericRequest {
timeZone: string;
filters: string;
parameter: FilterParameter;
- pastMinutes?: number;
pastMinutesStart?: number;
pastMinutesEnd?: number;
limit?: number;
@@ -112,7 +107,6 @@ export async function getOverview(
endDate,
timeZone,
filters,
- pastMinutes,
pastMinutesStart,
pastMinutesEnd,
} = req.query;
@@ -132,7 +126,6 @@ export async function getOverview(
endDate,
timeZone,
filters,
- pastMinutes: Number(pastMinutes),
pastMinutesRange: pastMinutesRange,
});
diff --git a/server/src/api/analytics/getOverviewBucketed.ts b/server/src/api/analytics/getOverviewBucketed.ts
index 39a78b4a1..61bb4b888 100644
--- a/server/src/api/analytics/getOverviewBucketed.ts
+++ b/server/src/api/analytics/getOverviewBucketed.ts
@@ -36,17 +36,15 @@ const bucketIntervalMap = {
function getTimeStatementFill(
{
date,
- pastMinutes,
pastMinutesRange,
}: {
date?: { startDate: string; endDate: string; timeZone: string };
- pastMinutes?: number;
pastMinutesRange?: { start: number; end: number };
},
bucket: TimeBucket
) {
const { params, bucket: validatedBucket } = validateTimeStatementFillParams(
- { date, pastMinutes, pastMinutesRange },
+ { date, pastMinutesRange },
bucket
);
@@ -75,45 +73,25 @@ function getTimeStatementFill(
)
) STEP INTERVAL ${bucketIntervalMap[validatedBucket]}`;
}
- // For specific past minutes range
+ // For specific past minutes range - convert to exact timestamps for better performance
if (params.pastMinutesRange) {
const { start, end } = params.pastMinutesRange;
+
+ // Calculate exact timestamps in JavaScript to avoid runtime ClickHouse calculations
+ const now = new Date();
+ const startTimestamp = new Date(now.getTime() - start * 60 * 1000);
+ const endTimestamp = new Date(now.getTime() - end * 60 * 1000);
+
+ // Format as YYYY-MM-DD HH:MM:SS without milliseconds for ClickHouse
+ const startIso = startTimestamp
+ .toISOString()
+ .slice(0, 19)
+ .replace("T", " ");
+ const endIso = endTimestamp.toISOString().slice(0, 19).replace("T", " ");
+
return ` WITH FILL
- FROM ${
- TimeBucketToFn[validatedBucket]
- }(toDateTime(now() - INTERVAL ${SqlString.escape(start)} MINUTE))
- TO ${
- TimeBucketToFn[validatedBucket]
- }(toDateTime(now() - INTERVAL ${SqlString.escape(
- end
- )} MINUTE)) + INTERVAL 1 ${
- validatedBucket === "minute"
- ? "MINUTE"
- : validatedBucket === "five_minutes"
- ? "MINUTE"
- : validatedBucket === "ten_minutes"
- ? "MINUTE"
- : validatedBucket === "fifteen_minutes"
- ? "MINUTE"
- : validatedBucket === "month"
- ? "MONTH"
- : validatedBucket === "week"
- ? "WEEK"
- : validatedBucket === "day"
- ? "DAY"
- : "HOUR"
- }
- STEP INTERVAL ${bucketIntervalMap[validatedBucket]}`;
- }
- // For regular past minutes
- if (params.pastMinutes) {
- return ` WITH FILL
- FROM ${
- TimeBucketToFn[validatedBucket]
- }(toDateTime(now() - INTERVAL ${SqlString.escape(
- params.pastMinutes
- )} MINUTE))
- TO ${TimeBucketToFn[validatedBucket]}(toDateTime(now())) + INTERVAL 1 ${
+ FROM ${TimeBucketToFn[validatedBucket]}(toDateTime(${SqlString.escape(startIso)}))
+ TO ${TimeBucketToFn[validatedBucket]}(toDateTime(${SqlString.escape(endIso)})) + INTERVAL 1 ${
validatedBucket === "minute"
? "MINUTE"
: validatedBucket === "five_minutes"
@@ -141,7 +119,6 @@ const getQuery = ({
timeZone,
bucket,
filters,
- pastMinutes,
pastMinutesRange,
}: {
startDate: string;
@@ -149,18 +126,15 @@ const getQuery = ({
timeZone: string;
bucket: TimeBucket;
filters: string;
- pastMinutes?: number;
pastMinutesRange?: { start: number; end: number };
}) => {
const filterStatement = getFilterStatement(filters);
- const isAllTime = !startDate && !endDate && !pastMinutes && !pastMinutesRange;
+ const isAllTime = !startDate && !endDate && !pastMinutesRange;
const timeParams = pastMinutesRange
? { pastMinutesRange }
- : pastMinutes
- ? { pastMinutes }
- : { date: { startDate, endDate, timeZone } };
+ : { date: { startDate, endDate, timeZone } };
const query = `
SELECT
@@ -249,7 +223,6 @@ export async function getOverviewBucketed(
timeZone: string;
bucket: TimeBucket;
filters: string;
- pastMinutes?: number;
pastMinutesStart?: number;
pastMinutesEnd?: number;
};
@@ -262,7 +235,6 @@ export async function getOverviewBucketed(
timeZone,
bucket,
filters,
- pastMinutes,
pastMinutesStart,
pastMinutesEnd,
} = req.query;
@@ -285,7 +257,6 @@ export async function getOverviewBucketed(
timeZone,
bucket,
filters,
- pastMinutes: pastMinutes ? Number(pastMinutes) : undefined,
pastMinutesRange,
});
diff --git a/server/src/api/analytics/getPageTitles.ts b/server/src/api/analytics/getPageTitles.ts
index e2347f6cb..77a11fd03 100644
--- a/server/src/api/analytics/getPageTitles.ts
+++ b/server/src/api/analytics/getPageTitles.ts
@@ -14,7 +14,8 @@ interface GetPageTitlesRequest {
Querystring: {
startDate: string;
endDate: string;
- minutes: number;
+ pastMinutesStart?: number;
+ pastMinutesEnd?: number;
timeZone: string;
filters: string;
limit?: number;
@@ -40,13 +41,28 @@ const getPageTitlesQuery = (
request: FastifyRequest<GetPageTitlesRequest>,
isCountQuery: boolean = false
) => {
- const { startDate, endDate, timeZone, filters, limit, page, minutes } =
- request.query;
+ const {
+ startDate,
+ endDate,
+ timeZone,
+ filters,
+ limit,
+ page,
+ pastMinutesStart,
+ pastMinutesEnd,
+ } = request.query;
const filterStatement = getFilterStatement(filters);
+
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
const timeStatement = getTimeStatement(
- minutes
- ? { pastMinutes: Number(minutes) }
+ pastMinutesRange
+ ? { pastMinutesRange }
: {
date: { startDate, endDate, timeZone },
}
diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.ts
index a2f630a8a..d7635c48d 100644
--- a/server/src/api/analytics/getSessions.ts
+++ b/server/src/api/analytics/getSessions.ts
@@ -44,7 +44,6 @@ export interface GetSessionsRequest {
filters: string;
page: number;
userId?: string;
- minutes?: string;
pastMinutesStart?: string;
pastMinutesEnd?: string;
};
@@ -61,7 +60,6 @@ export async function getSessions(
filters,
page,
userId,
- minutes,
pastMinutesStart,
pastMinutesEnd,
} = req.query;
@@ -82,11 +80,9 @@ export async function getSessions(
// Set up time parameters
const timeParams = pastMinutesRange
? { pastMinutesRange }
- : minutes
- ? { pastMinutes: Number(minutes) }
: startDate && endDate
- ? { date: { startDate, endDate, timeZone } }
- : {};
+ ? { date: { startDate, endDate, timeZone } }
+ : {};
const timeStatement = getTimeStatement(timeParams);
diff --git a/server/src/api/analytics/getSingleCol.ts b/server/src/api/analytics/getSingleCol.ts
index 5df1d16ba..fe5d1adea 100644
--- a/server/src/api/analytics/getSingleCol.ts
+++ b/server/src/api/analytics/getSingleCol.ts
@@ -16,7 +16,8 @@ interface GetSingleColRequest {
Querystring: {
startDate: string;
endDate: string;
- minutes: number;
+ pastMinutesStart?: number;
+ pastMinutesEnd?: number;
timeZone: string;
filters: string;
parameter: FilterParameter;
@@ -68,13 +69,21 @@ const getQuery = (
parameter,
limit,
page,
- minutes,
+ pastMinutesStart,
+ pastMinutesEnd,
} = request.query;
const filterStatement = getFilterStatement(filters);
+
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
const timeStatement = getTimeStatement(
- minutes
- ? { pastMinutes: Number(minutes) }
+ pastMinutesRange
+ ? { pastMinutesRange }
: {
date: { startDate, endDate, timeZone },
}
diff --git a/server/src/api/analytics/getUsers.ts b/server/src/api/analytics/getUsers.ts
index cd421ccff..7ebd09ca4 100644
--- a/server/src/api/analytics/getUsers.ts
+++ b/server/src/api/analytics/getUsers.ts
@@ -36,7 +36,6 @@ export interface GetUsersRequest {
pageSize?: string;
sortBy?: string;
sortOrder?: string;
- minutes?: string;
pastMinutesStart?: string;
pastMinutesEnd?: string;
};
@@ -55,7 +54,6 @@ export async function getUsers(
pageSize = "20",
sortBy = "last_seen",
sortOrder = "desc",
- minutes,
pastMinutesStart,
pastMinutesEnd,
} = req.query;
@@ -90,11 +88,9 @@ export async function getUsers(
// Set up time parameters
const timeParams = pastMinutesRange
? { pastMinutesRange }
- : minutes
- ? { pastMinutes: Number(minutes) }
: startDate && endDate
- ? { date: { startDate, endDate, timeZone } }
- : {};
+ ? { date: { startDate, endDate, timeZone } }
+ : {};
// Generate filter statement and time statement
const filterStatement = getFilterStatement(filters);
diff --git a/server/src/api/analytics/query-validation.ts b/server/src/api/analytics/query-validation.ts
index 5296ebd68..816d5c920 100644
--- a/server/src/api/analytics/query-validation.ts
+++ b/server/src/api/analytics/query-validation.ts
@@ -88,12 +88,11 @@ const fillDateParamsSchema = z.object({
/**
* Schema for parameters to getTimeStatement() function
- * Either date, pastMinutes, or pastMinutesRange must be provided
+ * Either date or pastMinutesRange must be provided
*/
const timeStatementParamsSchema = z
.object({
date: fillDateParamsSchema.optional(),
- pastMinutes: z.number().nonnegative().optional(),
pastMinutesRange: z
.object({
start: z.number().nonnegative(),
@@ -105,29 +104,24 @@ const timeStatementParamsSchema = z
}),
})
.refine(
- (data) =>
- data.date !== undefined ||
- data.pastMinutes !== undefined ||
- data.pastMinutesRange !== undefined,
+ (data) => data.date !== undefined || data.pastMinutesRange !== undefined,
{
- message: "Either date, pastMinutes, or pastMinutesRange must be provided",
+ message: "Either date or pastMinutesRange must be provided",
}
)
// Set default empty objects if schema validation fails
.catch({
date: undefined,
- pastMinutes: undefined,
pastMinutesRange: undefined,
});
/**
* Schema for parameters to getTimeStatementFill() function
- * Either date, pastMinutes, or pastMinutesRange must be provided
+ * Either date or pastMinutesRange must be provided
*/
const timeStatementFillParamsSchema = z
.object({
date: fillDateParamsSchema.optional(),
- pastMinutes: z.number().nonnegative().optional(),
pastMinutesRange: z
.object({
start: z.number().nonnegative(),
@@ -139,12 +133,9 @@ const timeStatementFillParamsSchema = z
}),
})
.refine(
- (data) =>
- data.date !== undefined ||
- data.pastMinutes !== undefined ||
- data.pastMinutesRange !== undefined,
+ (data) => data.date !== undefined || data.pastMinutesRange !== undefined,
{
- message: "Either date, pastMinutes, or pastMinutesRange must be provided",
+ message: "Either date or pastMinutesRange must be provided",
}
);
diff --git a/server/src/api/analytics/utils.ts b/server/src/api/analytics/utils.ts
index e45930298..cddea3c54 100644
--- a/server/src/api/analytics/utils.ts
+++ b/server/src/api/analytics/utils.ts
@@ -9,8 +9,6 @@ import SqlString from "sqlstring";
export function getTimeStatement({
date,
- pastMinutes,
- minutes,
pastMinutesRange,
}: {
date?: {
@@ -19,17 +17,11 @@ export function getTimeStatement({
timeZone?: string;
table?: "events" | "sessions";
};
- pastMinutes?: number;
- minutes?: number; // Alternative name for pastMinutes for compatibility
pastMinutesRange?: { start: number; end: number };
}) {
- // For backward compatibility, support both minutes and pastMinutes
- const actualPastMinutes = pastMinutes || minutes;
-
// Sanitize inputs with Zod
const sanitized = validateTimeStatementParams({
date,
- pastMinutes: actualPastMinutes,
pastMinutesRange,
});
@@ -48,8 +40,8 @@ export function getTimeStatement({
)
AND timestamp < if(
toDate(${SqlString.escape(endDate)}) = toDate(now(), ${SqlString.escape(
- timeZone
- )}),
+ timeZone
+ )}),
now(),
toTimeZone(
toStartOfDay(toDateTime(${SqlString.escape(
@@ -60,22 +52,23 @@ export function getTimeStatement({
)`;
}
- // Handle specific range of past minutes
+ // Handle specific range of past minutes - convert to exact timestamps for better performance
if (sanitized.pastMinutesRange) {
const { start, end } = sanitized.pastMinutesRange;
- return `AND timestamp > now() - interval ${SqlString.escape(
- start
- )} minute AND timestamp <= now() - interval ${SqlString.escape(
- end
- )} minute`;
- }
- // Handle standard past minutes
- if (sanitized.pastMinutes) {
- // Use SqlString.escape for pastMinutes (it handles numbers)
- return `AND timestamp > now() - interval ${SqlString.escape(
- sanitized.pastMinutes
- )} minute`;
+ // Calculate exact timestamps in JavaScript to avoid runtime ClickHouse calculations
+ const now = new Date();
+ const startTimestamp = new Date(now.getTime() - start * 60 * 1000);
+ const endTimestamp = new Date(now.getTime() - end * 60 * 1000);
+
+ // Format as YYYY-MM-DD HH:MM:SS without milliseconds for ClickHouse
+ const startIso = startTimestamp
+ .toISOString()
+ .slice(0, 19)
+ .replace("T", " ");
+ const endIso = endTimestamp.toISOString().slice(0, 19).replace("T", " ");
+
+ return `AND timestamp > toDateTime(${SqlString.escape(startIso)}) AND timestamp <= toDateTime(${SqlString.escape(endIso)})`;
}
// If no valid time parameters were provided, return empty string