File size: 3,454 Bytes
d084ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
  useQuery,
  useMutation,
} from '@tanstack/react-query';
import { apiClient } from '@/services/api-client';
import type {
  OverwatchDocumentsParams,
  OverwatchEventsParams,
  OverwatchInsights,
} from '@/types';

export const overwatchKeys = {
  all: ['overwatch'] as const,
  insights: () => [...overwatchKeys.all, 'insights'] as const,
  logs: (filters?: Record<string, unknown>) =>
    [...overwatchKeys.all, 'logs', filters ?? {}] as const,
  documents: (params?: OverwatchDocumentsParams) =>
    [...overwatchKeys.all, 'documents', params ?? {}] as const,
  documentSummary: (documentId: string) =>
    [...overwatchKeys.all, 'document', documentId, 'summary'] as const,
  events: (params?: OverwatchEventsParams) =>
    [...overwatchKeys.all, 'events', params ?? {}] as const,
};

// ── Legacy UAT hooks (kept for backward compatibility) ────────────────────────

export const useOverwatchInsights = () => {
  return useQuery<OverwatchInsights>({
    queryKey: overwatchKeys.insights(),
    queryFn: () => apiClient.getOverwatchInsights(),
    refetchInterval: 30 * 1000,
  });
};

export const useOverwatchLogs = (params: {

  log_type: 'audit' | 'classification';

  username?: string;

  action?: string;

  category?: string;

  engine?: string;

  from_date?: string;

  to_date?: string;

  search?: string;

  page?: number;

  page_size?: number;

}) => {
  return useQuery({
    queryKey: overwatchKeys.logs(params),
    queryFn: () => apiClient.getOverwatchLogs(params),
  });
};

export const useEmailOverwatchReport = () => {
  return useMutation({
    mutationFn: (payload: {

      email: string;

      log_type: 'audit' | 'classification' | 'both';

      from_date?: string;

      to_date?: string;

      username?: string;

      category?: string;

      engine?: string;

    }) => apiClient.emailOverwatchReport(payload),
  });
};

// ── Structured Event Store hooks ──────────────────────────────────────────────

/** Paginated list of documents with their processing health summary. */
export const useOverwatchDocuments = (params: OverwatchDocumentsParams) => {
  return useQuery({
    queryKey: overwatchKeys.documents(params),
    queryFn: () => apiClient.getOverwatchDocuments(params),
    staleTime: 10_000,
  });
};

/**

 * Full detail for a single document: extraction pipeline, classification

 * summary, and counters. Returns `isError` on 404 (no events yet) or 422.

 */
export const useOverwatchDocumentSummary = (documentId: string) => {
  return useQuery({
    queryKey: overwatchKeys.documentSummary(documentId),
    queryFn: () => apiClient.getOverwatchDocumentSummary(documentId),
    enabled: documentId.length > 0,
    retry: (failureCount, error: unknown) => {
      // Don't retry on 404/422 β€” these are definitive responses
      const status = (error as { response?: { status?: number } })?.response?.status;
      if (status === 404 || status === 422) return false;
      return failureCount < 2;
    },
  });
};

/** Paginated, filterable event log. */
export const useOverwatchEvents = (params: OverwatchEventsParams) => {
  return useQuery({
    queryKey: overwatchKeys.events(params),
    queryFn: () => apiClient.getOverwatchEvents(params),
  });
};