File size: 7,212 Bytes
4a75cb6
 
 
 
 
 
 
 
 
 
a481e76
 
 
 
 
 
4a75cb6
a481e76
 
 
 
 
 
 
 
 
4a75cb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Initialize charts
let liveFeedChart, distributionChart, anomalyChart, lossTrendChart;

// Simulate telemetry data
let packetCounter = 0;
let missingPackets = 0;
let anomalies = 0;
let isTimelinePaused = false;
let timelineData = [];
document.addEventListener('DOMContentLoaded', function() {
    // Only initialize if on dashboard page
    if (document.getElementById('live-feed-chart')) {
        initCharts();
        simulateTelemetryData();
        document.getElementById('pause-timeline').addEventListener('click', toggleTimeline);
    }
    
    // Set active nav link based on current page
    const navLinks = document.querySelectorAll('custom-navbar a');
    navLinks.forEach(link => {
        if (link.getAttribute('href') === window.location.pathname) {
            link.classList.add('active');
        } else {
            link.classList.remove('active');
        }
    });
});

function initCharts() {
    const ctx1 = document.getElementById('live-feed-chart').getContext('2d');
    liveFeedChart = new Chart(ctx1, {
        type: 'line',
        data: {
            labels: Array(20).fill().map((_, i) => i),
            datasets: [{
                label: 'Packet Rate',
                data: Array(20).fill(0),
                borderColor: 'rgb(96, 165, 250)',
                tension: 0.1,
                fill: true,
                backgroundColor: 'rgba(96, 165, 250, 0.1)'
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: { display: false }
            },
            scales: {
                y: { beginAtZero: true },
                x: { display: false }
            }
        }
    });

    const ctx2 = document.getElementById('packet-distribution-chart').getContext('2d');
    distributionChart = new Chart(ctx2, {
        type: 'doughnut',
        data: {
            labels: ['Command', 'Telemetry', 'Science', 'Engineering'],
            datasets: [{
                data: [25, 40, 20, 15],
                backgroundColor: [
                    'rgba(59, 130, 246, 0.8)',
                    'rgba(139, 92, 246, 0.8)',
                    'rgba(16, 185, 129, 0.8)',
                    'rgba(245, 158, 11, 0.8)'
                ]
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false
        }
    });

    const ctx3 = document.getElementById('anomaly-detection-chart').getContext('2d');
    anomalyChart = new Chart(ctx3, {
        type: 'bar',
        data: {
            labels: ['Temperature', 'Power', 'Radiation', 'Comms', 'Orientation'],
            datasets: [{
                label: 'Anomaly Score',
                data: [0.2, 0.1, 0.05, 0.3, 0.15],
                backgroundColor: 'rgba(239, 68, 68, 0.8)'
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            scales: {
                y: { max: 1 }
            }
        }
    });

    const ctx4 = document.getElementById('packet-loss-trend-chart').getContext('2d');
    lossTrendChart = new Chart(ctx4, {
        type: 'line',
        data: {
            labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
            datasets: [{
                label: 'Packet Loss %',
                data: [2.1, 1.8, 1.5, 1.2, 0.9, 1.1],
                borderColor: 'rgb(239, 68, 68)',
                tension: 0.1
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: { display: false }
            }
        }
    });
}

function simulateTelemetryData() {
    setInterval(() => {
        if (Math.random() < 0.95) { // 95% packet reception rate
            packetCounter++;
            
            // Update live feed chart
            liveFeedChart.data.datasets[0].data.shift();
            liveFeedChart.data.datasets[0].data.push(Math.floor(Math.random() * 100));
            liveFeedChart.update();
            
            // Add to timeline if not paused
            if (!isTimelinePaused) {
                const now = new Date();
                const packet = {
                    id: packetCounter,
                    timestamp: now.toISOString(),
                    type: ['CMD', 'TLM', 'SCI', 'ENG'][Math.floor(Math.random() * 4)],
                    size: Math.floor(Math.random() * 1024) + 128,
                    hasAnomaly: Math.random() < 0.05
                };
                
                if (packet.hasAnomaly) anomalies++;
                
                timelineData.unshift(packet);
                updateTimeline();
                updateRawData(packet);
            }
        } else {
            missingPackets++;
        }
        
        // Update counters
        document.getElementById('packet-count').textContent = packetCounter;
        document.getElementById('missing-count').textContent = missingPackets;
        document.getElementById('anomaly-count').textContent = anomalies;
    }, 500);
}

function updateTimeline() {
    const timeline = document.getElementById('packet-timeline');
    timeline.innerHTML = '';
    
    timelineData.slice(0, 20).forEach(packet => {
        const item = document.createElement('div');
        item.className = `flex items-center justify-between p-2 rounded-lg ${packet.hasAnomaly ? 'bg-red-900/50' : 'bg-gray-700/50'}`;
        
        item.innerHTML = `
            <div class="flex items-center">
                <span class="text-xs font-mono text-gray-400 mr-2">${packet.timestamp.split('T')[1].substring(0, 8)}</span>
                <span class="font-mono ${packet.hasAnomaly ? 'text-red-400' : 'text-blue-400'}">${packet.type}-${packet.id.toString().padStart(6, '0')}</span>
            </div>
            <span class="text-xs text-gray-400">${packet.size} bytes</span>
        `;
        
        timeline.appendChild(item);
    });
}

function updateRawData(packet) {
    const rawData = document.getElementById('raw-data');
    const packetText = `${packet.timestamp} - ${packet.type}-${packet.id.toString().padStart(6, '0')} - Size: ${packet.size} bytes${packet.hasAnomaly ? ' - ANOMALY DETECTED!' : ''}\n`;
    
    // Keep only last 20 lines
    const currentText = rawData.textContent;
    const lines = currentText.split('\n').slice(0, 19);
    lines.unshift(packetText);
    rawData.textContent = lines.join('\n');
}

function toggleTimeline() {
    isTimelinePaused = !isTimelinePaused;
    const button = document.getElementById('pause-timeline');
    const status = document.getElementById('timeline-status');
    
    if (isTimelinePaused) {
        button.innerHTML = '<i data-feather="play" class="mr-2"></i> Resume';
        status.textContent = 'Paused';
        status.classList.remove('text-green-400', 'live-indicator');
        status.classList.add('text-yellow-400');
    } else {
        button.innerHTML = '<i data-feather="pause" class="mr-2"></i> Pause';
        status.textContent = 'Live';
        status.classList.add('text-green-400', 'live-indicator');
        status.classList.remove('text-yellow-400');
    }
    feather.replace();
}