lfqian commited on
Commit
9addaf2
·
1 Parent(s): 46cdc7a

Improve data fetching: track max date and auto-fetch missing pages

Browse files
Files changed (1) hide show
  1. src/lib/dataService.js +80 -4
src/lib/dataService.js CHANGED
@@ -138,12 +138,33 @@ class DataService {
138
  * 从 Supabase 拉取所有数据
139
  */
140
  async _fetchAllFromRemote() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  const pageSize = 1000
142
  let from = 0
143
  const all = []
 
 
144
 
145
  while (true) {
146
  const to = from + pageSize - 1
 
 
 
147
  const { data, error } = await supabase
148
  .from('trading_decisions')
149
  .select('id, agent_name, asset, model, date, price, recommended_action, news_count, sentiment, created_at, updated_at')
@@ -155,16 +176,71 @@ class DataService {
155
  break
156
  }
157
 
158
- all.push(...(data || []))
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
- if (!data || data.length < pageSize) break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  from += pageSize
162
  }
163
 
164
- // Log the date range to help debug
165
  if (all.length > 0) {
166
  const dates = all.map(r => r && r.date).filter(Boolean).sort()
167
- console.log('[DataService] Fetched data date range:', dates[0], 'to', dates[dates.length - 1], 'total:', all.length)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  }
169
 
170
  return all
 
138
  * 从 Supabase 拉取所有数据
139
  */
140
  async _fetchAllFromRemote() {
141
+ // First, get the max date from DB to know what we're aiming for
142
+ let dbMaxDate = null
143
+ try {
144
+ const { data: maxDateData, error: maxDateError } = await supabase
145
+ .from('trading_decisions')
146
+ .select('date')
147
+ .order('date', { ascending: false })
148
+ .limit(1)
149
+ if (!maxDateError && maxDateData && maxDateData.length > 0) {
150
+ dbMaxDate = maxDateData[0].date
151
+ console.log(`[DataService] Target max date from DB: ${dbMaxDate}`)
152
+ }
153
+ } catch (e) {
154
+ console.error('[DataService] Error getting DB max date:', e)
155
+ }
156
+
157
  const pageSize = 1000
158
  let from = 0
159
  const all = []
160
+ let pageCount = 0
161
+ let lastFetchedDate = null
162
 
163
  while (true) {
164
  const to = from + pageSize - 1
165
+ pageCount++
166
+ console.log(`[DataService] Fetching page ${pageCount}: range(${from}, ${to})`)
167
+
168
  const { data, error } = await supabase
169
  .from('trading_decisions')
170
  .select('id, agent_name, asset, model, date, price, recommended_action, news_count, sentiment, created_at, updated_at')
 
176
  break
177
  }
178
 
179
+ const pageData = data || []
180
+ if (pageData.length === 0) {
181
+ console.log(`[DataService] Page ${pageCount}: 0 rows, stopping`)
182
+ break
183
+ }
184
+
185
+ all.push(...pageData)
186
+
187
+ // Track the last date in this page
188
+ const pageDates = pageData.map(r => r && r.date).filter(Boolean).sort()
189
+ if (pageDates.length > 0) {
190
+ lastFetchedDate = pageDates[pageDates.length - 1]
191
+ console.log(`[DataService] Page ${pageCount}: ${pageData.length} rows, date range: ${pageDates[0]} to ${lastFetchedDate}`)
192
+ }
193
 
194
+ // If we got less than pageSize, we're done
195
+ if (pageData.length < pageSize) {
196
+ console.log(`[DataService] Last page reached (got ${pageData.length} rows, expected ${pageSize})`)
197
+ break
198
+ }
199
+
200
+ // If we've reached or passed the max date, we're done
201
+ if (dbMaxDate && lastFetchedDate) {
202
+ const lastFetchedDateStr = typeof lastFetchedDate === 'string' ? lastFetchedDate.split('T')[0] : lastFetchedDate
203
+ const dbMaxDateStr = typeof dbMaxDate === 'string' ? dbMaxDate.split('T')[0] : dbMaxDate
204
+ if (lastFetchedDateStr >= dbMaxDateStr) {
205
+ console.log(`[DataService] Reached target max date ${dbMaxDateStr}, stopping`)
206
+ break
207
+ }
208
+ }
209
+
210
  from += pageSize
211
  }
212
 
213
+ // Log the final date range
214
  if (all.length > 0) {
215
  const dates = all.map(r => r && r.date).filter(Boolean).sort()
216
+ console.log(`[DataService] Total fetched: ${all.length} rows across ${pageCount} pages, date range: ${dates[0]} to ${dates[dates.length - 1]}`)
217
+ console.log(`[DataService] Last 10 dates:`, dates.slice(-10))
218
+
219
+ // Verify we got the max date
220
+ const fetchedMaxDate = dates[dates.length - 1]
221
+ if (dbMaxDate) {
222
+ const fetchedMaxDateStr = typeof fetchedMaxDate === 'string' ? fetchedMaxDate.split('T')[0] : fetchedMaxDate
223
+ const dbMaxDateStr = typeof dbMaxDate === 'string' ? dbMaxDate.split('T')[0] : dbMaxDate
224
+ console.log(`[DataService] Verification: DB max date = ${dbMaxDateStr}, Fetched max date = ${fetchedMaxDateStr}`)
225
+ if (fetchedMaxDateStr !== dbMaxDateStr) {
226
+ console.error(`[DataService] ERROR: Still missing data! DB has ${dbMaxDateStr} but we only fetched up to ${fetchedMaxDateStr}`)
227
+ // Try one more page to see if we can get the missing data
228
+ console.log(`[DataService] Attempting to fetch one more page starting from ${from}...`)
229
+ const { data: extraData, error: extraError } = await supabase
230
+ .from('trading_decisions')
231
+ .select('id, agent_name, asset, model, date, price, recommended_action, news_count, sentiment, created_at, updated_at')
232
+ .order('date', { ascending: true })
233
+ .range(from, from + pageSize - 1)
234
+ if (!extraError && extraData && extraData.length > 0) {
235
+ console.log(`[DataService] Extra page fetched: ${extraData.length} rows`)
236
+ all.push(...extraData)
237
+ const newDates = all.map(r => r && r.date).filter(Boolean).sort()
238
+ console.log(`[DataService] After extra page: date range: ${newDates[0]} to ${newDates[newDates.length - 1]}`)
239
+ }
240
+ }
241
+ }
242
+ } else {
243
+ console.warn('[DataService] No data fetched!')
244
  }
245
 
246
  return all