修接口流量图时区 bug:查询窗口差 8 小时导致图一直是空的

根因:InfluxDB 写点用真实 UTC(_get_utc_now),但流量接口
用 naive 本地时间 datetime.now() 算窗口。容器时区是
Asia/Shanghai(UTC+8),查询层又把 naive 时间当作 UTC,于
是 1 小时窗口被整体推后 8 小时、落在未来,永远查不到点。
接口表能显示最新值(aquery_latest 不用时间窗口),只有带时
间范围的流量图受影响。

修法:改用代码库已有的 utc_now_naive()(naive UTC),与
monitor 趋势接口保持一致。验证:18:34 CST 时旧窗口为 17:34Z
(未来,无数据)、新窗口为 09:34Z(覆盖最近 1h,60 个点全在
窗口内)。后端 69/69 测试通过。
This commit is contained in:
2026-11-16 18:35:05 +08:00
parent 4b47bf9561
commit 579f1de333
+8 -2
View File
@@ -8,7 +8,7 @@ query parameter instead of a path segment.
"""
import logging
from datetime import datetime, timedelta
from datetime import timedelta
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
@@ -20,6 +20,7 @@ from app.models import Host
from app.routers.auth import get_current_user
from app.models import User as UserModel
from app.services.alert_engine import host_offline_value
from app.utils import utc_now_naive
from app.services.snmp_collector import (
H3C_OPER_NOT_SUPPORTED,
H3C_TEMPERATURE_NOT_SUPPORTED,
@@ -120,7 +121,12 @@ async def interface_traffic(
each point {time, value}.
"""
await _get_snmp_host(host_id, db)
start = datetime.now() - timedelta(hours=hours)
# InfluxDB stores points in UTC and the query layer treats naive
# datetimes as UTC. datetime.now() is naive LOCAL time (the
# container runs Asia/Shanghai, UTC+8), which shifted the window 8h
# into the future so the chart always came back empty. Use the
# naive-UTC helper like the monitor trend endpoints do.
start = utc_now_naive() - timedelta(hours=hours)
in_data = await influxdb_module.aquery_metrics(
host_id, "network_if_in_bps", start
)