-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
354 lines (292 loc) · 9.61 KB
/
Copy pathapp.js
File metadata and controls
354 lines (292 loc) · 9.61 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const state = {
auditId: null,
pollHandle: null,
};
const apiBaseInput = document.getElementById("apiBase");
const companyNameInput = document.getElementById("companyName");
const domainInput = document.getElementById("domain");
const flash = document.getElementById("flash");
const statusBox = document.getElementById("statusBox");
const summaryBox = document.getElementById("summaryBox");
const findingsBox = document.getElementById("findings");
const reportLink = document.getElementById("reportLink");
const runbookLink = document.getElementById("runbookLink");
const shareRunbookLink = document.getElementById("shareRunbookLink");
const actionsBox = document.getElementById("actions");
const historyBox = document.getElementById("history");
const evidenceList = document.getElementById("evidenceList");
const notesList = document.getElementById("notesList");
const gapQuestions = document.getElementById("gapQuestions");
const storedBase = localStorage.getItem("nextGenItApiBase");
apiBaseInput.value = storedBase || "http://localhost:8000";
function getApiBase() {
const value = apiBaseInput.value.trim().replace(/\/$/, "");
localStorage.setItem("nextGenItApiBase", value);
return value;
}
function setFlash(message, isError = false) {
flash.textContent = message;
flash.style.color = isError ? "var(--danger)" : "var(--muted)";
}
function setStatus(message, statusClass = "status-idle") {
statusBox.className = `status ${statusClass}`;
statusBox.textContent = message;
}
function clearPolling() {
if (state.pollHandle) {
clearInterval(state.pollHandle);
state.pollHandle = null;
}
}
async function apiFetch(path, options = {}) {
const response = await fetch(`${getApiBase()}${path}`, options);
if (!response.ok) {
let message = `Request failed (${response.status})`;
try {
const payload = await response.json();
if (payload.detail) message = payload.detail;
} catch (_) {}
throw new Error(message);
}
return response;
}
function hideArtifactLinks() {
reportLink.classList.add("hidden");
runbookLink.classList.add("hidden");
shareRunbookLink.classList.add("hidden");
reportLink.removeAttribute("href");
runbookLink.removeAttribute("href");
shareRunbookLink.removeAttribute("href");
}
function showArtifactLinks(audit) {
const apiBase = getApiBase();
reportLink.classList.remove("hidden");
reportLink.href = `${apiBase}/api/audits/${audit.id}/report`;
runbookLink.classList.remove("hidden");
runbookLink.href = `${apiBase}/api/audits/${audit.id}/runbook`;
shareRunbookLink.classList.remove("hidden");
shareRunbookLink.href = `${apiBase}/share/${audit.id}/runbook`;
}
function renderFindings(findings) {
if (!findings || findings.length === 0) {
findingsBox.className = "findings empty";
findingsBox.innerHTML = "No findings yet.";
return;
}
findingsBox.className = "findings";
findingsBox.innerHTML = findings.map((finding) => `
<article class="finding">
<div class="badge badge-${finding.severity}">${finding.severity}</div>
<h3>${finding.title}</h3>
<p><strong>Category:</strong> ${finding.category}</p>
<p>${finding.description}</p>
<p><strong>Recommendation:</strong> ${finding.recommendation}</p>
<p class="meta"><strong>Evidence:</strong> ${finding.evidence}</p>
</article>
`).join("");
}
function renderEvidence(items) {
if (!items || items.length === 0) {
evidenceList.innerHTML = "<li>No evidence uploaded.</li>";
return;
}
evidenceList.innerHTML = items
.map((item) => `<li>${item.filename} <span class="meta">(${item.content_type})</span></li>`)
.join("");
}
function renderNotes(items) {
if (!items || items.length === 0) {
notesList.innerHTML = "<li>No notes yet.</li>";
return;
}
notesList.innerHTML = items
.map((item) => `<li><strong>${item.source}:</strong> ${item.content}</li>`)
.join("");
}
function renderQuestions(questions) {
if (!questions || questions.length === 0) {
gapQuestions.innerHTML = "<li>No follow-up questions yet.</li>";
return;
}
gapQuestions.innerHTML = questions.map((q) => `<li>${q}</li>`).join("");
}
async function loadGapQuestions(auditId) {
try {
const response = await apiFetch(`/api/audits/${auditId}/gaps`);
const payload = await response.json();
renderQuestions(payload.questions || []);
} catch (error) {
renderQuestions([`Could not load follow-up questions: ${error.message}`]);
}
}
async function loadAudit(auditId) {
const response = await apiFetch(`/api/audits/${auditId}`);
const audit = await response.json();
state.auditId = audit.id;
const statusClass = audit.status === "completed"
? "status-completed"
: audit.status === "failed"
? "status-failed"
: audit.status === "running" || audit.status === "queued"
? "status-running"
: "status-idle";
setStatus(`Audit ${audit.status}`, statusClass);
if (audit.summary) {
summaryBox.classList.remove("hidden");
summaryBox.textContent = audit.summary;
} else {
summaryBox.classList.add("hidden");
summaryBox.textContent = "";
}
actionsBox.classList.remove("hidden");
if (audit.status === "completed") {
showArtifactLinks(audit);
clearPolling();
} else {
hideArtifactLinks();
}
if (audit.status === "failed") {
clearPolling();
setFlash(audit.error || "Audit failed.", true);
}
renderFindings(audit.findings);
renderEvidence(audit.evidence_items);
renderNotes(audit.notes);
await loadGapQuestions(audit.id);
}
async function runAudit() {
const domain = domainInput.value.trim();
const companyName = companyNameInput.value.trim();
if (!domain) {
setFlash("Please enter a domain.", true);
return;
}
setFlash("Starting audit...");
setStatus("Submitting audit...", "status-running");
renderFindings([]);
renderEvidence([]);
renderNotes([]);
renderQuestions([]);
hideArtifactLinks();
clearPolling();
try {
const response = await apiFetch("/api/audits", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
domain,
company_name: companyName || null,
}),
});
const payload = await response.json();
state.auditId = payload.audit_id;
await loadAudit(state.auditId);
state.pollHandle = setInterval(() => loadAudit(state.auditId), 2500);
setFlash(`Audit queued: ${state.auditId}`);
} catch (error) {
setFlash(error.message, true);
setStatus("Audit could not be started.", "status-failed");
}
}
async function loadHistory() {
try {
const response = await apiFetch("/api/audits");
const audits = await response.json();
if (!audits.length) {
historyBox.className = "history empty";
historyBox.textContent = "No audits found.";
return;
}
historyBox.className = "history";
historyBox.innerHTML = audits.map((audit) => `
<div class="history-item">
<div><strong>${audit.company_name || audit.domain}</strong></div>
<div class="meta">${audit.domain} · ${audit.status} · score ${audit.score}</div>
<div class="row">
<button data-audit-id="${audit.id}" class="secondary history-load">Open</button>
${
audit.status === "completed"
? `<a class="button-link secondary" target="_blank" rel="noreferrer" href="${getApiBase()}/share/${audit.id}/runbook">Share Runbook</a>`
: ""
}
</div>
</div>
`).join("");
document.querySelectorAll(".history-load").forEach((btn) => {
btn.addEventListener("click", async (event) => {
const auditId = event.target.dataset.auditId;
await loadAudit(auditId);
});
});
} catch (error) {
historyBox.className = "history empty";
historyBox.textContent = `Could not load history: ${error.message}`;
}
}
async function uploadEvidence() {
const fileInput = document.getElementById("evidenceFile");
const file = fileInput.files[0];
if (!state.auditId) {
setFlash("Run or load an audit first.", true);
return;
}
if (!file) {
setFlash("Select a file to upload.", true);
return;
}
const formData = new FormData();
formData.append("file", file);
try {
await apiFetch(`/api/audits/${state.auditId}/evidence`, {
method: "POST",
body: formData,
});
setFlash("Evidence uploaded.");
fileInput.value = "";
await loadAudit(state.auditId);
} catch (error) {
setFlash(error.message, true);
}
}
async function saveNote() {
const noteText = document.getElementById("noteText");
const content = noteText.value.trim();
if (!state.auditId) {
setFlash("Run or load an audit first.", true);
return;
}
if (!content) {
setFlash("Enter a note first.", true);
return;
}
try {
await apiFetch(`/api/audits/${state.auditId}/notes`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
source: "portal",
content,
}),
});
noteText.value = "";
setFlash("Note saved.");
await loadAudit(state.auditId);
} catch (error) {
setFlash(error.message, true);
}
}
document.getElementById("runAuditBtn").addEventListener("click", runAudit);
document.getElementById("loadAuditsBtn").addEventListener("click", loadHistory);
document.getElementById("refreshBtn").addEventListener("click", async () => {
if (!state.auditId) {
setFlash("No audit selected.", true);
return;
}
await loadAudit(state.auditId);
});
document.getElementById("uploadBtn").addEventListener("click", uploadEvidence);
document.getElementById("saveNoteBtn").addEventListener("click", saveNote);