107 lines
2.6 KiB
TypeScript
107 lines
2.6 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { adminFetch } from './admin-api';
|
|
|
|
export interface OpenSearchStatus {
|
|
reachable: boolean;
|
|
clusterHealth: string;
|
|
version: string;
|
|
nodeCount: number;
|
|
host: string;
|
|
}
|
|
|
|
export interface PipelineStats {
|
|
queueDepth: number;
|
|
maxQueueSize: number;
|
|
indexedCount: number;
|
|
failedCount: number;
|
|
debounceMs: number;
|
|
indexingRate: number;
|
|
lastIndexedAt: string | null;
|
|
}
|
|
|
|
export interface IndexInfo {
|
|
name: string;
|
|
health: string;
|
|
docCount: number;
|
|
size: string;
|
|
sizeBytes: number;
|
|
primaryShards: number;
|
|
replicaShards: number;
|
|
}
|
|
|
|
export interface IndicesPageResponse {
|
|
indices: IndexInfo[];
|
|
totalIndices: number;
|
|
totalDocs: number;
|
|
totalSize: string;
|
|
page: number;
|
|
pageSize: number;
|
|
totalPages: number;
|
|
}
|
|
|
|
export interface PerformanceStats {
|
|
queryCacheHitRate: number;
|
|
requestCacheHitRate: number;
|
|
searchLatencyMs: number;
|
|
indexingLatencyMs: number;
|
|
jvmHeapUsedBytes: number;
|
|
jvmHeapMaxBytes: number;
|
|
}
|
|
|
|
export interface IndicesParams {
|
|
search?: string;
|
|
page?: number;
|
|
size?: number;
|
|
}
|
|
|
|
export function useOpenSearchStatus() {
|
|
return useQuery({
|
|
queryKey: ['admin', 'opensearch', 'status'],
|
|
queryFn: () => adminFetch<OpenSearchStatus>('/opensearch/status'),
|
|
});
|
|
}
|
|
|
|
export function usePipelineStats() {
|
|
return useQuery({
|
|
queryKey: ['admin', 'opensearch', 'pipeline'],
|
|
queryFn: () => adminFetch<PipelineStats>('/opensearch/pipeline'),
|
|
refetchInterval: 15000,
|
|
});
|
|
}
|
|
|
|
export function useIndices(params: IndicesParams) {
|
|
const query = new URLSearchParams();
|
|
if (params.search) query.set('search', params.search);
|
|
if (params.page !== undefined) query.set('page', String(params.page));
|
|
if (params.size !== undefined) query.set('size', String(params.size));
|
|
const qs = query.toString();
|
|
|
|
return useQuery({
|
|
queryKey: ['admin', 'opensearch', 'indices', params],
|
|
queryFn: () =>
|
|
adminFetch<IndicesPageResponse>(
|
|
`/opensearch/indices${qs ? `?${qs}` : ''}`,
|
|
),
|
|
});
|
|
}
|
|
|
|
export function usePerformanceStats() {
|
|
return useQuery({
|
|
queryKey: ['admin', 'opensearch', 'performance'],
|
|
queryFn: () => adminFetch<PerformanceStats>('/opensearch/performance'),
|
|
refetchInterval: 15000,
|
|
});
|
|
}
|
|
|
|
export function useDeleteIndex() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (indexName: string) => {
|
|
await adminFetch<void>(`/opensearch/indices/${encodeURIComponent(indexName)}`, {
|
|
method: 'DELETE',
|
|
});
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['admin', 'opensearch', 'indices'] }),
|
|
});
|
|
}
|