6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
"use client";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { useCallback, useMemo } from "react";
|
|
import type { LogFilters } from "@/contracts/types/log.types";
|
|
import { logsApiService } from "@/lib/apis/logs-api.service";
|
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
|
|
|
export type {
|
|
Log,
|
|
LogFilters,
|
|
LogLevel,
|
|
LogStatus,
|
|
LogSummary,
|
|
} from "@/contracts/types/log.types";
|
|
|
|
export function useLogs(workspaceId?: number, filters: LogFilters = {}) {
|
|
const filtersKey = JSON.stringify(filters);
|
|
// biome-ignore lint/correctness/useExhaustiveDependencies: stable serialized key used intentionally
|
|
const memoizedFilters = useMemo(() => filters, [filtersKey]);
|
|
|
|
const buildQueryParams = useCallback(
|
|
(customFilters: LogFilters = {}) => {
|
|
const params: Record<string, string> = {};
|
|
|
|
const allFilters = { ...memoizedFilters, ...customFilters };
|
|
|
|
if (allFilters.workspace_id) {
|
|
params.workspace_id = allFilters.workspace_id.toString();
|
|
}
|
|
if (allFilters.level) {
|
|
params.level = allFilters.level;
|
|
}
|
|
if (allFilters.status) {
|
|
params.status = allFilters.status;
|
|
}
|
|
if (allFilters.source) {
|
|
params.source = allFilters.source;
|
|
}
|
|
if (allFilters.start_date) {
|
|
params.start_date = allFilters.start_date;
|
|
}
|
|
if (allFilters.end_date) {
|
|
params.end_date = allFilters.end_date;
|
|
}
|
|
|
|
return params;
|
|
},
|
|
[memoizedFilters]
|
|
);
|
|
|
|
const {
|
|
data: logs,
|
|
isLoading: loading,
|
|
error,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: cacheKeys.logs.withQueryParams({
|
|
workspace_id: workspaceId,
|
|
...buildQueryParams(filters ?? {}),
|
|
}),
|
|
queryFn: () =>
|
|
logsApiService.getLogs({
|
|
queryParams: {
|
|
workspace_id: workspaceId,
|
|
...buildQueryParams(filters ?? {}),
|
|
},
|
|
}),
|
|
enabled: !!workspaceId,
|
|
staleTime: 3 * 60 * 1000,
|
|
});
|
|
|
|
return {
|
|
logs: logs ?? [],
|
|
loading,
|
|
error,
|
|
refreshLogs: refetch,
|
|
};
|
|
}
|
|
|
|
// Separate hook for log summary with smart polling support for document processing indicator UI
|
|
// Polling only happens when there are active tasks, otherwise it stops to save resources
|
|
export function useLogsSummary(
|
|
workspaceId: number,
|
|
hours: number = 24,
|
|
options: { refetchInterval?: number; enablePolling?: boolean } = {}
|
|
) {
|
|
const { enablePolling = false, refetchInterval = 10000 } = options;
|
|
|
|
const {
|
|
data: summary,
|
|
isLoading: loading,
|
|
error,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: cacheKeys.logs.summary(workspaceId),
|
|
queryFn: () =>
|
|
logsApiService.getLogSummary({
|
|
workspace_id: workspaceId,
|
|
hours: hours,
|
|
}),
|
|
enabled: !!workspaceId,
|
|
staleTime: 3 * 60 * 1000,
|
|
// Always refetch on mount to show fresh processing tasks when navigating to the page
|
|
refetchOnMount: "always",
|
|
// Smart polling: only poll when there are active tasks and polling is enabled
|
|
// This prevents unnecessary API calls when nothing is being processed
|
|
refetchInterval: enablePolling
|
|
? (query) => {
|
|
const data = query.state.data;
|
|
// Only continue polling if there are active tasks
|
|
if (data?.active_tasks && data.active_tasks.length > 0) {
|
|
return refetchInterval;
|
|
}
|
|
// No active tasks - stop polling but check again after a longer interval
|
|
// to catch any newly started tasks
|
|
return 30000; // Check every 30 seconds when idle
|
|
}
|
|
: undefined,
|
|
});
|
|
|
|
return { summary, loading, error, refreshSummary: refetch };
|
|
}
|