Skip to content

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

fix buckets

Bill Yang15374c9bf539
diff --git a/client/src/app/[site]/components/Header/DateSelector.tsx b/client/src/app/[site]/components/Header/DateSelector.tsx
index a0b6fbaea..9ac9292f6 100644
--- a/client/src/app/[site]/components/Header/DateSelector.tsx
+++ b/client/src/app/[site]/components/Header/DateSelector.tsx
@@ -44,6 +44,17 @@ export function DateSelector() {
>
Today
</DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() =>
+ setTime({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 2 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ })
+ }
+ >
+ Last 3 Days
+ </DropdownMenuItem>
<DropdownMenuItem
onClick={() =>
setTime({
@@ -55,6 +66,17 @@ export function DateSelector() {
>
Last 7 Days
</DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() =>
+ setTime({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 13 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ })
+ }
+ >
+ Last 14 Days
+ </DropdownMenuItem>
<DropdownMenuItem
onClick={() =>
setTime({
@@ -66,8 +88,48 @@ export function DateSelector() {
>
Last 30 Days
</DropdownMenuItem>
-
+ <DropdownMenuItem
+ onClick={() =>
+ setTime({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 59 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ })
+ }
+ >
+ Last 60 Days
+ </DropdownMenuItem>
<DropdownMenuSeparator />
+ <DropdownMenuItem
+ onClick={() =>
+ setTime({
+ mode: "week",
+ week: DateTime.now().startOf("week").toISODate(),
+ })
+ }
+ >
+ This Week
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() =>
+ setTime({
+ mode: "month",
+ month: DateTime.now().startOf("month").toISODate(),
+ })
+ }
+ >
+ This Month
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() =>
+ setTime({
+ mode: "year",
+ year: DateTime.now().startOf("year").toISODate(),
+ })
+ }
+ >
+ This Year
+ </DropdownMenuItem>
<CustomDateRangePicker />
</DropdownMenuContent>
</DropdownMenu>
diff --git a/client/src/app/[site]/components/MainSection/Chart.tsx b/client/src/app/[site]/components/MainSection/Chart.tsx
index f3e75dabb..e864280a7 100644
--- a/client/src/app/[site]/components/MainSection/Chart.tsx
+++ b/client/src/app/[site]/components/MainSection/Chart.tsx
@@ -4,17 +4,25 @@ import { round } from "lodash";
import { DateTime } from "luxon";
import { APIResponse, GetPageViewsResponse } from "@/hooks/api";
import { nivoTheme } from "@/lib/nivo";
-import { Time, useStore } from "@/lib/store";
+import { Time, TimeBucket, useStore } from "@/lib/store";
export const formatter = Intl.NumberFormat("en", { notation: "compact" });
-const getMax = (time: Time) => {
+const getMax = (time: Time, bucket: TimeBucket) => {
const now = DateTime.now();
if (time.mode === "day") {
const dayDate = DateTime.fromISO(time.day)
.endOf("day")
.minus({ minutes: 59 });
return now < dayDate ? dayDate.toJSDate() : undefined;
+ } else if (time.mode === "range") {
+ if (bucket === "hour") {
+ const endDate = DateTime.fromISO(time.endDate).endOf("day").minus({
+ minutes: 59,
+ });
+ return now < endDate ? endDate.toJSDate() : undefined;
+ }
+ return undefined;
} else if (time.mode === "week") {
const weekDate = DateTime.fromISO(time.week).endOf("week");
return now < weekDate ? weekDate.toJSDate() : undefined;
@@ -28,7 +36,7 @@ const getMax = (time: Time) => {
return undefined;
};
-const getMin = (time: Time) => {
+const getMin = (time: Time, bucket: TimeBucket) => {
if (time.mode === "day") {
const dayDate = DateTime.fromISO(time.day).startOf("day");
return dayDate.toJSDate();
@@ -79,13 +87,7 @@ export function Chart({
: undefined,
}));
- const timeRange =
- time.mode === "range"
- ? DateTime.fromISO(time.startDate).diff(
- DateTime.fromISO(time.endDate),
- "days"
- ).days
- : 0;
+ console.info(getMax(time, bucket));
return (
<ResponsiveLine
@@ -102,8 +104,8 @@ export function Chart({
format: "%Y-%m-%d %H:%M:%S",
precision: "second",
useUTC: true,
- max: getMax(time),
- min: getMin(time),
+ max: getMax(time, bucket),
+ min: getMin(time, bucket),
}}
yScale={{
type: "linear",
diff --git a/client/src/hooks/utils.ts b/client/src/hooks/utils.ts
index 7d22ebb69..e7279bcc1 100644
--- a/client/src/hooks/utils.ts
+++ b/client/src/hooks/utils.ts
@@ -1,17 +1,27 @@
import { Time } from "@/lib/store";
+import { DateTime } from "luxon";
export function getStartAndEndDate(time: Time) {
if (time.mode === "range") {
return { startDate: time.startDate, endDate: time.endDate };
}
+ if (time.mode === "week") {
+ return {
+ startDate: time.week,
+ endDate: DateTime.fromISO(time.week).endOf("week").toISODate(),
+ };
+ }
if (time.mode === "month") {
- return { startDate: time.month, endDate: time.month };
+ return {
+ startDate: time.month,
+ endDate: DateTime.fromISO(time.month).endOf("month").toISODate(),
+ };
}
if (time.mode === "year") {
- return { startDate: time.year, endDate: time.year };
- }
- if (time.mode === "week") {
- return { startDate: time.week, endDate: time.week };
+ return {
+ startDate: time.year,
+ endDate: DateTime.fromISO(time.year).endOf("year").toISODate(),
+ };
}
return { startDate: time.day, endDate: time.day };
}
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 676c36a30..38c0d4bab 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -29,7 +29,7 @@ type YearMode = {
export type Time = DateMode | DateRangeMode | WeekMode | MonthMode | YearMode;
-export type TimeBucket = "hour" | "day" | "week" | "month";
+export type TimeBucket = "hour" | "day" | "week" | "month" | "year";
type Store = {
site: string;
@@ -98,6 +98,12 @@ export const useStore = create<Store>((set) => ({
endDate:
DateTime.fromISO(time.startDate).minus({ days: 1 }).toISODate() ?? "",
};
+ } else if (time.mode === "week") {
+ bucketToUse = "day";
+ previousTime = {
+ mode: "week",
+ week: DateTime.fromISO(time.week).minus({ weeks: 1 }).toISODate() ?? "",
+ };
} else if (time.mode === "month") {
bucketToUse = "day";
previousTime = {
@@ -141,6 +147,22 @@ export const goBack = () => {
startDate.minus({ days: daysBetweenStartAndEnd }).toISODate() ?? "",
endDate: startDate.toISODate() ?? "",
});
+ } else if (time.mode === "week") {
+ setTime({
+ mode: "week",
+ week: DateTime.fromISO(time.week).minus({ weeks: 1 }).toISODate() ?? "",
+ });
+ } else if (time.mode === "month") {
+ setTime({
+ mode: "month",
+ month:
+ DateTime.fromISO(time.month).minus({ months: 1 }).toISODate() ?? "",
+ });
+ } else if (time.mode === "year") {
+ setTime({
+ mode: "year",
+ year: DateTime.fromISO(time.year).minus({ years: 1 }).toISODate() ?? "",
+ });
}
};
@@ -172,5 +194,20 @@ export const goForward = () => {
// Cap the end date at today
endDate: proposedEndDate.toISODate() ?? "",
});
+ } else if (time.mode === "week") {
+ setTime({
+ mode: "week",
+ week: DateTime.fromISO(time.week).plus({ weeks: 1 }).toISODate() ?? "",
+ });
+ } else if (time.mode === "month") {
+ setTime({
+ mode: "month",
+ month: DateTime.fromISO(time.month).plus({ months: 1 }).toISODate() ?? "",
+ });
+ } else if (time.mode === "year") {
+ setTime({
+ mode: "year",
+ year: DateTime.fromISO(time.year).plus({ years: 1 }).toISODate() ?? "",
+ });
}
};
diff --git a/server/src/api/getPageViews.ts b/server/src/api/getPageViews.ts
index 8b25a048d..72be71fd8 100644
--- a/server/src/api/getPageViews.ts
+++ b/server/src/api/getPageViews.ts
@@ -9,6 +9,13 @@ const TimeBucketToFn = {
month: "toStartOfMonth",
};
+const bucketIntervalMap = {
+ hour: "1 HOUR",
+ day: "1 DAY",
+ week: "7 DAY",
+ month: "1 MONTH",
+} as const;
+
type TimeBucket = "hour" | "day" | "week" | "month";
type GetPageViewsResponse = { time: string; pageviews: number }[];
@@ -38,7 +45,12 @@ export async function getPageViews(
GROUP BY
time
ORDER BY
- time ASC
+ time WITH FILL
+ FROM ${
+ TimeBucketToFn[bucket]
+ }(toDateTime('${startDate}', '${timezone}'))
+ TO ${TimeBucketToFn[bucket]}(toDateTime('${endDate}', '${timezone}'))
+ STEP INTERVAL ${bucketIntervalMap[bucket]}
`;
try {
diff --git a/server/src/api/utils.ts b/server/src/api/utils.ts
index d649e11f6..0d98347d7 100644
--- a/server/src/api/utils.ts
+++ b/server/src/api/utils.ts
@@ -6,18 +6,18 @@ export function getTimeStatement(
timezone: string
) {
return `
-timestamp >= toTimeZone(
- toStartOfDay(toDateTime('${startDate}', '${timezone}')),
- 'UTC'
-)
-AND timestamp < if(
- toDate('${endDate}') = toDate(now(), '${timezone}'),
- now(),
- toTimeZone(
+ timestamp >= toTimeZone(
+ toStartOfDay(toDateTime('${startDate}', '${timezone}')),
+ 'UTC'
+ )
+ AND timestamp < if(
+ toDate('${endDate}') = toDate(now(), '${timezone}'),
+ now(),
+ toTimeZone(
toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
'UTC'
+ )
)
-)
`;
}