Skip to content

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

Add all time option

Bill Yangd0d7585dff04
diff --git a/client/src/app/[site]/components/Header/DateSelector.tsx b/client/src/app/[site]/components/Header/DateSelector.tsx
index d9d4a2724..a1e0b20f3 100644
--- a/client/src/app/[site]/components/Header/DateSelector.tsx
+++ b/client/src/app/[site]/components/Header/DateSelector.tsx
@@ -60,6 +60,9 @@ const getLabel = (time: Time) => {
}
return time.year;
}
+ if (time.mode === "all-time") {
+ return "All Time";
+ }
};
export function DateSelector() {
@@ -173,8 +176,7 @@ export function DateSelector() {
<DropdownMenuItem
onClick={() =>
setTime({
- mode: "year",
- year: DateTime.now().startOf("year").toISODate(),
+ mode: "all-time",
})
}
>
diff --git a/client/src/app/[site]/components/MainSection/BucketSelection.tsx b/client/src/app/[site]/components/MainSection/BucketSelection.tsx
index eab8a3f06..8414b1569 100644
--- a/client/src/app/[site]/components/MainSection/BucketSelection.tsx
+++ b/client/src/app/[site]/components/MainSection/BucketSelection.tsx
@@ -44,6 +44,22 @@ const getOptions = (time: Time) => {
</SelectContent>
);
}
+ if (time.mode === "year" || time.mode === "all-time") {
+ return (
+ <SelectContent>
+ <SelectItem size="sm" value="day">
+ Day
+ </SelectItem>
+ <SelectItem size="sm" value="week">
+ Week
+ </SelectItem>
+ <SelectItem size="sm" value="month">
+ Month
+ </SelectItem>
+ </SelectContent>
+ );
+ }
+
if (time.mode === "range") {
const timeRangeLength = DateTime.fromISO(time.endDate).diff(
DateTime.fromISO(time.startDate),
diff --git a/client/src/app/[site]/components/MainSection/MainSection.tsx b/client/src/app/[site]/components/MainSection/MainSection.tsx
index 025637850..171354800 100644
--- a/client/src/app/[site]/components/MainSection/MainSection.tsx
+++ b/client/src/app/[site]/components/MainSection/MainSection.tsx
@@ -8,7 +8,7 @@ import { PreviousChart } from "./PreviousChart";
import { useStore } from "../../../../lib/store";
export function MainSection() {
- const { selectedStat } = useStore();
+ const { selectedStat, time } = useStore();
const { data, isFetching, error } = useGetOverviewBucketed();
const {
@@ -43,7 +43,7 @@ export function MainSection() {
<Chart
data={data}
max={maxOfDataAndPreviousData}
- previousData={previousData}
+ previousData={time.mode === "all-time" ? undefined : previousData}
/>
</div>
</div>
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index cde88153c..8ee2d4ee7 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -1,8 +1,4 @@
-import {
- keepPreviousData,
- useQuery,
- UseQueryResult,
-} from "@tanstack/react-query";
+import { useQuery, UseQueryResult } from "@tanstack/react-query";
import { BACKEND_URL } from "../lib/const";
import { useStore } from "../lib/store";
import { authedFetch, getStartAndEndDate } from "./utils";
@@ -38,7 +34,11 @@ export function useGenericQuery<T>(
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return authedFetch(
- `${BACKEND_URL}/${endpoint}?startDate=${startDate}&endDate=${endDate}&timezone=${timezone}&site=${site}`
+ `${BACKEND_URL}/${endpoint}?${
+ startDate ? `startDate=${startDate}&` : ""
+ }${
+ endDate ? `endDate=${endDate}&` : ""
+ }timezone=${timezone}&site=${site}`
).then((res) => res.json());
},
staleTime: Infinity,
@@ -138,7 +138,11 @@ export function useGetOverviewBucketed(
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return authedFetch(
- `${BACKEND_URL}/overview-bucketed?startDate=${startDate}&endDate=${endDate}&timezone=${timezone}&bucket=${bucket}&site=${site}`
+ `${BACKEND_URL}/overview-bucketed?${
+ startDate ? `startDate=${startDate}&` : ""
+ }${
+ endDate ? `endDate=${endDate}&` : ""
+ }timezone=${timezone}&bucket=${bucket}&site=${site}`
).then((res) => res.json());
},
placeholderData: (_, query: any) => {
diff --git a/client/src/hooks/utils.ts b/client/src/hooks/utils.ts
index e7279bcc1..20825a05a 100644
--- a/client/src/hooks/utils.ts
+++ b/client/src/hooks/utils.ts
@@ -23,6 +23,9 @@ export function getStartAndEndDate(time: Time) {
endDate: DateTime.fromISO(time.year).endOf("year").toISODate(),
};
}
+ if (time.mode === "all-time") {
+ return { startDate: null, endDate: null };
+ }
return { startDate: time.day, endDate: time.day };
}
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 450ef7437..7209a6bdd 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -33,7 +33,17 @@ type YearMode = {
year: string;
};
-export type Time = DateMode | DateRangeMode | WeekMode | MonthMode | YearMode;
+type AllTimeMode = {
+ mode: "all-time";
+};
+
+export type Time =
+ | DateMode
+ | DateRangeMode
+ | WeekMode
+ | MonthMode
+ | YearMode
+ | AllTimeMode;
export type TimeBucket = "hour" | "day" | "week" | "month" | "year";
@@ -134,6 +144,11 @@ export const useStore = create<Store>((set) => ({
mode: "year",
year: DateTime.fromISO(time.year).minus({ years: 1 }).toISODate() ?? "",
};
+ } else if (time.mode === "all-time") {
+ bucketToUse = "day";
+ previousTime = {
+ mode: "all-time",
+ };
} else {
previousTime = time; // fallback case
}
diff --git a/server/src/api/getBrowsers.ts b/server/src/api/getBrowsers.ts
index 8f3a32957..4c4413d8d 100644
--- a/server/src/api/getBrowsers.ts
+++ b/server/src/api/getBrowsers.ts
@@ -22,8 +22,8 @@ export async function getBrowsers(
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY browser ORDER BY count desc;
`;
diff --git a/server/src/api/getCountries.ts b/server/src/api/getCountries.ts
index 37b9283f2..dcb2d9ff9 100644
--- a/server/src/api/getCountries.ts
+++ b/server/src/api/getCountries.ts
@@ -22,8 +22,8 @@ export async function getCountries(
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY country ORDER BY count desc;
`;
diff --git a/server/src/api/getDevices.ts b/server/src/api/getDevices.ts
index 922a7d06b..3ee89cf02 100644
--- a/server/src/api/getDevices.ts
+++ b/server/src/api/getDevices.ts
@@ -22,8 +22,8 @@ export async function getDevices(
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY device_type ORDER BY count desc;
`;
diff --git a/server/src/api/getOperatingSystems.ts b/server/src/api/getOperatingSystems.ts
index e773c66db..2cc28f4d5 100644
--- a/server/src/api/getOperatingSystems.ts
+++ b/server/src/api/getOperatingSystems.ts
@@ -22,8 +22,8 @@ export async function getOperatingSystems(
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY operating_system ORDER BY count desc;
`;
diff --git a/server/src/api/getOverview.ts b/server/src/api/getOverview.ts
index 99c002a21..78bee1514 100644
--- a/server/src/api/getOverview.ts
+++ b/server/src/api/getOverview.ts
@@ -43,8 +43,8 @@ export async function getOverview(
COUNT(*) AS pages_in_session
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY session_id
)
) AS session_stats
@@ -56,13 +56,10 @@ export async function getOverview(
COUNT(DISTINCT user_id) AS users
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
) AS page_stats`;
- console.info;
- console.info(query);
-
try {
const result = await clickhouse.query({
query,
diff --git a/server/src/api/getOverviewBucketed.ts b/server/src/api/getOverviewBucketed.ts
index ce771b883..6d51a45e1 100644
--- a/server/src/api/getOverviewBucketed.ts
+++ b/server/src/api/getOverviewBucketed.ts
@@ -54,6 +54,8 @@ export async function getOverviewBucketed(
}>,
res: FastifyReply
) {
+ const isAllTime = !startDate && !endDate;
+
const query = `SELECT
session_stats.time AS time,
session_stats.sessions,
@@ -82,16 +84,15 @@ FROM
COUNT(*) AS pages_in_session
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY session_id
)
- GROUP BY time ORDER BY time ${getTimeStatementFill(
- startDate,
- endDate,
- timezone,
- bucket
- )}
+ GROUP BY time ORDER BY time ${
+ isAllTime
+ ? ""
+ : getTimeStatementFill(startDate, endDate, timezone, bucket)
+ }
) AS session_stats
FULL JOIN
(
@@ -103,18 +104,19 @@ FULL JOIN
COUNT(DISTINCT user_id) AS users
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
- GROUP BY time ORDER BY time ${getTimeStatementFill(
- startDate,
- endDate,
- timezone,
- bucket
- )}
+ GROUP BY time ORDER BY time ${
+ isAllTime
+ ? ""
+ : getTimeStatementFill(startDate, endDate, timezone, bucket)
+ }
) AS page_stats
USING time
ORDER BY time`;
+ console.info(query);
+
try {
const result = await clickhouse.query({
query,
diff --git a/server/src/api/getPages.ts b/server/src/api/getPages.ts
index 42914a185..c29fcfbeb 100644
--- a/server/src/api/getPages.ts
+++ b/server/src/api/getPages.ts
@@ -22,8 +22,8 @@ export async function getPages(
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY pathname
ORDER BY count desc
LIMIT 100;
diff --git a/server/src/api/getReferrers.ts b/server/src/api/getReferrers.ts
index f60015057..34100e8a0 100644
--- a/server/src/api/getReferrers.ts
+++ b/server/src/api/getReferrers.ts
@@ -22,8 +22,8 @@ export async function getReferrers(
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
FROM pageviews
WHERE
+ site_id = ${site}
${getTimeStatement(startDate, endDate, timezone)}
- AND site_id = ${site}
GROUP BY referrer
ORDER BY count desc
LIMIT 100;
diff --git a/server/src/api/utils.ts b/server/src/api/utils.ts
index cbcbbb5c3..5a820ce24 100644
--- a/server/src/api/utils.ts
+++ b/server/src/api/utils.ts
@@ -5,7 +5,10 @@ export function getTimeStatement(
endDate: string,
timezone: string
) {
- return `timestamp >= toTimeZone(
+ if (!startDate && !endDate) {
+ return "";
+ }
+ return `AND timestamp >= toTimeZone(
toStartOfDay(toDateTime('${startDate}', '${timezone}')),
'UTC'
)