File size: 12,269 Bytes
5fa7a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { supabase } from './supabase.js'
import { getAllDecisions, setAllDecisions, clearAllDecisions } from './dataCache.js'
import { writeRawDecisions, clearAllStores, readAllRawDecisions } from './idb.js'
import { filterRowsToNyseTradingDays, countNonTradingDaysBetweenForAsset, countTradingDaysBetweenForAsset } from './marketCalendar.js'
import { computeBuyHoldEquity, computeStrategyEquity, calculateMetricsFromSeries, computeWinRate } from './perf.js'
import { STRATEGIES } from './strategies.js'

/**
 * 全局数据服务
 * 负责从 Supabase 加载数据、缓存管理和数据计算
 */
class DataService {
  constructor() {
    this.loading = false
    this.loaded = false
    this.agents = []
    this.tableRows = []
    this.lastUpdated = null
    this.dateBounds = { min: null, max: null }
    this.listeners = new Set()
    
    // Filter options cache
    this.useDbFilterCache = this._getDefaultUseDbFilterCache()
    this.nameOptions = []
    this.assetOptions = []
    this.modelOptions = []
    this.strategyOptions = []
  }

  _getDefaultUseDbFilterCache() {
    try {
      if (localStorage.getItem('disableDbFilterCache') === '1') return false
    } catch (_) {}
    const envFlag = ((import.meta.env.VITE_USE_DB_FILTER_CACHE || '') + '').toLowerCase()
    return envFlag === '1' || envFlag === 'true' || envFlag === 'yes' || envFlag === 'on'
  }

  /**
   * 订阅数据变化
   */
  subscribe(callback) {
    this.listeners.add(callback)
    return () => this.listeners.delete(callback)
  }

  /**
   * 通知所有订阅者
   */
  _notify() {
    this.listeners.forEach(callback => {
      try {
        callback({
          loading: this.loading,
          loaded: this.loaded,
          agents: this.agents,
          tableRows: this.tableRows,
          lastUpdated: this.lastUpdated,
          dateBounds: this.dateBounds,
          nameOptions: this.nameOptions,
          assetOptions: this.assetOptions,
          modelOptions: this.modelOptions,
          strategyOptions: this.strategyOptions
        })
      } catch (e) {
        console.error('[DataService] Error notifying listener:', e)
      }
    })
  }

  /**
   * 从数据库加载筛选选项
   */
  async loadFilterOptionsFromDb() {
    if (!this.useDbFilterCache) return null

    try {
      const { data, error } = await supabase
        .from('filter_options')
        .select('names, assets, models, strategies, updated_at')
        .eq('id', 'latest')
        .maybeSingle()

      if (error || !data) {
        const msg = (error && error.message) ? String(error.message).toLowerCase() : ''
        const code = (error && error.code) ? String(error.code) : ''
        const isNotFound = msg.includes('not found') || msg.includes('does not exist') || code === '42P01' || code === 'PGRST116'
        
        if (isNotFound) {
          this.useDbFilterCache = false
          try { localStorage.setItem('disableDbFilterCache', '1') } catch (_) {}
        }
        return null
      }

      return {
        names: data.names || [],
        assets: data.assets || [],
        models: data.models || [],
        strategies: data.strategies || []
      }
    } catch (e) {
      const msg = e && e.message ? String(e.message).toLowerCase() : ''
      if (msg.includes('not found') || msg.includes('does not exist')) {
        this.useDbFilterCache = false
        try { localStorage.setItem('disableDbFilterCache', '1') } catch (_) {}
      }
      return null
    }
  }

  /**
   * 保存筛选选项到数据库
   */
  async saveFilterOptionsToDb(names, assets, models, strategies) {
    if (!this.useDbFilterCache) return

    try {
      await supabase
        .from('filter_options')
        .upsert({ 
          id: 'latest', 
          names, 
          assets, 
          models, 
          strategies, 
          updated_at: new Date().toISOString() 
        }, { onConflict: 'id' })
    } catch (e) {
      const msg = e && e.message ? String(e.message).toLowerCase() : ''
      if (msg.includes('not found') || msg.includes('does not exist')) {
        this.useDbFilterCache = false
        try { localStorage.setItem('disableDbFilterCache', '1') } catch (_) {}
      }
    }
  }

  /**
   * 从 Supabase 拉取所有数据
   */
  async _fetchAllFromRemote() {
    const pageSize = 1000
    let from = 0
    const all = []

    while (true) {
      const to = from + pageSize - 1
      const { data, error } = await supabase
        .from('trading_decisions')
        .select('id, agent_name, asset, model, date, price, recommended_action, news_count, sentiment, created_at, updated_at')
        .order('updated_at', { ascending: false })
        .range(from, to)

      if (error) {
        console.error('[DataService] Error fetching data:', error)
        break
      }

      all.push(...(data || []))
      
      if (!data || data.length < pageSize) break
      from += pageSize
    }

    return all
  }

  /**
   * 计算单个分组的指标
   */
  async _computeAgentMetrics(row, allDecisions) {
    try {
      // 构建完整时间序列
      const seq = allDecisions
        .filter(r => r.agent_name === row.agent_name && r.asset === row.asset && r.model === row.model)
        .sort((a, b) => (a.date > b.date ? 1 : -1))

      const isCrypto = row.asset === 'BTC' || row.asset === 'ETH'
      const seqFiltered = isCrypto ? seq : (await filterRowsToNyseTradingDays(seq))

      // 计算日期范围
      const dates = seqFiltered.map(s => s.date).filter(Boolean).sort()
      const start_date = dates[0] || '-'
      const end_date = dates[dates.length - 1] || '-'

      let closed_days = 0
      if (dates.length > 1) {
        closed_days = await countNonTradingDaysBetweenForAsset(row.asset, start_date, end_date)
      }
      const trading_days = await countTradingDaysBetweenForAsset(row.asset, start_date, end_date)

      // 为每个策略计算指标
      const results = []
      for (const s of STRATEGIES) {
        const st = computeStrategyEquity(seqFiltered, 100000, s.fee, s.strategy, s.tradingMode)
        const stNoFee = computeStrategyEquity(seqFiltered, 100000, 0, s.strategy, s.tradingMode)
        const metrics = calculateMetricsFromSeries(st, isCrypto ? 'crypto' : 'stock')
        const metricsNoFee = calculateMetricsFromSeries(stNoFee, isCrypto ? 'crypto' : 'stock')
        const { winRate, trades } = computeWinRate(seqFiltered, s.strategy, s.tradingMode)
        const bhSeries = computeBuyHoldEquity(seqFiltered, 100000)
        const buy_hold_return = bhSeries.length ? (bhSeries[bhSeries.length - 1] - bhSeries[0]) / bhSeries[0] : 0

        results.push({
          decision_ids: seqFiltered.map(r => r.id).filter(Boolean),
          agent_name: row.agent_name,
          asset: row.asset,
          model: row.model,
          strategy: s.id,
          strategy_label: s.label,
          balance: st.length ? st[st.length - 1] : 100000,
          return_with_fees: metrics.total_return / 100,
          return_no_fees: metricsNoFee.total_return / 100,
          buy_hold_return,
          sharpe_ratio: metrics.sharpe_ratio,
          win_rate: winRate,
          trades,
          start_date,
          end_date,
          closed_days,
          trading_days,
          fee: s.fee,
          tradingMode: s.tradingMode,
          series: seqFiltered.map(r => ({ 
            id: r.id, 
            date: r.date, 
            price: r.price, 
            recommended_action: r.recommended_action 
          })),
          key: `${row.agent_name}|${row.asset}|${row.model}|${s.id}`
        })
      }

      return results
    } catch (e) {
      console.error('[DataService] Error computing metrics:', e)
      return []
    }
  }

  /**
   * 加载数据(主方法)
   */
  async load(forceRefresh = false) {
    if (this.loading) {
      console.log('[DataService] Already loading, skipping...')
      return
    }

    this.loading = true
    this._notify()

    try {
      // 1. 尝试从数据库加载筛选选项
      await this.loadFilterOptionsFromDb()

      // 2. 获取所有决策数据
      let all = null

      if (forceRefresh) {
        // 强制刷新:清除所有缓存,从远程拉取
        await clearAllStores()
        clearAllDecisions()
        all = await this._fetchAllFromRemote()
        setAllDecisions(all)
        await writeRawDecisions(all)
      } else {
        // 正常加载:先尝试内存缓存
        all = getAllDecisions()

        if (!all) {
          // 再尝试 IndexedDB 缓存
          const cached = await readAllRawDecisions()
          if (cached && cached.length) {
            all = cached
            setAllDecisions(all)
          }
        }

        if (!all) {
          // 最后从远程拉取
          all = await this._fetchAllFromRemote()
          setAllDecisions(all)
          await writeRawDecisions(all)
        }
      }

      // 3. 计算全局日期范围
      const allDates = all.map(r => r && r.date).filter(Boolean).sort()
      this.dateBounds = {
        min: allDates.length ? new Date(allDates[0]) : null,
        max: allDates.length ? new Date(allDates[allDates.length - 1]) : null
      }

      // 4. 按 agent_name|asset|model 分组去重
      const keyToRow = new Map()
      for (const row of all) {
        const key = `${row.agent_name}|${row.asset}|${row.model}`
        if (!keyToRow.has(key)) {
          keyToRow.set(key, row)
        }
      }

      // 5. 计算每个分组的指标
      const agents = []
      for (const row of keyToRow.values()) {
        const metrics = await this._computeAgentMetrics(row, all)
        agents.push(...metrics)
      }

      this.agents = agents

      // 6. 转换为表格行
      this.tableRows = agents.map(a => ({
        agent_name: a.agent_name,
        asset: a.asset,
        model: a.model,
        strategy: a.strategy,
        strategy_label: a.strategy_label,
        balance: a.balance,
        ret_with_fees: a.return_with_fees,
        ret_no_fees: a.return_no_fees,
        buy_hold: a.buy_hold_return,
        vs_bh_with_fees: a.return_with_fees - a.buy_hold_return,
        sharpe: a.sharpe_ratio,
        trading_days: a.trading_days,
        win_rate: a.win_rate,
        trades: a.trades,
        start_date: a.start_date,
        end_date: a.end_date,
        closed_date: a.closed_days,
        decision_ids: a.decision_ids,
        fee: a.fee,
        tradingMode: a.tradingMode,
        series: a.series,
        key: a.key
      }))

      // 7. 生成筛选选项
      this.nameOptions = Array.from(new Set(agents.map(a => a.agent_name)))
        .map(v => ({ label: v, value: v }))
      this.assetOptions = Array.from(new Set(agents.map(a => a.asset)))
        .map(v => ({ label: v, value: v }))
      this.modelOptions = Array.from(new Set(agents.map(a => a.model)))
        .map(v => ({ label: v, value: v }))
      this.strategyOptions = STRATEGIES.map(s => ({ label: s.label, value: s.id }))

      // 8. 保存筛选选项到数据库
      await this.saveFilterOptionsToDb(
        this.nameOptions.map(o => o.value),
        this.assetOptions.map(o => o.value),
        this.modelOptions.map(o => o.value),
        this.strategyOptions.map(o => o.value)
      )

      this.lastUpdated = Date.now()
      this.loaded = true

      console.log('[DataService] Data loaded successfully:', {
        agents: this.agents.length,
        tableRows: this.tableRows.length,
        names: this.nameOptions.length,
        assets: this.assetOptions.length
      })
    } catch (e) {
      console.error('[DataService] Error loading data:', e)
    } finally {
      this.loading = false
      this._notify()
    }
  }

  /**
   * 强制刷新数据
   */
  async forceRefresh() {
    return this.load(true)
  }

  /**
   * 获取当前状态
   */
  getState() {
    return {
      loading: this.loading,
      loaded: this.loaded,
      agents: this.agents,
      tableRows: this.tableRows,
      lastUpdated: this.lastUpdated,
      dateBounds: this.dateBounds,
      nameOptions: this.nameOptions,
      assetOptions: this.assetOptions,
      modelOptions: this.modelOptions,
      strategyOptions: this.strategyOptions
    }
  }
}

// 导出单例
export const dataService = new DataService()