2026-03-17 16:09:31 +01:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
import { adminFetch } from './admin-api';
|
|
|
|
|
|
|
|
|
|
export interface DatabaseStatus {
|
|
|
|
|
connected: boolean;
|
|
|
|
|
version: string;
|
|
|
|
|
host: string;
|
|
|
|
|
schema: string;
|
2026-03-17 16:36:11 +01:00
|
|
|
timescaleDb: boolean;
|
2026-03-17 16:09:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface PoolStats {
|
|
|
|
|
activeConnections: number;
|
|
|
|
|
idleConnections: number;
|
2026-03-17 16:36:11 +01:00
|
|
|
pendingThreads: number;
|
|
|
|
|
maxPoolSize: number;
|
|
|
|
|
maxWaitMs: number;
|
2026-03-17 16:09:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TableInfo {
|
|
|
|
|
tableName: string;
|
2026-03-17 16:36:11 +01:00
|
|
|
rowCount: number;
|
2026-03-17 16:09:31 +01:00
|
|
|
dataSize: string;
|
|
|
|
|
indexSize: string;
|
2026-03-17 16:36:11 +01:00
|
|
|
dataSizeBytes: number;
|
|
|
|
|
indexSizeBytes: number;
|
2026-03-17 16:09:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ActiveQuery {
|
|
|
|
|
pid: number;
|
2026-03-17 16:36:11 +01:00
|
|
|
durationSeconds: number;
|
2026-03-17 16:09:31 +01:00
|
|
|
state: string;
|
|
|
|
|
query: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDatabaseStatus() {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: ['admin', 'database', 'status'],
|
|
|
|
|
queryFn: () => adminFetch<DatabaseStatus>('/database/status'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDatabasePool() {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: ['admin', 'database', 'pool'],
|
|
|
|
|
queryFn: () => adminFetch<PoolStats>('/database/pool'),
|
|
|
|
|
refetchInterval: 15000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDatabaseTables() {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: ['admin', 'database', 'tables'],
|
|
|
|
|
queryFn: () => adminFetch<TableInfo[]>('/database/tables'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDatabaseQueries() {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: ['admin', 'database', 'queries'],
|
|
|
|
|
queryFn: () => adminFetch<ActiveQuery[]>('/database/queries'),
|
|
|
|
|
refetchInterval: 15000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useKillQuery() {
|
|
|
|
|
const qc = useQueryClient();
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: async (pid: number) => {
|
2026-03-17 16:36:11 +01:00
|
|
|
await adminFetch<void>(`/database/queries/${pid}/kill`, { method: 'POST' });
|
2026-03-17 16:09:31 +01:00
|
|
|
},
|
|
|
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['admin', 'database', 'queries'] }),
|
|
|
|
|
});
|
|
|
|
|
}
|