Skip to content

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

wip

Bill Yang553810b2d056
diff --git a/client/Dockerfile b/client/Dockerfile
index 6ae5fffcc..e0b0a5517 100644
--- a/client/Dockerfile
+++ b/client/Dockerfile
@@ -7,7 +7,7 @@ WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
-RUN npm ci
+RUN npm ci --legacy-peer-deps
# Rebuild the source code only when needed
FROM base AS builder
diff --git a/client/package-lock.json b/client/package-lock.json
index 493c1b8e2..ff412ed8e 100644
--- a/client/package-lock.json
+++ b/client/package-lock.json
@@ -32,6 +32,7 @@
"react-day-picker": "^8.10.1",
"react-dom": "^19.0.0",
"recharts": "^2.15.0",
+ "spinners-react": "^1.0.10",
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"zustand": "^5.0.3"
@@ -4282,6 +4283,18 @@
"source-map": "^0.6.0"
}
},
+ "node_modules/spinners-react": {
+ "version": "1.0.10",
+ "resolved": "https://registry.npmjs.org/spinners-react/-/spinners-react-1.0.10.tgz",
+ "integrity": "sha512-VZy4IhnIVTw8tcsuWtUGz1VfRK4EDnnlIVd88ezkfiDQyYlmo+QbIAjJD1BRghTBr5RJBULuJ4MIPsJapTp1xw==",
+ "license": "MIT",
+ "peerDependencies": {
+ "@types/react": "^16.x || ^17.x || ^18.x",
+ "@types/react-dom": "^16.x || ^17.x || ^18.x",
+ "react": "^16.x || ^17.x || ^18.x",
+ "react-dom": "^16.x || ^17.x || ^18.x"
+ }
+ },
"node_modules/split2": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
diff --git a/client/package.json b/client/package.json
index 96b98a326..47de5469b 100644
--- a/client/package.json
+++ b/client/package.json
@@ -33,6 +33,7 @@
"react-day-picker": "^8.10.1",
"react-dom": "^19.0.0",
"recharts": "^2.15.0",
+ "spinners-react": "^1.0.10",
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"zustand": "^5.0.3"
diff --git a/client/src/app/actions/getPageviews.ts b/client/src/app/actions/getPageviews.ts
index f78ced50b..108e340b5 100644
--- a/client/src/app/actions/getPageviews.ts
+++ b/client/src/app/actions/getPageviews.ts
@@ -5,16 +5,14 @@ import { clickhouse } from "@/lib/clickhouse";
type Response = { time: string; pageviews: number }[];
export async function getPageViews({
- days = 1,
+ startDate,
+ endDate,
timezone = "America/Los_Angeles",
}: {
- days: number;
+ startDate: string;
+ endDate: string;
timezone: string;
}): Promise<{ data?: Response; error?: string }> {
- if (isNaN(days)) {
- days = 1;
- }
-
const query = `
SELECT
toStartOfHour(toTimeZone(timestamp, '${timezone}')) AS time,
@@ -22,15 +20,25 @@ export async function getPageViews({
FROM pageviews
WHERE
timestamp >= toTimeZone(
- toStartOfDay(toTimeZone(now(), '${timezone}')),
+ toStartOfDay(toDateTime('${startDate}', '${timezone}')),
'UTC'
)
+ AND timestamp < if(
+ toDate('${endDate}') = toDate(now(), '${timezone}'),
+ now(),
+ toTimeZone(
+ toStartOfDay(toDateTime('${endDate}', '${timezone}')) + INTERVAL 1 DAY,
+ 'UTC'
+ )
+ )
GROUP BY
time
ORDER BY
time ASC
`;
+ console.info(query);
+
try {
const result = await clickhouse.query({
query,
diff --git a/client/src/app/components/Header/Header.tsx b/client/src/app/components/Header/Header.tsx
index d463215a8..5fb93f1f4 100644
--- a/client/src/app/components/Header/Header.tsx
+++ b/client/src/app/components/Header/Header.tsx
@@ -14,13 +14,7 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { CustomDateRangePicker } from "./CustomDateRangePicker";
-
-const DATE_OPTIONS = [
- { value: "1d", label: "Today" },
- { value: "7d", label: "Last 7 days" },
- { value: "30d", label: "Last 30 days" },
- { value: "90d", label: "Last 3 months" },
-];
+import { DateTime } from "luxon";
export function Header() {
const { data } = useQuery<{ count: number }>({
@@ -32,6 +26,13 @@ export function Header() {
});
const { date, setDate } = useStore();
+ const DATE_OPTIONS = [
+ { value: 1, label: "Today" },
+ { value: 7, label: "Last 7 days" },
+ { value: 30, label: "Last 30 days" },
+ { value: 90, label: "Last 3 months" },
+ ];
+
return (
<div className="flex items-center justify-between py-2">
<div className="flex gap-3">
@@ -55,13 +56,24 @@ export function Header() {
<DropdownMenu>
<DropdownMenuTrigger>
<Button variant="outline">
- {DATE_OPTIONS.find((option) => option.value === date)?.label}
+ {
+ DATE_OPTIONS.find(
+ (option) =>
+ DateTime.now().minus({ days: option.value }).toISODate() ===
+ date[0]
+ )?.label
+ }
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{DATE_OPTIONS.map((option) => (
<DropdownMenuItem
- onClick={() => setDate(option.value)}
+ onClick={() =>
+ setDate([
+ DateTime.now().minus({ days: option.value }).toISODate(),
+ DateTime.now().toISODate(),
+ ])
+ }
key={option.value}
>
{option.label}
diff --git a/client/src/app/components/MainSection/MainSection.tsx b/client/src/app/components/MainSection/MainSection.tsx
index 5777995ed..22b9e84d2 100644
--- a/client/src/app/components/MainSection/MainSection.tsx
+++ b/client/src/app/components/MainSection/MainSection.tsx
@@ -1,32 +1,19 @@
"use client";
-import * as React from "react";
-import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts";
-import {
- Card,
- CardContent,
- CardDescription,
- CardHeader,
- CardTitle,
-} from "@/components/ui/card";
+import { Card, CardContent } from "@/components/ui/card";
import {
ChartConfig,
ChartContainer,
- ChartLegend,
- ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
} from "@/components/ui/chart";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
-import { getPageViews } from "../../actions/getPageviews";
import { useQuery } from "@tanstack/react-query";
import { DateTime } from "luxon";
+import * as React from "react";
+import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts";
import { formatter } from "../../../lib/utils";
+import { getPageViews } from "../../actions/getPageviews";
+import { Spinner } from "@/components/ui/spinner";
+import { useStore } from "../../../lib/store";
const chartConfig = {
visitors: {
@@ -43,159 +30,123 @@ const chartConfig = {
// },
} satisfies ChartConfig;
-// Helper function to generate nice round numbers
-function generateNiceTicks(max: number, count: number = 5) {
- let roundedTo;
-
- const ticks = [];
- // Round max up to a nice number
- const niceMax = Math.ceil(max / 1000) * 1000;
- const step = Math.ceil(niceMax / (count - 1));
-
- for (let i = 0; i <= count - 1; i++) {
- ticks.push(step * i);
- }
-
- return ticks;
-}
-
export function MainSection() {
+ const { date } = useStore();
const { data, isLoading, error } = useQuery({
queryKey: ["pageviews"],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- return getPageViews({ days: 1, timezone });
+ return getPageViews({
+ startDate: date[0],
+ endDate: date[1],
+ timezone,
+ });
},
});
- const [timeRange, setTimeRange] = React.useState("90d");
- if (isLoading || !data?.data) return <div>Loading...</div>;
+ let body = <Spinner />;
- const filteredData = data.data.map((e) => ({
- date: e.time,
- desktop: e.pageviews,
- // mobile: e.pageviews,
- }));
+ if (data?.data) {
+ const filteredData = data.data.map((e) => ({
+ date: e.time,
+ desktop: e.pageviews,
+ // mobile: e.pageviews,
+ }));
- const maxPageviews = Math.max(...filteredData.map((e) => e.desktop));
+ const maxPageviews = Math.max(...filteredData.map((e) => e.desktop));
+
+ body = (
+ <AreaChart data={filteredData}>
+ <defs>
+ <linearGradient id="fillDesktop" x1="0" y1="0" x2="0" y2="1">
+ <stop
+ offset="5%"
+ stopColor="var(--color-desktop)"
+ stopOpacity={0.8}
+ />
+ <stop
+ offset="95%"
+ stopColor="var(--color-desktop)"
+ stopOpacity={0.1}
+ />
+ </linearGradient>
+ <linearGradient id="fillMobile" x1="0" y1="0" x2="0" y2="1">
+ <stop
+ offset="5%"
+ stopColor="var(--color-mobile)"
+ stopOpacity={0.8}
+ />
+ <stop
+ offset="95%"
+ stopColor="var(--color-mobile)"
+ stopOpacity={0.1}
+ />
+ </linearGradient>
+ </defs>
+ <CartesianGrid vertical={false} />
+ <XAxis
+ dataKey="date"
+ tickLine={false}
+ axisLine={false}
+ tickMargin={8}
+ minTickGap={-100}
+ // tickCount={10}
+ tickFormatter={(value) => {
+ const date2 = DateTime.fromFormat(value, "yyyy-MM-dd HH:mm:ss");
+ return date2.toFormat("ha");
+ }}
+ />
+ <YAxis
+ dataKey="desktop"
+ tickLine={false}
+ axisLine={false}
+ tickMargin={8}
+ minTickGap={10}
+ tickCount={5}
+ domain={[0, maxPageviews]}
+ tickFormatter={(value) => {
+ return formatter(value);
+ }}
+ />
+ <ChartTooltip
+ cursor={false}
+ content={
+ <ChartTooltipContent
+ labelFormatter={(value) => {
+ const date2 = DateTime.fromFormat(value, "yyyy-MM-dd HH:mm:ss");
+ return date2.toFormat("ha");
+ }}
+ indicator="dot"
+ />
+ }
+ />
+ <Area
+ dataKey="mobile"
+ type="linear"
+ fill="url(#fillMobile)"
+ stroke="var(--color-mobile)"
+ stackId="a"
+ />
+ <Area
+ dataKey="desktop"
+ type="linear"
+ fill="url(#fillDesktop)"
+ stroke="var(--color-desktop)"
+ stackId="a"
+ />
+ {/* <ChartLegend content={<ChartLegendContent />} /> */}
+ </AreaChart>
+ );
+ }
return (
<Card>
- {/* <CardHeader className="flex items-center gap-2 space-y-0 border-b py-5 sm:flex-row">
- <div className="grid flex-1 gap-1 text-center sm:text-left">
- <CardTitle>Area Chart - Interactive</CardTitle>
- <CardDescription>
- Showing total visitors for the last 3 months
- </CardDescription>
- </div>
- <Select value={timeRange} onValueChange={setTimeRange}>
- <SelectTrigger
- className="w-[160px] rounded-lg sm:ml-auto"
- aria-label="Select a value"
- >
- <SelectValue placeholder="Last 3 months" />
- </SelectTrigger>
- <SelectContent className="rounded-xl">
- <SelectItem value="90d" className="rounded-lg">
- Last 3 months
- </SelectItem>
- <SelectItem value="30d" className="rounded-lg">
- Last 30 days
- </SelectItem>
- <SelectItem value="7d" className="rounded-lg">
- Last 7 days
- </SelectItem>
- </SelectContent>
- </Select>
- </CardHeader> */}
<CardContent className="pt-4 sm:px-6 sm:pt-6">
<ChartContainer
config={chartConfig}
className="aspect-auto h-[300px] w-full"
>
- <AreaChart data={filteredData}>
- <defs>
- <linearGradient id="fillDesktop" x1="0" y1="0" x2="0" y2="1">
- <stop
- offset="5%"
- stopColor="var(--color-desktop)"
- stopOpacity={0.8}
- />
- <stop
- offset="95%"
- stopColor="var(--color-desktop)"
- stopOpacity={0.1}
- />
- </linearGradient>
- <linearGradient id="fillMobile" x1="0" y1="0" x2="0" y2="1">
- <stop
- offset="5%"
- stopColor="var(--color-mobile)"
- stopOpacity={0.8}
- />
- <stop
- offset="95%"
- stopColor="var(--color-mobile)"
- stopOpacity={0.1}
- />
- </linearGradient>
- </defs>
- <CartesianGrid vertical={false} />
- <XAxis
- dataKey="date"
- tickLine={false}
- axisLine={false}
- tickMargin={8}
- minTickGap={-100}
- // tickCount={10}
- tickFormatter={(value) => {
- const date2 = DateTime.fromFormat(value, "yyyy-MM-dd HH:mm:ss");
- return date2.toFormat("ha");
- }}
- />
- <YAxis
- dataKey="desktop"
- tickLine={false}
- axisLine={false}
- tickMargin={8}
- minTickGap={10}
- tickCount={5}
- domain={[0, maxPageviews]}
- tickFormatter={(value) => {
- return formatter(value);
- }}
- />
- <ChartTooltip
- cursor={false}
- content={
- <ChartTooltipContent
- labelFormatter={(value) => {
- const date2 = DateTime.fromFormat(
- value,
- "yyyy-MM-dd HH:mm:ss"
- );
- return date2.toFormat("ha");
- }}
- indicator="dot"
- />
- }
- />
- <Area
- dataKey="mobile"
- type="linear"
- fill="url(#fillMobile)"
- stroke="var(--color-mobile)"
- stackId="a"
- />
- <Area
- dataKey="desktop"
- type="linear"
- fill="url(#fillDesktop)"
- stroke="var(--color-desktop)"
- stackId="a"
- />
- {/* <ChartLegend content={<ChartLegendContent />} /> */}
- </AreaChart>
+ {body}
</ChartContainer>
</CardContent>
</Card>
diff --git a/client/src/components/ui/spinner.tsx b/client/src/components/ui/spinner.tsx
new file mode 100644
index 000000000..f9dc6cc3b
--- /dev/null
+++ b/client/src/components/ui/spinner.tsx
@@ -0,0 +1,26 @@
+import { cn } from "../../lib/utils";
+
+export interface ISVGProps extends React.SVGProps<SVGSVGElement> {
+ size?: number;
+ className?: string;
+}
+
+export const Spinner = ({ size = 24, className, ...props }: ISVGProps) => {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width={size}
+ height={size}
+ {...props}
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ className={cn("animate-spin", className)}
+ >
+ <path d="M21 12a9 9 0 1 1-6.219-8.56" />
+ </svg>
+ );
+};
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 6f66f0cda..11af486ba 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -1,11 +1,20 @@
+import { DateTime } from "luxon";
import { create } from "zustand";
+export type DateMode = "day" | "week" | "month" | "year" | "custom";
+
type Store = {
- date: string;
- setDate: (date: string) => void;
+ date: string[];
+ setDate: (date: string[]) => void;
+
+ mode: DateMode;
+ setMode: (mode: DateMode) => void;
};
export const useStore = create<Store>((set) => ({
- date: "1d",
+ date: [DateTime.now().toISODate(), DateTime.now().toISODate()],
setDate: (date) => set({ date }),
+
+ mode: "day",
+ setMode: (mode) => set({ mode }),
}));