// AI Server Admin Dashboard JavaScript class AdminDashboard { constructor() { this.apiKey = this.getApiKey(); this.baseUrl = window.location.origin; this.init(); } getApiKey() { // 테스트 모드에서는 기본 API 키 사용 let apiKey = localStorage.getItem('ai_admin_api_key'); if (!apiKey) { // 테스트 모드 기본 키 apiKey = 'test-admin-key-123'; localStorage.setItem('ai_admin_api_key', apiKey); // 사용자에게 알림 setTimeout(() => { alert('테스트 모드입니다.\nAPI Key: test-admin-key-123'); }, 1000); } return apiKey; } async init() { this.updateCurrentTime(); setInterval(() => this.updateCurrentTime(), 1000); await this.loadSystemStatus(); await this.loadModels(); await this.loadApiKeys(); // Auto-refresh every 30 seconds setInterval(() => { this.loadSystemStatus(); this.loadModels(); }, 30000); } updateCurrentTime() { const now = new Date(); document.getElementById('current-time').textContent = now.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }); } async apiRequest(endpoint, options = {}) { const url = `${this.baseUrl}${endpoint}`; const defaultOptions = { headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey } }; try { const response = await fetch(url, { ...defaultOptions, ...options }); if (!response.ok) { if (response.status === 401) { localStorage.removeItem('ai_admin_api_key'); location.reload(); return; } throw new Error(`HTTP ${response.status}: ${response.statusText}`); } return await response.json(); } catch (error) { console.error('API Request failed:', error); throw error; } } async loadSystemStatus() { try { // Check AI Server status const healthResponse = await this.apiRequest('/health'); document.getElementById('server-status').textContent = 'Online'; document.getElementById('server-status').className = 'status-value'; // Check Ollama status try { const ollamaResponse = await this.apiRequest('/admin/ollama/status'); document.getElementById('ollama-status').textContent = ollamaResponse.status === 'online' ? 'Online' : 'Offline'; document.getElementById('ollama-status').className = `status-value ${ollamaResponse.status === 'online' ? '' : 'error'}`; } catch (error) { document.getElementById('ollama-status').textContent = 'Offline'; document.getElementById('ollama-status').className = 'status-value error'; } // Load active model try { const modelResponse = await this.apiRequest('/admin/models/active'); document.getElementById('active-model').textContent = modelResponse.model || 'None'; } catch (error) { document.getElementById('active-model').textContent = 'Unknown'; } // Load API call stats (placeholder) document.getElementById('api-calls').textContent = '0'; } catch (error) { console.error('Failed to load system status:', error); document.getElementById('server-status').textContent = 'Error'; document.getElementById('server-status').className = 'status-value error'; } } async loadModels() { try { const response = await this.apiRequest('/admin/models'); const models = response.models || []; const tbody = document.getElementById('models-tbody'); if (models.length === 0) { tbody.innerHTML = '