-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
471 lines (402 loc) · 18.6 KB
/
Copy pathindex.html
File metadata and controls
471 lines (402 loc) · 18.6 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Analysis Agent</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body, html {
margin: 0;
padding: 0;
height: 100vh;
overflow: hidden;
}
.scrollbar-thin::-webkit-scrollbar { width: 6px; height: 6px; }
.scrollbar-thin::-webkit-scrollbar-thumb { background-color: #cbd5e1; border-radius: 3px; }
.scrollbar-thin { scrollbar-width: thin; scrollbar-color: #cbd5e1 transparent; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px; text-align: left; border: 1px solid #e5e7eb; }
th { background-color: #f3f4f6; font-weight: 600; }
tr:hover { background-color: #f9fafb; }
.sql-box {
background: #1e293b;
color: #e2e8f0;
padding: 12px;
border-radius: 6px;
font-family: 'Courier New', monospace;
font-size: 13px;
white-space: pre-wrap;
max-height: 200px;
overflow-y: auto;
}
.main-grid {
display: grid;
grid-template-columns: 320px 1fr 320px;
height: calc(100vh - 80px);
}
.chat-message {
padding: 8px 12px;
margin: 4px 0;
border-radius: 8px;
font-size: 14px;
}
.user-message {
background-color: #dbeafe;
margin-left: 20px;
text-align: left;
}
.agent-message {
background-color: #f3f4f6;
margin-right: 20px;
text-align: left;
}
.off-topic-message {
background-color: #fef3c7;
margin-right: 20px;
padding: 12px;
border-left: 4px solid #f59e0b;
}
/* Fix left panel layout */
.left-panel {
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
}
.settings-section {
flex-shrink: 0;
}
.chat-section {
flex: 1;
overflow-y: auto;
min-height: 0;
}
.input-section {
flex-shrink: 0;
}
</style>
</head>
<body class="bg-gray-100">
<header class="bg-indigo-600 text-white p-5 shadow-lg">
<div class="flex justify-between items-center max-w-7xl mx-auto">
<div class="w-32"></div>
<h1 class="text-3xl font-bold">Data Analysis Agent</h1>
<button id="clear-session-btn" class="bg-red-600 hover:bg-red-700 px-3 py-1.5 rounded text-xs font-semibold">
Clear Session
</button>
</div>
</header>
<div class="main-grid">
<div class="bg-white border-r border-gray-200 left-panel">
<div class="settings-section p-4 border-b border-gray-200 bg-gray-50">
<label class="block text-sm font-semibold mb-2">Database</label>
<select id="database-select" class="w-full p-2 border border-gray-300 rounded-lg text-sm">
<option value="chinook">chinook (Music Store)</option>
<option value="sakila">sakila (Movie Rental)</option>
<option value="northwind">northwind (Business)</option>
</select>
<label class="block text-sm font-semibold mt-3 mb-2">Role</label>
<select id="role-select" class="w-full p-2 border border-gray-300 rounded-lg text-sm">
<option value="analyst">analyst (Standard)</option>
<option value="admin">admin (Full Access)</option>
<option value="viewer">viewer (Limited)</option>
</select>
<label class="block text-sm font-semibold mt-3 mb-2">Chart Type</label>
<select id="chart-select" class="w-full p-2 border border-gray-300 rounded-lg text-sm">
<option value="auto">Auto (AI Decides)</option>
<option value="bar">Bar Chart</option>
<option value="line">Line Chart</option>
<option value="pie">Pie Chart</option>
<option value="scatter">Scatter Plot</option>
</select>
<div class="mt-3 p-2 bg-blue-50 border border-blue-200 rounded text-xs">
<div class="font-semibold text-blue-800 mb-1">Session Active</div>
<div id="session-info" class="text-blue-600">No session</div>
</div>
</div>
<div id="chat-history" class="chat-section p-4 space-y-2">
<div class="text-sm text-gray-500 text-center py-4">
Ask the agent...
</div>
</div>
<div class="input-section p-4 border-t border-gray-200 bg-gray-50">
<form id="query-form" class="space-y-2">
<textarea
id="query-input"
placeholder="Ask the agent..."
class="w-full p-3 border border-gray-300 rounded-lg text-base resize-none"
rows="3"
required
></textarea>
<button
type="submit"
id="send-btn"
class="w-full bg-indigo-600 text-white px-4 py-3 rounded-lg hover:bg-indigo-700 text-base font-semibold"
>
Send Query
</button>
</form>
</div>
</div>
<div class="bg-white flex flex-col overflow-hidden">
<div class="flex-1 border-b border-gray-200 overflow-hidden flex flex-col">
<div class="p-3 bg-gray-50 border-b border-gray-200 flex justify-between items-center">
<h2 class="font-semibold text-gray-700">Query Results</h2>
<span id="row-count" class="text-sm text-gray-500"></span>
</div>
<div id="data-container" class="flex-1 overflow-auto scrollbar-thin p-4">
<div class="text-center text-gray-400 py-8">No data yet</div>
</div>
</div>
<div class="flex-1 overflow-hidden flex flex-col">
<div class="p-3 bg-gray-50 border-b border-gray-200 flex justify-between items-center">
<h2 class="font-semibold text-gray-700">Visualization</h2>
<span id="chart-info" class="text-sm text-gray-500"></span>
</div>
<div id="viz-container" class="flex-1 overflow-auto scrollbar-thin p-4 flex items-center justify-center">
<div class="text-center text-gray-400">No visualization yet</div>
</div>
</div>
</div>
<div class="bg-white border-l border-gray-200 flex flex-col overflow-hidden">
<div class="bg-gray-50">
<button
id="toggle-sql-btn"
class="w-full p-3 text-left font-semibold text-gray-700 hover:bg-gray-100 flex justify-between items-center"
>
<span>Generated SQL</span>
<span id="sql-arrow" class="text-gray-400">▼</span>
</button>
<div id="sql-container" class="hidden p-4 max-h-60 overflow-y-auto scrollbar-thin">
<div class="sql-box">No SQL query yet</div>
</div>
</div>
<div class="border-t border-gray-200 bg-gray-50 flex-1 flex flex-col">
<button
id="toggle-insights-btn"
class="w-full p-3 text-left font-semibold text-gray-700 hover:bg-gray-100 flex justify-between items-center"
>
<span>Insights</span>
<span id="insights-arrow" class="text-gray-400">▼</span>
</button>
<div id="insights-container" class="hidden p-4 overflow-y-auto scrollbar-thin text-sm flex-1">
<div class="text-gray-400">No insights yet</div>
</div>
</div>
</div>
</div>
<script>
const API_BASE = 'http://127.0.0.1:5000/api';
const OUTPUT_BASE = 'http://127.0.0.1:5000/outputs';
let currentSessionId = null;
const dbSelect = document.getElementById('database-select');
const roleSelect = document.getElementById('role-select');
const chartSelect = document.getElementById('chart-select');
const queryForm = document.getElementById('query-form');
const queryInput = document.getElementById('query-input');
const sendBtn = document.getElementById('send-btn');
const chatHistory = document.getElementById('chat-history');
const dataContainer = document.getElementById('data-container');
const vizContainer = document.getElementById('viz-container');
const sqlContainer = document.getElementById('sql-container');
const insightsContainer = document.getElementById('insights-container');
const rowCount = document.getElementById('row-count');
const chartInfo = document.getElementById('chart-info');
const toggleSqlBtn = document.getElementById('toggle-sql-btn');
const toggleInsightsBtn = document.getElementById('toggle-insights-btn');
const sqlArrow = document.getElementById('sql-arrow');
const insightsArrow = document.getElementById('insights-arrow');
const sessionInfo = document.getElementById('session-info');
const clearSessionBtn = document.getElementById('clear-session-btn');
toggleSqlBtn.addEventListener('click', () => {
sqlContainer.classList.toggle('hidden');
sqlArrow.textContent = sqlContainer.classList.contains('hidden') ? '▼' : '▲';
});
toggleInsightsBtn.addEventListener('click', () => {
insightsContainer.classList.toggle('hidden');
insightsArrow.textContent = insightsContainer.classList.contains('hidden') ? '▼' : '▲';
});
clearSessionBtn.addEventListener('click', async () => {
if (!currentSessionId) {
alert('No active session');
return;
}
if (!confirm('Clear conversation history?')) {
return;
}
try {
await fetch(`${API_BASE}/clear_session`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session_id: currentSessionId })
});
currentSessionId = null;
updateSessionInfo();
chatHistory.innerHTML = '<div class="text-sm text-gray-500 text-center py-4">Ask the agent...</div>';
clearResults();
} catch (error) {
console.error('Failed to clear session:', error);
alert('Failed to clear session');
}
});
function updateSessionInfo() {
if (currentSessionId) {
const shortId = currentSessionId.substring(0, 12) + '...';
sessionInfo.textContent = shortId;
} else {
sessionInfo.textContent = 'No session';
}
}
queryForm.addEventListener('submit', async (e) => {
e.preventDefault();
const query = queryInput.value.trim();
if (!query) return;
const database = dbSelect.value;
const role = roleSelect.value;
const chartType = chartSelect.value;
addMessage(query, 'user');
queryInput.value = '';
sendBtn.disabled = true;
addMessage('Analyzing your query...', 'agent', true);
try {
const response = await fetch(`${API_BASE}/query`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query,
database,
role,
create_viz: true,
chart_type: chartType === 'auto' ? null : chartType,
session_id: currentSessionId
})
});
removeLoadingMessage();
const result = await response.json();
if (result.session_id) {
currentSessionId = result.session_id;
updateSessionInfo();
}
if (result.status === 'success') {
if (result.intent === 'off_topic') {
addMessage(result.message, 'off-topic');
clearResults();
} else {
addMessage('Query executed successfully!', 'agent');
displayResults(result);
}
} else {
addMessage(`Error: ${result.error}`, 'agent');
clearResults();
}
} catch (error) {
removeLoadingMessage();
addMessage(`Connection error: ${error.message}`, 'agent');
clearResults();
} finally {
sendBtn.disabled = false;
}
});
function addMessage(text, sender, isLoading = false) {
const div = document.createElement('div');
if (isLoading) {
div.id = 'loading-message';
div.className = 'chat-message agent-message';
div.innerHTML = `<span class="animate-pulse">${escapeHtml(text)}</span>`;
} else if (sender === 'user') {
div.className = 'chat-message user-message';
div.textContent = text;
} else if (sender === 'off-topic') {
div.className = 'off-topic-message';
div.textContent = text;
} else {
div.className = 'chat-message agent-message';
div.textContent = text;
}
chatHistory.appendChild(div);
chatHistory.scrollTop = chatHistory.scrollHeight;
}
function removeLoadingMessage() {
const loading = document.getElementById('loading-message');
if (loading) loading.remove();
}
function displayResults(result) {
if (result.full_data) {
const data = JSON.parse(result.full_data);
displayDataTable(data);
rowCount.textContent = `${result.row_count} rows`;
}
if (result.visualization_path) {
const filename = result.visualization_path.split('/').pop();
vizContainer.innerHTML = `
<img src="${OUTPUT_BASE}/${filename}?t=${Date.now()}"
alt="Visualization"
class="max-w-full max-h-full object-contain"
>
`;
chartInfo.textContent = result.chart_type ? `Type: ${result.chart_type}` : '';
} else {
vizContainer.innerHTML = '<div class="text-gray-400">No visualization generated</div>';
chartInfo.textContent = '';
}
if (result.sql_query) {
sqlContainer.innerHTML = `<div class="sql-box">${escapeHtml(result.sql_query)}</div>`;
}
if (result.insights) {
displayInsights(result.insights);
}
}
function displayDataTable(data) {
if (!data || data.length === 0) {
dataContainer.innerHTML = '<div class="text-gray-400 text-center py-4">No data</div>';
return;
}
const columns = Object.keys(data[0]);
let html = '<table><thead><tr>';
columns.forEach(col => {
html += `<th>${escapeHtml(col)}</th>`;
});
html += '</tr></thead><tbody>';
data.forEach(row => {
html += '<tr>';
columns.forEach(col => {
const value = row[col];
const displayValue = value === null ? '<span class="text-gray-400">null</span>' : escapeHtml(String(value));
html += `<td>${displayValue}</td>`;
});
html += '</tr>';
});
html += '</tbody></table>';
dataContainer.innerHTML = html;
}
function displayInsights(insights) {
let html = '';
if (insights.summary) {
html += `<div class="mb-3"><strong class="text-indigo-600">Summary:</strong><br/>${escapeHtml(insights.summary)}</div>`;
}
if (insights.key_findings && insights.key_findings.length > 0) {
html += '<div class="mb-3"><strong class="text-indigo-600">Key Findings:</strong><ul class="list-disc ml-4 mt-1 space-y-1">';
insights.key_findings.forEach(finding => {
html += `<li>${escapeHtml(finding)}</li>`;
});
html += '</ul></div>';
}
insightsContainer.innerHTML = html || '<div class="text-gray-400">No insights available</div>';
}
function clearResults() {
dataContainer.innerHTML = '<div class="text-gray-400 text-center py-4">No data</div>';
vizContainer.innerHTML = '<div class="text-gray-400">No visualization</div>';
sqlContainer.innerHTML = '<div class="sql-box">No SQL query</div>';
insightsContainer.innerHTML = '<div class="text-gray-400">No insights</div>';
rowCount.textContent = '';
chartInfo.textContent = '';
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
</script>
</body>
</html>