File size: 11,203 Bytes
4387c3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LLM Dictionary β€” Usage Dashboard</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>

        :root {

            --bg: #f8fafc; --card: #fff; --text: #0f172a; --muted: #64748b;

            --accent: #2563eb; --accent-light: #dbeafe; --border: #e2e8f0;

            --green: #059669; --green-bg: #ecfdf5;

            --orange: #d97706; --orange-bg: #fffbeb;

            --purple: #7c3aed; --purple-bg: #f5f3ff;

        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body { font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; }

        .container { max-width: 900px; margin: 0 auto; padding: 2rem 1.5rem; }

        header { margin-bottom: 2rem; }

        header h1 { font-size: 1.8rem; font-weight: 700; }

        header p { color: var(--muted); font-size: 0.9rem; margin-top: 0.25rem; }

        .back-link { color: var(--accent); text-decoration: none; font-size: 0.85rem; display: inline-block; margin-bottom: 1rem; }

        .back-link:hover { text-decoration: underline; }



        /* KPI Cards */

        .kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 2rem; }

        .kpi-card {

            background: var(--card); border: 1px solid var(--border); border-radius: 12px;

            padding: 1.25rem; text-align: center; box-shadow: 0 1px 3px rgba(0,0,0,0.04);

        }

        .kpi-card .kpi-value { font-size: 2rem; font-weight: 700; }

        .kpi-card .kpi-label { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-top: 0.25rem; }

        .kpi-card.blue .kpi-value { color: var(--accent); }

        .kpi-card.green .kpi-value { color: var(--green); }

        .kpi-card.orange .kpi-value { color: var(--orange); }

        .kpi-card.purple .kpi-value { color: var(--purple); }



        /* Sections */

        .section { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 1.5rem; margin-bottom: 1.5rem; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }

        .section h2 { font-size: 1rem; font-weight: 600; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.5rem; }



        /* Bar chart */

        .bar-chart { display: flex; flex-direction: column; gap: 0.5rem; }

        .bar-row { display: flex; align-items: center; gap: 0.75rem; }

        .bar-label { font-size: 0.85rem; min-width: 100px; text-align: right; color: var(--text); font-weight: 500; }

        .bar-track { flex: 1; height: 28px; background: #f1f5f9; border-radius: 6px; overflow: hidden; position: relative; }

        .bar-fill { height: 100%; border-radius: 6px; transition: width 0.5s ease; display: flex; align-items: center; padding-left: 0.5rem; }

        .bar-fill span { font-size: 0.75rem; font-weight: 600; color: white; }

        .bar-fill.blue { background: var(--accent); }

        .bar-fill.green { background: var(--green); }

        .bar-fill.orange { background: var(--orange); }

        .bar-fill.purple { background: var(--purple); }



        /* Timeline */

        .timeline-chart { display: flex; align-items: flex-end; gap: 2px; height: 120px; padding-top: 1rem; }

        .timeline-bar { flex: 1; background: var(--accent-light); border-radius: 3px 3px 0 0; min-width: 8px; transition: height 0.3s ease; position: relative; }

        .timeline-bar:hover { background: var(--accent); }

        .timeline-bar:hover .timeline-tooltip { display: block; }

        .timeline-tooltip {

            display: none; position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);

            background: var(--text); color: white; padding: 0.25rem 0.5rem; border-radius: 4px;

            font-size: 0.7rem; white-space: nowrap; margin-bottom: 4px;

        }



        /* Recent lookups table */

        .recent-table { width: 100%; border-collapse: collapse; }

        .recent-table th { text-align: left; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; padding: 0.5rem 0.75rem; border-bottom: 2px solid var(--border); }

        .recent-table td { padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border); font-size: 0.9rem; }

        .recent-table tr:last-child td { border-bottom: none; }

        .lang-badge { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 10px; font-size: 0.75rem; font-weight: 500; }

        .lang-badge.english { background: var(--accent-light); color: var(--accent); }

        .lang-badge.telugu { background: var(--green-bg); color: var(--green); }

        .lang-badge.hindi { background: var(--orange-bg); color: var(--orange); }

        .lang-badge.sanskrit { background: var(--purple-bg); color: var(--purple); }



        .loading-msg { text-align: center; color: var(--muted); padding: 3rem; }

        .auto-refresh { font-size: 0.75rem; color: var(--muted); }



        @media (max-width: 600px) {

            .container { padding: 1rem; }

            .kpi-grid { grid-template-columns: 1fr 1fr; }

            .bar-label { min-width: 70px; font-size: 0.8rem; }

        }

    </style>
</head>
<body>
    <div class="container">
        <a href="/" class="back-link">← Back to Dictionary</a>
        <header>
            <h1>πŸ“Š Usage Dashboard</h1>
            <p>Real-time analytics for LLM Dictionary <span class="auto-refresh">(refreshes every 15s)</span></p>
        </header>

        <div id="content"><p class="loading-msg">Loading analytics...</p></div>
    </div>

    <script>

    const LANG_COLORS = { english: 'blue', telugu: 'green', hindi: 'orange', sanskrit: 'purple' };



    async function loadStats() {

        try {

            const resp = await fetch('/api/stats');

            if (!resp.ok) throw new Error('Failed');

            const data = await resp.json();

            render(data);

        } catch (e) {

            document.getElementById('content').innerHTML = '<p class="loading-msg">Could not load analytics.</p>';

        }

    }



    function render(d) {

        const langCount = Object.keys(d.by_language).length;

        const topWord = d.top_words.length ? d.top_words[0].word : 'β€”';

        const uptime = timeSince(d.started_at);



        let html = '';



        // KPIs

        html += `<div class="kpi-grid">

            <div class="kpi-card blue"><div class="kpi-value">${d.total_lookups}</div><div class="kpi-label">Total Lookups</div></div>

            <div class="kpi-card green"><div class="kpi-value">${langCount}</div><div class="kpi-label">Languages Used</div></div>

            <div class="kpi-card orange"><div class="kpi-value">${esc(topWord)}</div><div class="kpi-label">Most Searched</div></div>

            <div class="kpi-card purple"><div class="kpi-value">${uptime}</div><div class="kpi-label">Uptime</div></div>

        </div>`;



        // Language breakdown

        if (Object.keys(d.by_language).length) {

            const maxLang = Math.max(...Object.values(d.by_language));

            html += `<div class="section"><h2>🌐 Lookups by Language</h2><div class="bar-chart">`;

            for (const [lang, count] of Object.entries(d.by_language).sort((a, b) => b[1] - a[1])) {

                const pct = maxLang ? (count / maxLang) * 100 : 0;

                const color = LANG_COLORS[lang] || 'blue';

                html += `<div class="bar-row">

                    <span class="bar-label">${esc(lang)}</span>

                    <div class="bar-track"><div class="bar-fill ${color}" style="width:${Math.max(pct, 5)}%"><span>${count}</span></div></div>

                </div>`;

            }

            html += `</div></div>`;

        }



        // Hourly timeline

        if (d.hourly.length) {

            const maxH = Math.max(...d.hourly.map(h => h.count));

            html += `<div class="section"><h2>πŸ“ˆ Lookups Over Time</h2><div class="timeline-chart">`;

            for (const h of d.hourly) {

                const pct = maxH ? (h.count / maxH) * 100 : 0;

                html += `<div class="timeline-bar" style="height:${Math.max(pct, 4)}%">

                    <div class="timeline-tooltip">${h.hour} β€” ${h.count} lookups</div>

                </div>`;

            }

            html += `</div></div>`;

        }



        // Top words

        if (d.top_words.length) {

            const maxW = d.top_words[0].count;

            html += `<div class="section"><h2>πŸ”€ Top Searched Words</h2><div class="bar-chart">`;

            for (const w of d.top_words.slice(0, 10)) {

                const pct = maxW ? (w.count / maxW) * 100 : 0;

                html += `<div class="bar-row">

                    <span class="bar-label">${esc(w.word)}</span>

                    <div class="bar-track"><div class="bar-fill blue" style="width:${Math.max(pct, 5)}%"><span>${w.count}</span></div></div>

                </div>`;

            }

            html += `</div></div>`;

        }



        // Recent lookups

        if (d.recent.length) {

            html += `<div class="section"><h2>πŸ• Recent Lookups</h2><table class="recent-table">

                <thead><tr><th>Word</th><th>Language</th><th>Time</th></tr></thead><tbody>`;

            for (const r of d.recent) {

                const color = LANG_COLORS[r.language] || 'english';

                const time = new Date(r.time).toLocaleTimeString();

                html += `<tr>

                    <td><strong>${esc(r.word)}</strong></td>

                    <td><span class="lang-badge ${color}">${esc(r.language)}</span></td>

                    <td style="color:var(--muted);font-size:0.85rem">${time}</td>

                </tr>`;

            }

            html += `</tbody></table></div>`;

        }



        if (!d.total_lookups) {

            html += `<div class="section" style="text-align:center;color:var(--muted);padding:2rem;">

                <p style="font-size:2rem;">πŸ“­</p>

                <p>No lookups yet. Go search some words!</p>

                <a href="/" style="color:var(--accent);">← Go to Dictionary</a>

            </div>`;

        }



        document.getElementById('content').innerHTML = html;

    }



    function esc(t) { const d = document.createElement('div'); d.textContent = t; return d.innerHTML; }



    function timeSince(iso) {

        const ms = Date.now() - new Date(iso).getTime();

        const mins = Math.floor(ms / 60000);

        if (mins < 60) return `${mins}m`;

        const hrs = Math.floor(mins / 60);

        if (hrs < 24) return `${hrs}h ${mins % 60}m`;

        return `${Math.floor(hrs / 24)}d ${hrs % 24}h`;

    }



    loadStats();

    setInterval(loadStats, 15000);

    </script>
</body>
</html>