🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
feat(analytics): enhance useGetOverviewPastMinutes with start and end time parameters
Bill Yang113915a1bc8f
diff --git a/client/src/api/analytics/useGetOverview.ts b/client/src/api/analytics/useGetOverview.tsindex 6c6d85236..8000a3059 100644--- a/client/src/api/analytics/useGetOverview.ts+++ b/client/src/api/analytics/useGetOverview.ts@@ -52,17 +52,29 @@ export function useGetOverview({export function useGetOverviewPastMinutes({pastMinutes,+ pastMinutesStart,+ pastMinutesEnd,site,}: {- pastMinutes: number;+ pastMinutes?: number;+ pastMinutesStart?: number;+ pastMinutesEnd?: number;site?: number | string;}) {return useQuery({- queryKey: ["overview-past-minutes", pastMinutes, site],+ queryKey: [+ "overview-past-minutes",+ pastMinutes,+ pastMinutesStart,+ pastMinutesEnd,+ site,+ ],queryFn: () => {return authedFetch(`${BACKEND_URL}/overview/${site}`, {pastMinutes,timeZone,+ pastMinutesStart,+ pastMinutesEnd,}).then((res) => res.json());},staleTime: Infinity,diff --git a/client/src/app/[site]/main/components/MainSection/Overview.tsx b/client/src/app/[site]/main/components/MainSection/Overview.tsxindex 461ca6437..aa3cb5737 100644--- a/client/src/app/[site]/main/components/MainSection/Overview.tsx+++ b/client/src/app/[site]/main/components/MainSection/Overview.tsx@@ -208,7 +208,8 @@ export function Overview() {isLoading: isPastMinutesOverviewLoadingPrevious,} = useGetOverviewPastMinutes({site,- pastMinutes: 48 * 60, // Use 48 hours instead of a isPrevious flag+ pastMinutesStart: 48 * 60,+ pastMinutesEnd: 24 * 60,});// Combine the data based on the modediff --git a/server/src/api/analytics/getOverview.ts b/server/src/api/analytics/getOverview.tsindex 613091714..5db8e9d0b 100644--- a/server/src/api/analytics/getOverview.ts+++ b/server/src/api/analytics/getOverview.ts@@ -21,17 +21,23 @@ const getQuery = ({startDate,endDate,timeZone,- site,filters,pastMinutes,+ pastMinutesRange,}: {startDate: string;endDate: string;timeZone: string;- site: string;filters: string;- pastMinutes: number;+ pastMinutes?: number;+ pastMinutesRange?: { start: number; end: number };}) => {+ const timeParams = pastMinutesRange+ ? { pastMinutesRange }+ : pastMinutes+ ? { pastMinutes }+ : { date: { startDate, endDate, timeZone } };+const filterStatement = getFilterStatement(filters);return `SELECT@@ -61,13 +67,7 @@ const getQuery = ({WHEREsite_id = {siteId:Int32}${filterStatement}- ${getTimeStatement(- pastMinutes- ? { pastMinutes }- : {- date: { startDate, endDate, timeZone },- }- )}+ ${getTimeStatement(timeParams)}GROUP BY session_id)) AS session_stats@@ -81,13 +81,7 @@ const getQuery = ({WHEREsite_id = {siteId:Int32}${filterStatement}- ${getTimeStatement(- pastMinutes- ? { pastMinutes }- : {- date: { startDate, endDate, timeZone },- }- )}+ ${getTimeStatement(timeParams)}AND type = 'pageview') AS page_stats`;};@@ -103,6 +97,8 @@ export interface GenericRequest {filters: string;parameter: FilterParameter;pastMinutes?: number;+ pastMinutesStart?: number;+ pastMinutesEnd?: number;limit?: number;};}@@ -111,20 +107,33 @@ export async function getOverview(req: FastifyRequest<GenericRequest>,res: FastifyReply) {- const { startDate, endDate, timeZone, filters, pastMinutes } = req.query;+ const {+ startDate,+ endDate,+ timeZone,+ filters,+ pastMinutes,+ pastMinutesStart,+ pastMinutesEnd,+ } = req.query;const site = req.params.site;const userHasAccessToSite = await getUserHasAccessToSitePublic(req, site);if (!userHasAccessToSite) {return res.status(403).send({ error: "Forbidden" });}+ const pastMinutesRange =+ pastMinutesStart && pastMinutesEnd+ ? { start: Number(pastMinutesStart), end: Number(pastMinutesEnd) }+ : undefined;+const query = getQuery({startDate,endDate,timeZone,- site,filters,pastMinutes: Number(pastMinutes),+ pastMinutesRange: pastMinutesRange,});try {diff --git a/server/src/api/analytics/getOverviewBucketed.ts b/server/src/api/analytics/getOverviewBucketed.tsindex e2eb6c99c..39a78b4a1 100644--- a/server/src/api/analytics/getOverviewBucketed.ts+++ b/server/src/api/analytics/getOverviewBucketed.ts@@ -56,21 +56,21 @@ function getTimeStatementFill(toDateTime(${TimeBucketToFn[validatedBucket]}(toDateTime(${SqlString.escape(startDate)}, ${SqlString.escape(- timeZone- )}))),+ timeZone+ )}))),'UTC')TO if(toDate(${SqlString.escape(endDate)}) = toDate(now(), ${SqlString.escape(- timeZone- )}),+ timeZone+ )}),now(),toTimeZone(toDateTime(${TimeBucketToFn[validatedBucket]}(toDateTime(${SqlString.escape(endDate)}, ${SqlString.escape(- timeZone- )}))) + INTERVAL 1 DAY,+ timeZone+ )}))) + INTERVAL 1 DAY,'UTC')) STEP INTERVAL ${bucketIntervalMap[validatedBucket]}`;@@ -85,24 +85,24 @@ function getTimeStatementFill(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"- }+ 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@@ -111,25 +111,25 @@ function getTimeStatementFill(FROM ${TimeBucketToFn[validatedBucket]}(toDateTime(now() - INTERVAL ${SqlString.escape(- params.pastMinutes- )} MINUTE))+ params.pastMinutes+ )} MINUTE))TO ${TimeBucketToFn[validatedBucket]}(toDateTime(now())) + 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"- }+ 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]}`;}return "";@@ -140,7 +140,6 @@ const getQuery = ({endDate,timeZone,bucket,- site,filters,pastMinutes,pastMinutesRange,@@ -149,7 +148,6 @@ const getQuery = ({endDate: string;timeZone: string;bucket: TimeBucket;- site: string;filters: string;pastMinutes?: number;pastMinutesRange?: { start: number; end: number };@@ -161,8 +159,8 @@ const getQuery = ({const timeParams = pastMinutesRange? { pastMinutesRange }: pastMinutes- ? { pastMinutes }- : { date: { startDate, endDate, timeZone } };+ ? { pastMinutes }+ : { date: { startDate, endDate, timeZone } };const query = `SELECT@@ -286,7 +284,6 @@ export async function getOverviewBucketed(endDate,timeZone,bucket,- site,filters,pastMinutes: pastMinutes ? Number(pastMinutes) : undefined,pastMinutesRange,