package db import "time" // SurgeEventRecord represents a persisted surge detection event. type SurgeEventRecord struct { ID int64 `json:"id"` Coin string `json:"coin"` Timestamp string `json:"timestamp"` BnPrice float64 `json:"bn_price"` OkxPrice float64 `json:"okx_price"` BgPrice float64 `json:"bg_price"` SpreadPct float64 `json:"spread_pct"` BaselinePct float64 `json:"baseline_pct"` ThresholdPct float64 `json:"threshold_pct"` Ratio float64 `json:"ratio"` Direction string `json:"direction"` LeadingExchange string `json:"leading_exchange"` MidPrice float64 `json:"mid_price"` CreatedAt string `json:"created_at"` } // InsertSurgeEvent saves a surge event to the database. func (d *DB) InsertSurgeEvent(coin string, ts time.Time, bnPrice, okxPrice, bgPrice, spreadPct, baselinePct, thresholdPct, ratio float64, direction, leadingExchange string, midPrice float64) error { _, err := d.Exec(` INSERT INTO surge_events (coin, timestamp, bn_price, okx_price, bg_price, spread_pct, baseline_pct, threshold_pct, ratio, direction, leading_exchange, mid_price, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, coin, ts.Format(time.RFC3339), bnPrice, okxPrice, bgPrice, spreadPct, baselinePct, thresholdPct, ratio, direction, leadingExchange, midPrice, Now().Format(time.RFC3339)) return err } // GetSurgeEvents returns surge events ordered by creation time descending. func (d *DB) GetSurgeEvents(limit int) ([]SurgeEventRecord, error) { if limit <= 0 { limit = 100 } rows, err := d.Query(` SELECT id, coin, timestamp, bn_price, okx_price, bg_price, spread_pct, baseline_pct, threshold_pct, ratio, direction, leading_exchange, mid_price, created_at FROM surge_events ORDER BY created_at DESC LIMIT ?`, limit) if err != nil { return nil, err } defer rows.Close() var result []SurgeEventRecord for rows.Next() { var r SurgeEventRecord if err := rows.Scan(&r.ID, &r.Coin, &r.Timestamp, &r.BnPrice, &r.OkxPrice, &r.BgPrice, &r.SpreadPct, &r.BaselinePct, &r.ThresholdPct, &r.Ratio, &r.Direction, &r.LeadingExchange, &r.MidPrice, &r.CreatedAt); err != nil { return nil, err } result = append(result, r) } return result, rows.Err() }