Skip to content

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

Add sessions table

Bill Yang3dcf189f07c0
diff --git a/client/package-lock.json b/client/package-lock.json
index 01518d715..5b383b34c 100644
--- a/client/package-lock.json
+++ b/client/package-lock.json
@@ -23,6 +23,7 @@
"@radix-ui/themes": "^3.2.0",
"@tanstack/react-query": "^5.64.2",
"@tanstack/react-query-devtools": "^5.64.2",
+ "@tanstack/react-table": "^8.21.2",
"@types/country-flag-icons": "^1.2.2",
"@uidotdev/usehooks": "^2.4.1",
"better-auth": "^1.1.16",
@@ -3289,6 +3290,37 @@
"react": "^18 || ^19"
}
},
+ "node_modules/@tanstack/react-table": {
+ "version": "8.21.2",
+ "resolved": "https://registry.npmjs.org/@tanstack/react-table/-/react-table-8.21.2.tgz",
+ "integrity": "sha512-11tNlEDTdIhMJba2RBH+ecJ9l1zgS2kjmexDPAraulc8jeNA4xocSNeyzextT0XJyASil4XsCYlJmf5jEWAtYg==",
+ "dependencies": {
+ "@tanstack/table-core": "8.21.2"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ },
+ "peerDependencies": {
+ "react": ">=16.8",
+ "react-dom": ">=16.8"
+ }
+ },
+ "node_modules/@tanstack/table-core": {
+ "version": "8.21.2",
+ "resolved": "https://registry.npmjs.org/@tanstack/table-core/-/table-core-8.21.2.tgz",
+ "integrity": "sha512-uvXk/U4cBiFMxt+p9/G7yUWI/UbHYbyghLCjlpWZ3mLeIZiUBSKcUnw9UnKkdRz7Z/N4UBuFLWQdJCjUe7HjvA==",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/tannerlinsley"
+ }
+ },
"node_modules/@types/country-flag-icons": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/@types/country-flag-icons/-/country-flag-icons-1.2.2.tgz",
diff --git a/client/package.json b/client/package.json
index 0b2f763d5..dc4fb2659 100644
--- a/client/package.json
+++ b/client/package.json
@@ -24,6 +24,7 @@
"@radix-ui/themes": "^3.2.0",
"@tanstack/react-query": "^5.64.2",
"@tanstack/react-query-devtools": "^5.64.2",
+ "@tanstack/react-table": "^8.21.2",
"@types/country-flag-icons": "^1.2.2",
"@uidotdev/usehooks": "^2.4.1",
"better-auth": "^1.1.16",
diff --git a/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx b/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx
index 39ca6fa45..d4271a34f 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/Filters.tsx
@@ -9,6 +9,7 @@ import {
useStore,
} from "../../../../../lib/store";
import { NewFilterButton } from "./NewFilterButton";
+import { getCountryName } from "../../../../../lib/utils";
function getParameterNameLabel(parameter: FilterParameter) {
switch (parameter) {
@@ -49,9 +50,7 @@ const filterTypeToLabel = (type: FilterType) => {
function getParameterValueLabel(filter: Filter) {
switch (filter.parameter) {
case "country":
- return (
- countries[filter.value as keyof typeof countries]?.name ?? filter.value
- );
+ return getCountryName(filter.value as string);
default:
return filter.value;
}
diff --git a/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx b/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx
index 6a26a2824..8f5ea4b5c 100644
--- a/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx
+++ b/client/src/app/[site]/components/SubHeader/Filters/ValueFilter.tsx
@@ -4,9 +4,9 @@ import { useMemo, useState } from "react";
import { Button } from "../../../../../components/ui/button";
-import { Check, ChevronsUpDown, Plus } from "lucide-react";
+import { ChevronsUpDown, Plus } from "lucide-react";
-import { cn } from "../../../../../lib/utils";
+import { getCountryName } from "../../../../../lib/utils";
import {
Command,
@@ -26,7 +26,6 @@ import {
import { FilterParameter, addFilter } from "../../../../../lib/store";
import { useSingleCol } from "../../../../../hooks/api";
-import { countries } from "countries-list";
interface ValueFilterProps {
parameter: FilterParameter;
@@ -52,7 +51,7 @@ export function ValueFilter({ parameter, type, onComplete }: ValueFilterProps) {
if (parameter === "country") {
return {
value: val,
- label: countries[val as keyof typeof countries].name,
+ label: getCountryName(val),
};
}
return {
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index e4389f6fc..d16414bb0 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -1,9 +1,9 @@
"use client";
import { useSingleCol } from "@/hooks/api";
-import { countries } from "countries-list";
import * as CountryFlags from "country-flag-icons/react/3x2";
import React from "react";
+import { getCountryName } from "../../../../../lib/utils";
import { StandardCard } from "../../../components/shared/StandardCard";
export function Countries() {
@@ -17,18 +17,18 @@ export function Countries() {
getKey={(e) => e.value}
getLabel={(e) => (
<span className="flex items-center gap-2">
- {e.value && countries[e.value as keyof typeof countries] ? (
+ {e.value && getCountryName(e.value) ? (
<>
{CountryFlags[e.value as keyof typeof CountryFlags]
? React.createElement(
CountryFlags[e.value as keyof typeof CountryFlags],
{
- title: countries[e.value as keyof typeof countries].name,
+ title: getCountryName(e.value),
className: "w-5",
}
)
: null}
- {countries[e.value as keyof typeof countries].name}
+ {getCountryName(e.value)}
</>
) : (
"Unknown"
diff --git a/client/src/app/[site]/main/components/sections/Map.tsx b/client/src/app/[site]/main/components/sections/Map.tsx
index fb45b7e5e..383babba4 100644
--- a/client/src/app/[site]/main/components/sections/Map.tsx
+++ b/client/src/app/[site]/main/components/sections/Map.tsx
@@ -7,7 +7,6 @@ import {
CardLoader,
CardTitle,
} from "@/components/ui/card";
-import { countries } from "countries-list";
import * as CountryFlags from "country-flag-icons/react/3x2";
import { scaleLinear } from "d3-scale";
import { Feature, Geometry } from "geojson";
@@ -22,6 +21,7 @@ import {
import { useSingleCol } from "@/hooks/api";
import React from "react";
+import { getCountryName } from "../../../../../lib/utils";
const geoUrl = "/countries.geojson";
@@ -146,15 +146,12 @@ export function Map() {
tooltipData.name as keyof typeof CountryFlags
],
{
- title:
- countries[
- tooltipData.name as keyof typeof countries
- ].name,
+ title: getCountryName(tooltipData.name),
className: "w-4",
}
)
: null}
- {countries[tooltipData.name as keyof typeof countries]?.name}
+ {getCountryName(tooltipData.name)}
</div>
<>
<div>
diff --git a/client/src/app/[site]/sessions/page.tsx b/client/src/app/[site]/sessions/page.tsx
index 8296ba954..239241671 100644
--- a/client/src/app/[site]/sessions/page.tsx
+++ b/client/src/app/[site]/sessions/page.tsx
@@ -1,10 +1,195 @@
"use client";
+import { useGetSessionsInfinite } from "@/hooks/api";
+import {
+ createColumnHelper,
+ flexRender,
+ getCoreRowModel,
+ useReactTable,
+} from "@tanstack/react-table";
+import { format, parseISO } from "date-fns";
+import { useEffect, useMemo, useRef } from "react";
+import { getCountryName } from "../../../lib/utils";
+
+type Session = {
+ session_id: string;
+ user_id: string;
+ country: string;
+ iso_3166_2: string;
+ language: string;
+ device_type: string;
+ browser: string;
+ operating_system: string;
+ referrer: string;
+ last_pageview_timestamp: string;
+ pageviews: number;
+};
+
export default function SessionsPage() {
+ const {
+ data,
+ isLoading,
+ error,
+ fetchNextPage,
+ hasNextPage,
+ isFetchingNextPage,
+ } = useGetSessionsInfinite();
+
+ // Combine all pages of data
+ const flattenedData = useMemo(() => {
+ if (!data) return [];
+ return data.pages.flatMap((page) => page.data || []);
+ }, [data]);
+
+ // Reference for the scroll container
+ const tableContainerRef = useRef<HTMLDivElement>(null);
+
+ // Infinite scroll implementation
+ useEffect(() => {
+ const handleScroll = () => {
+ if (!tableContainerRef.current) return;
+
+ const { scrollHeight, scrollTop, clientHeight } =
+ tableContainerRef.current;
+
+ // Load more when user scrolls to bottom (with a 200px buffer)
+ if (
+ scrollHeight - scrollTop - clientHeight < 200 &&
+ !isFetchingNextPage &&
+ hasNextPage
+ ) {
+ fetchNextPage();
+ }
+ };
+
+ const tableContainer = tableContainerRef.current;
+ if (tableContainer) {
+ tableContainer.addEventListener("scroll", handleScroll);
+ return () => {
+ tableContainer.removeEventListener("scroll", handleScroll);
+ };
+ }
+ }, [fetchNextPage, hasNextPage, isFetchingNextPage]);
+
+ // Column definition
+ const columnHelper = createColumnHelper<Session>();
+
+ const columns = useMemo(
+ () => [
+ columnHelper.accessor("last_pageview_timestamp", {
+ header: "Last Seen",
+ cell: (info) => {
+ const date = parseISO(info.getValue());
+ return format(date, "MMM d, yyyy h:mm a");
+ },
+ }),
+ columnHelper.accessor("pageviews", {
+ header: "Pageviews",
+ cell: (info) => info.getValue(),
+ }),
+ columnHelper.accessor("country", {
+ header: "Country",
+ cell: (info) => getCountryName(info.getValue() || "Unknown"),
+ }),
+ columnHelper.accessor("browser", {
+ header: "Browser",
+ cell: (info) => info.getValue() || "Unknown",
+ }),
+ columnHelper.accessor("operating_system", {
+ header: "OS",
+ cell: (info) => info.getValue() || "Unknown",
+ }),
+ columnHelper.accessor("device_type", {
+ header: "Device",
+ cell: (info) => info.getValue() || "Unknown",
+ }),
+ columnHelper.accessor("referrer", {
+ header: "Referrer",
+ cell: (info) => info.getValue() || "Direct",
+ }),
+ ],
+ []
+ );
+
+ // Table instance
+ const table = useReactTable({
+ data: flattenedData,
+ columns,
+ getCoreRowModel: getCoreRowModel(),
+ });
+
+ if (error)
+ return <div className="p-6">Error: {(error as Error).message}</div>;
+
return (
<div className="p-6">
<h1 className="text-2xl font-bold mb-4">Sessions</h1>
- <p className="text-neutral-400">Sessions page content will go here.</p>
+ <div
+ ref={tableContainerRef}
+ className="overflow-auto rounded-lg"
+ style={{ height: "calc(100vh - 200px)" }}
+ >
+ <table className="min-w-full divide-y divide-gray-200">
+ <thead className="bg-neutral-900 sticky top-0">
+ {table.getHeaderGroups().map((headerGroup) => (
+ <tr key={headerGroup.id}>
+ {headerGroup.headers.map((header) => (
+ <th
+ key={header.id}
+ className="px-6 py-3 text-left font-medium text-gray-400 tracking-wider"
+ >
+ <div className="flex items-center">
+ {flexRender(
+ header.column.columnDef.header,
+ header.getContext()
+ )}
+ </div>
+ </th>
+ ))}
+ </tr>
+ ))}
+ </thead>
+ <tbody className="divide-y divide-neutral-650">
+ {isLoading ? (
+ <tr>
+ <td colSpan={columns.length} className="px-6 py-4">
+ <div className="flex justify-center">Loading...</div>
+ </td>
+ </tr>
+ ) : flattenedData.length === 0 ? (
+ <tr>
+ <td colSpan={columns.length} className="px-6 py-4">
+ <div className="flex justify-center">No sessions found</div>
+ </td>
+ </tr>
+ ) : (
+ table.getRowModel().rows.map((row) => (
+ <tr
+ key={row.id}
+ className="hover:bg-neutral-850 bg-neutral-900"
+ >
+ {row.getVisibleCells().map((cell) => (
+ <td key={cell.id} className="px-6 py-4 whitespace-nowrap">
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext()
+ )}
+ </td>
+ ))}
+ </tr>
+ ))
+ )}
+ </tbody>
+ </table>
+
+ {isFetchingNextPage && (
+ <div className="flex justify-center p-4">
+ <div className="text-sm text-gray-500">
+ Loading more sessions...
+ </div>
+ </div>
+ )}
+ </div>
</div>
);
}
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index f12b449dc..7f115152f 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -1,4 +1,8 @@
-import { useQuery, UseQueryResult } from "@tanstack/react-query";
+import {
+ useQuery,
+ UseQueryResult,
+ useInfiniteQuery,
+} from "@tanstack/react-query";
import { BACKEND_URL } from "../lib/const";
import { FilterParameter, useStore } from "../lib/store";
import { authedFetch, getStartAndEndDate } from "./utils";
@@ -206,3 +210,50 @@ export function deleteSite(siteId: number) {
export function useSiteHasData(siteId: string) {
return useGenericQuery<boolean>(`site-has-data/${siteId}`);
}
+
+type GetSessionsResponse = {
+ session_id: string;
+ user_id: string;
+ country: string;
+ iso_3166_2: string;
+ language: string;
+ device_type: string;
+ browser: string;
+ operating_system: string;
+ referrer: string;
+ last_pageview_timestamp: string;
+ pageviews: number;
+}[];
+
+export function useGetSessionsInfinite() {
+ const { time, site, filters } = useStore();
+ const { startDate, endDate } = getStartAndEndDate(time);
+
+ return useInfiniteQuery<APIResponse<GetSessionsResponse>>({
+ queryKey: ["sessions-infinite", time, site, filters],
+ queryFn: ({ pageParam = 1 }) => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ let url = `${BACKEND_URL}/sessions?${
+ startDate ? `startDate=${startDate}&` : ""
+ }${
+ endDate ? `endDate=${endDate}&` : ""
+ }timezone=${timezone}&site=${site}&filters=${JSON.stringify(
+ filters
+ )}&page=${pageParam}`;
+
+ return authedFetch(url).then((res) => res.json());
+ },
+ initialPageParam: 1,
+ getNextPageParam: (
+ lastPage: APIResponse<GetSessionsResponse>,
+ allPages
+ ) => {
+ // If we have data and it's a full page (100 items), there might be more
+ if (lastPage?.data && lastPage.data.length === 100) {
+ return allPages.length + 1;
+ }
+ return undefined;
+ },
+ staleTime: Infinity,
+ });
+}
diff --git a/client/src/lib/utils.ts b/client/src/lib/utils.ts
index 398a8ee0d..216d05656 100644
--- a/client/src/lib/utils.ts
+++ b/client/src/lib/utils.ts
@@ -1,4 +1,5 @@
import { clsx, type ClassValue } from "clsx";
+import { countries } from "countries-list";
import { Duration } from "luxon";
import { twMerge } from "tailwind-merge";
@@ -27,3 +28,7 @@ export function formatSecondsAsMinutesAndSeconds(value: number) {
export const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
+
+export const getCountryName = (countryCode: string) => {
+ return countries[countryCode as keyof typeof countries].name;
+};
diff --git a/server/src/api/getSessions.ts b/server/src/api/getSessions.ts
new file mode 100644
index 000000000..e7671cdf2
--- /dev/null
+++ b/server/src/api/getSessions.ts
@@ -0,0 +1,86 @@
+import { FastifyReply, FastifyRequest } from "fastify";
+import clickhouse from "../db/clickhouse/clickhouse.js";
+import {
+ getFilterStatement,
+ getTimeStatement,
+ processResults,
+} from "./utils.js";
+
+type GetSessionsResponse = {
+ session_id: string;
+ user_id: string;
+ country: string;
+ iso_3166_2: string;
+ language: string;
+ device_type: string;
+ browser: string;
+ operating_system: string;
+ referrer: string;
+ last_pageview_timestamp: string;
+ pageviews: number;
+}[];
+
+export interface GetSessionsRequest {
+ Querystring: {
+ startDate: string;
+ endDate: string;
+ timezone: string;
+ site: string;
+ filters: string;
+ page: number;
+ };
+}
+
+export async function getSessions(
+ {
+ query: { startDate, endDate, timezone, site, filters, page },
+ }: FastifyRequest<GetSessionsRequest>,
+ res: FastifyReply
+) {
+ const filterStatement = getFilterStatement(filters);
+
+ const query = `
+SELECT
+ session_id,
+ user_id,
+ country,
+ iso_3166_2,
+ language,
+ device_type,
+ browser,
+ operating_system,
+ referrer,
+ MAX(timestamp) AS last_pageview_timestamp,
+ COUNT(*) AS pageviews
+FROM pageviews
+WHERE
+ site_id = ${site}
+ ${filterStatement}
+ ${getTimeStatement(startDate, endDate, timezone)}
+GROUP BY
+ session_id,
+ user_id,
+ browser,
+ country,
+ iso_3166_2,
+ language,
+ device_type,
+ operating_system,
+ referrer
+ORDER BY last_pageview_timestamp DESC
+LIMIT 100 OFFSET ${(page - 1) * 100}
+ `;
+
+ try {
+ const result = await clickhouse.query({
+ query,
+ format: "JSONEachRow",
+ });
+
+ const data = await processResults<GetSessionsResponse[number]>(result);
+ return res.send({ data });
+ } catch (error) {
+ console.error("Error fetching devices:", error);
+ return res.status(500).send({ error: "Failed to fetch devices" });
+ }
+}
diff --git a/server/src/api/getSingleCol.ts b/server/src/api/getSingleCol.ts
index da6bb1f81..a7f327d61 100644
--- a/server/src/api/getSingleCol.ts
+++ b/server/src/api/getSingleCol.ts
@@ -45,7 +45,7 @@ export async function getSingleCol(
const data = await processResults<GetSingleColResponse[number]>(result);
return res.send({ data });
} catch (error) {
- console.error("Error fetching devices:", error);
- return res.status(500).send({ error: "Failed to fetch devices" });
+ console.error(`Error fetching ${parameter}:`, error);
+ return res.status(500).send({ error: `Failed to fetch ${parameter}` });
}
}
diff --git a/server/src/index.ts b/server/src/index.ts
index 7f2884fc5..14a3d74bb 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -23,6 +23,7 @@ import { auth } from "./lib/auth.js";
import { mapHeaders } from "./lib/betterAuth.js";
import { trackPageView } from "./tracker/trackPageView.js";
import { createAccount } from "./api/createAccount.js";
+import { getSessions } from "./api/getSessions.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -120,6 +121,7 @@ server.get("/overview", getOverview);
server.get("/overview-bucketed", getOverviewBucketed);
server.get("/single-col", getSingleCol);
server.get("/site-has-data/:site", getSiteHasData);
+server.get("/sessions", getSessions);
// Administrative
server.post("/add-site", addSite);