Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
392 lines (336 loc) · 13 KB
/
Copy pathscript.js
File metadata and controls
392 lines (336 loc) · 13 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Data Discovery & Governance Mapping - script.js
*/
const CONFIG = {
API_BASE_URL: 'https://cms-datadiscovery-be.securedapp.io'
};
// 1. App State
const appState = {
source: {
name: "",
dbType: ""
},
schemaRaw: "", // Store raw content from .sql file
schema: [], // [{ table, column, datatype }]
aiMappings: [], // [{ table, column, datatype, suggested_field_id, display_name, category, sensitivity, confidence, reasoning, status }]
governanceMap: []
};
/// 2. Data Catalogue (Synced from Backend)
let DATA_CATALOGUE = [];
async function fetchCatalogue() {
try {
const response = await fetch(`${CONFIG.API_BASE_URL}/api/catalogue`);
const data = await response.json();
DATA_CATALOGUE = data.catalogue;
populateRemapSelect();
} catch (error) {
console.error("Failed to fetch catalogue:", error);
showToast("Error loading Data Catalogue");
}
}
// 3. Selectors
const selectors = {
loginScreen: document.getElementById('login-screen'),
appContent: document.getElementById('app-content'),
userName: document.getElementById('user-name'),
userAvatar: document.getElementById('user-avatar'),
logoutBtn: document.getElementById('logout-btn'),
sourceName: document.getElementById('source-name'),
dbType: document.getElementById('db-type'),
saveSourceBtn: document.getElementById('save-source-btn'),
fileInput: document.getElementById('file-input'),
dropZone: document.getElementById('drop-zone'),
processSchemaBtn: document.getElementById('process-schema-btn'),
parsedTableBody: document.querySelector('#parsed-table tbody'),
resultsTableBody: document.querySelector('#results-table tbody'),
summaryGrid: document.getElementById('summary-grid'),
downloadJsonBtn: document.getElementById('download-json'),
downloadCsvBtn: document.getElementById('download-csv'),
toastContainer: document.getElementById('toast-container'),
remapModal: document.getElementById('remap-modal'),
remapClose: document.getElementById('remap-close'),
remapSelect: document.getElementById('remap-select'),
remapConfirm: document.getElementById('remap-confirm'),
themeToggle: document.getElementById('theme-toggle')
};
let currentRemapIndex = null;
// 4. Initial Setup
window.onload = () => {
initGoogleAuth();
fetchCatalogue();
// Check for existing session
const savedUser = localStorage.getItem('gov_ai_user');
if (savedUser) {
appState.user = JSON.parse(savedUser);
updateAuthUI(appState.user);
showToast(`Welcome back, ${appState.user.given_name}!`);
}
// Theme Toggle Logic
const savedTheme = localStorage.getItem('gov_ai_theme') || 'light';
if (savedTheme === 'light') {
document.documentElement.setAttribute('data-theme', 'light');
}
selectors.themeToggle.onclick = () => {
const isLight = document.documentElement.getAttribute('data-theme') === 'light';
if (isLight) {
document.documentElement.removeAttribute('data-theme');
localStorage.setItem('gov_ai_theme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('gov_ai_theme', 'light');
}
};
};
// Google Auth Logic
function initGoogleAuth() {
if (!window.google || !window.google.accounts) {
console.error("Google Identity Services not loaded.");
return;
}
window.google.accounts.id.initialize({
client_id: "67645799772-7ulh4borrg608nnb61kvf9g08utmqksr.apps.googleusercontent.com",
callback: handleAuthResponse
});
window.google.accounts.id.renderButton(
document.getElementById("google-login-btn"),
{ theme: "filled_blue", size: "large", width: 250, shape: "pill" }
);
}
function handleAuthResponse(response) {
const payload = decodeJwt(response.credential);
appState.user = payload;
// Persist session
localStorage.setItem('gov_ai_user', JSON.stringify(payload));
updateAuthUI(payload);
showToast(`Welcome, ${payload.given_name}!`);
}
function updateAuthUI(user) {
selectors.userName.innerText = user.name;
selectors.userAvatar.src = user.picture;
selectors.loginScreen.hidden = true;
selectors.appContent.hidden = false;
}
function decodeJwt(token) {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
selectors.logoutBtn.onclick = () => {
localStorage.removeItem('gov_ai_user');
location.reload();
};
// 5. Functions
function showToast(message) {
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerText = message;
selectors.toastContainer.appendChild(toast);
setTimeout(() => toast.remove(), 3000);
}
window.toggleSection = (wrapperId) => {
const wrapper = document.getElementById(wrapperId);
if (!wrapper) return;
const card = wrapper.closest('.card');
card.classList.toggle('collapsed');
};
// Populate remap dropdown
function populateRemapSelect() {
selectors.remapSelect.innerHTML = DATA_CATALOGUE.map(item =>
`<option value="${item.field_id}">${item.display_name} (${item.category})</option>`
).join('');
}
// Save Source
selectors.saveSourceBtn.addEventListener('click', () => {
appState.source.name = selectors.sourceName.value;
appState.source.dbType = selectors.dbType.value;
if (!appState.source.name) return showToast("Please enter a source name");
if (!appState.source.dbType) return showToast("Please select a DB type");
showToast("Source configuration saved!");
});
// Parse & Discover Logic (Combined)
selectors.processSchemaBtn.addEventListener('click', async () => {
const { name, dbType } = appState.source;
const schemaRaw = appState.schemaRaw;
if (!name || !dbType) return showToast("Please save source info first");
if (!schemaRaw) return showToast("Please upload a .sql file first");
selectors.processSchemaBtn.disabled = true;
selectors.processSchemaBtn.innerHTML = `Processing <div class="spinner"></div>`;
try {
const response = await fetch(`${CONFIG.API_BASE_URL}/api/discovery`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
schemaRaw,
dbType
})
});
if (!response.ok) throw new Error("Processing failed");
const data = await response.json();
appState.schema = data.mappings.map(m => ({ table: m.table, column: m.column, datatype: m.datatype }));
appState.aiMappings = data.mappings;
renderParsedTable();
renderResultsTable();
updateSummary();
showToast(`Discovery completed successfully!`);
} catch (error) {
console.error(error);
showToast("Error processing schema via backend");
} finally {
selectors.processSchemaBtn.disabled = false;
selectors.processSchemaBtn.innerText = "Process & Run Discovery";
}
});
function renderParsedTable() {
// Sort by table, then column
appState.schema.sort((a, b) => {
const tableComp = a.table.localeCompare(b.table);
if (tableComp !== 0) return tableComp;
return a.column.localeCompare(b.column);
});
selectors.parsedTableBody.innerHTML = appState.schema.map(row => `
<tr>
<td>${row.table}</td>
<td>${row.column}</td>
<td><code>${row.datatype || 'N/A'}</code></td>
</tr>
`).join('');
}
function renderResultsTable() {
// Sort by table, then column
appState.aiMappings.sort((a, b) => {
const tableComp = a.table.localeCompare(b.table);
if (tableComp !== 0) return tableComp;
return a.column.localeCompare(b.column);
});
selectors.resultsTableBody.innerHTML = appState.aiMappings.map((row, index) => `
<tr>
<td>${row.table}</td>
<td>${row.column}</td>
<td><span style="font-weight:600; color:var(--accent);">${row.display_name}</span></td>
<td>${row.category}</td>
<td><span class="badge" style="background: ${getSensitivityColor(row.sensitivity)}22; color: ${getSensitivityColor(row.sensitivity)}; border-color: ${getSensitivityColor(row.sensitivity)}44;">${row.sensitivity}</span></td>
<td><span class="confidence-${getConfidenceClass(row.confidence)}">${row.confidence}%</span></td>
<td><small style="color:var(--text-muted)">${row.reasoning}</small></td>
<td><span class="badge badge-${row.status}">${row.status}</span></td>
<td>
<div class="btn-group" style="gap:4px">
<button class="primary-btn" style="padding: 6px 10px; font-size: 11px;" onclick="updateStatus(${index}, 'approved')">Approve</button>
<button class="secondary-btn" style="padding: 6px 10px; font-size: 11px;" onclick="openRemap(${index})">Remap</button>
</div>
</td>
</tr>
`).join('');
}
function getSensitivityColor(s) {
if (s === 'high' || s === 'critical') return '#ef4444';
if (s === 'medium') return '#f59e0b';
return '#10b981';
}
function getConfidenceClass(c) {
if (c > 80) return 'high';
if (c > 50) return 'medium';
return 'low';
}
// Actions
window.updateStatus = (index, status) => {
appState.aiMappings[index].status = status;
renderResultsTable();
updateSummary();
showToast(`Row marked as ${status}`);
};
window.openRemap = (index) => {
currentRemapIndex = index;
selectors.remapModal.hidden = false;
};
selectors.remapClose.onclick = () => selectors.remapModal.hidden = true;
selectors.remapConfirm.onclick = () => {
const fieldId = selectors.remapSelect.value;
const catalogueItem = DATA_CATALOGUE.find(i => i.field_id === fieldId);
if (currentRemapIndex !== null) {
const row = appState.aiMappings[currentRemapIndex];
row.suggested_field_id = catalogueItem.field_id;
row.display_name = catalogueItem.display_name;
row.category = catalogueItem.category;
row.sensitivity = catalogueItem.sensitivity;
row.status = 'remapped';
row.reasoning = "Manually Override";
row.confidence = 100;
}
selectors.remapModal.hidden = true;
renderResultsTable();
updateSummary();
showToast("Column remapped successfully");
};
// Summary Logic
function updateSummary() {
const m = appState.aiMappings;
const stats = {
total: m.length,
classified: m.filter(r => r.suggested_field_id !== 'unknown').length,
approved: m.filter(r => r.status === 'approved').length,
rejected: m.filter(r => r.status === 'rejected').length,
ignored: m.filter(r => r.status === 'ignored').length,
high: m.filter(r => r.sensitivity === 'high' && r.status === 'approved').length
};
selectors.summaryGrid.innerHTML = `
<div class="summary-card"><h4>Total Fields</h4><p>${stats.total}</p></div>
<div class="summary-card"><h4>Classified</h4><p>${stats.classified}</p></div>
<div class="summary-card"><h4>Approved</h4><p>${stats.approved}</p></div>
<div class="summary-card"><h4>Rejected</h4><p>${stats.rejected}</p></div>
<div class="summary-card"><h4>High Sens.</h4><p>${stats.high}</p></div>
`;
}
// Downloads
selectors.downloadJsonBtn.onclick = () => {
const approvedOnly = appState.aiMappings.filter(m => m.status === 'approved' || m.status === 'remapped');
if (approvedOnly.length === 0) return showToast("No approved mappings to download");
const fileName = (appState.source.name || 'governance-map').replace(/\s+/g, '_').toLowerCase();
const blob = new Blob([JSON.stringify(approvedOnly, null, 2)], { type: 'application/json' });
downloadBlob(blob, `${fileName}.json`);
};
selectors.downloadCsvBtn.onclick = () => {
const approvedOnly = appState.aiMappings.filter(m => m.status === 'approved' || m.status === 'remapped');
if (approvedOnly.length === 0) return showToast("No approved mappings to download");
let csv = "source_name,table,column,datatype,field_id,display_name,category,sensitivity,confidence,status\n";
approvedOnly.forEach(r => {
csv += `"${appState.source.name}","${r.table}","${r.column}","${r.datatype}","${r.suggested_field_id}","${r.display_name}","${r.category}","${r.sensitivity}",${r.confidence},"${r.status}"\n`;
});
const fileName = (appState.source.name || 'governance-map').replace(/\s+/g, '_').toLowerCase();
const blob = new Blob([csv], { type: 'text/csv' });
downloadBlob(blob, `${fileName}.csv`);
};
function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
// File Upload Logic
selectors.fileInput.onchange = (e) => {
const file = e.target.files[0];
if (!file) return;
readFile(file);
};
selectors.dropZone.ondragover = (e) => { e.preventDefault(); e.currentTarget.style.borderColor = "var(--primary)"; };
selectors.dropZone.ondragleave = (e) => { e.preventDefault(); e.currentTarget.style.borderColor = "var(--border)"; };
selectors.dropZone.ondrop = (e) => {
e.preventDefault();
const file = e.dataTransfer.files[0];
if (file) readFile(file);
};
function readFile(file) {
if (!file.name.endsWith('.sql')) {
return showToast("Only .sql files are allowed");
}
const reader = new FileReader();
reader.onload = (e) => {
appState.schemaRaw = e.target.result;
showToast(`File loaded: ${file.name}`);
};
reader.readAsText(file);
}