🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add @nivo/bar dependency and enhance analytics query functionality
Bill Yangf4d7e79d5664
diff --git a/client/package-lock.json b/client/package-lock.jsonindex 689d6918d..f6b41106b 100644--- a/client/package-lock.json+++ b/client/package-lock.json@@ -9,6 +9,7 @@"version": "0.1.0","dependencies": {"@better-auth/stripe": "^1.2.3",+ "@nivo/bar": "^0.88.0","@nivo/core": "^0.88.0","@nivo/line": "^0.88.0","@phosphor-icons/react": "^2.1.7",@@ -1739,6 +1740,29 @@"react": ">= 16.14.0 < 19.0.0"}},+ "node_modules/@nivo/bar": {+ "version": "0.88.0",+ "resolved": "https://registry.npmjs.org/@nivo/bar/-/bar-0.88.0.tgz",+ "integrity": "sha512-wckwuHWeCikxGvvdRfGL+dVFsUD9uHk1r9s7bWUfOD+p8BWhxtYqfXpHolEfgGg3UyPaHtpGA7P4zgE5vgo7gQ==",+ "dependencies": {+ "@nivo/annotations": "0.88.0",+ "@nivo/axes": "0.88.0",+ "@nivo/colors": "0.88.0",+ "@nivo/core": "0.88.0",+ "@nivo/legends": "0.88.0",+ "@nivo/scales": "0.88.0",+ "@nivo/tooltip": "0.88.0",+ "@react-spring/web": "9.4.5 || ^9.7.2",+ "@types/d3-scale": "^4.0.8",+ "@types/d3-shape": "^3.1.6",+ "d3-scale": "^4.0.2",+ "d3-shape": "^3.2.0",+ "lodash": "^4.17.21"+ },+ "peerDependencies": {+ "react": ">= 16.14.0 < 19.0.0"+ }+ },"node_modules/@nivo/colors": {"version": "0.88.0","resolved": "https://registry.npmjs.org/@nivo/colors/-/colors-0.88.0.tgz",diff --git a/client/package.json b/client/package.jsonindex f54e0b84c..670dec1ac 100644--- a/client/package.json+++ b/client/package.json@@ -10,6 +10,7 @@},"dependencies": {"@better-auth/stripe": "^1.2.3",+ "@nivo/bar": "^0.88.0","@nivo/core": "^0.88.0","@nivo/line": "^0.88.0","@phosphor-icons/react": "^2.1.7",diff --git a/client/src/api/analytics/useGetOverviewBucketed.ts b/client/src/api/analytics/useGetOverviewBucketed.tsindex 82464b662..393af888a 100644--- a/client/src/api/analytics/useGetOverviewBucketed.ts+++ b/client/src/api/analytics/useGetOverviewBucketed.ts@@ -2,7 +2,7 @@ import { UseQueryResult, useQuery } from "@tanstack/react-query";import { BACKEND_URL } from "../../lib/const";import { APIResponse } from "../types";import { getStartAndEndDate, authedFetch } from "../utils";-import { useStore } from "../../lib/store";+import { TimeBucket, useStore } from "../../lib/store";type PeriodTime = "current" | "previous";@@ -59,14 +59,16 @@ export function useGetOverviewBucketed({export function useGetOverviewBucketedPastMinutes({pastMinutes = 24 * 60,site,+ bucket = "hour",+ refetchInterval,}: {pastMinutes: number;site?: number | string;+ bucket?: TimeBucket;+ refetchInterval?: number;}): UseQueryResult<APIResponse<GetOverviewBucketedResponse>> {- const { bucket, filters } = useStore();-return useQuery({- queryKey: ["overview-bucketed-past-minutes", pastMinutes, site, filters],+ queryKey: ["overview-bucketed-past-minutes", pastMinutes, site, bucket],queryFn: () => {const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;return authedFetch(`${BACKEND_URL}/overview-bucketed`, {@@ -76,6 +78,7 @@ export function useGetOverviewBucketedPastMinutes({pastMinutes,}).then((res) => res.json());},+ refetchInterval,placeholderData: (_, query: any) => {if (!query?.queryKey) return undefined;const prevQueryKey = query.queryKey as [string, string, string];diff --git a/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx b/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsxnew file mode 100644index 000000000..a2bbd305c--- /dev/null+++ b/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx@@ -0,0 +1,127 @@+import { useMemo } from "react";+import { useGetOverviewBucketedPastMinutes } from "../../../../api/analytics/useGetOverviewBucketed";+import { useStore } from "../../../../lib/store";+import { DateTime } from "luxon";+import { ResponsiveBar } from "@nivo/bar";+import { formatter } from "../../../../lib/utils";+import { nivoTheme } from "../../../../lib/nivo";++export function RealtimeChart() {+ const { site } = useStore();++ const { data, isLoading } = useGetOverviewBucketedPastMinutes({+ pastMinutes: 30,+ site,+ bucket: "minute",+ refetchInterval: 3000,+ });++ const chartData = useMemo(() => {+ if (!data?.data || data.data.length === 0) {+ return [];+ }++ return data.data.map((point) => ({+ time: DateTime.fromSQL(point.time)+ .toUTC()+ .toFormat("yyyy-MM-dd HH:mm:ss"),+ users: point.users,+ }));+ }, [data]);++ if (isLoading) {+ return <div>Loading...</div>;+ }++ if (!chartData || chartData.length === 0) {+ return (+ <div+ style={{+ height: 300,+ display: "flex",+ alignItems: "center",+ justifyContent: "center",+ }}+ >+ No data available for this period.+ </div>+ );+ }++ return (+ <div style={{ height: 300 }}>+ <ResponsiveBar+ data={chartData}+ keys={["users"]}+ indexBy="time"+ margin={{ top: 10, right: 10, bottom: 40, left: 35 }}+ padding={0.3}+ valueScale={{ type: "linear" }}+ indexScale={{ type: "band", round: true }}+ // colors={["hsla(210, 40%, 50%, 0.7)"]}+ colors={["hsl(var(--accent-500))"]}+ theme={nivoTheme}+ axisTop={null}+ axisRight={null}+ axisBottom={{+ tickSize: 5,+ tickPadding: 5,+ legend: "",+ legendPosition: "middle",+ legendOffset: 32,+ format: (value: string) => {+ const dt = DateTime.fromFormat(value, "yyyy-MM-dd HH:mm:ss", {+ zone: "utc",+ }).toLocal();+ return dt.isValid ? dt.toFormat("h:mm") : "";+ },+ }}+ axisLeft={{+ tickSize: 5,+ tickPadding: 5,+ tickRotation: 0,+ legend: "",+ legendPosition: "middle",+ legendOffset: -30,+ format: formatter,+ tickValues: 3,+ }}+ enableLabel={false}+ enableGridX={false}+ enableGridY={true}+ tooltip={({+ id,+ value,+ data,+ }: {+ id: string | number;+ value: number;+ data: { time: string; users: number };+ }) => {+ const currentTime = DateTime.fromFormat(+ data.time,+ "yyyy-MM-dd HH:mm:ss",+ { zone: "utc" }+ ).toLocal();+ const currentY = Number(value);++ return (+ <div className="bg-neutral-950 p-2 rounded-md text-xs">+ <div className="font-semibold mb-1">+ {currentTime.isValid+ ? currentTime.toFormat("HH:mm")+ : "Invalid Time"}+ </div>+ <div className="flex justify-between items-center">+ <span className="font-medium">{currentY.toLocaleString()}</span>+ <span className="text-muted-foreground ml-1">{id}</span>+ </div>+ </div>+ );+ }}+ animate={true}+ motionConfig="gentle"+ />+ </div>+ );+}diff --git a/client/src/app/[site]/realtime/page.tsx b/client/src/app/[site]/realtime/page.tsxindex 659f21848..80b7ac92f 100644--- a/client/src/app/[site]/realtime/page.tsx+++ b/client/src/app/[site]/realtime/page.tsx@@ -4,11 +4,13 @@ import { Countries } from "../main/components/sections/Countries";import { Events } from "../main/components/sections/Events";import { Pages } from "../main/components/sections/Pages";import { Referrers } from "../main/components/sections/Referrers";+import { RealtimeChart } from "./RealtimeChart/RealtimeChart";import { RealtimeMap } from "./RealtimeMap/RealtimeMap";export default function RealtimePage() {return (<div className="flex flex-col gap-4">+ <RealtimeChart /><RealtimeMap /><div className="grid grid-cols-1 md:grid-cols-2 gap-4"><Pages isRealtime />diff --git a/client/src/components/SiteCard.tsx b/client/src/components/SiteCard.tsxindex c32b20af3..0fbe5386d 100644--- a/client/src/components/SiteCard.tsx+++ b/client/src/components/SiteCard.tsx@@ -16,6 +16,7 @@ export function SiteCard({ siteId, domain }: SiteCardProps) {const { data, isLoading } = useGetOverviewBucketedPastMinutes({pastMinutes: 24 * 60,site: siteId,+ bucket: "hour",});const { data: overviewData, isLoading: isOverviewLoading } =