🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
update admin page (#951)
Goldflage0893f03366e
diff --git a/client/package-lock.json b/client/package-lock.jsonindex bef3a5ee3..5296b4ba6 100644--- a/client/package-lock.json+++ b/client/package-lock.json@@ -83,6 +83,7 @@"react-select": "5.10.2","react-syntax-highlighter": "^16.1.0","rrweb-player": "^2.0.0-alpha.20",+ "sql-formatter": "^15.7.2","tailwind-merge": "2.6.0","tailwindcss-animate": "1.0.7","unique-names-generator": "^4.7.1",@@ -6402,7 +6403,6 @@"version": "2.0.1","resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz","integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",- "dev": true,"license": "Python-2.0"},"node_modules/aria-hidden": {@@ -7906,6 +7906,12 @@"integrity": "sha512-ypdmJU/TbBby2Dxibuv7ZLW3Bs1QEmM7nHjEANfohJLvE0XVujisn1qPJcZxg+qDucsr+bP6fLD1rPS3AhJ7EQ==","license": "MIT"},+ "node_modules/discontinuous-range": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/discontinuous-range/-/discontinuous-range-1.0.0.tgz",+ "integrity": "sha512-c68LpLbO+7kP/b1Hr1qs8/BJ09F5khZGTxqxZuhzxpmwJKOgRFHJWIb9/KmqnqHhLdO55aOxFH/EGBvUQbL/RQ==",+ "license": "MIT"+ },"node_modules/doctrine": {"version": "2.1.0","resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz",@@ -10809,6 +10815,12 @@"integrity": "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw==","license": "MIT"},+ "node_modules/moo": {+ "version": "0.5.3",+ "resolved": "https://registry.npmjs.org/moo/-/moo-0.5.3.tgz",+ "integrity": "sha512-m2fmM2dDm7GZQsY7KK2cme8agi+AAljILjQnof7p1ZMDe6dQ4bdnSMx0cPppudoeNv5hEFQirN6u+O4fDE0IWA==",+ "license": "BSD-3-Clause"+ },"node_modules/motion-dom": {"version": "12.23.23","resolved": "https://registry.npmjs.org/motion-dom/-/motion-dom-12.23.23.tgz",@@ -10892,6 +10904,34 @@"dev": true,"license": "MIT"},+ "node_modules/nearley": {+ "version": "2.20.1",+ "resolved": "https://registry.npmjs.org/nearley/-/nearley-2.20.1.tgz",+ "integrity": "sha512-+Mc8UaAebFzgV+KpI5n7DasuuQCHA89dmwm7JXw3TV43ukfNQ9DnBH3Mdb2g/I4Fdxc26pwimBWvjIw0UAILSQ==",+ "license": "MIT",+ "dependencies": {+ "commander": "^2.19.0",+ "moo": "^0.5.0",+ "railroad-diagrams": "^1.0.0",+ "randexp": "0.4.6"+ },+ "bin": {+ "nearley-railroad": "bin/nearley-railroad.js",+ "nearley-test": "bin/nearley-test.js",+ "nearley-unparse": "bin/nearley-unparse.js",+ "nearleyc": "bin/nearleyc.js"+ },+ "funding": {+ "type": "individual",+ "url": "https://nearley.js.org/#give-to-nearley"+ }+ },+ "node_modules/nearley/node_modules/commander": {+ "version": "2.20.3",+ "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",+ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",+ "license": "MIT"+ },"node_modules/negotiator": {"version": "1.0.0","resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz",@@ -11677,6 +11717,25 @@"integrity": "sha512-XdjUArbK4Bm5fLLvlm5KpTFOiOThgfWWI4axAZDWg4E/0mKdZyI9tNEfds27qCi1ze/vwTR16kvmmGhRra3c2g==","license": "ISC"},+ "node_modules/railroad-diagrams": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz",+ "integrity": "sha512-cz93DjNeLY0idrCNOH6PviZGRN9GJhsdm9hpn1YCS879fj4W+x5IFJhhkRZcwVgMmFF7R82UA/7Oh+R8lLZg6A==",+ "license": "CC0-1.0"+ },+ "node_modules/randexp": {+ "version": "0.4.6",+ "resolved": "https://registry.npmjs.org/randexp/-/randexp-0.4.6.tgz",+ "integrity": "sha512-80WNmd9DA0tmZrw9qQa62GPPWfuXJknrmVmLcxvq4uZBdYqb1wYoKTmnlGUchvVWe0XiLupYkBoXVOxz3C8DYQ==",+ "license": "MIT",+ "dependencies": {+ "discontinuous-range": "1.0.0",+ "ret": "~0.1.10"+ },+ "engines": {+ "node": ">=0.12"+ }+ },"node_modules/rbush": {"version": "4.0.1","resolved": "https://registry.npmjs.org/rbush/-/rbush-4.0.1.tgz",@@ -12016,6 +12075,15 @@"protocol-buffers-schema": "^3.3.1"}},+ "node_modules/ret": {+ "version": "0.1.15",+ "resolved": "https://registry.npmjs.org/ret/-/ret-0.1.15.tgz",+ "integrity": "sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==",+ "license": "MIT",+ "engines": {+ "node": ">=0.12"+ }+ },"node_modules/reusify": {"version": "1.1.0","resolved": "https://registry.npmjs.org/reusify/-/reusify-1.1.0.tgz",@@ -12476,6 +12544,19 @@"integrity": "sha512-D50hKrjZgBzqD3FT2Ek53f2dcDLAQT8SSGrzj3vidNH5ISRgceeGVJ2dQIthKOuayqFXfFjXheHNo4bbt9LhRQ==","license": "MIT"},+ "node_modules/sql-formatter": {+ "version": "15.7.2",+ "resolved": "https://registry.npmjs.org/sql-formatter/-/sql-formatter-15.7.2.tgz",+ "integrity": "sha512-b0BGoM81KFRVSpZFwPpIPU5gng4YD8DI/taLD96NXCFRf5af3FzSE4aSwjKmxcyTmf/MfPu91j75883nRrWDBw==",+ "license": "MIT",+ "dependencies": {+ "argparse": "^2.0.1",+ "nearley": "^2.20.1"+ },+ "bin": {+ "sql-formatter": "bin/sql-formatter-cli.cjs"+ }+ },"node_modules/stable-hash": {"version": "0.0.5","resolved": "https://registry.npmjs.org/stable-hash/-/stable-hash-0.0.5.tgz",diff --git a/client/package.json b/client/package.jsonindex bc00caf1a..074389c2e 100644--- a/client/package.json+++ b/client/package.json@@ -88,6 +88,7 @@"react-select": "5.10.2","react-syntax-highlighter": "^16.1.0","rrweb-player": "^2.0.0-alpha.20",+ "sql-formatter": "^15.7.2","tailwind-merge": "2.6.0","tailwindcss-animate": "1.0.7","unique-names-generator": "^4.7.1",diff --git a/client/src/api/admin/endpoints/clickhouseStats.ts b/client/src/api/admin/endpoints/clickhouseStats.tsindex b0a434cc7..df9d52a86 100644--- a/client/src/api/admin/endpoints/clickhouseStats.ts+++ b/client/src/api/admin/endpoints/clickhouseStats.ts@@ -38,24 +38,56 @@ export interface ClickhouseStatsResponse {unavailableFeatures: string[];}-export interface ActiveQuery {+export interface QueryLogEntry {+ eventTime: string;queryId: string;- user: string;- query: string;- elapsed: number;+ queryKind: string;+ queryDurationMs: number;readRows: number;+ readBytes: number;+ writtenRows: number;+ writtenBytes: number;memoryUsage: number;- queryKind: string;+ type: string;+ exceptionCode: number;+ query: string;+ user: string;+ databases: string[];+ tables: string[];+}++export interface ClickhouseQueryLogResponse {+ items: QueryLogEntry[];+ total: number;+ page: number;+ pageSize: number;+ unavailable?: boolean;}-export interface ClickhouseActiveQueriesResponse {- activeQueries: ActiveQuery[];+export interface QueryLogParams {+ page?: number;+ pageSize?: number;+ sortBy?: string;+ sortOrder?: "asc" | "desc";+ queryKind?: string;+ type?: string;}-export function getClickhouseStats() {- return authedFetch<ClickhouseStatsResponse>("/admin/clickhouse-stats");+export function getClickhouseStats(days?: number) {+ const params = days !== undefined ? `?days=${days}` : "";+ return authedFetch<ClickhouseStatsResponse>(`/admin/clickhouse-stats${params}`);}-export function getClickhouseActiveQueries() {- return authedFetch<ClickhouseActiveQueriesResponse>("/admin/clickhouse-active-queries");+export function getClickhouseQueryLog(params: QueryLogParams = {}) {+ const searchParams = new URLSearchParams();+ if (params.page) searchParams.set("page", String(params.page));+ if (params.pageSize) searchParams.set("pageSize", String(params.pageSize));+ if (params.sortBy) searchParams.set("sortBy", params.sortBy);+ if (params.sortOrder) searchParams.set("sortOrder", params.sortOrder);+ if (params.queryKind) searchParams.set("queryKind", params.queryKind);+ if (params.type) searchParams.set("type", params.type);+ const qs = searchParams.toString();+ return authedFetch<ClickhouseQueryLogResponse>(+ `/admin/clickhouse-query-log${qs ? `?${qs}` : ""}`+ );}diff --git a/client/src/api/admin/endpoints/index.ts b/client/src/api/admin/endpoints/index.tsindex 70a32f640..c79abae77 100644--- a/client/src/api/admin/endpoints/index.ts+++ b/client/src/api/admin/endpoints/index.ts@@ -82,13 +82,14 @@ export { getOrganizationMembers } from "./auth";export type { GetOrganizationMembersResponse } from "./auth";// ClickHouse Stats endpoints-export { getClickhouseStats, getClickhouseActiveQueries } from "./clickhouseStats";+export { getClickhouseStats, getClickhouseQueryLog } from "./clickhouseStats";export type {TableStats,RowsByDate,InsertRate,QueryError,ClickhouseStatsResponse,- ActiveQuery,- ClickhouseActiveQueriesResponse,+ QueryLogEntry,+ ClickhouseQueryLogResponse,+ QueryLogParams,} from "./clickhouseStats";diff --git a/client/src/api/admin/hooks/useClickhouseStats.ts b/client/src/api/admin/hooks/useClickhouseStats.tsindex b87a6912c..0c385fd79 100644--- a/client/src/api/admin/hooks/useClickhouseStats.ts+++ b/client/src/api/admin/hooks/useClickhouseStats.ts@@ -1,23 +1,25 @@-import { useQuery } from "@tanstack/react-query";+import { useQuery, keepPreviousData } from "@tanstack/react-query";import {getClickhouseStats,- getClickhouseActiveQueries,+ getClickhouseQueryLog,ClickhouseStatsResponse,- ClickhouseActiveQueriesResponse,+ ClickhouseQueryLogResponse,+ QueryLogParams,} from "../endpoints/clickhouseStats";-export function useClickhouseStats() {+export function useClickhouseStats(days?: number) {return useQuery<ClickhouseStatsResponse>({- queryKey: ["clickhouse-stats"],- queryFn: getClickhouseStats,+ queryKey: ["clickhouse-stats", days],+ queryFn: () => getClickhouseStats(days),+ placeholderData: keepPreviousData,refetchInterval: 60000, // 60 seconds});}-export function useClickhouseActiveQueries() {- return useQuery<ClickhouseActiveQueriesResponse>({- queryKey: ["clickhouse-active-queries"],- queryFn: getClickhouseActiveQueries,- refetchInterval: 10000, // 10 seconds+export function useClickhouseQueryLog(params: QueryLogParams = {}) {+ return useQuery<ClickhouseQueryLogResponse>({+ queryKey: ["clickhouse-query-log", params],+ queryFn: () => getClickhouseQueryLog(params),+ placeholderData: keepPreviousData,});}diff --git a/client/src/app/admin/components/database/ActiveQueriesTable.tsx b/client/src/app/admin/components/database/ActiveQueriesTable.tsxdeleted file mode 100644index 2706f9889..000000000--- a/client/src/app/admin/components/database/ActiveQueriesTable.tsx+++ /dev/null@@ -1,106 +0,0 @@-"use client";--import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";-import { Skeleton } from "@/components/ui/skeleton";-import { ActiveQuery } from "@/api/admin/endpoints/clickhouseStats";-import { Badge } from "@/components/ui/badge";--interface ActiveQueriesTableProps {- activeQueries: ActiveQuery[] | undefined;- isLoading: boolean;-}--function formatDuration(seconds: number): string {- if (seconds < 1) {- return `${(seconds * 1000).toFixed(0)}ms`;- }- if (seconds < 60) {- return `${seconds.toFixed(1)}s`;- }- const minutes = Math.floor(seconds / 60);- const remainingSeconds = seconds % 60;- return `${minutes}m ${remainingSeconds.toFixed(0)}s`;-}--function formatBytes(bytes: number): string {- if (bytes >= 1024 * 1024 * 1024) {- return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB";- }- if (bytes >= 1024 * 1024) {- return (bytes / (1024 * 1024)).toFixed(2) + " MB";- }- if (bytes >= 1024) {- return (bytes / 1024).toFixed(2) + " KB";- }- return bytes + " B";-}--function formatNumber(num: number): string {- if (num >= 1_000_000_000) {- return (num / 1_000_000_000).toFixed(1) + "B";- }- if (num >= 1_000_000) {- return (num / 1_000_000).toFixed(1) + "M";- }- if (num >= 1_000) {- return (num / 1_000).toFixed(1) + "K";- }- return num.toLocaleString();-}--export function ActiveQueriesTable({ activeQueries, isLoading }: ActiveQueriesTableProps) {- if (isLoading) {- return (- <div className="space-y-2">- {[...Array(3)].map((_, i) => (- <Skeleton key={i} className="h-12 w-full" />- ))}- </div>- );- }-- if (!activeQueries || activeQueries.length === 0) {- return (- <div className="text-center py-8 text-neutral-500 dark:text-neutral-400">No active queries running</div>- );- }-- return (- <div className="border rounded-lg overflow-hidden dark:border-neutral-800">- <Table>- <TableHeader>- <TableRow>- <TableHead className="w-[100px]">User</TableHead>- <TableHead className="w-[100px]">Type</TableHead>- <TableHead className="w-[100px]">Elapsed</TableHead>- <TableHead className="w-[100px]">Rows Read</TableHead>- <TableHead className="w-[100px]">Memory</TableHead>- <TableHead>Query</TableHead>- </TableRow>- </TableHeader>- <TableBody>- {activeQueries.map(query => (- <TableRow key={query.queryId}>- <TableCell className="font-medium">{query.user}</TableCell>- <TableCell>- <Badge variant="secondary" className="text-xs">- {query.queryKind || "Unknown"}- </Badge>- </TableCell>- <TableCell className={query.elapsed > 10 ? "text-amber-500" : ""}>- {formatDuration(query.elapsed)}- </TableCell>- <TableCell>{formatNumber(query.readRows)}</TableCell>- <TableCell>{formatBytes(query.memoryUsage)}</TableCell>- <TableCell>- <div className="max-w-md truncate font-mono text-xs text-neutral-600 dark:text-neutral-400">- {query.query}- </div>- </TableCell>- </TableRow>- ))}- </TableBody>- </Table>- </div>- );-}diff --git a/client/src/app/admin/components/database/Database.tsx b/client/src/app/admin/components/database/Database.tsxindex 015382374..7d5c4a67c 100644--- a/client/src/app/admin/components/database/Database.tsx+++ b/client/src/app/admin/components/database/Database.tsx@@ -4,12 +4,12 @@ import { useState } from "react";import { useQueryClient } from "@tanstack/react-query";import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";-import { useClickhouseStats, useClickhouseActiveQueries } from "@/api/admin/hooks/useClickhouseStats";+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";+import { useClickhouseStats } from "@/api/admin/hooks/useClickhouseStats";import { TableStatsCards } from "./TableStatsCards";import { RowsTrendChart } from "./RowsTrendChart";import { InsertRateChart } from "./InsertRateChart";-import { ActiveQueriesTable } from "./ActiveQueriesTable";-import { QueryErrorsTable } from "./QueryErrorsTable";+import { QueryLogTable } from "./QueryLogTable";import { RefreshControls } from "./RefreshControls";import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";import { Skeleton } from "@/components/ui/skeleton";@@ -86,14 +86,9 @@ export function Database() {const queryClient = useQueryClient();const [activeTab, setActiveTab] = useState("overview");const [lastUpdated, setLastUpdated] = useState<Date | null>(null);+ const [rowsDays, setRowsDays] = useState(30);- const { data: stats, isLoading: statsLoading, isRefetching: statsRefetching, dataUpdatedAt } = useClickhouseStats();-- const {- data: activeQueriesData,- isLoading: queriesLoading,- isRefetching: queriesRefetching,- } = useClickhouseActiveQueries();+ const { data: stats, isLoading: statsLoading, isRefetching: statsRefetching, dataUpdatedAt } = useClickhouseStats(rowsDays);// Update last updated time when data changesif (dataUpdatedAt && (!lastUpdated || dataUpdatedAt > lastUpdated.getTime())) {@@ -102,13 +97,12 @@ export function Database() {const handleRefresh = () => {queryClient.invalidateQueries({ queryKey: ["clickhouse-stats"] });- queryClient.invalidateQueries({ queryKey: ["clickhouse-active-queries"] });+ queryClient.invalidateQueries({ queryKey: ["clickhouse-query-log"] });};- const isRefetching = statsRefetching || queriesRefetching;+ const isRefetching = statsRefetching;const isInsertRateUnavailable = stats?.unavailableFeatures?.includes("insertRate") ?? false;- const isQueryErrorsUnavailable = stats?.unavailableFeatures?.includes("queryErrors") ?? false;return (<div className="space-y-6">@@ -127,8 +121,7 @@ export function Database() {<Tabs defaultValue="overview" value={activeTab} onValueChange={setActiveTab}><TabsList><TabsTrigger value="overview">Overview</TabsTrigger>- <TabsTrigger value="queries">Active Queries</TabsTrigger>- <TabsTrigger value="errors">Query Errors</TabsTrigger>+ <TabsTrigger value="query-log">Query Log</TabsTrigger></TabsList><TabsContent value="overview" className="space-y-4 mt-4">@@ -153,41 +146,46 @@ export function Database() {</div><Card>- <CardHeader>- <CardTitle className="text-base">Rows Inserted by Table (Last 30 days)</CardTitle>+ <CardHeader className="flex flex-row items-center justify-between">+ <CardTitle className="text-base">Rows Inserted by Table</CardTitle>+ <Select value={String(rowsDays)} onValueChange={(v) => setRowsDays(Number(v))}>+ <SelectTrigger className="w-[140px] h-8 text-xs">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="7">Last 7 days</SelectItem>+ <SelectItem value="14">Last 14 days</SelectItem>+ <SelectItem value="30">Last 30 days</SelectItem>+ <SelectItem value="60">Last 60 days</SelectItem>+ <SelectItem value="90">Last 90 days</SelectItem>+ <SelectItem value="180">Last 180 days</SelectItem>+ <SelectItem value="365">Last 365 days</SelectItem>+ <SelectItem value="0">All time</SelectItem>+ </SelectContent>+ </Select></CardHeader><CardContent>- <RowsTrendChart rowsByDate={stats?.rowsByDate} isLoading={statsLoading} />+ <RowsTrendChart rowsByDate={stats?.rowsByDate} isLoading={statsLoading} days={rowsDays} /></CardContent></Card></TabsContent>- <TabsContent value="queries" className="mt-4">+ <TabsContent value="query-log" className="mt-4"><Card><CardHeader><CardTitle className="text-base">- Active Queries+ Query Log<span className="ml-2 text-sm font-normal text-neutral-500 dark:text-neutral-400">- (auto-refreshes every 10s)+ (last 24 hours)</span></CardTitle></CardHeader><CardContent>- <ActiveQueriesTable activeQueries={activeQueriesData?.activeQueries} isLoading={queriesLoading} />+ <QueryLogTable /></CardContent></Card></TabsContent>- <TabsContent value="errors" className="mt-4">- <Card>- <CardHeader>- <CardTitle className="text-base">Query Errors (Last 24h)</CardTitle>- </CardHeader>- <CardContent>- <QueryErrorsTable queryErrors={stats?.queryErrors} isLoading={statsLoading} isUnavailable={isQueryErrorsUnavailable} />- </CardContent>- </Card>- </TabsContent></Tabs></div>);diff --git a/client/src/app/admin/components/database/QueryErrorsTable.tsx b/client/src/app/admin/components/database/QueryErrorsTable.tsxdeleted file mode 100644index 236347bd8..000000000--- a/client/src/app/admin/components/database/QueryErrorsTable.tsx+++ /dev/null@@ -1,84 +0,0 @@-"use client";--import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";-import { Skeleton } from "@/components/ui/skeleton";-import { QueryError } from "@/api/admin/endpoints/clickhouseStats";-import { Badge } from "@/components/ui/badge";-import { DateTime } from "luxon";--interface QueryErrorsTableProps {- queryErrors: QueryError[] | undefined;- isLoading: boolean;- isUnavailable?: boolean;-}--export function QueryErrorsTable({ queryErrors, isLoading, isUnavailable }: QueryErrorsTableProps) {- if (isLoading) {- return (- <div className="space-y-2">- {[...Array(3)].map((_, i) => (- <Skeleton key={i} className="h-12 w-full" />- ))}- </div>- );- }-- if (isUnavailable) {- return (- <div className="text-center py-8 text-neutral-500 dark:text-neutral-400">- <p>Feature unavailable</p>- <p className="text-xs mt-1">system.query_log is disabled to save disk space</p>- </div>- );- }-- if (!queryErrors || queryErrors.length === 0) {- return (- <div className="text-center py-8 text-neutral-500 dark:text-neutral-400">- No query errors in the last 24 hours- </div>- );- }-- return (- <div className="border rounded-lg overflow-hidden dark:border-neutral-800">- <Table>- <TableHeader>- <TableRow>- <TableHead className="w-[160px]">Time</TableHead>- <TableHead className="w-[100px]">Error Code</TableHead>- <TableHead className="w-[300px]">Exception</TableHead>- <TableHead>Query</TableHead>- </TableRow>- </TableHeader>- <TableBody>- {queryErrors.map(error => {- const eventTime = DateTime.fromSQL(error.eventTime, { zone: "utc" });- return (- <TableRow key={error.queryId}>- <TableCell className="text-xs">- {eventTime.toLocaleString(DateTime.DATETIME_SHORT)}- </TableCell>- <TableCell>- <Badge variant="destructive" className="text-xs">- {error.exceptionCode}- </Badge>- </TableCell>- <TableCell>- <div className="max-w-[300px] truncate text-xs text-red-600 dark:text-red-400">- {error.exception}- </div>- </TableCell>- <TableCell>- <div className="max-w-md truncate font-mono text-xs text-neutral-600 dark:text-neutral-400">- {error.query}- </div>- </TableCell>- </TableRow>- );- })}- </TableBody>- </Table>- </div>- );-}diff --git a/client/src/app/admin/components/database/QueryLogTable.tsx b/client/src/app/admin/components/database/QueryLogTable.tsxnew file mode 100644index 000000000..ae13ec28c--- /dev/null+++ b/client/src/app/admin/components/database/QueryLogTable.tsx@@ -0,0 +1,341 @@+"use client";++import { useState } from "react";+import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";+import { Skeleton } from "@/components/ui/skeleton";+import { Badge } from "@/components/ui/badge";+import { Button } from "@/components/ui/button";+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";+import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, ArrowUpDown, ArrowUp, ArrowDown } from "lucide-react";+import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription } from "@/components/ui/sheet";+import { DateTime } from "luxon";+import { format as formatSql } from "sql-formatter";+import { useClickhouseQueryLog } from "@/api/admin/hooks/useClickhouseStats";+import { QueryLogEntry, QueryLogParams } from "@/api/admin/endpoints/clickhouseStats";+import { getTimezone } from "../../../../lib/store";++function formatDuration(ms: number): string {+ if (ms < 1) return "<1ms";+ if (ms < 1000) return `${Math.round(ms)}ms`;+ if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;+ const minutes = Math.floor(ms / 60000);+ const seconds = ((ms % 60000) / 1000).toFixed(0);+ return `${minutes}m ${seconds}s`;+}++function formatBytes(bytes: number): string {+ if (bytes >= 1024 * 1024 * 1024) return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB";+ if (bytes >= 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(2) + " MB";+ if (bytes >= 1024) return (bytes / 1024).toFixed(2) + " KB";+ return bytes + " B";+}++function formatNumber(num: number): string {+ if (num >= 1_000_000_000) return (num / 1_000_000_000).toFixed(1) + "B";+ if (num >= 1_000_000) return (num / 1_000_000).toFixed(1) + "M";+ if (num >= 1_000) return (num / 1_000).toFixed(1) + "K";+ return num.toLocaleString();+}++type SortColumn = QueryLogParams["sortBy"];+type SortOrder = "asc" | "desc";++function SortableHeader({+ column,+ label,+ currentSort,+ currentOrder,+ onSort,+}: {+ column: string;+ label: string;+ currentSort: SortColumn;+ currentOrder: SortOrder;+ onSort: (col: string) => void;+}) {+ const isActive = currentSort === column;+ return (+ <TableHead+ className="cursor-pointer select-none hover:bg-neutral-100 dark:hover:bg-neutral-800 transition-colors"+ onClick={() => onSort(column)}+ >+ <div className="flex items-center gap-1">+ {label}+ {isActive ? (+ currentOrder === "asc" ? (+ <ArrowUp className="h-3 w-3" />+ ) : (+ <ArrowDown className="h-3 w-3" />+ )+ ) : (+ <ArrowUpDown className="h-3 w-3 opacity-30" />+ )}+ </div>+ </TableHead>+ );+}++export function QueryLogTable() {+ const [page, setPage] = useState(1);+ const [pageSize, setPageSize] = useState(25);+ const [sortBy, setSortBy] = useState<SortColumn>("event_time");+ const [sortOrder, setSortOrder] = useState<SortOrder>("desc");+ const [queryKind, setQueryKind] = useState<string>("");+ const [type, setType] = useState<string>("");+ const [selectedEntry, setSelectedEntry] = useState<QueryLogEntry | null>(null);++ const params: QueryLogParams = {+ page,+ pageSize,+ sortBy,+ sortOrder,+ ...(queryKind && { queryKind }),+ ...(type && { type }),+ };++ const { data, isLoading, isFetching } = useClickhouseQueryLog(params);++ const handleSort = (column: string) => {+ if (sortBy === column) {+ setSortOrder(sortOrder === "asc" ? "desc" : "asc");+ } else {+ setSortBy(column);+ setSortOrder("desc");+ }+ setPage(1);+ };++ const totalPages = data ? Math.max(1, Math.ceil(data.total / pageSize)) : 1;++ if (data?.unavailable) {+ return (+ <div className="text-center py-8 text-neutral-500 dark:text-neutral-400">+ <p>Feature unavailable</p>+ <p className="text-xs mt-1">system.query_log is disabled to save disk space</p>+ </div>+ );+ }++ return (+ <div className="space-y-4">+ {/* Filters */}+ <div className="flex items-center gap-3">+ <div className="flex items-center gap-2">+ <span className="text-sm text-neutral-500">Kind:</span>+ <Select value={queryKind || "all"} onValueChange={(v) => { setQueryKind(v === "all" ? "" : v); setPage(1); }}>+ <SelectTrigger className="h-8 w-28">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="all">All</SelectItem>+ <SelectItem value="Select">Select</SelectItem>+ <SelectItem value="Insert">Insert</SelectItem>+ <SelectItem value="Other">Other</SelectItem>+ </SelectContent>+ </Select>+ </div>+ <div className="flex items-center gap-2">+ <span className="text-sm text-neutral-500">Status:</span>+ <Select value={type || "all"} onValueChange={(v) => { setType(v === "all" ? "" : v); setPage(1); }}>+ <SelectTrigger className="h-8 w-40">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="all">All</SelectItem>+ <SelectItem value="QueryFinish">Success</SelectItem>+ <SelectItem value="ExceptionWhileProcessing">Error</SelectItem>+ </SelectContent>+ </Select>+ </div>+ {isFetching && !isLoading && (+ <span className="text-xs text-neutral-400">Updating...</span>+ )}+ </div>++ {/* Table */}+ <div className="border rounded-lg overflow-hidden dark:border-neutral-800">+ <div className="overflow-x-auto">+ <Table>+ <TableHeader>+ <TableRow>+ <SortableHeader column="event_time" label="Time" currentSort={sortBy} currentOrder={sortOrder} onSort={handleSort} />+ <TableHead>User</TableHead>+ <TableHead>Kind</TableHead>+ <TableHead>Status</TableHead>+ <SortableHeader column="query_duration_ms" label="Duration" currentSort={sortBy} currentOrder={sortOrder} onSort={handleSort} />+ <SortableHeader column="read_rows" label="Rows Read" currentSort={sortBy} currentOrder={sortOrder} onSort={handleSort} />+ <SortableHeader column="read_bytes" label="Bytes Read" currentSort={sortBy} currentOrder={sortOrder} onSort={handleSort} />+ <SortableHeader column="written_rows" label="Rows Written" currentSort={sortBy} currentOrder={sortOrder} onSort={handleSort} />+ <SortableHeader column="memory_usage" label="Memory" currentSort={sortBy} currentOrder={sortOrder} onSort={handleSort} />+ <TableHead>Query</TableHead>+ </TableRow>+ </TableHeader>+ <TableBody>+ {isLoading ? (+ [...Array(pageSize)].map((_, i) => (+ <TableRow key={i}>+ <TableCell><Skeleton className="h-4 w-28" /></TableCell>+ <TableCell><Skeleton className="h-4 w-16" /></TableCell>+ <TableCell><Skeleton className="h-5 w-14 rounded-full" /></TableCell>+ <TableCell><Skeleton className="h-5 w-10 rounded-full" /></TableCell>+ <TableCell><Skeleton className="h-4 w-14" /></TableCell>+ <TableCell><Skeleton className="h-4 w-12" /></TableCell>+ <TableCell><Skeleton className="h-4 w-14" /></TableCell>+ <TableCell><Skeleton className="h-4 w-12" /></TableCell>+ <TableCell><Skeleton className="h-4 w-14" /></TableCell>+ <TableCell><Skeleton className="h-4 w-48" /></TableCell>+ </TableRow>+ ))+ ) : !data?.items?.length ? (+ <TableRow>+ <TableCell colSpan={10} className="text-center py-8 text-neutral-500 dark:text-neutral-400">+ No queries found in the last 24 hours+ </TableCell>+ </TableRow>+ ) : (+ data.items.map((entry, i) => {+ const eventTime = DateTime.fromSQL(entry.eventTime, { zone: "utc" }).setZone(getTimezone());+ const isError = entry.type === "ExceptionWhileProcessing";+ return (+ <TableRow key={`${entry.queryId}-${i}`} className={isError ? "bg-red-50/50 dark:bg-red-950/20" : ""}>+ <TableCell className="text-xs whitespace-nowrap">+ {eventTime.toLocaleString(DateTime.DATETIME_SHORT_WITH_SECONDS)}+ </TableCell>+ <TableCell className="text-xs">{entry.user}</TableCell>+ <TableCell>+ <Badge variant="secondary" className="text-xs">+ {entry.queryKind || "Other"}+ </Badge>+ </TableCell>+ <TableCell>+ {isError ? (+ <Badge variant="destructive" className="text-xs">Error</Badge>+ ) : (+ <Badge variant="outline" className="text-xs text-emerald-600 dark:text-emerald-400 border-emerald-300 dark:border-emerald-700">OK</Badge>+ )}+ </TableCell>+ <TableCell className={`text-xs whitespace-nowrap ${entry.queryDurationMs > 5000 ? "text-amber-500" : ""}`}>+ {formatDuration(entry.queryDurationMs)}+ </TableCell>+ <TableCell className="text-xs whitespace-nowrap">{formatNumber(entry.readRows)}</TableCell>+ <TableCell className="text-xs whitespace-nowrap">{formatBytes(entry.readBytes)}</TableCell>+ <TableCell className="text-xs whitespace-nowrap">{formatNumber(entry.writtenRows)}</TableCell>+ <TableCell className="text-xs whitespace-nowrap">{formatBytes(entry.memoryUsage)}</TableCell>+ <TableCell>+ <div+ className="max-w-sm truncate font-mono text-xs text-neutral-600 dark:text-neutral-400 cursor-pointer hover:text-neutral-900 dark:hover:text-neutral-200 transition-colors"+ onClick={() => setSelectedEntry(entry)}+ >+ {entry.query}+ </div>+ </TableCell>+ </TableRow>+ );+ })+ )}+ </TableBody>+ </Table>+ </div>+ </div>++ {/* Query Detail Sheet */}+ <Sheet open={!!selectedEntry} onOpenChange={(open) => { if (!open) setSelectedEntry(null); }}>+ <SheetContent side="right" className="sm:max-w-2xl w-full overflow-y-auto">+ <SheetHeader>+ <SheetTitle>Query Details</SheetTitle>+ {selectedEntry && (+ <SheetDescription>+ {DateTime.fromSQL(selectedEntry.eventTime, { zone: "utc" }).toLocaleString(DateTime.DATETIME_SHORT_WITH_SECONDS)}+ {" \u00b7 "}{selectedEntry.queryKind || "Other"}+ {" \u00b7 "}{selectedEntry.type === "ExceptionWhileProcessing" ? "Error" : "OK"}+ </SheetDescription>+ )}+ </SheetHeader>+ {selectedEntry && (+ <div className="mt-6 space-y-4">+ <div className="grid grid-cols-2 gap-3 text-sm">+ <div>+ <span className="text-neutral-500">User</span>+ <p className="font-medium">{selectedEntry.user}</p>+ </div>+ <div>+ <span className="text-neutral-500">Duration</span>+ <p className="font-medium">{formatDuration(selectedEntry.queryDurationMs)}</p>+ </div>+ <div>+ <span className="text-neutral-500">Rows Read</span>+ <p className="font-medium">{formatNumber(selectedEntry.readRows)}</p>+ </div>+ <div>+ <span className="text-neutral-500">Bytes Read</span>+ <p className="font-medium">{formatBytes(selectedEntry.readBytes)}</p>+ </div>+ <div>+ <span className="text-neutral-500">Rows Written</span>+ <p className="font-medium">{formatNumber(selectedEntry.writtenRows)}</p>+ </div>+ <div>+ <span className="text-neutral-500">Memory</span>+ <p className="font-medium">{formatBytes(selectedEntry.memoryUsage)}</p>+ </div>+ </div>+ <div>+ <span className="text-sm text-neutral-500">Query</span>+ <pre className="mt-1 p-3 rounded-lg bg-neutral-100 dark:bg-neutral-900 text-xs font-mono whitespace-pre-wrap break-all overflow-x-auto max-h-[60vh]">+ {(() => { try { return formatSql(selectedEntry.query, { language: "sql" }); } catch { return selectedEntry.query; } })()}+ </pre>+ </div>+ </div>+ )}+ </SheetContent>+ </Sheet>++ {/* Pagination */}+ {data && data.total > 0 && (+ <div className="flex items-center justify-between">+ <div className="text-sm text-neutral-400">+ Showing {(page - 1) * pageSize + 1} to{" "}+ {Math.min(page * pageSize, data.total)} of {data.total.toLocaleString()} queries+ </div>+ <div className="flex items-center gap-2">+ <div className="flex items-center gap-2 mr-4">+ <span className="text-sm text-neutral-400">Page size:</span>+ <Select+ value={pageSize.toString()}+ onValueChange={(v) => {+ setPageSize(Number(v));+ setPage(1);+ }}+ >+ <SelectTrigger className="h-8 w-16">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="10">10</SelectItem>+ <SelectItem value="25">25</SelectItem>+ <SelectItem value="50">50</SelectItem>+ <SelectItem value="100">100</SelectItem>+ </SelectContent>+ </Select>+ </div>+ <Button variant="outline" size="icon" onClick={() => setPage(1)} disabled={page <= 1}>+ <ChevronsLeft className="h-4 w-4" />+ </Button>+ <Button variant="outline" size="icon" onClick={() => setPage(page - 1)} disabled={page <= 1}>+ <ChevronLeft className="h-4 w-4" />+ </Button>+ <span className="text-sm text-neutral-400">+ Page {page} of {totalPages}+ </span>+ <Button variant="outline" size="icon" onClick={() => setPage(page + 1)} disabled={page >= totalPages}>+ <ChevronRight className="h-4 w-4" />+ </Button>+ <Button variant="outline" size="icon" onClick={() => setPage(totalPages)} disabled={page >= totalPages}>+ <ChevronsRight className="h-4 w-4" />+ </Button>+ </div>+ </div>+ )}+ </div>+ );+}diff --git a/client/src/app/admin/components/database/RowsTrendChart.tsx b/client/src/app/admin/components/database/RowsTrendChart.tsxindex dd4031570..eac261965 100644--- a/client/src/app/admin/components/database/RowsTrendChart.tsx+++ b/client/src/app/admin/components/database/RowsTrendChart.tsx@@ -9,16 +9,26 @@ import { DateTime } from "luxon";import { userLocale } from "@/lib/dateTimeUtils";import { useWindowSize } from "@uidotdev/usehooks";import { formatter } from "@/lib/utils";+import { useState } from "react";++const CHART_COLORS = [+ "#60a5fa", // blue-400+ "#818cf8", // indigo-400+ "#a78bfa", // violet-400+ "#34d399", // emerald-400+];interface RowsTrendChartProps {rowsByDate: RowsByDate[] | undefined;isLoading: boolean;+ days: number;}-export function RowsTrendChart({ rowsByDate, isLoading }: RowsTrendChartProps) {+export function RowsTrendChart({ rowsByDate, isLoading, days }: RowsTrendChartProps) {const nivoTheme = useNivoTheme();const { width } = useWindowSize();const maxTicks = Math.round((width ?? Infinity) / 100);+ const [hiddenSeries, setHiddenSeries] = useState<Set<string>>(new Set());if (isLoading) {return (@@ -48,19 +58,11 @@ export function RowsTrendChart({ rowsByDate, isLoading }: RowsTrendChartProps) {{} as Record<string, RowsByDate[]>);- const colors = [- "hsl(var(--blue-400))",- "hsl(var(--indigo-400))",- "hsl(var(--violet-400))",- "hsl(var(--emerald-400))",- ];-- // Filter to last 30 days to exclude bad/future dates+ // Filter to exclude bad/future datesconst now = DateTime.now();- const thirtyDaysAgo = now.minus({ days: 30 });+ const cutoff = days > 0 ? now.minus({ days }) : null;- // Match GrowthChart pattern: use string dates directly- const chartData = Object.entries(tableGroups).map(([table, rows]) => ({+ const allSeries = Object.entries(tableGroups).map(([table, rows]) => ({id: table,data: rows.map(row => {@@ -71,100 +73,163 @@ export function RowsTrendChart({ rowsByDate, isLoading }: RowsTrendChartProps) {currentTime: dt,};})- // Filter out invalid dates (future dates or dates outside the 30-day window)- .filter(point => point.currentTime.isValid && point.currentTime <= now && point.currentTime >= thirtyDaysAgo)+ .filter(point => point.currentTime.isValid && point.currentTime <= now && (!cutoff || point.currentTime >= cutoff)).sort((a, b) => a.currentTime.toMillis() - b.currentTime.toMillis()),}));+ const seriesTotals = allSeries+ .map((series, i) => ({+ id: series.id,+ total: series.data.reduce((acc, d) => acc + d.y, 0),+ color: CHART_COLORS[i % CHART_COLORS.length],+ }))+ .sort((a, b) => b.total - a.total);++ const visibleSeries = allSeries.filter(s => !hiddenSeries.has(s.id));+ const visibleColors = allSeries+ .map((s, i) => ({ color: CHART_COLORS[i % CHART_COLORS.length], hidden: hiddenSeries.has(s.id) }))+ .filter(c => !c.hidden)+ .map(c => c.color);++ const totalRows = seriesTotals.reduce((acc, s) => acc + s.total, 0);++ const toggleSeries = (id: string) => {+ setHiddenSeries(prev => {+ const next = new Set(prev);+ if (next.has(id)) {+ next.delete(id);+ } else {+ next.add(id);+ }+ return next;+ });+ };+return (- <div className="h-64">- <ResponsiveLine- data={chartData}- theme={nivoTheme}- margin={{ top: 10, right: 110, bottom: 25, left: 40 }}- xScale={{- type: "time",- format: "%Y-%m-%d",- precision: "day",- useUTC: true,- }}- yScale={{- type: "linear",- min: 0,- stacked: false,- reverse: false,- }}- enableGridX={true}- enableGridY={true}- gridYValues={5}- yFormat=" >-.0f"- axisTop={null}- axisRight={null}- axisBottom={{- tickSize: 0,- tickPadding: 10,- tickRotation: 0,- truncateTickAt: 0,- tickValues: Math.min(maxTicks, 10),- format: value => {- const dt = DateTime.fromJSDate(value).setLocale(userLocale);- return dt.toFormat("MMM d");- },- }}- axisLeft={{- tickSize: 0,- tickPadding: 10,- tickRotation: 0,- truncateTickAt: 0,- tickValues: 5,- format: formatter,- }}- enableTouchCrosshair={true}- enablePoints={false}- useMesh={true}- animate={false}- enableSlices="x"- colors={colors}- enableArea={false}- legends={[- {- anchor: "bottom-right",- direction: "column",- justify: false,- translateX: 100,- translateY: 0,- itemsSpacing: 0,- itemDirection: "left-to-right",- itemWidth: 80,- itemHeight: 20,- itemOpacity: 0.75,- symbolSize: 12,- symbolShape: "circle",- },- ]}- sliceTooltip={({ slice }: any) => {- const point = slice.points[0];+ <div className="flex gap-6">+ <div className="h-64 flex-1 min-w-0">+ {visibleSeries.length === 0 ? (+ <div className="h-full flex items-center justify-center text-neutral-500 dark:text-neutral-400">+ No series selected+ </div>+ ) : (+ <ResponsiveLine+ data={visibleSeries}+ theme={nivoTheme}+ margin={{ top: 10, right: 10, bottom: 25, left: 40 }}+ xScale={{+ type: "time",+ format: "%Y-%m-%d",+ precision: "day",+ useUTC: true,+ }}+ yScale={{+ type: "linear",+ min: 0,+ stacked: false,+ reverse: false,+ }}+ enableGridX={true}+ enableGridY={true}+ gridYValues={5}+ yFormat=" >-.0f"+ axisTop={null}+ axisRight={null}+ axisBottom={{+ tickSize: 0,+ tickPadding: 10,+ tickRotation: 0,+ truncateTickAt: 0,+ tickValues: Math.min(maxTicks, 10),+ format: value => {+ const dt = DateTime.fromJSDate(value).setLocale(userLocale);+ return dt.toFormat("MMM d");+ },+ }}+ axisLeft={{+ tickSize: 0,+ tickPadding: 10,+ tickRotation: 0,+ truncateTickAt: 0,+ tickValues: 5,+ format: formatter,+ }}+ enableTouchCrosshair={true}+ enablePoints={false}+ useMesh={true}+ animate={false}+ enableSlices="x"+ colors={visibleColors}+ enableArea={false}+ sliceTooltip={({ slice }: any) => {+ const point = slice.points[0];- return (- <ChartTooltip>- <div className="p-3 min-w-[140px]">- <div className="font-medium mb-1">- {point.data.currentTime.toLocaleString(DateTime.DATE_SHORT)}- </div>- {slice.points.map((point: any) => (- <div key={point.seriesId} className="flex justify-between gap-4">- <div className="flex items-center gap-2">- <div className="w-2 h-2 rounded-full" style={{ backgroundColor: point.seriesColor }} />- <span className="text-xs truncate max-w-[80px]">{point.seriesId}</span>+ return (+ <ChartTooltip>+ <div className="p-3 min-w-[140px]">+ <div className="font-medium mb-1">+ {point.data.currentTime.toLocaleString(DateTime.DATE_SHORT)}</div>- <div>{formatter(Number(point.data.yFormatted))}</div>+ {slice.points.map((point: any) => (+ <div key={point.seriesId} className="flex justify-between gap-4">+ <div className="flex items-center gap-2">+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: point.seriesColor }} />+ <span className="text-xs truncate max-w-[80px]">{point.seriesId}</span>+ </div>+ <div>{formatter(Number(point.data.yFormatted))}</div>+ </div>+ ))}</div>- ))}+ </ChartTooltip>+ );+ }}+ />+ )}+ </div>+ <div className="flex flex-col justify-center text-xs min-w-[180px] gap-1.5">+ {seriesTotals.map(s => {+ const isHidden = hiddenSeries.has(s.id);+ return (+ <div+ key={s.id}+ className="flex items-center justify-between gap-3 cursor-pointer select-none"+ onClick={() => toggleSeries(s.id)}+ >+ <div className="flex items-center gap-1.5">+ <div+ className="w-2 h-2 rounded-full shrink-0 transition-opacity"+ style={{+ backgroundColor: s.color,+ opacity: isHidden ? 0.3 : 1,+ }}+ />+ <span+ className={`truncate font-mono transition-opacity ${+ isHidden+ ? "text-neutral-400 dark:text-neutral-600 line-through"+ : "text-neutral-600 dark:text-neutral-400"+ }`}+ >+ {s.id}+ </span></div>- </ChartTooltip>+ <span+ className={`font-medium tabular-nums transition-opacity ${+ isHidden ? "opacity-30" : ""+ }`}+ >+ {formatter(s.total)}+ </span>+ </div>);- }}- />+ })}+ <div className="flex items-center justify-between gap-3 border-t border-neutral-200 dark:border-neutral-700 pt-1.5 mt-0.5">+ <span className="text-neutral-600 dark:text-neutral-400">Total</span>+ <span className="font-semibold tabular-nums">+ {formatter(totalRows)}+ </span>+ </div>+ </div></div>);}diff --git a/client/src/app/admin/components/organizations/Organizations.tsx b/client/src/app/admin/components/organizations/Organizations.tsxindex 68e863f63..5ac3ec0cd 100644--- a/client/src/app/admin/components/organizations/Organizations.tsx+++ b/client/src/app/admin/components/organizations/Organizations.tsx@@ -3,7 +3,14 @@import { useState, useMemo } from "react";import { useAdminOrganizations } from "@/api/admin/hooks/useAdminOrganizations";import { Button } from "@/components/ui/button";-import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";+import { Tabs, TabsContent } from "@/components/ui/tabs";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";import { DateTime } from "luxon";import { ErrorAlert } from "../shared/ErrorAlert";import { AdminLayout } from "../shared/AdminLayout";@@ -21,6 +28,7 @@ export function Organizations() {const { data: organizations, isLoading, isError } = useAdminOrganizations();const t = useExtracted();+ const [activeTab, setActiveTab] = useState("growth");const [searchQuery, setSearchQuery] = useState("");// Filter states@@ -73,13 +81,18 @@ export function Organizations() {return (<AdminLayout>- <Tabs defaultValue="growth" className="mb-6">+ <Tabs value={activeTab} onValueChange={setActiveTab} className="mb-6"><div className="flex items-center justify-between mb-2">- <TabsList>- <TabsTrigger value="growth">{t("Organization Growth")}</TabsTrigger>- <TabsTrigger value="usage">{t("Service Usage")}</TabsTrigger>- <TabsTrigger value="subscriptions">{t("Subscription Tiers")}</TabsTrigger>- </TabsList>+ <Select value={activeTab} onValueChange={setActiveTab}>+ <SelectTrigger className="w-[200px]">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="growth">{t("Organization Growth")}</SelectItem>+ <SelectItem value="usage">{t("Service Usage")}</SelectItem>+ <SelectItem value="subscriptions">{t("Subscription Tiers")}</SelectItem>+ </SelectContent>+ </Select></div><TabsContent value="growth"><GrowthChart data={organizations} color="#8b5cf6" title={t("Organizations")} />diff --git a/server/src/api/admin/getClickhouseActiveQueries.ts b/server/src/api/admin/getClickhouseActiveQueries.tsdeleted file mode 100644index c537828a5..000000000--- a/server/src/api/admin/getClickhouseActiveQueries.ts+++ /dev/null@@ -1,51 +0,0 @@-import { FastifyReply, FastifyRequest } from "fastify";-import { clickhouse } from "../../db/clickhouse/clickhouse.js";--interface ActiveQueryRow {- query_id: string;- user: string;- query: string;- elapsed: number;- read_rows: string;- memory_usage: string;- query_kind: string;-}--export async function getClickhouseActiveQueries(request: FastifyRequest, reply: FastifyReply) {- try {- const activeQueriesResult = await clickhouse.query({- query: `- SELECT- query_id,- user,- substring(query, 1, 500) as query,- elapsed,- read_rows,- memory_usage,- query_kind- FROM system.processes- WHERE is_cancelled = 0- AND query NOT LIKE '%system.processes%'- ORDER BY elapsed DESC- LIMIT 20- `,- format: "JSONEachRow",- });- const activeQueries = (await activeQueriesResult.json()) as ActiveQueryRow[];-- return reply.status(200).send({- activeQueries: activeQueries.map(row => ({- queryId: row.query_id,- user: row.user,- query: row.query,- elapsed: row.elapsed,- readRows: Number(row.read_rows),- memoryUsage: Number(row.memory_usage),- queryKind: row.query_kind,- })),- });- } catch (error) {- request.log.error(error, "Failed to get ClickHouse active queries");- return reply.status(500).send({ error: "Failed to get ClickHouse active queries" });- }-}diff --git a/server/src/api/admin/getClickhouseQueryLog.ts b/server/src/api/admin/getClickhouseQueryLog.tsnew file mode 100644index 000000000..dad6f504d--- /dev/null+++ b/server/src/api/admin/getClickhouseQueryLog.ts@@ -0,0 +1,158 @@+import { FastifyReply, FastifyRequest } from "fastify";+import { clickhouse } from "../../db/clickhouse/clickhouse.js";++interface QueryLogRow {+ event_time: string;+ query_id: string;+ query_kind: string;+ query_duration_ms: string;+ read_rows: string;+ read_bytes: string;+ written_rows: string;+ written_bytes: string;+ memory_usage: string;+ type: string;+ exception_code: string;+ query: string;+ user: string;+ databases: string[];+ tables: string[];+}++interface CountRow {+ total: string;+}++export async function getClickhouseQueryLog(+ request: FastifyRequest<{+ Querystring: {+ page?: string;+ pageSize?: string;+ sortBy?: string;+ sortOrder?: string;+ queryKind?: string;+ type?: string;+ };+ }>,+ reply: FastifyReply+) {+ try {+ const page = Math.max(1, parseInt(request.query.page || "1", 10));+ const pageSize = Math.min(100, Math.max(1, parseInt(request.query.pageSize || "25", 10)));+ const sortBy = request.query.sortBy || "event_time";+ const sortOrder = request.query.sortOrder === "asc" ? "ASC" : "DESC";+ const queryKind = request.query.queryKind;+ const type = request.query.type;++ // Whitelist sortable columns to prevent injection+ const allowedSortColumns: Record<string, string> = {+ event_time: "event_time",+ query_duration_ms: "query_duration_ms",+ read_rows: "read_rows",+ read_bytes: "read_bytes",+ written_rows: "written_rows",+ written_bytes: "written_bytes",+ memory_usage: "memory_usage",+ type: "type",+ query_kind: "query_kind",+ };+ const sortColumn = allowedSortColumns[sortBy] || "event_time";++ const offset = (page - 1) * pageSize;++ // Build WHERE clause+ const conditions = [+ "event_time >= now() - INTERVAL 24 HOUR",+ "type != 'QueryStart'",+ "query NOT LIKE '%system.query_log%'",+ "query NOT LIKE '%system.processes%'",+ ];+ if (queryKind && ["Select", "Insert", "Other"].includes(queryKind)) {+ conditions.push(`query_kind = '${queryKind}'`);+ }+ if (type && ["QueryFinish", "ExceptionWhileProcessing"].includes(type)) {+ conditions.push(`type = '${type}'`);+ }++ const whereClause = conditions.join(" AND ");++ // Get total count+ const countResult = await clickhouse.query({+ query: `+ SELECT count() as total+ FROM system.query_log+ WHERE ${whereClause}+ `,+ format: "JSONEachRow",+ });+ const countRows = (await countResult.json()) as CountRow[];+ const total = Number(countRows[0]?.total || 0);++ // Get paginated results+ const queryResult = await clickhouse.query({+ query: `+ SELECT+ event_time,+ query_id,+ query_kind,+ query_duration_ms,+ read_rows,+ read_bytes,+ written_rows,+ written_bytes,+ memory_usage,+ type,+ exception_code,+ substring(query, 1, 500) as query,+ user,+ databases,+ tables+ FROM system.query_log+ WHERE ${whereClause}+ ORDER BY ${sortColumn} ${sortOrder}+ LIMIT ${pageSize}+ OFFSET ${offset}+ `,+ format: "JSONEachRow",+ });+ const rows = (await queryResult.json()) as QueryLogRow[];++ return reply.status(200).send({+ items: rows.map((row) => ({+ eventTime: row.event_time,+ queryId: row.query_id,+ queryKind: row.query_kind,+ queryDurationMs: Number(row.query_duration_ms),+ readRows: Number(row.read_rows),+ readBytes: Number(row.read_bytes),+ writtenRows: Number(row.written_rows),+ writtenBytes: Number(row.written_bytes),+ memoryUsage: Number(row.memory_usage),+ type: row.type,+ exceptionCode: Number(row.exception_code),+ query: row.query,+ user: row.user,+ databases: row.databases,+ tables: row.tables,+ })),+ total,+ page,+ pageSize,+ });+ } catch (error: any) {+ if (+ error?.message?.includes("query_log") ||+ error?.message?.includes("UNKNOWN_TABLE")+ ) {+ return reply.status(200).send({+ items: [],+ total: 0,+ page: 1,+ pageSize: 25,+ unavailable: true,+ });+ }+ request.log.error(error, "Failed to get ClickHouse query log");+ return reply.status(500).send({ error: "Failed to get ClickHouse query log" });+ }+}diff --git a/server/src/api/admin/getClickhouseStats.ts b/server/src/api/admin/getClickhouseStats.tsindex 05902af1a..d656f8178 100644--- a/server/src/api/admin/getClickhouseStats.ts+++ b/server/src/api/admin/getClickhouseStats.ts@@ -31,8 +31,10 @@ interface QueryErrorRow {query: string;}-export async function getClickhouseStats(request: FastifyRequest, reply: FastifyReply) {+export async function getClickhouseStats(request: FastifyRequest<{ Querystring: { days?: string } }>, reply: FastifyReply) {const unavailableFeatures: string[] = [];+ const days = request.query.days ? parseInt(request.query.days, 10) : 30;+ const daysInterval = days > 0 ? days : 30;try {// Get table statistics (uses system.parts - always available)@@ -56,23 +58,29 @@ export async function getClickhouseStats(request: FastifyRequest, reply: Fastify});const tableStats = (await tableStatsResult.json()) as TableStatsRow[];- // Get rows by date (last 30 days) - query actual tables for accurate daily counts+ // Get rows by date - query actual tables for accurate daily counts+ const dateFilter = days === 0+ ? ""+ : `WHERE timestamp >= now() - INTERVAL ${daysInterval} DAY`;+ const dateFilterStartTime = days === 0+ ? ""+ : `WHERE start_time >= now() - INTERVAL ${daysInterval} DAY`;const rowsByDateResult = await clickhouse.query({query: `SELECT date, table, rows_inserted FROM (SELECT toDate(timestamp) as date, 'events' as table, count() as rows_insertedFROM events- WHERE timestamp >= now() - INTERVAL 30 DAY+ ${dateFilter}GROUP BY dateUNION ALLSELECT toDate(timestamp) as date, 'session_replay_events' as table, count() as rows_insertedFROM session_replay_events- WHERE timestamp >= now() - INTERVAL 30 DAY+ ${dateFilter}GROUP BY dateUNION ALLSELECT toDate(start_time) as date, 'session_replay_metadata' as table, count() as rows_insertedFROM session_replay_metadata- WHERE start_time >= now() - INTERVAL 30 DAY+ ${dateFilterStartTime}GROUP BY date)ORDER BY date, tablediff --git a/server/src/api/admin/index.ts b/server/src/api/admin/index.tsindex b72b6cd1f..f099cf1aa 100644--- a/server/src/api/admin/index.ts+++ b/server/src/api/admin/index.ts@@ -4,4 +4,4 @@ export { getAdminOrganizations } from "./getAdminOrganizations.js";export { getAdminServiceEventCount } from "./getAdminServiceEventCount.js";export { getAdminSites } from "./getAdminSites.js";export { getClickhouseStats } from "./getClickhouseStats.js";-export { getClickhouseActiveQueries } from "./getClickhouseActiveQueries.js";+export { getClickhouseQueryLog } from "./getClickhouseQueryLog.js";diff --git a/server/src/index.ts b/server/src/index.tsindex c91ca63d6..b0d80ec88 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -11,7 +11,7 @@ import {getAdminServiceEventCount,getAdminSites,getClickhouseStats,- getClickhouseActiveQueries,+ getClickhouseQueryLog,} from "./api/admin/index.js";import {createFunnel,@@ -349,7 +349,7 @@ async function gscRoutes(fastify: FastifyInstance) {async function stripeAdminRoutes(fastify: FastifyInstance) {// ClickHouse stats (available for all admins)fastify.get("/admin/clickhouse-stats", adminOnly, getClickhouseStats);- fastify.get("/admin/clickhouse-active-queries", adminOnly, getClickhouseActiveQueries);+ fastify.get("/admin/clickhouse-query-log", adminOnly, getClickhouseQueryLog);// STRIPE & ADMINif (IS_CLOUD) {