File size: 14,927 Bytes
0f07ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
{{template "views/partials/head" .}}

<body class="bg-[var(--color-bg-primary)] text-[var(--color-text-primary)]">
<div class="flex flex-col min-h-screen" x-data="tracesApp()" x-init="init()">

    {{template "views/partials/navbar" .}}

    <!-- Notifications -->
    <div class="fixed top-20 right-4 z-50 space-y-2" style="max-width: 400px;">
        <template x-for="notification in notifications" :key="notification.id">
            <div x-show="true"
                 x-transition:enter="transition ease-out duration-200"
                 x-transition:enter-start="opacity-0"
                 x-transition:enter-end="opacity-100"
                 x-transition:leave="transition ease-in duration-150"
                 x-transition:leave-start="opacity-100"
                 x-transition:leave-end="opacity-0"
                 :class="notification.type === 'error' ? 'bg-red-500' : 'bg-green-500'"
                 class="rounded-lg p-4 text-white flex items-start space-x-3">
                <div class="flex-shrink-0">
                    <i :class="notification.type === 'error' ? 'fas fa-exclamation-circle' : 'fas fa-check-circle'" class="text-xl"></i>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="text-sm font-medium break-words" x-text="notification.message"></p>
                </div>
                <button @click="dismissNotification(notification.id)" class="flex-shrink-0 text-white hover:opacity-80 transition-opacity">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        </template>
    </div>

    <div class="container mx-auto px-4 py-8 flex-grow">

        <!-- Hero Header -->
        <div class="hero-section">
            <div class="hero-content">
                <h1 class="hero-title">
                    API Traces
                </h1>
                <p class="hero-subtitle">View logged API requests and responses</p>
                <div class="flex flex-wrap justify-center gap-3">
                    <button @click="clearTraces()" class="btn-secondary text-sm py-1.5 px-3">
                        <i class="fas fa-trash mr-1.5 text-[10px]"></i>
                        <span>Clear Traces</span>
                    </button>
                    <a href="/api/traces" download="traces.json" class="btn-secondary text-sm py-1.5 px-3">
                        <i class="fas fa-download mr-1.5 text-[10px]"></i>
                        <span>Export Traces</span>
                    </a>
                </div>
            </div>
        </div>

        <!-- Tracing Settings -->
        <div class="bg-[var(--color-bg-secondary)] border border-[var(--color-primary)]/20 rounded-lg p-6 mb-8">
            <h2 class="text-xl font-semibold text-[var(--color-text-primary)] mb-4 flex items-center">
                <i class="fas fa-bug mr-2 text-[var(--color-primary)] text-sm"></i>
                Tracing Settings
            </h2>
            <p class="text-xs text-[var(--color-text-secondary)] mb-4">Configure API tracing</p>

            <div class="space-y-4">
                <!-- Enable Tracing -->
                <div class="flex items-center justify-between">
                    <div>
                        <label class="text-sm font-medium text-[var(--color-text-primary)]">Enable Tracing</label>
                        <p class="text-xs text-[var(--color-text-secondary)] mt-1">Enable tracing of requests and responses</p>
                    </div>
                    <label class="relative inline-flex items-center cursor-pointer">
                        <input type="checkbox" x-model="settings.enable_tracing"
                               @change="updateTracingEnabled()"
                               class="sr-only peer">
                        <div class="w-11 h-6 bg-[var(--color-bg-primary)] peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-[var(--color-primary-light)] rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-[var(--color-primary)]"></div>
                    </label>
                </div>

                <!-- Tracing Max Items -->
                <div>
                    <label class="block text-sm font-medium text-[var(--color-text-primary)] mb-2">Tracing Max Items</label>
                    <p class="text-xs text-[var(--color-text-secondary)] mb-2">Maximum number of tracing items to keep (0 = unlimited)</p>
                    <input type="number" x-model="settings.tracing_max_items"
                           min="0"
                           placeholder="1000"
                           :disabled="!settings.enable_tracing"
                           class="w-full px-3 py-2 bg-[var(--color-bg-primary)] border border-[var(--color-primary)]/20 rounded text-sm text-[var(--color-text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-primary)]/50"
                           :class="!settings.enable_tracing ? 'opacity-50 cursor-not-allowed' : ''">
                </div>

                <!-- Save Button -->
                <div class="flex justify-end pt-2">
                    <button @click="saveTracingSettings()"
                            :disabled="saving"
                            class="btn-primary px-4 py-2 text-sm">
                        <i class="fas fa-save mr-2" :class="saving ? 'fa-spin fa-spinner' : ''"></i>
                        <span x-text="saving ? 'Saving...' : 'Save Settings'"></span>
                    </button>
                </div>
            </div>
        </div>

        <!-- Traces Table -->
        <div class="mt-8">
            <div class="overflow-x-auto">
                <table class="w-full border-collapse">
                    <thead>
                        <tr class="border-b border-[var(--color-bg-secondary)]">
                            <th class="text-left p-2 text-xs font-semibold text-[var(--color-text-secondary)]">Method</th>
                            <th class="text-left p-2 text-xs font-semibold text-[var(--color-text-secondary)]">Path</th>
                            <th class="text-left p-2 text-xs font-semibold text-[var(--color-text-secondary)]">Status</th>
                            <th class="text-right p-2 text-xs font-semibold text-[var(--color-text-secondary)]">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <template x-for="(trace, index) in traces" :key="index">
                            <tr class="hover:bg-[var(--color-bg-secondary)]/50 border-b border-[var(--color-bg-secondary)] transition-colors">
                                <td class="p-2" x-text="trace.request.method"></td>
                                <td class="p-2" x-text="trace.request.path"></td>
                                <td class="p-2" x-text="trace.response.status"></td>
                                <td class="p-2 text-right">
                                    <button @click="showDetails(index)" class="text-[var(--color-primary)]/60 hover:text-[var(--color-primary)] hover:bg-[var(--color-primary)]/10 rounded p-1 transition-colors">
                                        <i class="fas fa-eye text-xs"></i>
                                    </button>
                                </td>
                            </tr>
                        </template>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Details Modal -->
        <div x-show="selectedTrace !== null" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50" @click="selectedTrace = null">
            <div class="bg-[var(--color-bg-secondary)] rounded-lg p-6 max-w-4xl w-full max-h-[90vh] overflow-auto" @click.stop>
                <div class="flex justify-between mb-4">
                    <h2 class="h3">Trace Details</h2>
                    <button @click="selectedTrace = null" class="text-[var(--color-text-secondary)] hover:text-[var(--color-text-primary)]">
                        <i class="fas fa-times"></i>
                    </button>
                </div>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <h3 class="text-lg font-semibold mb-2">Request Body</h3>
                        <div id="requestEditor" class="h-96 border border-[var(--color-primary-border)]/20"></div>
                    </div>
                    <div>
                        <h3 class="text-lg font-semibold mb-2">Response Body</h3>
                        <div id="responseEditor" class="h-96 border border-[var(--color-primary-border)]/20"></div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    {{template "views/partials/footer" .}}

</div>

<!-- CodeMirror -->
<link rel="stylesheet" href="static/assets/codemirror.min.css">
<script src="static/assets/codemirror.min.js"></script>
<script src="static/assets/javascript.min.js"></script>

<!-- Styles from model-editor -->
<style>
.CodeMirror {
    height: 100% !important;
    font-family: monospace;
}
</style>

<script>
function tracesApp() {
    return {
        traces: [],
        selectedTrace: null,
        requestEditor: null,
        responseEditor: null,
        notifications: [],
        settings: {
            enable_tracing: false,
            tracing_max_items: 0
        },
        saving: false,

        init() {
            this.loadTracingSettings();
            this.fetchTraces();
            setInterval(() => this.fetchTraces(), 5000);
        },

        async loadTracingSettings() {
            try {
                const response = await fetch('/api/settings');
                const data = await response.json();

                if (response.ok) {
                    this.settings.enable_tracing = data.enable_tracing || false;
                    this.settings.tracing_max_items = data.tracing_max_items || 0;
                } else {
                    this.addNotification('Failed to load tracing settings: ' + (data.error || 'Unknown error'), 'error');
                }
            } catch (error) {
                console.error('Error loading tracing settings:', error);
                this.addNotification('Failed to load tracing settings: ' + error.message, 'error');
            }
        },

        updateTracingEnabled() {
            if (!this.settings.enable_tracing) {
                this.settings.tracing_max_items = 0;
            }
        },

        async saveTracingSettings() {
            if (this.saving) return;

            this.saving = true;

            try {
                const payload = {
                    enable_tracing: this.settings.enable_tracing,
                    tracing_max_items: parseInt(this.settings.tracing_max_items) || 0
                };

                const response = await fetch('/api/settings', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(payload)
                });

                const data = await response.json();

                if (response.ok && data.success) {
                    this.addNotification('Tracing settings saved successfully!', 'success');
                } else {
                    this.addNotification('Failed to save tracing settings: ' + (data.error || 'Unknown error'), 'error');
                }
            } catch (error) {
                console.error('Error saving tracing settings:', error);
                this.addNotification('Failed to save tracing settings: ' + error.message, 'error');
            } finally {
                this.saving = false;
            }
        },

        addNotification(message, type = 'success') {
            const id = Date.now();
            this.notifications.push({ id, message, type });
            setTimeout(() => this.dismissNotification(id), 5000);
        },

        dismissNotification(id) {
            this.notifications = this.notifications.filter(n => n.id !== id);
        },

        async fetchTraces() {
            const response = await fetch('/api/traces');
            this.traces = await response.json();
        },

        async clearTraces() {
            if (confirm('Clear all traces?')) {
                await fetch('/api/traces/clear', { method: 'POST' });
                this.traces = [];
            }
        },

        showDetails(index) {
            this.selectedTrace = index;
            this.$nextTick(() => {
                const trace = this.traces[index];

                const decodeBase64 = (base64) => {
                    const binaryString = atob(base64);
                    const bytes = new Uint8Array(binaryString.length);
                    for (let i = 0; i < binaryString.length; i++) {
                        bytes[i] = binaryString.charCodeAt(i);
                    }
                    return new TextDecoder().decode(bytes);
                };

                const formatBody = (bodyText) => {
                    try {
                        const json = JSON.parse(bodyText);
                        return JSON.stringify(json, null, 2);
                    } catch {
                        return bodyText;
                    }
                };

                const reqBody = formatBody(decodeBase64(trace.request.body));
                const resBody = formatBody(decodeBase64(trace.response.body));

                if (!this.requestEditor) {
                    this.requestEditor = CodeMirror(document.getElementById('requestEditor'), {
                        value: reqBody,
                        mode: 'javascript',
                        json: true,
                        theme: 'default',
                        lineNumbers: true,
                        readOnly: true,
                        lineWrapping: true
                    });
                } else {
                    this.requestEditor.setValue(reqBody);
                }

                if (!this.responseEditor) {
                    this.responseEditor = CodeMirror(document.getElementById('responseEditor'), {
                        value: resBody,
                        mode: 'javascript',
                        json: true,
                        theme: 'default',
                        lineNumbers: true,
                        readOnly: true,
                        lineWrapping: true
                    });
                } else {
                    this.responseEditor.setValue(resBody);
                }
            });
        }
    }
}
</script>

</body>
</html>