Skip to content

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

Revert "refactor: Standardize time parameter handling across analytics APIs"

Bill Yang8d106b868cf8
diff --git a/server/src/api/analytics/getEventNames.ts b/server/src/api/analytics/getEventNames.ts
index 8c4769eda..1bd55b71c 100644
--- a/server/src/api/analytics/getEventNames.ts
+++ b/server/src/api/analytics/getEventNames.ts
@@ -36,12 +36,11 @@ export async function getEventNames(
return res.status(403).send({ error: "Forbidden" });
}
- const timeStatement = getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes,
- });
+ const timeStatement = getTimeStatement(
+ minutes
+ ? { pastMinutes: Number(minutes) }
+ : { date: { startDate, endDate, timezone } }
+ );
const filterStatement = filters ? getFilterStatement(filters) : "";
diff --git a/server/src/api/analytics/getEventProperties.ts b/server/src/api/analytics/getEventProperties.ts
index df6d323a3..72f0adeab 100644
--- a/server/src/api/analytics/getEventProperties.ts
+++ b/server/src/api/analytics/getEventProperties.ts
@@ -43,12 +43,11 @@ export async function getEventProperties(
return res.status(400).send({ error: "Event name is required" });
}
- const timeStatement = getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes,
- });
+ const timeStatement = getTimeStatement(
+ minutes
+ ? { pastMinutes: Number(minutes) }
+ : { date: { startDate, endDate, timezone } }
+ );
const filterStatement = filters ? getFilterStatement(filters) : "";
diff --git a/server/src/api/analytics/getEvents.ts b/server/src/api/analytics/getEvents.ts
index f0c5080ea..dea4611b0 100644
--- a/server/src/api/analytics/getEvents.ts
+++ b/server/src/api/analytics/getEvents.ts
@@ -66,7 +66,7 @@ export async function getEvents(
// Get time and filter statements if parameters are provided
const timeStatement =
startDate || endDate
- ? getTimeStatement({ startDate, endDate, timezone })
+ ? getTimeStatement({ date: { startDate, endDate, timezone } })
: "AND timestamp > now() - INTERVAL 30 MINUTE"; // Default to last 30 minutes if no time range specified
const filterStatement = filters ? getFilterStatement(filters) : "";
diff --git a/server/src/api/analytics/getGoals.ts b/server/src/api/analytics/getGoals.ts
index 34e017288..13323c652 100644
--- a/server/src/api/analytics/getGoals.ts
+++ b/server/src/api/analytics/getGoals.ts
@@ -155,14 +155,23 @@ export async function getGoals(
// Build filter and time clauses for ClickHouse queries
const filterStatement = filters ? getFilterStatement(filters) : "";
- const timeStatement = getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes,
- pastMinutesStart,
- pastMinutesEnd,
- });
+
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
+ // Set up time parameters
+ const timeParams = pastMinutesRange
+ ? { pastMinutesRange }
+ : minutes
+ ? { pastMinutes: Number(minutes) }
+ : startDate || endDate
+ ? { date: { startDate, endDate, timezone } }
+ : {};
+
+ const timeStatement = getTimeStatement(timeParams);
// First, get the total number of unique sessions (denominator for conversion rate)
const totalSessionsQuery = `
diff --git a/server/src/api/analytics/getOverview.ts b/server/src/api/analytics/getOverview.ts
index d0f2c8e93..58fa112a4 100644
--- a/server/src/api/analytics/getOverview.ts
+++ b/server/src/api/analytics/getOverview.ts
@@ -34,8 +34,7 @@ const getQuery = ({
}) => {
const filterStatement = getFilterStatement(filters);
- return `
- SELECT
+ return `SELECT
session_stats.sessions,
session_stats.pages_per_session,
session_stats.bounce_rate * 100 AS bounce_rate,
@@ -62,12 +61,13 @@ const getQuery = ({
WHERE
site_id = {siteId:Int32}
${filterStatement}
- ${getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes: pastMinutes,
- })}
+ ${getTimeStatement(
+ pastMinutes
+ ? { pastMinutes }
+ : {
+ date: { startDate, endDate, timezone },
+ }
+ )}
GROUP BY session_id
)
) AS session_stats
@@ -81,12 +81,13 @@ const getQuery = ({
WHERE
site_id = {siteId:Int32}
${filterStatement}
- ${getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes: pastMinutes,
- })}
+ ${getTimeStatement(
+ pastMinutes
+ ? { pastMinutes }
+ : {
+ date: { startDate, endDate, timezone },
+ }
+ )}
AND type = 'pageview'
) AS page_stats`;
};
diff --git a/server/src/api/analytics/getOverviewBucketed.ts b/server/src/api/analytics/getOverviewBucketed.ts
index c623495d8..9bc45cffa 100644
--- a/server/src/api/analytics/getOverviewBucketed.ts
+++ b/server/src/api/analytics/getOverviewBucketed.ts
@@ -101,8 +101,7 @@ const getQuery = ({
site,
filters,
pastMinutes,
- pastMinutesStart,
- pastMinutesEnd,
+ pastMinutesRange,
}: {
startDate: string;
endDate: string;
@@ -111,20 +110,14 @@ const getQuery = ({
site: string;
filters: string;
pastMinutes?: number;
- pastMinutesStart?: number;
- pastMinutesEnd?: number;
+ pastMinutesRange?: { start: number; end: number };
}) => {
const filterStatement = getFilterStatement(filters);
const isAllTime = !startDate && !endDate;
- const timeParams = pastMinutesEnd
- ? {
- pastMinutesRange: {
- start: Number(pastMinutesStart),
- end: Number(pastMinutesEnd),
- },
- }
+ const timeParams = pastMinutesRange
+ ? { pastMinutesRange }
: pastMinutes
? { pastMinutes }
: { date: { startDate, endDate, timezone } };
@@ -231,6 +224,12 @@ export async function getOverviewBucketed(
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 query = getQuery({
startDate,
endDate,
@@ -238,9 +237,8 @@ export async function getOverviewBucketed(
bucket,
site,
filters,
- pastMinutes,
- pastMinutesStart,
- pastMinutesEnd,
+ pastMinutes: pastMinutes ? Number(pastMinutes) : undefined,
+ pastMinutesRange,
});
try {
diff --git a/server/src/api/analytics/getSessions.ts b/server/src/api/analytics/getSessions.ts
index 06855ea4f..9cea88781 100644
--- a/server/src/api/analytics/getSessions.ts
+++ b/server/src/api/analytics/getSessions.ts
@@ -72,14 +72,23 @@ export async function getSessions(
}
const filterStatement = getFilterStatement(filters);
- const timeStatement = getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes,
- pastMinutesStart,
- pastMinutesEnd,
- });
+
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
+ // Set up time parameters
+ const timeParams = pastMinutesRange
+ ? { pastMinutesRange }
+ : minutes
+ ? { pastMinutes: Number(minutes) }
+ : startDate && endDate
+ ? { date: { startDate, endDate, timezone } }
+ : {};
+
+ const timeStatement = getTimeStatement(timeParams);
const query = `
WITH AggregatedSessions AS (
diff --git a/server/src/api/analytics/getSingleCol.ts b/server/src/api/analytics/getSingleCol.ts
index 17401f3b3..7ecaefc85 100644
--- a/server/src/api/analytics/getSingleCol.ts
+++ b/server/src/api/analytics/getSingleCol.ts
@@ -40,12 +40,13 @@ const getQuery = (request: FastifyRequest<GenericRequest>) => {
request.query;
const filterStatement = getFilterStatement(filters);
- const timeStatement = getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes,
- });
+ const timeStatement = getTimeStatement(
+ minutes
+ ? { pastMinutes: Number(minutes) }
+ : {
+ date: { startDate, endDate, timezone },
+ }
+ );
// Validate and sanitize the limit parameter
let validatedLimit: number | null = null;
diff --git a/server/src/api/analytics/getUsers.ts b/server/src/api/analytics/getUsers.ts
index 29b3acfb4..7766f54b6 100644
--- a/server/src/api/analytics/getUsers.ts
+++ b/server/src/api/analytics/getUsers.ts
@@ -81,16 +81,24 @@ export async function getUsers(
const actualSortBy = validSortFields.includes(sortBy) ? sortBy : "last_seen";
const actualSortOrder = sortOrder === "asc" ? "ASC" : "DESC";
+ // Handle specific past minutes range if provided
+ const pastMinutesRange =
+ pastMinutesStart && pastMinutesEnd
+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }
+ : undefined;
+
+ // Set up time parameters
+ const timeParams = pastMinutesRange
+ ? { pastMinutesRange }
+ : minutes
+ ? { pastMinutes: Number(minutes) }
+ : startDate && endDate
+ ? { date: { startDate, endDate, timezone } }
+ : {};
+
// Generate filter statement and time statement
const filterStatement = getFilterStatement(filters);
- const timeStatement = getTimeStatement({
- startDate,
- endDate,
- timezone,
- minutes,
- pastMinutesStart,
- pastMinutesEnd,
- });
+ const timeStatement = getTimeStatement(timeParams);
const query = `
WITH AggregatedUsers AS (
diff --git a/server/src/api/analytics/utils.ts b/server/src/api/analytics/utils.ts
index bf045bcf3..fc8c23ea7 100644
--- a/server/src/api/analytics/utils.ts
+++ b/server/src/api/analytics/utils.ts
@@ -7,62 +7,31 @@ import {
} from "./query-validation.js";
import SqlString from "sqlstring";
-export function getTimeStatement(
- params:
- | {
- date?: {
- startDate?: string;
- endDate?: string;
- timezone?: string;
- table?: "events" | "sessions";
- };
- pastMinutes?: number;
- pastMinutesRange?: { start: number; end: number };
- }
- | {
- startDate?: string;
- endDate?: string;
- timezone?: string;
- minutes?: string | number;
- pastMinutesStart?: string | number;
- pastMinutesEnd?: string | number;
- }
-) {
- // Handle if raw query parameters are passed
- if ("pastMinutesStart" in params || "minutes" in params) {
- // Extract and normalize raw parameters
- const {
- startDate,
- endDate,
- timezone,
- minutes,
- pastMinutesStart,
- pastMinutesEnd,
- } = params;
-
- // Convert to the internal format
- const pastMinutesRange =
- pastMinutesStart && pastMinutesEnd
- ? {
- start: Number(pastMinutesStart),
- end: Number(pastMinutesEnd),
- }
- : undefined;
+export function getTimeStatement({
+ date,
+ pastMinutes,
+ minutes,
+ pastMinutesRange,
+}: {
+ date?: {
+ startDate?: string;
+ endDate?: string;
+ 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;
- const normalizedParams = pastMinutesRange
- ? { pastMinutesRange }
- : minutes
- ? { pastMinutes: Number(minutes) }
- : startDate && endDate
- ? { date: { startDate, endDate, timezone } }
- : {};
-
- // Call self with normalized parameters
- return getTimeStatement(normalizedParams);
- }
-
- // Original function implementation with sanitized parameters
- const sanitized = validateTimeStatementParams(params);
+ // Sanitize inputs with Zod
+ const sanitized = validateTimeStatementParams({
+ date,
+ pastMinutes: actualPastMinutes,
+ pastMinutesRange,
+ });
if (sanitized.date) {
const { startDate, endDate, timezone } = sanitized.date;