Pārlūkot izejas kodu

fix(API): 修复时间戳解析问题并更新API端点路径

修复时间字符串解析逻辑,支持多种ISO格式时间输入并自动添加时区信息。同时更新前端API请求路径,移除多余的'/api'前缀。
skyfffire 1 nedēļu atpakaļ
vecāks
revīzija
119a9c2430
2 mainītis faili ar 29 papildinājumiem un 3 dzēšanām
  1. 28 2
      src/dashboard/app.py
  2. 1 1
      src/dashboard/static/index.html

+ 28 - 2
src/dashboard/app.py

@@ -340,10 +340,36 @@ def get_range_data(symbol):
     """获取指定时间点前后各1800条数据(约1小时)"""
     try:
         # 获取中心时间戳参数
-        center_time = request.args.get('timestamp')  # ISO格式时间字符串
-        if not center_time:
+        center_time_str = request.args.get('timestamp')  # ISO格式时间字符串
+        if not center_time_str:
             return jsonify({"error": "缺少timestamp参数"}), 400
         
+        # 解析ISO格式时间字符串
+        try:
+            center_time_str = center_time_str.strip()
+            # 检查并替换时间与时区之间的空格
+            if 'T' in center_time_str and ' ' in center_time_str:
+                parts = center_time_str.split(' ')
+                # 确认格式为 '...T... HH:MM'
+                if len(parts) == 2 and ':' in parts[1] and parts[1].replace(':', '').isdigit():
+                    center_time_str = parts[0] + '+' + parts[1]
+
+            # 如果没有时区信息,默认添加东八区
+            if '+' not in center_time_str and '-' not in center_time_str and 'Z' not in center_time_str:
+                center_time_str += '+08:00'
+
+            # fromisoformat 可以处理带':'和不带':'的时区, e.g. +08:00 and +0800
+            center_dt = datetime.fromisoformat(center_time_str)
+
+            # 转换为UTC时间
+            center_dt = center_dt.astimezone(pytz.utc)
+
+            # 格式化为QuestDB接受的格式
+            center_time = center_dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
+        except Exception as e:
+            logger.error(f"时间格式解析失败: {str(e)}")
+            return jsonify({"error": f"无效的时间格式: {str(e)}"}), 400
+        
         conn = get_db_connection()
         if not conn:
             return jsonify({"error": "数据库连接失败"}), 500

+ 1 - 1
src/dashboard/static/index.html

@@ -723,7 +723,7 @@
         async function loadDetailData(symbol, centerTimestamp) {
             try {
                 showLoading();
-                const response = await fetch(`${API_BASE}/api/range/${symbol}?timestamp=${centerTimestamp}`);
+                const response = await fetch(`${API_BASE}/range/${symbol}?timestamp=${centerTimestamp}`);
                 
                 if (!response.ok) {
                     throw new Error(`HTTP error! status: ${response.status}`);