File dashboard-v2.js of Package PersistenceOS

/**
 * PersistenceOS Dashboard Module v2
 * Modular dashboard system for PersistenceOS
 * Extracted for Phase 3 modular architecture
 */

/**
 * Dashboard Configuration
 */
const DASHBOARD_CONFIG = {
    REFRESH_INTERVAL: 30000, // 30 seconds
    API_ENDPOINTS: {
        SYSTEM_INFO: '/api/system/info',
        VMS: '/api/vms',
        STORAGE: '/api/storage/pools',
        NETWORK: '/api/network/interfaces',
        UPDATES: '/api/system/updates'
    },
    DEBUG: true
};

/**
 * Dashboard Data Manager
 * Handles all dashboard data and state management
 */
class PersistenceDashboard {
    constructor() {
        this.data = {
            systemStats: {
                hostname: 'persistenceos-test',
                version: '6.1.0',
                uptime: '2 days, 14:32:15',
                cpu_usage: 45.2,
                memory_usage: 67.8,
                disk_usage: 23.1
            },
            vmStats: {
                running: 3,
                stopped: 2,
                total: 5
            },
            storageStats: {
                healthy: 2,
                degraded: 0,
                total: 2
            },
            networkStats: {
                interfaces: [
                    { name: 'eth0', rx: '2.5 MB/s', tx: '0.8 MB/s' },
                    { name: 'virbr0', rx: '0.2 MB/s', tx: '0.1 MB/s' }
                ]
            },
            updateStats: {
                upToDate: true,
                lastChecked: 'Today at 08:15 AM'
            }
        };
        
        this.isRefreshing = false;
        this.refreshInterval = null;
        
        this.init();
    }

    init() {
        if (DASHBOARD_CONFIG.DEBUG) {
            console.log('📊 PersistenceOS Dashboard Module v2 initialized');
        }
        
        // Start auto-refresh
        this.startAutoRefresh();
    }

    /**
     * Get current system statistics
     */
    getSystemStats() {
        return { ...this.data.systemStats };
    }

    /**
     * Get VM statistics
     */
    getVMStats() {
        return { ...this.data.vmStats };
    }

    /**
     * Get storage statistics
     */
    getStorageStats() {
        return { ...this.data.storageStats };
    }

    /**
     * Get network statistics
     */
    getNetworkStats() {
        return { ...this.data.networkStats };
    }

    /**
     * Get update statistics
     */
    getUpdateStats() {
        return { ...this.data.updateStats };
    }

    /**
     * Refresh system information
     */
    async refreshSystemInfo() {
        if (this.isRefreshing) return;
        
        this.isRefreshing = true;
        
        if (DASHBOARD_CONFIG.DEBUG) {
            console.log('🔄 Refreshing dashboard data...');
        }

        try {
            // Simulate API calls with random data for demo
            await this.simulateDataRefresh();
            
            if (DASHBOARD_CONFIG.DEBUG) {
                console.log('✅ Dashboard data refreshed successfully');
            }
            
            // Notify listeners
            this.notifyDataUpdate();
            
        } catch (error) {
            if (DASHBOARD_CONFIG.DEBUG) {
                console.error('❌ Dashboard refresh failed:', error);
            }
            throw error;
        } finally {
            this.isRefreshing = false;
        }
    }

    /**
     * Simulate data refresh with random values
     */
    async simulateDataRefresh() {
        return new Promise((resolve) => {
            setTimeout(() => {
                // Update system stats with random values
                this.data.systemStats.cpu_usage = Math.floor(Math.random() * 100);
                this.data.systemStats.memory_usage = Math.floor(Math.random() * 100);
                this.data.systemStats.disk_usage = Math.floor(Math.random() * 100);
                this.data.systemStats.uptime = new Date().toLocaleString();
                
                // Update network stats
                this.data.networkStats.interfaces.forEach(iface => {
                    iface.rx = (Math.random() * 5).toFixed(1) + ' MB/s';
                    iface.tx = (Math.random() * 2).toFixed(1) + ' MB/s';
                });
                
                resolve();
            }, 1000);
        });
    }

    /**
     * Start auto-refresh timer
     */
    startAutoRefresh() {
        if (this.refreshInterval) {
            clearInterval(this.refreshInterval);
        }
        
        this.refreshInterval = setInterval(() => {
            this.refreshSystemInfo().catch(error => {
                if (DASHBOARD_CONFIG.DEBUG) {
                    console.warn('âš ī¸ Auto-refresh failed:', error);
                }
            });
        }, DASHBOARD_CONFIG.REFRESH_INTERVAL);
        
        if (DASHBOARD_CONFIG.DEBUG) {
            console.log('⏰ Auto-refresh started (30s interval)');
        }
    }

    /**
     * Stop auto-refresh timer
     */
    stopAutoRefresh() {
        if (this.refreshInterval) {
            clearInterval(this.refreshInterval);
            this.refreshInterval = null;
            
            if (DASHBOARD_CONFIG.DEBUG) {
                console.log('âšī¸ Auto-refresh stopped');
            }
        }
    }

    /**
     * Notify data update listeners
     */
    notifyDataUpdate() {
        // Dispatch custom event for Vue reactivity
        window.dispatchEvent(new CustomEvent('dashboardDataUpdate', {
            detail: {
                systemStats: this.getSystemStats(),
                vmStats: this.getVMStats(),
                storageStats: this.getStorageStats(),
                networkStats: this.getNetworkStats(),
                updateStats: this.getUpdateStats()
            }
        }));
    }

    /**
     * Get section title for navigation
     */
    getSectionTitle(section) {
        const titles = {
            'dashboard': 'Dashboard',
            'vms': 'Virtual Machines',
            'storage': 'Storage',
            'network': 'Network',
            'snapshots': 'Snapshots',
            'settings': 'Settings'
        };
        return titles[section] || section.charAt(0).toUpperCase() + section.slice(1);
    }

    /**
     * Cleanup resources
     */
    destroy() {
        this.stopAutoRefresh();
        
        if (DASHBOARD_CONFIG.DEBUG) {
            console.log('đŸ—‘ī¸ Dashboard module destroyed');
        }
    }
}

// Create global dashboard instance
window.PersistenceDashboard = new PersistenceDashboard();

// Backward compatibility
window.Dashboard = window.PersistenceDashboard;

// Export for module systems
if (typeof module !== 'undefined' && module.exports) {
    module.exports = { PersistenceDashboard, DASHBOARD_CONFIG };
}

console.log('✅ PersistenceOS Dashboard Module v2 loaded successfully');
openSUSE Build Service is sponsored by