Skip to content

Commit 6706142

Browse files
committed
fix: Improve progress calculation and centralize API state mapping for clarity
1 parent eac1aa6 commit 6706142

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

src/tools/rca-agent-utils/rca-data.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,33 @@ function calculateProgress(
3333
totalCount: number,
3434
baseProgress: number = 10,
3535
): number {
36+
if (totalCount === 0) return 100; // ✅ fix divide by zero
3637
const progressRange = 90 - baseProgress;
3738
const completionProgress = (resolvedCount / totalCount) * progressRange;
3839
return Math.min(100, baseProgress + completionProgress);
3940
}
4041

42+
// ✅ centralized mapping function
43+
function mapApiState(apiState?: string): RCAState {
44+
const state = apiState?.toLowerCase();
45+
switch (state) {
46+
case "completed":
47+
return RCAState.COMPLETED;
48+
case "pending":
49+
return RCAState.PENDING;
50+
case "fetching_logs":
51+
return RCAState.FETCHING_LOGS;
52+
case "generating_rca":
53+
return RCAState.GENERATING_RCA;
54+
case "generated_rca":
55+
return RCAState.GENERATED_RCA;
56+
case "error":
57+
return RCAState.UNKNOWN_ERROR;
58+
default:
59+
return RCAState.UNKNOWN_ERROR;
60+
}
61+
}
62+
4163
async function notifyProgress(
4264
context: ScanProgressContext | undefined,
4365
message: string,
@@ -96,22 +118,7 @@ async function fetchInitialRCA(
96118
}
97119

98120
const data = await response.json();
99-
100-
const apiState = data.state?.toLowerCase();
101-
let resultState: RCAState;
102-
103-
if (apiState === "completed") resultState = RCAState.COMPLETED;
104-
else if (apiState === "pending") resultState = RCAState.PENDING;
105-
else if (apiState === "fetching_logs") resultState = RCAState.FETCHING_LOGS;
106-
else if (apiState === "generating_rca")
107-
resultState = RCAState.GENERATING_RCA;
108-
else if (apiState === "generated_rca") resultState = RCAState.GENERATED_RCA;
109-
else if (apiState === "processing" || apiState === "running")
110-
resultState = RCAState.GENERATING_RCA;
111-
else if (apiState === "failed" || apiState === "error")
112-
resultState = RCAState.UNKNOWN_ERROR;
113-
else if (apiState) resultState = RCAState.UNKNOWN_ERROR;
114-
else resultState = RCAState.PENDING;
121+
const resultState = mapApiState(data.state); // ✅ reuse helper
115122

116123
return {
117124
id: testId,
@@ -185,29 +192,14 @@ async function pollRCAResults(
185192

186193
const data = await response.json();
187194
if (!isFailedState(tc.state)) {
188-
const apiState = data.state?.toLowerCase();
189-
if (apiState === "completed") {
190-
tc.state = RCAState.COMPLETED;
195+
const mappedState = mapApiState(data.state); // ✅ reuse helper
196+
tc.state = mappedState;
197+
198+
if (mappedState === RCAState.COMPLETED) {
191199
tc.rcaData = data;
192-
} else if (apiState === "failed" || apiState === "error") {
193-
tc.state = RCAState.UNKNOWN_ERROR;
194-
tc.rcaData = {
195-
error: `API returned error state: ${data.state}`,
196-
originalResponse: data,
197-
};
198-
} else if (apiState === "pending") tc.state = RCAState.PENDING;
199-
else if (apiState === "fetching_logs")
200-
tc.state = RCAState.FETCHING_LOGS;
201-
else if (apiState === "generating_rca")
202-
tc.state = RCAState.GENERATING_RCA;
203-
else if (apiState === "generated_rca")
204-
tc.state = RCAState.GENERATED_RCA;
205-
else if (apiState === "processing" || apiState === "running")
206-
tc.state = RCAState.GENERATING_RCA;
207-
else {
208-
tc.state = RCAState.UNKNOWN_ERROR;
200+
} else if (mappedState === RCAState.UNKNOWN_ERROR) {
209201
tc.rcaData = {
210-
error: `API returned unknown state: ${data.state}`,
202+
error: `API returned state: ${data.state}`,
211203
originalResponse: data,
212204
};
213205
}

0 commit comments

Comments
 (0)