Spaces:
Running
Running
Fix cache staleness: auto-refresh when DB has newer data than cache
Browse files- src/lib/dataService.js +53 -3
src/lib/dataService.js
CHANGED
|
@@ -215,7 +215,8 @@ class DataService {
|
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
-
// 计算日期范围 -
|
|
|
|
| 219 |
const dates = seqFiltered
|
| 220 |
.map(s => {
|
| 221 |
const dateStr = s.dateNormalized || (typeof s.date === 'string' ? s.date.split('T')[0] : s.date)
|
|
@@ -224,9 +225,21 @@ class DataService {
|
|
| 224 |
.filter(Boolean)
|
| 225 |
.sort()
|
| 226 |
const start_date = dates[0] || '-'
|
| 227 |
-
const end_date = dates[dates.length - 1] || '-'
|
| 228 |
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
let closed_days = 0
|
| 232 |
if (dates.length > 1) {
|
|
@@ -321,6 +334,43 @@ class DataService {
|
|
| 321 |
}
|
| 322 |
}
|
| 323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
if (!all) {
|
| 325 |
// 最后从远程拉取
|
| 326 |
all = await this._fetchAllFromRemote()
|
|
|
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
+
// 计算日期范围 - 直接使用 seqFiltered 的最后一条记录的日期,而不是从日期数组中取
|
| 219 |
+
// 这样可以确保使用的是实际数据的最后一天,而不是排序后的日期数组
|
| 220 |
const dates = seqFiltered
|
| 221 |
.map(s => {
|
| 222 |
const dateStr = s.dateNormalized || (typeof s.date === 'string' ? s.date.split('T')[0] : s.date)
|
|
|
|
| 225 |
.filter(Boolean)
|
| 226 |
.sort()
|
| 227 |
const start_date = dates[0] || '-'
|
|
|
|
| 228 |
|
| 229 |
+
// 使用 seqFiltered 的最后一条记录的日期作为 end_date,确保是实际数据的最后一天
|
| 230 |
+
let end_date = '-'
|
| 231 |
+
if (seqFiltered.length > 0) {
|
| 232 |
+
const lastRow = seqFiltered[seqFiltered.length - 1]
|
| 233 |
+
end_date = lastRow.dateNormalized || (typeof lastRow.date === 'string' ? lastRow.date.split('T')[0] : lastRow.date) || '-'
|
| 234 |
+
} else if (dates.length > 0) {
|
| 235 |
+
end_date = dates[dates.length - 1]
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
console.log(`[DataService] STEP3 - ${row.agent_name}|${row.asset}|${row.model}: Final end_date = ${end_date}, dates array length = ${dates.length}, seqFiltered length = ${seqFiltered.length}`)
|
| 239 |
+
if (seqFiltered.length > 0) {
|
| 240 |
+
const lastRowDate = seqFiltered[seqFiltered.length - 1].dateNormalized || (typeof seqFiltered[seqFiltered.length - 1].date === 'string' ? seqFiltered[seqFiltered.length - 1].date.split('T')[0] : seqFiltered[seqFiltered.length - 1].date)
|
| 241 |
+
console.log(`[DataService] STEP3 - Last row date: ${lastRowDate}, dates array last: ${dates.length > 0 ? dates[dates.length - 1] : 'N/A'}`)
|
| 242 |
+
}
|
| 243 |
|
| 244 |
let closed_days = 0
|
| 245 |
if (dates.length > 1) {
|
|
|
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
| 337 |
+
// 检查缓存数据是否是最新的:从 Supabase 查询最新日期,如果缓存中没有最新日期,则强制刷新
|
| 338 |
+
if (all && all.length > 0) {
|
| 339 |
+
try {
|
| 340 |
+
// 获取缓存中的最新日期
|
| 341 |
+
const cachedDates = all.map(r => r && r.date).filter(Boolean).sort()
|
| 342 |
+
const cachedMaxDate = cachedDates.length > 0 ? cachedDates[cachedDates.length - 1] : null
|
| 343 |
+
|
| 344 |
+
// 从 Supabase 查询最新日期(只查询日期,不查询所有数据)
|
| 345 |
+
const { data: latestData, error } = await supabase
|
| 346 |
+
.from('trading_decisions')
|
| 347 |
+
.select('date')
|
| 348 |
+
.order('date', { ascending: false })
|
| 349 |
+
.limit(1)
|
| 350 |
+
|
| 351 |
+
if (!error && latestData && latestData.length > 0) {
|
| 352 |
+
const dbMaxDate = latestData[0].date
|
| 353 |
+
const cachedMaxDateStr = cachedMaxDate ? (typeof cachedMaxDate === 'string' ? cachedMaxDate.split('T')[0] : cachedMaxDate) : null
|
| 354 |
+
const dbMaxDateStr = typeof dbMaxDate === 'string' ? dbMaxDate.split('T')[0] : dbMaxDate
|
| 355 |
+
|
| 356 |
+
console.log(`[DataService] Cache check: cached max date = ${cachedMaxDateStr}, DB max date = ${dbMaxDateStr}`)
|
| 357 |
+
|
| 358 |
+
// 如果数据库中的最新日期比缓存中的新,强制刷新
|
| 359 |
+
if (cachedMaxDateStr && dbMaxDateStr && dbMaxDateStr > cachedMaxDateStr) {
|
| 360 |
+
console.log(`[DataService] Cache is stale (${cachedMaxDateStr} < ${dbMaxDateStr}), fetching fresh data...`)
|
| 361 |
+
await clearAllStores()
|
| 362 |
+
clearAllDecisions()
|
| 363 |
+
all = await this._fetchAllFromRemote()
|
| 364 |
+
setAllDecisions(all)
|
| 365 |
+
await writeRawDecisions(all)
|
| 366 |
+
}
|
| 367 |
+
}
|
| 368 |
+
} catch (e) {
|
| 369 |
+
console.error('[DataService] Error checking cache freshness:', e)
|
| 370 |
+
// 如果检查失败,继续使用缓存
|
| 371 |
+
}
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
if (!all) {
|
| 375 |
// 最后从远程拉取
|
| 376 |
all = await this._fetchAllFromRemote()
|