Browse Source

尝试修复区域选择问题

skyfffire 2 days ago
parent
commit
e1d835a658
3 changed files with 85 additions and 28 deletions
  1. 44 11
      check_db.py
  2. 8 8
      src/leadlag/templates/dashboard.html
  3. 33 9
      src/leadlag/web_dashboard.py

+ 44 - 11
check_db.py

@@ -1,19 +1,52 @@
 import sqlite3
 from datetime import datetime
+import os
 
-conn = sqlite3.connect('data/trading_data_20251103.db')
+# 找到最新的数据库文件
+data_dir = 'data'
+db_files = [f for f in os.listdir(data_dir) if f.startswith('trading_data_') and f.endswith('.db')]
+db_files.sort(reverse=True)
+
+if not db_files:
+    print("没有找到数据库文件")
+    exit(1)
+
+db_path = os.path.join(data_dir, db_files[0])
+print(f"使用数据库: {db_path}")
+
+conn = sqlite3.connect(db_path)
 cursor = conn.cursor()
 
-print('=== Price Data (最新10条) ===')
-cursor.execute('SELECT * FROM price_data ORDER BY timestamp DESC LIMIT 10')
-rows = cursor.fetchall()
-for row in rows:
-    ts = datetime.fromtimestamp(row[1]).strftime('%Y-%m-%d %H:%M:%S')
-    print(f"所有列: {row}")
-    print(f"  时间: {ts}")
-    print(f"  ID: {row[0]}, 时间戳: {row[1]}, 会话ID: {row[2]}, 标的: {row[3]}")
-    print(f"  Lighter价格: {row[4]}, Binance价格: {row[5]}, 价差(bps): {row[6]}")
-    print()
+# 先查看所有表
+print('\n=== 数据库中的表 ===')
+cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
+tables = cursor.fetchall()
+for table in tables:
+    print(f"  - {table[0]}")
+
+# 查看price_data表的结构
+print('\n=== price_data 表结构 ===')
+try:
+    cursor.execute("PRAGMA table_info(price_data)")
+    columns = cursor.fetchall()
+    for col in columns:
+        print(f"  {col[1]}: {col[2]}")
+except Exception as e:
+    print(f"  错误: {e}")
+
+print('\n=== Price Data (最新10条) ===')
+try:
+    cursor.execute('SELECT * FROM price_data ORDER BY timestamp DESC LIMIT 10')
+    rows = cursor.fetchall()
+    for row in rows:
+        ts = datetime.fromtimestamp(row[1]).strftime('%Y-%m-%d %H:%M:%S')
+        print(f"所有列: {row}")
+        print(f"  时间: {ts}")
+        print(f"  ID: {row[0]}, 时间戳: {row[1]}, 会话ID: {row[2]}, 标的: {row[3]}")
+        print(f"  Lighter价格: {row[4]}, Binance价格: {row[5]}, 价差(bps): {row[6]}")
+        print()
+except Exception as e:
+    print(f"  错误: {e}")
 
 print('\n=== Trading Events (最新10条) ===')
 cursor.execute('SELECT * FROM trading_events ORDER BY timestamp DESC LIMIT 10')

+ 8 - 8
src/leadlag/templates/dashboard.html

@@ -968,29 +968,29 @@
         
         // 显示缩略图选择区域
         function showThumbnailSelection(canvasId, dataIndex) {
-            const wrapper = document.getElementById(canvasId).parentElement;
-            
+            const thumbnailWrapper = document.getElementById(canvasId);
+
             // 移除之前的选择区域
-            const existingSelection = wrapper.querySelector('.thumbnail-selection');
+            const existingSelection = thumbnailWrapper.querySelector('.thumbnail-selection');
             if (existingSelection) {
                 existingSelection.remove();
             }
-            
+
             // 创建新的选择区域
             const selection = document.createElement('div');
             selection.className = 'thumbnail-selection';
-            
+
             // 计算选择区域的位置和宽度
             const totalDataPoints = thumbnailData.length;
             const selectionWidth = (20 / totalDataPoints) * 100; // 20分钟范围对应的百分比宽度
             const centerPosition = (dataIndex / totalDataPoints) * 100;
             const leftPosition = Math.max(0, centerPosition - selectionWidth / 2);
             const actualWidth = Math.min(selectionWidth, 100 - leftPosition);
-            
+
             selection.style.left = leftPosition + '%';
             selection.style.width = actualWidth + '%';
-            
-            wrapper.appendChild(selection);
+
+            thumbnailWrapper.appendChild(selection);
         }
         
         // 根据选中的时间范围更新主图表

+ 33 - 9
src/leadlag/web_dashboard.py

@@ -171,11 +171,20 @@ class TradingDashboard:
                     found_db = True
                     break
             if not found_db:
-                if db_path is None:
+                # 如果找不到对应日期的数据库,使用最新的可用数据库
+                available_dbs = self.get_available_databases()
+                if available_dbs:
+                    db_path = available_dbs[0]['path']
+                elif db_path is None:
                     db_path = self.db_path
         elif db_path is None:
-            db_path = self.db_path
-            
+            # 如果没有指定db_path,使用最新的可用数据库
+            available_dbs = self.get_available_databases()
+            if available_dbs:
+                db_path = available_dbs[0]['path']
+            else:
+                db_path = self.db_path
+
         if not os.path.exists(db_path):
             return []
             
@@ -230,8 +239,13 @@ class TradingDashboard:
     def get_thumbnail_data(self, hours: float = 24, symbol: str = '', db_path: str = None) -> List[Dict[str, Any]]:
         """获取缩略图数据 - 只返回币安价格,每秒最多一条数据"""
         if db_path is None:
-            db_path = self.db_path
-            
+            # 使用最新的可用数据库
+            available_dbs = self.get_available_databases()
+            if available_dbs:
+                db_path = available_dbs[0]['path']
+            else:
+                db_path = self.db_path
+
         if not os.path.exists(db_path):
             return []
             
@@ -278,8 +292,13 @@ class TradingDashboard:
     def get_trading_events(self, hours: float = 24, symbol: str = '', db_path: str = None) -> List[Dict[str, Any]]:
         """获取交易事件"""
         if db_path is None:
-            db_path = self.db_path
-            
+            # 使用最新的可用数据库
+            available_dbs = self.get_available_databases()
+            if available_dbs:
+                db_path = available_dbs[0]['path']
+            else:
+                db_path = self.db_path
+
         if not os.path.exists(db_path):
             return []
             
@@ -341,8 +360,13 @@ class TradingDashboard:
     def get_statistics(self, hours: float = 24, symbol: str = '', db_path: str = None) -> Dict[str, Any]:
         """获取统计数据"""
         if db_path is None:
-            db_path = self.db_path
-            
+            # 使用最新的可用数据库
+            available_dbs = self.get_available_databases()
+            if available_dbs:
+                db_path = available_dbs[0]['path']
+            else:
+                db_path = self.db_path
+
         if not os.path.exists(db_path):
             return {}