bug fix: last sync time is the lastest file modified in data_cache, change app.py and index.html

This commit is contained in:
2026-02-06 22:30:16 +08:00
parent c0f158684f
commit 4e69acb3b5
18 changed files with 605 additions and 578 deletions
+39 -44
View File
@@ -273,46 +273,40 @@
console.log("Starting loadData...");
const loading = document.getElementById('loading');
const tbody = document.getElementById('tableBody');
const syncDisplay = document.getElementById('lastSyncTime'); // Get our new element
const syncDisplay = document.getElementById('lastSyncTime');
if (loading) loading.style.display = 'inline';
try {
const response = await fetch('/api/summary');
const data = await response.json();
const result = await response.json(); // result is { last_sync: "...", data: [...] }
const syncDisplay = document.getElementById('lastSyncTime');
if (syncDisplay) {
const now = new Date();
syncDisplay.innerText = now.toLocaleTimeString([], { hour12: false });
// Brief highlight effect
// 1. Update the Navbar Sync Time from Server
if (syncDisplay && result.last_sync) {
syncDisplay.innerText = result.last_sync;
syncDisplay.classList.add('text-success');
setTimeout(() => syncDisplay.classList.remove('text-success'), 2000);
}
// --- Update the Sync Time Display ---
const now = new Date();
const timeStr = now.toLocaleTimeString([], { hour12: false }); // e.g. 02:45:10
const dateStr = now.toLocaleDateString();
if (syncDisplay) {
syncDisplay.innerHTML = `${dateStr} ${timeStr}`;
}
// -----------------------------------------
// 2. Extract the instruments list
const data = result.data;
if (!data || data.length === 0) {
tbody.innerHTML = '<tr><td colspan="10" class="p-4 text-center">No data found. Please run Sync.</td></tr>';
return;
}
let htmlContent = '';
// 3. Prepare variables for the loop
let htmlContent = '';
const todayStr = new Date().toISOString().split('T')[0];
// 4. Loop through each item
data.forEach(item => {
// --- 1. Error Row Handling ---
if (item.error || !item.last_close) {
// --- A. Error Row Handling ---
if (item.error || !item.last_close || item.last_close === 'N/A') {
htmlContent += `
<tr>
<td>${item.name || 'Unknown'}</td>
<td><div class="fw-bold text-muted">${item.name || item.symbol}</div></td>
<td colspan="9" class="text-center p-3">
<span class="badge bg-warning text-dark">Needs Sync</span>
<small class="text-muted ms-2">Local CSV not found or corrupted.</small>
@@ -321,21 +315,21 @@
return; // Skip to next item
}
// --- 2. Date & Style Calculations ---
let displayDate = "N/A";
let dateColor = '#dc3545'; // Default red
// --- B. Date & Style Calculations ---
let displayDate = item.last_date || "N/A";
let dateColor = '#dc3545'; // Default red for stale
if (item.last_date && item.last_date.includes('-')) {
const parts = item.last_date.split('-');
displayDate = `${parts[2]}/${parts[1]}`;
displayDate = `${parts[2]}/${parts[1]}`; // DD/MM format
dateColor = (item.last_date === todayStr) ? '#28a745' : '#dc3545';
}
const current = parseFloat(item.last_close) || 0;
const low = parseFloat(item.low_52) || 0;
const high = parseFloat(item.high_52) || 0;
// 52W Range Progress Bar calculation
// 52W Range Progress Bar
let rangePct = high > low ? Math.min(Math.max(((current - low) / (high - low)) * 100, 0), 100) : 0;
const rangeColor = rangePct > 80 ? 'text-danger' : (rangePct < 20 ? 'text-success' : 'text-muted');
@@ -343,14 +337,12 @@
const isFresh = item.last_date === todayStr;
const dateBadgeClass = isFresh ? 'badge bg-success-subtle text-success border border-success-subtle' : 'text-muted';
// --- 3. Construct Row ---
// --- C. Construct Row ---
htmlContent += `
<tr>
<td><div class="fw-bold">${item.name || item.symbol}</div></td>
<td>
<div class="fw-bold">${item.name || item.symbol}</div>
</td>
<td>
<span class="${dateBadgeClass} table-date" style="color: ${dateColor} !important; font-size: 0.75rem; padding: 2px 5px; border-radius: 4px;">
<span class="${dateBadgeClass}" style="color: ${dateColor} !important; font-size: 0.75rem; padding: 2px 5px; border-radius: 4px;">
${displayDate}
</span>
</td>
@@ -366,19 +358,23 @@
<div class="progress-bar bg-primary" style="width: ${rangePct}%"></div>
</div>
</td>
<td>${formatEma(item.last_ema20)}</td>
<td>${formatEma(item.last_ema50)}</td>
<td>${formatEma(item.last_ema100)}</td>
<td>${formatEma(item.last_ema200)}</td>
<td>${formatKD(item.kd_values)}</td>
<td>${typeof formatEma === 'function' ? formatEma(item.last_ema20) : item.last_ema20}</td>
<td>${typeof formatEma === 'function' ? formatEma(item.last_ema50) : item.last_ema50}</td>
<td>${typeof formatEma === 'function' ? formatEma(item.last_ema100) : item.last_ema100}</td>
<td>${typeof formatEma === 'function' ? formatEma(item.last_ema200) : item.last_ema200}</td>
<td>${typeof formatKD === 'function' ? formatKD(item.kd_values) : 'N/A'}</td>
</tr>`;
}); // End forEach
});
// 4. Final Injection
// 5. Final Injection
tbody.innerHTML = htmlContent;
} catch (error) {
console.error("Fetch error:", error);
if (syncDisplay) {
syncDisplay.innerText = "OFFLINE";
syncDisplay.classList.add('text-danger');
}
if (tbody) {
tbody.innerHTML = '<tr><td colspan="10" class="text-danger p-4 text-center">Error loading summary. Check server logs.</td></tr>';
}
@@ -386,7 +382,6 @@
if (loading) loading.style.display = 'none';
}
}
// --- 4. Global Sync ---
async function runGlobalSync() {
const syncBtn = document.getElementById('syncBtn');