File size: 21,615 Bytes
70c7696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5bf75f
70c7696
 
 
 
d5bf75f
70c7696
 
 
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
 
 
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
 
 
d5bf75f
 
 
 
70c7696
d5bf75f
70c7696
 
 
 
d5bf75f
 
 
70c7696
d5bf75f
 
 
 
 
 
 
 
70c7696
 
d5bf75f
70c7696
 
 
 
d5bf75f
 
 
70c7696
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
 
d5bf75f
70c7696
 
 
 
d5bf75f
 
 
70c7696
d5bf75f
 
 
 
 
70c7696
 
d5bf75f
70c7696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5bf75f
70c7696
 
 
 
d5bf75f
 
 
 
 
70c7696
d5bf75f
 
70c7696
d5bf75f
 
70c7696
d5bf75f
 
 
 
 
70c7696
 
 
 
 
 
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
d5bf75f
 
70c7696
 
d5bf75f
 
 
70c7696
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
d5bf75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c7696
d5bf75f
 
 
 
70c7696
 
d5bf75f
 
 
70c7696
d5bf75f
 
 
70c7696
d5bf75f
 
 
70c7696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
 * Status Drawer - Slide-out panel from right side
 * Shows ONLY: Resources, Endpoints, Providers status
 * Real-time updates, NO CPU/Memory stats
 */

class StatusDrawer {
  constructor(options = {}) {
    this.options = {
      apiEndpoint: options.apiEndpoint || '/api/system/status',
      updateInterval: options.updateInterval || 3000, // 3 seconds
      ...options
    };
    
    this.isOpen = false;
    this.pollTimer = null;
    this.lastData = null;
    this.drawerElement = null;
    this.buttonElement = null;
    
    this.createDrawer();
    this.createFloatingButton();
  }
  
  /**
   * Create floating button
   */
  createFloatingButton() {
    const button = document.createElement('button');
    button.id = 'status-drawer-btn';
    button.className = 'status-drawer-floating-btn';
    button.setAttribute('aria-label', 'Open status panel');
    button.innerHTML = `
      <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <circle cx="12" cy="12" r="10"/>
        <path d="M12 6v6l4 2"/>
      </svg>
    `;
    
    button.addEventListener('click', () => this.toggle());
    
    document.body.appendChild(button);
    this.buttonElement = button;
  }
  
  /**
   * Create drawer panel - ENHANCED with detailed provider metrics
   */
  createDrawer() {
    const drawer = document.createElement('div');
    drawer.id = 'status-drawer';
    drawer.className = 'status-drawer status-drawer-enhanced';
    drawer.innerHTML = `
      <div class="status-drawer-header">
        <h3>System Status</h3>
        <div class="header-actions">
          <button class="refresh-btn" id="refresh-status" aria-label="Refresh">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="23 4 23 10 17 10"></polyline>
              <polyline points="1 20 1 14 7 14"></polyline>
              <path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path>
            </svg>
          </button>
          <button class="drawer-close" aria-label="Close">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M9 18l6-6-6-6"/>
            </svg>
          </button>
        </div>
      </div>
      
      <div class="status-drawer-body">
        <!-- ALL PROVIDER STATUS -->
        <div class="status-section providers-detailed">
          <div class="section-title collapsible" data-target="providers-list">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <rect x="2" y="3" width="20" height="14" rx="2"/>
              <line x1="8" y1="21" x2="16" y2="21"/>
              <line x1="12" y1="17" x2="12" y2="21"/>
            </svg>
            <span>All Providers</span>
            <svg class="chevron" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </div>
          <div class="collapsible-content" id="providers-list">
            <div class="summary-loading">Loading...</div>
          </div>
        </div>
        
        <!-- AI MODELS -->
        <div class="status-section ai-models">
          <div class="section-title collapsible" data-target="ai-models-list">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>
            </svg>
            <span>AI Models</span>
            <svg class="chevron" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </div>
          <div class="collapsible-content" id="ai-models-list">
            <div class="summary-loading">Loading...</div>
          </div>
        </div>
        
        <!-- INFRASTRUCTURE -->
        <div class="status-section infrastructure">
          <div class="section-title collapsible" data-target="infrastructure-list">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <rect x="2" y="2" width="20" height="8" rx="2" ry="2"></rect>
              <rect x="2" y="14" width="20" height="8" rx="2" ry="2"></rect>
              <line x1="6" y1="6" x2="6" y2="6"></line>
              <line x1="6" y1="18" x2="6" y2="18"></line>
            </svg>
            <span>Infrastructure</span>
            <svg class="chevron" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </div>
          <div class="collapsible-content" id="infrastructure-list">
            <div class="summary-loading">Loading...</div>
          </div>
        </div>
        
        <!-- RESOURCE BREAKDOWN -->
        <div class="status-section resource-breakdown">
          <div class="section-title collapsible" data-target="resources-breakdown">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <line x1="8" y1="6" x2="21" y2="6"></line>
              <line x1="8" y1="12" x2="21" y2="12"></line>
              <line x1="8" y1="18" x2="21" y2="18"></line>
              <line x1="3" y1="6" x2="3" y2="6"></line>
              <line x1="3" y1="12" x2="3" y2="12"></line>
              <line x1="3" y1="18" x2="3" y2="18"></line>
            </svg>
            <span>Resource Breakdown</span>
            <svg class="chevron" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </div>
          <div class="collapsible-content" id="resources-breakdown">
            <div class="summary-loading">Loading...</div>
          </div>
        </div>
        
        <!-- ERROR DETAILS -->
        <div class="status-section error-details">
          <div class="section-title collapsible" data-target="error-list">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <circle cx="12" cy="12" r="10"></circle>
              <line x1="12" y1="8" x2="12" y2="12"></line>
              <line x1="12" y1="16" x2="12" y2="16"></line>
            </svg>
            <span>Recent Errors</span>
            <svg class="chevron" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </div>
          <div class="collapsible-content collapsed" id="error-list">
            <div class="summary-loading">Loading...</div>
          </div>
        </div>
        
        <!-- PERFORMANCE -->
        <div class="status-section performance">
          <div class="section-title collapsible" data-target="performance-metrics">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
            </svg>
            <span>Performance</span>
            <svg class="chevron" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </div>
          <div class="collapsible-content" id="performance-metrics">
            <div class="summary-loading">Loading...</div>
          </div>
        </div>
        
        <!-- Last Update -->
        <div class="drawer-footer">
          <span class="last-update-label">Last update:</span>
          <span class="last-update-time" id="last-update-time">--</span>
        </div>
      </div>
    `;
    
    document.body.appendChild(drawer);
    this.drawerElement = drawer;
    
    // Close button
    drawer.querySelector('.drawer-close').addEventListener('click', () => this.close());
    
    // Refresh button
    drawer.querySelector('#refresh-status').addEventListener('click', () => this.fetchStatus());
    
    // Collapsible sections
    drawer.querySelectorAll('.section-title.collapsible').forEach(title => {
      title.addEventListener('click', (e) => {
        const target = title.dataset.target;
        const content = document.getElementById(target);
        if (content) {
          content.classList.toggle('collapsed');
          title.classList.toggle('collapsed');
        }
      });
    });
  }
  
  /**
   * Toggle drawer
   */
  toggle() {
    if (this.isOpen) {
      this.close();
    } else {
      this.open();
    }
  }
  
  /**
   * Open drawer
   */
  open() {
    if (this.isOpen) return;
    
    this.isOpen = true;
    this.drawerElement.classList.add('open');
    this.buttonElement.classList.add('hidden');
    
    // Start polling
    this.startPolling();
  }
  
  /**
   * Close drawer
   */
  close() {
    if (!this.isOpen) return;
    
    this.isOpen = false;
    this.drawerElement.classList.remove('open');
    this.buttonElement.classList.remove('hidden');
    
    // Stop polling
    this.stopPolling();
  }
  
  /**
   * Start polling (only when open)
   */
  startPolling() {
    if (!this.isOpen) return;
    
    this.fetchStatus();
    this.pollTimer = setTimeout(() => this.startPolling(), this.options.updateInterval);
  }
  
  /**
   * Stop polling
   */
  stopPolling() {
    if (this.pollTimer) {
      clearTimeout(this.pollTimer);
      this.pollTimer = null;
    }
  }
  
  /**
   * Fetch status from API
   */
  async fetchStatus() {
    if (!this.isOpen) return;
    
    try {
      const response = await fetch(this.options.apiEndpoint);
      if (!response.ok) throw new Error(`HTTP ${response.status}`);
      
      const data = await response.json();
      this.updateUI(data);
      
    } catch (error) {
      console.error('Status Drawer: Failed to fetch:', error);
      this.showError();
    }
  }
  
  /**
   * Update UI with data - ENHANCED
   */
  updateUI(data) {
    this.lastData = data;
    
    // Update all providers with detailed metrics
    this.updateProvidersDetailed(data.providers_detailed || data.services || []);
    
    // Update AI models
    this.updateAIModels(data.ai_models || {});
    
    // Update infrastructure
    this.updateInfrastructure(data.infrastructure || {});
    
    // Update resource breakdown
    this.updateResourceBreakdown(data.resource_breakdown || {});
    
    // Update error details
    this.updateErrorDetails(data.error_details || []);
    
    // Update performance
    this.updatePerformance(data.performance || {});
    
    // Update timestamp
    this.updateTimestamp(data.timestamp);
  }
  
  /**
   * Update providers with detailed metrics
   */
  updateProvidersDetailed(providers) {
    const container = document.getElementById('providers-list');
    if (!container) return;
    
    if (!providers.length) {
      container.innerHTML = '<div class="empty-state">No providers configured</div>';
      return;
    }
    
    container.innerHTML = providers.map(provider => {
      const isOnline = provider.status === 'online' || provider.status === 'active';
      const statusEmoji = isOnline ? '🟒' : 
                         provider.status === 'rate_limited' ? 'πŸ”΄' : 
                         provider.status === 'degraded' ? '🟑' : '⚫';
      
      let statusText = '';
      if (isOnline) {
        statusText = `${provider.response_time_ms || 0}ms | Success: ${provider.success_rate || 100}%`;
        if (provider.last_check) {
          const elapsed = Math.floor((Date.now() / 1000) - new Date(provider.last_check).getTime() / 1000);
          statusText += ` | Last: ${elapsed}s ago`;
        }
      } else if (provider.status === 'rate_limited') {
        statusText = `Rate Limited (${provider.status_code || 429})`;
        if (provider.cached_until) {
          statusText += ` | Cached ${provider.cached_until}`;
        }
      } else if (provider.status === 'degraded') {
        statusText = provider.error || 'Degraded performance';
      } else {
        statusText = provider.error || 'Offline';
      }
      
      const resourceInfo = provider.resource_count ? ` | ${provider.resource_count} resources` : '';
      
      return `
        <div class="provider-item ${isOnline ? 'online' : 'offline'}">
          <div class="provider-status">
            <span class="status-emoji">${statusEmoji}</span>
            <span class="provider-name">${provider.name}</span>
          </div>
          <div class="provider-metrics">${statusText}${resourceInfo}</div>
        </div>
      `;
    }).join('');
  }
  
  /**
   * Update AI models section
   */
  updateAIModels(aiModels) {
    const container = document.getElementById('ai-models-list');
    if (!container) return;
    
    const transformersStatus = aiModels.transformers_loaded ? '🟒 Loaded (CPU mode)' : 'πŸ”΄ Not loaded';
    const sentimentModels = aiModels.sentiment_models || 0;
    const hfApiStatus = aiModels.hf_api_active ? '🟒 Active' : 'πŸ”΄ Inactive';
    
    container.innerHTML = `
      <div class="metric-item">
        <span class="metric-label">Transformers:</span>
        <span class="metric-value">${transformersStatus}</span>
      </div>
      <div class="metric-item">
        <span class="metric-label">Sentiment Models:</span>
        <span class="metric-value">${sentimentModels} available</span>
      </div>
      <div class="metric-item">
        <span class="metric-label">HuggingFace API:</span>
        <span class="metric-value">${hfApiStatus}</span>
      </div>
    `;
  }
  
  /**
   * Update infrastructure section
   */
  updateInfrastructure(infrastructure) {
    const container = document.getElementById('infrastructure-list');
    if (!container) return;
    
    const dbStatus = infrastructure.database_status || 'unknown';
    const dbEntries = infrastructure.database_entries || 0;
    const workerStatus = infrastructure.background_worker || 'unknown';
    const workerNextRun = infrastructure.worker_next_run || 'N/A';
    const wsStatus = infrastructure.websocket_active ? '🟒 Active' : '⚫ Inactive';
    
    container.innerHTML = `
      <div class="metric-item">
        <span class="metric-label">Database:</span>
        <span class="metric-value">${dbStatus === 'online' ? '🟒' : 'πŸ”΄'} SQLite (${dbEntries} cached)</span>
      </div>
      <div class="metric-item">
        <span class="metric-label">Background Worker:</span>
        <span class="metric-value">${workerStatus === 'active' ? '🟒' : '⚫'} ${workerNextRun}</span>
      </div>
      <div class="metric-item">
        <span class="metric-label">WebSocket:</span>
        <span class="metric-value">${wsStatus}</span>
      </div>
    `;
  }
  
  /**
   * Update resource breakdown section
   */
  updateResourceBreakdown(breakdown) {
    const container = document.getElementById('resources-breakdown');
    if (!container) return;
    
    const total = breakdown.total || 0;
    const bySource = breakdown.by_source || {};
    const byCategory = breakdown.by_category || {};
    
    let sourceHTML = '';
    for (const [source, count] of Object.entries(bySource)) {
      sourceHTML += `
        <div class="breakdown-item">
          <span class="breakdown-label">${source}:</span>
          <span class="breakdown-value">${count}</span>
        </div>
      `;
    }
    
    let categoryHTML = '';
    for (const [category, count] of Object.entries(byCategory)) {
      categoryHTML += `
        <div class="breakdown-item">
          <span class="breakdown-label">${category}:</span>
          <span class="breakdown-value">${count} online</span>
        </div>
      `;
    }
    
    container.innerHTML = `
      <div class="breakdown-section">
        <div class="breakdown-title">Total: ${total}+ resources</div>
        ${sourceHTML}
      </div>
      <div class="breakdown-section">
        <div class="breakdown-title">By Category:</div>
        ${categoryHTML}
      </div>
    `;
  }
  
  /**
   * Update error details section
   */
  updateErrorDetails(errors) {
    const container = document.getElementById('error-list');
    if (!container) return;
    
    if (!errors || errors.length === 0) {
      container.innerHTML = '<div class="empty-state">No recent errors</div>';
      return;
    }
    
    container.innerHTML = errors.map(error => `
      <div class="error-item">
        <div class="error-provider">${error.provider || 'Unknown'}: ${error.count || 1}x ${error.type || 'error'}</div>
        <div class="error-message">${error.message || 'Unknown error'}</div>
        ${error.action ? `<div class="error-action">Action: ${error.action}</div>` : ''}
      </div>
    `).join('');
  }
  
  /**
   * Update performance section
   */
  updatePerformance(performance) {
    const container = document.getElementById('performance-metrics');
    if (!container) return;
    
    const avgResponse = performance.avg_response_ms || 0;
    const fastest = performance.fastest_provider || 'N/A';
    const fastestTime = performance.fastest_time_ms || 0;
    const cacheHit = performance.cache_hit_rate || 0;
    
    container.innerHTML = `
      <div class="metric-item">
        <span class="metric-label">Avg Response:</span>
        <span class="metric-value">${avgResponse}ms</span>
      </div>
      <div class="metric-item">
        <span class="metric-label">Fastest:</span>
        <span class="metric-value">${fastest} (${fastestTime}ms)</span>
      </div>
      <div class="metric-item">
        <span class="metric-label">Cache Hit:</span>
        <span class="metric-value">${cacheHit}%</span>
      </div>
    `;
  }
  
  /**
   * Update endpoints
   */
  updateEndpoints(endpoints) {
    const container = document.getElementById('endpoints-status');
    if (!container) return;
    
    if (!endpoints.length) {
      container.innerHTML = '<div class="empty-state">No endpoints</div>';
      return;
    }
    
    container.innerHTML = endpoints.map(endpoint => {
      const statusClass = endpoint.status === 'online' ? 'status-online' : 'status-offline';
      return `
        <div class="status-item ${statusClass}">
          <div class="status-dot"></div>
          <div class="status-info">
            <div class="status-name">${endpoint.path}</div>
            <div class="status-meta">
              ${endpoint.avg_response_ms ? `${endpoint.avg_response_ms.toFixed(0)}ms` : '--'} β€’ 
              ${endpoint.success_rate ? `${endpoint.success_rate.toFixed(1)}%` : '--'}
            </div>
          </div>
        </div>
      `;
    }).join('');
  }
  
  /**
   * Update providers
   */
  updateProviders(services) {
    const container = document.getElementById('providers-status');
    if (!container) return;
    
    if (!services.length) {
      container.innerHTML = '<div class="empty-state">No providers</div>';
      return;
    }
    
    container.innerHTML = services.map(service => {
      const statusClass = service.status === 'online' ? 'status-online' : 'status-offline';
      return `
        <div class="status-item ${statusClass}">
          <div class="status-dot"></div>
          <div class="status-info">
            <div class="status-name">${service.name}</div>
            <div class="status-meta">
              ${service.response_time_ms ? `${service.response_time_ms.toFixed(0)}ms` : 'Offline'}
            </div>
          </div>
        </div>
      `;
    }).join('');
  }
  
  /**
   * Update coins
   */
  updateCoins(coins) {
    const container = document.getElementById('coins-status');
    if (!container) return;
    
    if (!coins.length) {
      container.innerHTML = '<div class="empty-state">No coins</div>';
      return;
    }
    
    container.innerHTML = coins.map(coin => {
      const statusClass = coin.status === 'online' ? 'status-online' : 'status-offline';
      return `
        <div class="status-item ${statusClass}">
          <div class="status-dot"></div>
          <div class="status-info">
            <div class="status-name">${coin.symbol}</div>
            <div class="status-meta">
              ${coin.price ? `$${coin.price.toLocaleString()}` : 'Unavailable'}
            </div>
          </div>
        </div>
      `;
    }).join('');
  }
  
  /**
   * Update timestamp
   */
  updateTimestamp(timestamp) {
    const element = document.getElementById('last-update-time');
    if (element) {
      const date = new Date(timestamp * 1000);
      element.textContent = date.toLocaleTimeString();
    }
  }
  
  /**
   * Show error state
   */
  showError() {
    const sections = ['resources-summary', 'endpoints-status', 'providers-status', 'coins-status'];
    sections.forEach(id => {
      const element = document.getElementById(id);
      if (element) {
        element.innerHTML = '<div class="error-state">Failed to load</div>';
      }
    });
  }
  
  /**
   * Destroy drawer
   */
  destroy() {
    this.stopPolling();
    if (this.drawerElement) this.drawerElement.remove();
    if (this.buttonElement) this.buttonElement.remove();
  }
}

// Export
if (typeof window !== 'undefined') {
  window.StatusDrawer = StatusDrawer;
}