File size: 7,824 Bytes
b190b45 |
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 |
/**
* Diagnostics Page
*/
import { apiClient } from '../../shared/js/core/api-client.js';
class DiagnosticsPage {
constructor() {
this.isRunning = false;
this.requestLog = [];
}
async init() {
console.log('[Diagnostics] Initializing...');
this.bindEvents();
await this.loadHealthData();
await this.loadLogs();
this.startRequestTracking();
}
bindEvents() {
document.getElementById('health-refresh')?.addEventListener('click', () => {
this.loadHealthData();
});
document.getElementById('logs-refresh')?.addEventListener('click', () => {
this.loadLogs();
});
document.getElementById('logs-clear')?.addEventListener('click', () => {
this.clearLogs();
});
document.getElementById('refresh-btn')?.addEventListener('click', () => {
this.refreshAll();
});
document.getElementById('log-type')?.addEventListener('change', () => {
this.loadLogs();
});
}
/** Load system health data */
async loadHealthData() {
const container = document.getElementById('health-grid');
if (!container) return;
container.innerHTML = '<div class="loading-container"><div class="spinner"></div></div>';
try {
const response = await apiClient.fetch('/api/health');
const data = await response.json();
const services = [
{ name: 'Backend Server', status: data.status === 'healthy' ? 'online' : 'offline', key: 'backend' },
{ name: 'CoinMarketCap', status: data.sources?.coinmarketcap || 'unknown', key: 'coinmarketcap' },
{ name: 'NewsAPI', status: data.sources?.newsapi || 'unknown', key: 'newsapi' },
{ name: 'Etherscan', status: data.sources?.etherscan || 'unknown', key: 'etherscan' },
{ name: 'BSCScan', status: data.sources?.bscscan || 'unknown', key: 'bscscan' },
{ name: 'TronScan', status: data.sources?.tronscan || 'unknown', key: 'tronscan' }
];
container.innerHTML = services.map(service => `
<div class="health-card ${service.status}">
<div class="health-icon ${service.status}">
${this.getStatusIcon(service.status)}
</div>
<div class="health-info">
<h4>${service.name}</h4>
<span class="status-badge ${service.status}">${service.status}</span>
</div>
</div>
`).join('');
this.updateLastUpdate();
} catch (error) {
console.error('Failed to load health data:', error);
container.innerHTML = `
<div class="error-message">
<p>Failed to load health data: ${error.message}</p>
</div>
`;
}
}
/** Load system logs */
async loadLogs() {
const container = document.getElementById('logs-container');
if (!container) return;
const logType = document.getElementById('log-type')?.value || 'recent';
const endpoint = logType === 'errors' ? '/api/logs/errors' : '/api/logs/recent';
container.innerHTML = '<div class="loading-container"><div class="spinner"></div></div>';
try {
const response = await apiClient.fetch(endpoint);
const data = await response.json();
const logs = data.logs || data.errors || [];
if (logs.length === 0) {
container.innerHTML = '<p class="text-center text-muted">No logs found</p>';
return;
}
container.innerHTML = `
<div class="logs-list">
${logs.map(log => `
<div class="log-entry ${log.level?.toLowerCase() || 'info'}">
<span class="log-time">${log.timestamp ? new Date(log.timestamp).toLocaleTimeString() : 'N/A'}</span>
<span class="log-level ${log.level?.toLowerCase() || 'info'}">${log.level || 'INFO'}</span>
<span class="log-message">${log.message || log.msg || log.text || ''}</span>
</div>
`).join('')}
</div>
`;
} catch (error) {
console.error('Failed to load logs:', error);
container.innerHTML = `
<div class="error-message">
<p>Failed to load logs: ${error.message}</p>
</div>
`;
}
}
/** Clear logs */
async clearLogs() {
const container = document.getElementById('logs-container');
if (!container) return;
container.innerHTML = '<p class="text-center text-muted">Logs cleared</p>';
}
/** Track API requests */
startRequestTracking() {
// Intercept apiClient requests
const originalFetch = apiClient.fetch.bind(apiClient);
apiClient.fetch = async (...args) => {
const startTime = Date.now();
const url = args[0];
try {
const response = await originalFetch(...args);
const duration = Date.now() - startTime;
this.logRequest({
time: new Date(),
method: 'GET',
endpoint: url,
status: response.status,
duration
});
return response;
} catch (error) {
const duration = Date.now() - startTime;
this.logRequest({
time: new Date(),
method: 'GET',
endpoint: url,
status: 'ERROR',
duration
});
throw error;
}
};
}
/** Log a request */
logRequest(request) {
this.requestLog.unshift(request);
if (this.requestLog.length > 50) {
this.requestLog = this.requestLog.slice(0, 50);
}
this.updateRequestsTable();
}
/** Update requests table */
updateRequestsTable() {
const tbody = document.getElementById('requests-tbody');
if (!tbody) return;
if (this.requestLog.length === 0) {
tbody.innerHTML = '<tr><td colspan="5" class="text-center">No requests logged yet</td></tr>';
return;
}
tbody.innerHTML = this.requestLog.map(req => `
<tr>
<td>${req.time.toLocaleTimeString()}</td>
<td><span class="method-badge">${req.method}</span></td>
<td>${req.endpoint}</td>
<td><span class="status-badge status-${Math.floor(req.status / 100)}xx">${req.status}</span></td>
<td>${req.duration}ms</td>
</tr>
`).join('');
}
/** Refresh all sections */
async refreshAll() {
await Promise.all([
this.loadHealthData(),
this.loadLogs()
]);
}
/** Update last update timestamp */
updateLastUpdate() {
const elem = document.getElementById('last-update');
if (elem) {
elem.textContent = new Date().toLocaleTimeString();
}
}
/** Get status icon SVG */
getStatusIcon(status) {
const normalized = status?.toLowerCase();
if (normalized === 'online' || normalized === 'healthy' || normalized === 'operational') {
return '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"></polyline></svg>';
} else if (normalized === 'degraded' || normalized === 'warning') {
return '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path><line x1="12" y1="9" x2="12" y2="13"></line><line x1="12" y1="17" x2="12.01" y2="17"></line></svg>';
} else {
return '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="15" y1="9" x2="9" y2="15"></line><line x1="9" y1="9" x2="15" y2="15"></line></svg>';
}
}
}
const diagnosticsPage = new DiagnosticsPage();
diagnosticsPage.init();
|