File size: 3,837 Bytes
d1ac27a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
 * Typed fetch wrappers for the Regression RCA workbench API (/api/rca).
 * Mirrors the style of services/megaeval.ts.
 */
import type {
  Confidence,
  DecisionTree,
  DiagnosticsResponse,
  Experiments,
  Finding,
  FindingStatus,
  Investigation,
  InvestigationSummary,
  RcaReport,
  RevealResponse,
  RootCause,
  Scoreboard,
  Severity,
  StageMeta,
  StageState,
} from '../types/rca';

async function handle<T>(res: Response): Promise<T> {
  if (!res.ok) {
    let detail = '';
    try {
      const body = await res.json();
      detail = typeof body?.detail === 'string' ? body.detail : JSON.stringify(body);
    } catch {
      detail = await res.text().catch(() => '');
    }
    const err = new Error(detail || res.statusText) as Error & { status?: number };
    err.status = res.status;
    throw err;
  }
  return res.json() as Promise<T>;
}

const JSON_HEADERS = { 'Content-Type': 'application/json' };

const get = <T,>(url: string): Promise<T> => fetch(url).then((r) => handle<T>(r));
const post = <T,>(url: string, body?: unknown): Promise<T> =>
  fetch(url, {
    method: 'POST',
    headers: JSON_HEADERS,
    body: body === undefined ? undefined : JSON.stringify(body),
  }).then((r) => handle<T>(r));

export const getCauses = () =>
  get<{ causes: { id: RootCause; label: string }[] }>('/api/rca/causes');

export const getStages = () => get<{ stages: StageMeta[] }>('/api/rca/stages');

export const createInvestigation = (body: {
  name?: string;
  baseline_model?: string;
  candidate_model?: string;
  cause?: RootCause | null;
  seed?: number | null;
  training_mode?: boolean;
}) => post<Investigation>('/api/rca/investigations', body);

export const listInvestigations = () =>
  get<{ investigations: InvestigationSummary[] }>('/api/rca/investigations');

export const getInvestigation = (id: string) =>
  get<Investigation>(`/api/rca/investigations/${encodeURIComponent(id)}`);

export const completeStage = (id: string, index: number, body: {
  acknowledge_unknowns?: boolean;
  note?: string;
}) =>
  post<{ stage: StageState; investigation: Investigation }>(
    `/api/rca/investigations/${encodeURIComponent(id)}/stages/${index}/complete`,
    body
  );

export const reopenStage = (id: string, index: number) =>
  post<{ stage: StageState; investigation: Investigation }>(
    `/api/rca/investigations/${encodeURIComponent(id)}/stages/${index}/reopen`
  );

export const recordFinding = (id: string, body: {
  stage: string;
  title: string;
  status: FindingStatus;
  severity: Severity;
  detail?: string;
  code?: string;
}) =>
  post<{ finding: Finding }>(
    `/api/rca/investigations/${encodeURIComponent(id)}/findings`,
    body
  );

export const getDiagnostics = (id: string, stageKey: string) =>
  get<DiagnosticsResponse>(
    `/api/rca/investigations/${encodeURIComponent(id)}/diagnostics/${encodeURIComponent(stageKey)}`
  );

export const getScoreboard = (id: string, recordedOnly = false) =>
  get<Scoreboard>(
    `/api/rca/investigations/${encodeURIComponent(id)}/scoreboard${recordedOnly ? '?recorded_only=true' : ''}`
  );

export const assessHypothesis = (id: string, body: {
  hypothesis: RootCause;
  confidence: Confidence;
  note?: string;
}) =>
  post<Scoreboard>(
    `/api/rca/investigations/${encodeURIComponent(id)}/scoreboard/assess`,
    body
  );

export const getDecisionTree = (id: string) =>
  get<DecisionTree>(`/api/rca/investigations/${encodeURIComponent(id)}/decision-tree`);

export const getExperiments = (id: string) =>
  get<Experiments>(`/api/rca/investigations/${encodeURIComponent(id)}/experiments`);

export const getReport = (id: string) =>
  get<RcaReport>(`/api/rca/investigations/${encodeURIComponent(id)}/report`);

export const revealCause = (id: string) =>
  post<RevealResponse>(`/api/rca/investigations/${encodeURIComponent(id)}/reveal`);