// Main Dashboard Controller class Dashboard { constructor() { this.currentPage = 'overview'; this.init(); } init() { if (!window.authManager || !window.authManager.isAuthenticated) { return; } this.setupNavigation(); this.setupSidebar(); this.setupRefresh(); this.updateTime(); // Load initial page from hash or default to overview const initialPage = window.location.hash.substring(1) || 'overview'; this.loadPage(initialPage); setInterval(() => this.updateTime(), 1000); } setupNavigation() { const menuItems = document.querySelectorAll('.menu-item'); menuItems.forEach(item => { item.onclick = (e) => { e.preventDefault(); const page = item.getAttribute('data-page') || item.getAttribute('href').substring(1); this.loadPage(page); }; }); window.onhashchange = () => { const page = window.location.hash.substring(1) || 'overview'; if (page !== this.currentPage) { this.loadPage(page); } }; } setupSidebar() { const toggleBtn = document.getElementById('sidebar-toggle'); const sidebar = document.querySelector('.sidebar'); const logoutBtn = document.getElementById('logout-btn'); if (toggleBtn && sidebar) { toggleBtn.onclick = () => { sidebar.classList.toggle('collapsed'); localStorage.setItem('sidebar_collapsed', sidebar.classList.contains('collapsed')); }; if (localStorage.getItem('sidebar_collapsed') === 'true') { sidebar.classList.add('collapsed'); } } if (logoutBtn) { logoutBtn.onclick = () => { window.authManager.logout(); }; } } setupRefresh() { const refreshBtn = document.getElementById('refresh-btn'); if (refreshBtn) { refreshBtn.onclick = () => this.refreshCurrentPage(); } } updateTime() { const timeElement = document.getElementById('current-time'); if (timeElement) { timeElement.textContent = luxon.DateTime.now().toFormat('HH:mm:ss'); } } async loadPage(page) { if (window.chartManager) { window.chartManager.destroyAllCharts(); } this.currentPage = page; window.location.hash = page; window.scrollTo(0, 0); // Update menu active state document.querySelectorAll('.menu-item').forEach(item => { item.classList.toggle('active', (item.dataset.page || item.getAttribute('href').substring(1)) === page); }); const titleElement = document.getElementById('page-title'); const titles = { 'overview': 'Overview', 'analytics': 'Analytics', 'costs': 'Costs', 'clients': 'Clients', 'providers': 'Providers', 'monitoring': 'Monitoring', 'settings': 'Settings', 'logs': 'Logs' }; if (titleElement) titleElement.textContent = titles[page] || 'Dashboard'; this.showLoading(); try { const content = document.getElementById('page-content'); if (content) { content.innerHTML = await this.getPageTemplate(page); await this.initializePageScript(page); } } catch (error) { console.error(`Error loading page ${page}:`, error); this.showError(`Failed to load ${page}`); } finally { this.hideLoading(); } } showLoading() { const content = document.getElementById('page-content'); if (content) content.classList.add('loading'); } hideLoading() { const content = document.getElementById('page-content'); if (content) content.classList.remove('loading'); } async getPageTemplate(page) { // Return templates directly based on the page name switch (page) { case 'overview': return this.getOverviewTemplate(); case 'clients': return this.getClientsTemplate(); case 'providers': return this.getProvidersTemplate(); case 'models': return this.getModelsTemplate(); case 'logs': return this.getLogsTemplate(); case 'monitoring': return this.getMonitoringTemplate(); case 'settings': return '
Loading settings...
'; case 'analytics': return this.getAnalyticsTemplate(); case 'costs': return this.getCostsTemplate(); default: return '

Page not found

'; } } async initializePageScript(page) { const initFn = `init${page.charAt(0).toUpperCase() + page.slice(1)}`; if (typeof window[initFn] === 'function') { await window[initFn](); } } refreshCurrentPage() { const refreshBtn = document.getElementById('refresh-btn'); if (refreshBtn) { refreshBtn.classList.add('fa-spin'); setTimeout(() => refreshBtn.classList.remove('fa-spin'), 1000); } this.loadPage(this.currentPage); } showError(message) { const content = document.getElementById('page-content'); if (content) { content.innerHTML = `

Error

${message}

`; } } // Templates getOverviewTemplate() { return `

Request Volume (Last 24h)

Provider Share

System Health

Recent Activity

TimeClientProviderModelTokensStatus
`; } getClientsTemplate() { return `

API Clients

Manage tokens and access

IDNameTokenCreatedLast UsedRequestsStatusActions

Usage by Client

`; } getProvidersTemplate() { return `
`; } getModelsTemplate() { return `

Model Registry

Manage model availability and custom pricing

IDDisplay NameProviderPricing (In/Out)ContextStatusActions
`; } getLogsTemplate() { return `

Detailed Request Logs

TimestampStatusContextRequest Details
`; } getAnalyticsTemplate() { return `

Usage Analytics

Request volume and token distribution

Volume by Client

Model Distribution

Historical Usage Log

DateClientProviderModelRequestsTokensCost
`; } getCostsTemplate() { return `

Provider Spending

Budget Tracking

Active Model Pricing

ProviderModelInput CostOutput CostLast Updated
`; } getMonitoringTemplate() { return `

Real-time Stream

Live request activity and system metrics

Incoming Requests

System Performance

Latency (ms)

Error Rate (%)

Rate Limiting

`; } } document.addEventListener('DOMContentLoaded', () => { window.initDashboard = () => { window.dashboard = new Dashboard(); }; if (window.authManager && window.authManager.isAuthenticated) { window.initDashboard(); } });