Spaces:
Paused
Paused
File size: 7,839 Bytes
bd6de9f |
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 |
// Configuration
const API_BASE_URL = '/api'; // Use relative URL
const UPDATE_INTERVAL = 1000; // 1 second
const DEBUG = true; // Enable debug logging
// Debug logger
function debug(message, data = null) {
if (DEBUG) {
if (data) {
console.log(`[Debug] ${message}`, data);
} else {
console.log(`[Debug] ${message}`);
}
}
}
// Chart configuration
const chartConfig = {
hashrate: {
data: {
labels: [],
datasets: [{
label: 'Hashrate (KH/s)',
data: [],
borderColor: '#2ecc71',
tension: 0.4,
fill: false
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(255, 255, 255, 0.1)'
}
},
x: {
grid: {
color: 'rgba(255, 255, 255, 0.1)'
}
}
},
plugins: {
legend: {
display: false
}
}
}
},
totalHashes: {
data: {
labels: [],
datasets: [{
label: 'Total Hashes',
data: [],
borderColor: '#3498db',
tension: 0.4,
fill: false
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(255, 255, 255, 0.1)'
}
},
x: {
grid: {
color: 'rgba(255, 255, 255, 0.1)'
}
}
},
plugins: {
legend: {
display: false
}
}
}
}
};
// DOM Elements
const elements = {
startButton: document.getElementById('startButton'),
stopButton: document.getElementById('stopButton'),
statusText: document.getElementById('statusText'),
statusDot: document.querySelector('.status-dot'),
lastUpdate: document.getElementById('lastUpdate'),
hashrateValue: document.getElementById('hashrateValue'),
totalHashesValue: document.getElementById('totalHashesValue'),
blocksFoundValue: document.getElementById('blocksFoundValue'),
difficultyValue: document.getElementById('difficultyValue'),
bestHashValue: document.getElementById('bestHashValue'),
hashrateChart: document.getElementById('hashrateChart').getContext('2d'),
totalHashesChart: document.getElementById('totalHashesChart').getContext('2d')
};
// Initialize charts
const charts = {
hashrate: new Chart(elements.hashrateChart, chartConfig.hashrate),
totalHashes: new Chart(elements.totalHashesChart, chartConfig.totalHashes)
};
// Helper functions
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function updateLastUpdateTime() {
const now = new Date();
elements.lastUpdate.textContent = now.toLocaleTimeString();
}
function updateStatusIndicator(status) {
elements.statusText.textContent = status;
elements.statusDot.classList.toggle('active', status === 'Running');
}
function updateCharts(hashrate, totalHashes) {
const timestamp = new Date().toLocaleTimeString();
// Update hashrate chart
if (chartConfig.hashrate.data.labels.length > 20) {
chartConfig.hashrate.data.labels.shift();
chartConfig.hashrate.data.datasets[0].data.shift();
}
chartConfig.hashrate.data.labels.push(timestamp);
chartConfig.hashrate.data.datasets[0].data.push(hashrate);
charts.hashrate.update();
// Update total hashes chart
if (chartConfig.totalHashes.data.labels.length > 20) {
chartConfig.totalHashes.data.labels.shift();
chartConfig.totalHashes.data.datasets[0].data.shift();
}
chartConfig.totalHashes.data.labels.push(timestamp);
chartConfig.totalHashes.data.datasets[0].data.push(totalHashes);
charts.totalHashes.update();
}
// API functions
async function fetchStatus() {
try {
debug('Fetching status...');
const response = await fetch(`${API_BASE_URL}/status`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
debug('Received status data:', data);
// Update UI
elements.hashrateValue.textContent = data.hashrate.toFixed(2);
elements.totalHashesValue.textContent = formatNumber(data.total_hashes);
elements.blocksFoundValue.textContent = data.blocks_found;
elements.difficultyValue.textContent = formatNumber(data.difficulty);
elements.bestHashValue.textContent = data.best_hash || 'None';
updateStatusIndicator(data.status);
updateLastUpdateTime();
updateCharts(data.hashrate, data.total_hashes);
debug('UI updated successfully');
} catch (error) {
console.error('Error fetching status:', error);
updateStatusIndicator('Error');
// Show error in UI
elements.hashrateValue.textContent = 'ERR';
elements.totalHashesValue.textContent = 'ERR';
elements.blocksFoundValue.textContent = 'ERR';
elements.difficultyValue.textContent = 'ERR';
elements.bestHashValue.textContent = 'Error fetching data';
}
}
async function startMining() {
try {
debug('Starting mining...');
elements.startButton.disabled = true;
const response = await fetch(`${API_BASE_URL}/start`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
debug('Mining started:', data);
alert(data.message);
} catch (error) {
console.error('Error starting mining:', error);
alert('Failed to start mining: ' + error.message);
} finally {
elements.startButton.disabled = false;
}
}
async function stopMining() {
try {
debug('Stopping mining...');
elements.stopButton.disabled = true;
const response = await fetch(`${API_BASE_URL}/stop`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
debug('Mining stopped:', data);
alert(data.message);
} catch (error) {
console.error('Error stopping mining:', error);
alert('Failed to stop mining: ' + error.message);
} finally {
elements.stopButton.disabled = false;
}
}
// Event listeners
elements.startButton.addEventListener('click', startMining);
elements.stopButton.addEventListener('click', stopMining);
// Start periodic updates
setInterval(fetchStatus, UPDATE_INTERVAL);
// Initial status fetch
fetchStatus(); |