Hfzune commited on
Commit
72aed5a
·
verified ·
1 Parent(s): 3569dee

Create panel.html

Browse files
Files changed (1) hide show
  1. panel.html +366 -0
panel.html ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="robots" content="noindex,nofollow">
6
+ <title>Admin Panel — Log Bot</title>
7
+ <style>
8
+ * { box-sizing: border-box; }
9
+ body { margin:0; font-family:system-ui,-apple-system,sans-serif;
10
+ background:#0b0d10; color:#e6e6e6; font-size:13px; }
11
+ header { display:flex; gap:1rem; align-items:center; padding:.7rem 1rem;
12
+ background:#14171c; border-bottom:1px solid #222831; position:sticky; top:0; z-index:10; }
13
+ header h1 { margin:0; font-size:15px; font-weight:600; color:#6cf; }
14
+ header .meta { color:#7d8590; font-size:12px; }
15
+ header .spacer { flex:1; }
16
+ button { background:#1c2128; color:#e6e6e6; border:1px solid #30363d; border-radius:5px;
17
+ padding:.35rem .7rem; cursor:pointer; font-size:12px; }
18
+ button:hover { background:#222831; border-color:#6cf; }
19
+ button.primary { background:#1f6feb; border-color:#1f6feb; }
20
+ button.primary:hover { background:#2d7ff9; }
21
+ .dropdown { position:relative; display:inline-block; }
22
+ .dropdown-menu { display:none; position:absolute; right:0; top:100%; margin-top:4px;
23
+ background:#14171c; border:1px solid #30363d; border-radius:6px;
24
+ min-width:220px; z-index:20; }
25
+ .dropdown-menu.open { display:block; }
26
+ .dropdown-menu a { display:block; padding:.5rem .8rem; color:#e6e6e6; text-decoration:none; font-size:12px; }
27
+ .dropdown-menu a:hover { background:#222831; color:#6cf; }
28
+ nav.tabs { display:flex; gap:0; border-bottom:1px solid #222831; padding:0 1rem; background:#0b0d10; }
29
+ nav.tabs button { border:none; border-bottom:2px solid transparent; border-radius:0;
30
+ background:none; padding:.7rem 1rem; color:#7d8590; }
31
+ nav.tabs button.active { color:#6cf; border-bottom-color:#6cf; }
32
+ main { padding:1rem; max-width:1400px; margin:0 auto; }
33
+ section.tab { display:none; }
34
+ section.tab.active { display:block; }
35
+ .cards { display:grid; grid-template-columns:repeat(auto-fit,minmax(160px,1fr)); gap:.7rem; }
36
+ .card { background:#14171c; border:1px solid #222831; border-radius:8px; padding:.9rem; }
37
+ .card .label { color:#7d8590; font-size:11px; text-transform:uppercase; letter-spacing:.04em; }
38
+ .card .value { font-size:22px; font-weight:600; margin-top:.2rem; color:#6cf; }
39
+ .filters { display:flex; gap:.5rem; flex-wrap:wrap; margin-bottom:.7rem; align-items:center; }
40
+ .filters input, .filters select { background:#14171c; color:#e6e6e6; border:1px solid #30363d;
41
+ border-radius:5px; padding:.35rem .5rem; font-size:12px; }
42
+ .filters input[type=date] { color-scheme:dark; }
43
+ table { width:100%; border-collapse:collapse; background:#14171c; border-radius:8px; overflow:hidden; }
44
+ th { text-align:left; padding:.5rem .6rem; background:#1a1f26; color:#7d8590; font-weight:500;
45
+ font-size:11px; text-transform:uppercase; letter-spacing:.04em; border-bottom:1px solid #222831; }
46
+ td { padding:.45rem .6rem; border-bottom:1px solid #1c2128; vertical-align:top; }
47
+ tr:hover td { background:#171b21; }
48
+ td.mono, .mono { font-family:ui-monospace,Menlo,Consolas,monospace; font-size:12px; }
49
+ .pill { display:inline-block; padding:.1rem .45rem; border-radius:999px; font-size:11px; font-weight:500; }
50
+ .pill.success { background:#1a3a2e; color:#4ade80; }
51
+ .pill.fail { background:#3a1a1f; color:#f87171; }
52
+ .pill.type { background:#1c2c3f; color:#7dd3fc; }
53
+ .pagination { display:flex; gap:.4rem; align-items:center; margin-top:.7rem; color:#7d8590; }
54
+ .row-expand td { background:#0f1216 !important; padding:.8rem; }
55
+ details summary { cursor:pointer; color:#6cf; }
56
+ pre { background:#05070a; padding:.6rem; border-radius:5px; overflow:auto;
57
+ max-height:400px; font-family:ui-monospace,monospace; font-size:11.5px;
58
+ color:#c9d1d9; border:1px solid #1c2128; white-space:pre-wrap; word-break:break-all; }
59
+ iframe.reply { width:100%; min-height:180px; border:1px solid #1c2128; border-radius:5px;
60
+ background:#0d1117; color:#e6e6e6; }
61
+ .kv { display:grid; grid-template-columns:130px 1fr; gap:.3rem .8rem; margin-bottom:.6rem; }
62
+ .kv .k { color:#7d8590; }
63
+ footer { padding:1rem; text-align:center; color:#4b5563; font-size:11px; }
64
+ .err { color:#f87171; }
65
+ .spinner { display:inline-block; width:12px; height:12px; border:2px solid #6cf;
66
+ border-top-color:transparent; border-radius:50%; animation:spin .7s linear infinite; }
67
+ @keyframes spin { to { transform:rotate(360deg); } }
68
+ </style>
69
+ </head>
70
+ <body>
71
+
72
+ <header>
73
+ <h1>📊 Log Bot — Admin Panel</h1>
74
+ <span class="meta" id="meta">loading…</span>
75
+ <span class="spacer"></span>
76
+ <div class="dropdown">
77
+ <button id="dlBtn">⬇ Download ▾</button>
78
+ <div class="dropdown-menu" id="dlMenu">
79
+ <a href="/api/logs/download?format=jsonl" download>Full log — JSONL (gzip)</a>
80
+ <a href="/api/logs/download?format=csv" download>Full log — CSV</a>
81
+ <a href="/api/logs/download?format=jsonl&limit=500" download>Last 500 — JSONL</a>
82
+ <a href="/api/users/download" download>Users — CSV</a>
83
+ </div>
84
+ </div>
85
+ <button onclick="logout()">🚪 Logout</button>
86
+ </header>
87
+
88
+ <nav class="tabs">
89
+ <button data-tab="overview" class="active">Overview</button>
90
+ <button data-tab="searches">Searches</button>
91
+ <button data-tab="users">Users</button>
92
+ <button data-tab="errors">Errors</button>
93
+ <button data-tab="audit">Audit</button>
94
+ </nav>
95
+
96
+ <main>
97
+ <section class="tab active" id="tab-overview">
98
+ <div class="cards" id="statCards"></div>
99
+ </section>
100
+
101
+ <section class="tab" id="tab-searches">
102
+ <div class="filters">
103
+ <select id="fType">
104
+ <option value="">All types</option>
105
+ <option value="mobile">mobile</option>
106
+ <option value="aadhaar_info">aadhaar_info</option>
107
+ <option value="aadhaar_family">aadhaar_family</option>
108
+ <option value="aadhaar_ration">aadhaar_ration</option>
109
+ </select>
110
+ <select id="fStatus">
111
+ <option value="">All status</option>
112
+ <option value="success">success</option>
113
+ <option value="fail">fail</option>
114
+ </select>
115
+ <input id="fUser" type="text" placeholder="user_id">
116
+ <input id="fFrom" type="date">
117
+ <input id="fTo" type="date">
118
+ <button class="primary" onclick="loadSearches(1)">Apply</button>
119
+ <button onclick="resetFilters()">Reset</button>
120
+ </div>
121
+ <table>
122
+ <thead><tr>
123
+ <th>#</th><th>Time (UTC)</th><th>User</th><th>Type</th>
124
+ <th>Query</th><th>Status</th><th>HTTP</th><th>ms</th><th></th>
125
+ </tr></thead>
126
+ <tbody id="searchBody"></tbody>
127
+ </table>
128
+ <div class="pagination" id="searchPag"></div>
129
+ </section>
130
+
131
+ <section class="tab" id="tab-users">
132
+ <table>
133
+ <thead><tr>
134
+ <th>User ID</th><th>Username</th><th>First name</th>
135
+ <th>First seen</th><th>Last seen</th><th>Searches</th><th>Last type</th>
136
+ </tr></thead>
137
+ <tbody id="usersBody"></tbody>
138
+ </table>
139
+ <div class="pagination" id="usersPag"></div>
140
+ </section>
141
+
142
+ <section class="tab" id="tab-errors">
143
+ <table>
144
+ <thead><tr>
145
+ <th>Time (UTC)</th><th>User</th><th>Type</th><th>Query</th><th>HTTP</th><th>ms</th>
146
+ </tr></thead>
147
+ <tbody id="errorsBody"></tbody>
148
+ </table>
149
+ </section>
150
+
151
+ <section class="tab" id="tab-audit">
152
+ <table>
153
+ <thead><tr><th>Time</th><th>User</th><th>Action</th><th>Detail</th></tr></thead>
154
+ <tbody id="auditBody"></tbody>
155
+ </table>
156
+ </section>
157
+ </main>
158
+
159
+ <footer id="foot">—</footer>
160
+
161
+ <script>
162
+ const state = { tab: 'overview', searchPage: 1, userPage: 1 };
163
+
164
+ function el(id) { return document.getElementById(id); }
165
+ function esc(s) {
166
+ return String(s ?? '').replace(/[&<>"']/g, c =>
167
+ ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
168
+ }
169
+ function fmtTs(s) {
170
+ if (!s) return '';
171
+ try { return new Date(s).toISOString().replace('T',' ').replace('Z',''); }
172
+ catch { return s; }
173
+ }
174
+
175
+ // Tabs
176
+ document.querySelectorAll('nav.tabs button').forEach(b => {
177
+ b.onclick = () => {
178
+ document.querySelectorAll('nav.tabs button').forEach(x => x.classList.remove('active'));
179
+ document.querySelectorAll('section.tab').forEach(x => x.classList.remove('active'));
180
+ b.classList.add('active');
181
+ state.tab = b.dataset.tab;
182
+ el('tab-' + state.tab).classList.add('active');
183
+ refreshTab();
184
+ };
185
+ });
186
+
187
+ // Download menu
188
+ el('dlBtn').onclick = (e) => {
189
+ e.stopPropagation();
190
+ el('dlMenu').classList.toggle('open');
191
+ };
192
+ document.addEventListener('click', () => el('dlMenu').classList.remove('open'));
193
+
194
+ async function api(path) {
195
+ const r = await fetch(path, { credentials: 'same-origin' });
196
+ if (r.status === 401) { location.reload(); return null; }
197
+ if (!r.ok) { console.error(path, r.status); return null; }
198
+ return r.json();
199
+ }
200
+
201
+ async function loadStats() {
202
+ const s = await api('/api/stats');
203
+ if (!s) return;
204
+ const cards = [
205
+ ['Total Users', s.total_users],
206
+ ['Total Searches', s.total_searches],
207
+ ['Today', s.today],
208
+ ['Last 24h', s.last_24h],
209
+ ['Errors', s.errors],
210
+ ['In memory', `${s.inmem_count}/${s.max_inmem}`],
211
+ ['Log file', `${s.log_file_mb} MB`],
212
+ ];
213
+ el('statCards').innerHTML = cards.map(([l, v]) =>
214
+ `<div class="card"><div class="label">${esc(l)}</div><div class="value">${esc(v)}</div></div>`
215
+ ).join('');
216
+ el('meta').textContent = `since ${fmtTs(s.since)}`;
217
+ el('foot').textContent =
218
+ `In-memory: ${s.inmem_count}/${s.max_inmem} · File: ${s.log_file_mb} MB · Since: ${fmtTs(s.since)}`;
219
+ }
220
+
221
+ async function loadSearches(page=1) {
222
+ state.searchPage = page;
223
+ const q = new URLSearchParams({
224
+ page,
225
+ type: el('fType').value,
226
+ status: el('fStatus').value,
227
+ user: el('fUser').value.trim(),
228
+ from: el('fFrom').value,
229
+ to: el('fTo').value,
230
+ });
231
+ const d = await api('/api/searches?' + q);
232
+ if (!d) return;
233
+ const rows = d.rows.map((r, i) => {
234
+ const idx = (d.page - 1) * d.per_page + i + 1;
235
+ return `<tr data-id="${esc(r.id)}">
236
+ <td class="mono">${idx}</td>
237
+ <td class="mono">${esc(fmtTs(r.ts_utc))}</td>
238
+ <td>${esc(r.username || '@' + r.user_id)}</td>
239
+ <td><span class="pill type">${esc(r.type)}</span></td>
240
+ <td class="mono">${esc(r.query)}</td>
241
+ <td><span class="pill ${esc(r.status)}">${esc(r.status)}</span></td>
242
+ <td class="mono">${esc(r.http_code)}</td>
243
+ <td class="mono">${esc(r.elapsed_ms)}</td>
244
+ <td><button onclick="toggleRow('${esc(r.id)}', this)">Open</button></td>
245
+ </tr>`;
246
+ }).join('');
247
+ el('searchBody').innerHTML = rows || `<tr><td colspan="9" style="text-align:center;color:#4b5563">no records</td></tr>`;
248
+ const totalPages = Math.max(1, Math.ceil(d.total / d.per_page));
249
+ el('searchPag').innerHTML =
250
+ `<button ${page<=1?'disabled':''} onclick="loadSearches(${page-1})">‹ prev</button>
251
+ <span>page ${d.page} / ${totalPages} — ${d.total} records</span>
252
+ <button ${page>=totalPages?'disabled':''} onclick="loadSearches(${page+1})">next ›</button>`;
253
+ }
254
+
255
+ async function toggleRow(id, btn) {
256
+ const tr = btn.closest('tr');
257
+ if (tr.nextElementSibling && tr.nextElementSibling.classList.contains('row-expand')) {
258
+ tr.nextElementSibling.remove(); btn.textContent = 'Open'; return;
259
+ }
260
+ btn.textContent = '…';
261
+ const r = await api('/api/search/' + encodeURIComponent(id));
262
+ if (!r) { btn.textContent = 'Open'; return; }
263
+ const bodyJson = JSON.stringify(r.resp_body, null, 2);
264
+ const replyHtml = r.bot_reply_html || '';
265
+ const expand = document.createElement('tr');
266
+ expand.className = 'row-expand';
267
+ expand.innerHTML = `<td colspan="9">
268
+ <div class="kv">
269
+ <div class="k">Timestamp</div>
270
+ <div class="mono">UTC: ${esc(r.ts_utc)}<br>Local: ${esc(r.ts_local)}</div>
271
+ <div class="k">User</div>
272
+ <div>${esc(r.username || '—')} (<code>${esc(r.user_id)}</code>)</div>
273
+ <div class="k">Type</div>
274
+ <div><span class="pill type">${esc(r.type)}</span></div>
275
+ <div class="k">Request URL</div>
276
+ <div class="mono">${esc(r.req_url_redacted)}</div>
277
+ <div class="k">Request params</div>
278
+ <div class="mono">${esc(JSON.stringify(r.req_params))}</div>
279
+ <div class="k">HTTP / Status</div>
280
+ <div>${esc(r.http_code)} · <span class="pill ${esc(r.status)}">${esc(r.status)}</span> · ${esc(r.elapsed_ms)} ms</div>
281
+ </div>
282
+ <details open>
283
+ <summary>Raw JSON response</summary>
284
+ <pre>${esc(bodyJson)}</pre>
285
+ </details>
286
+ <details>
287
+ <summary>Bot reply (rendered)</summary>
288
+ <iframe class="reply" sandbox srcdoc="<html><body style='color:#e6e6e6;font-family:system-ui;font-size:13px;padding:8px;'>${esc(replyHtml).replace(/\n/g,'<br>')}</body></html>"></iframe>
289
+ </details>
290
+ <div style="margin-top:.5rem;display:flex;gap:.4rem">
291
+ <button onclick='navigator.clipboard.writeText(${JSON.stringify(bodyJson)})'>📋 Copy JSON</button>
292
+ </div>
293
+ </td>`;
294
+ tr.after(expand);
295
+ btn.textContent = 'Close';
296
+ }
297
+
298
+ async function loadUsers(page=1) {
299
+ state.userPage = page;
300
+ const d = await api('/api/users?page=' + page);
301
+ if (!d) return;
302
+ el('usersBody').innerHTML = d.rows.map(u => `<tr>
303
+ <td class="mono">${esc(u.user_id)}</td>
304
+ <td>${u.username ? '@' + esc(u.username) : '—'}</td>
305
+ <td>${esc(u.first_name)}</td>
306
+ <td class="mono">${esc(fmtTs(u.first_seen))}</td>
307
+ <td class="mono">${esc(fmtTs(u.last_seen))}</td>
308
+ <td>${esc(u.total_searches)}</td>
309
+ <td><span class="pill type">${esc(u.last_query_type || '—')}</span></td>
310
+ </tr>`).join('') || `<tr><td colspan="7" style="text-align:center;color:#4b5563">no users</td></tr>`;
311
+ const totalPages = Math.max(1, Math.ceil(d.total / d.per_page));
312
+ el('usersPag').innerHTML =
313
+ `<button ${page<=1?'disabled':''} onclick="loadUsers(${page-1})">‹ prev</button>
314
+ <span>page ${d.page} / ${totalPages} — ${d.total} users</span>
315
+ <button ${page>=totalPages?'disabled':''} onclick="loadUsers(${page+1})">next ›</button>`;
316
+ }
317
+
318
+ async function loadErrors() {
319
+ const d = await api('/api/searches?status=fail&per_page=100&page=1');
320
+ if (!d) return;
321
+ el('errorsBody').innerHTML = d.rows.map(r => `<tr>
322
+ <td class="mono">${esc(fmtTs(r.ts_utc))}</td>
323
+ <td>${esc(r.username || r.user_id)}</td>
324
+ <td><span class="pill type">${esc(r.type)}</span></td>
325
+ <td class="mono">${esc(r.query)}</td>
326
+ <td class="mono">${esc(r.http_code)}</td>
327
+ <td class="mono">${esc(r.elapsed_ms)}</td>
328
+ </tr>`).join('') || `<tr><td colspan="6" style="text-align:center;color:#4b5563">no errors 🎉</td></tr>`;
329
+ }
330
+
331
+ async function loadAudit() {
332
+ const d = await api('/api/audit');
333
+ if (!d) return;
334
+ el('auditBody').innerHTML = d.rows.map(r => `<tr>
335
+ <td class="mono">${esc(fmtTs(r.ts))}</td>
336
+ <td class="mono">${esc(r.user_id)}</td>
337
+ <td>${esc(r.action)}</td>
338
+ <td class="mono">${esc(r.detail)}</td>
339
+ </tr>`).join('') || `<tr><td colspan="4" style="text-align:center;color:#4b5563">no audit entries</td></tr>`;
340
+ }
341
+
342
+ function resetFilters() {
343
+ el('fType').value = ''; el('fStatus').value = '';
344
+ el('fUser').value = ''; el('fFrom').value = ''; el('fTo').value = '';
345
+ loadSearches(1);
346
+ }
347
+
348
+ function refreshTab() {
349
+ if (state.tab === 'overview') loadStats();
350
+ else if (state.tab === 'searches') loadSearches(state.searchPage);
351
+ else if (state.tab === 'users') loadUsers(state.userPage);
352
+ else if (state.tab === 'errors') loadErrors();
353
+ else if (state.tab === 'audit') loadAudit();
354
+ }
355
+
356
+ function logout() {
357
+ document.cookie = 'panel_session=; Max-Age=0; path=/';
358
+ location.reload();
359
+ }
360
+
361
+ // Initial + auto-refresh (only on Overview)
362
+ loadStats();
363
+ setInterval(() => { if (state.tab === 'overview') loadStats(); }, 30000);
364
+ </script>
365
+ </body>
366
+ </html>