diff --git a/internal/handler/agent_admin_handler.go b/internal/handler/agent_admin_handler.go index 87e777c..308270b 100644 --- a/internal/handler/agent_admin_handler.go +++ b/internal/handler/agent_admin_handler.go @@ -281,7 +281,6 @@ func (h *AgentHandler) UpdateTrafficConfig(c echo.Context) error { Enabled bool `json:"enabled"` Type string `json:"type"` // 流量类型: recv/send/both Limit uint64 `json:"limit"` - Used uint64 `json:"used"` // 手动设置已使用流量(可选) ResetDay int `json:"resetDay"` } @@ -290,7 +289,7 @@ func (h *AgentHandler) UpdateTrafficConfig(c echo.Context) error { } ctx := c.Request().Context() - if err := h.trafficService.UpdateTrafficConfig(ctx, agentID, req.Enabled, req.Type, req.Limit, req.ResetDay, req.Used); err != nil { + if err := h.trafficService.UpdateTrafficConfig(ctx, agentID, req.Enabled, req.Type, req.Limit, req.ResetDay); err != nil { return err } diff --git a/internal/models/agent.go b/internal/models/agent.go index 2ba571a..96a81f2 100644 --- a/internal/models/agent.go +++ b/internal/models/agent.go @@ -37,7 +37,7 @@ type TrafficStatsData struct { Type string `json:"type"` // 统计类型: "recv"进站, "send"出站, "both"全部 Limit uint64 `json:"limit"` // 流量限额(字节), 0表示不限制 Used uint64 `json:"used"` // 当前周期已使用流量(字节) - UsedOffset int64 `json:"usedOffset"` // 手动调整流量偏移(字节), 允许为负数 + UsedOffset uint64 `json:"usedOffset"` // 基线调整使用偏移(字节) ResetDay int `json:"resetDay"` // 流量重置日期(1-31), 0表示不自动重置 PeriodStart int64 `json:"periodStart"` // 当前周期开始时间(时间戳毫秒) BaselineRecv uint64 `json:"baselineRecv"` // 当前周期流量基线(BytesRecvTotal) diff --git a/internal/service/traffic_service.go b/internal/service/traffic_service.go index d1f228b..c65cb91 100644 --- a/internal/service/traffic_service.go +++ b/internal/service/traffic_service.go @@ -59,19 +59,26 @@ func (s *TrafficService) UpdateAgentTraffic(ctx context.Context, agentID string, now := time.Now().UnixMilli() baselineChanged := false + if stats.PeriodStart == 0 { + stats.PeriodStart = now + } + + needRecv := stats.Type == "recv" || stats.Type == "both" + needSend := stats.Type == "send" || stats.Type == "both" + // 初始化基线(首次统计或基线丢失) - if stats.BaselineRecv == 0 && stats.BaselineSend == 0 { + if needRecv && stats.BaselineRecv == 0 { stats.BaselineRecv = currentRecvTotal + baselineChanged = true + } + if needSend && stats.BaselineSend == 0 { stats.BaselineSend = currentSentTotal - if stats.PeriodStart == 0 { - stats.PeriodStart = now - } baselineChanged = true } // 检测计数器重置(探针重启) - recvReset := currentRecvTotal < stats.BaselineRecv - sendReset := currentSentTotal < stats.BaselineSend + recvReset := needRecv && currentRecvTotal < stats.BaselineRecv + sendReset := needSend && currentSentTotal < stats.BaselineSend if recvReset || sendReset { s.logger.Warn("检测到流量计数器重置", @@ -208,8 +215,7 @@ func formatBytes(bytes uint64) string { } // UpdateTrafficConfig 更新流量配置 -// used: 如果大于0,则使用该值作为已使用流量;如果为0,则保持当前值或重置 -func (s *TrafficService) UpdateTrafficConfig(ctx context.Context, agentID string, enabled bool, trafficType string, limit uint64, resetDay int, used uint64) error { +func (s *TrafficService) UpdateTrafficConfig(ctx context.Context, agentID string, enabled bool, trafficType string, limit uint64, resetDay int) error { if resetDay < 0 || resetDay > 31 { return fmt.Errorf("重置日期必须在0-31之间") } @@ -254,12 +260,6 @@ func (s *TrafficService) UpdateTrafficConfig(ctx context.Context, agentID string stats.AlertSent100 = false } - // 如果提供了 used 参数且大于 0,则使用该值 - if used > 0 { - stats.UsedOffset = addOffset(stats.UsedOffset, calculateOffset(used, stats.Used)) - stats.Used = used - } - // 如果禁用流量统计,清空相关数据 if !stats.Enabled { stats.Used = 0 @@ -448,51 +448,15 @@ func calculateBaseDelta(trafficType string, currentRecvTotal, currentSentTotal, } } -func calculateOffset(targetUsed, baseDelta uint64) int64 { - const maxInt64 = int64(^uint64(0) >> 1) - const minInt64 = -maxInt64 - 1 - - if targetUsed >= baseDelta { - diff := targetUsed - baseDelta - if diff > uint64(maxInt64) { - return maxInt64 - } - return int64(diff) - } - - diff := baseDelta - targetUsed - if diff > uint64(maxInt64) { - return minInt64 - } - return -int64(diff) -} - -func applyOffset(baseDelta uint64, offset int64) uint64 { - if offset >= 0 { - return baseDelta + uint64(offset) - } - const minInt64 = -1 - (int64(^uint64(0) >> 1)) - if offset == minInt64 { +func calculateOffset(currentUsed, baseDelta uint64) uint64 { + if currentUsed <= baseDelta { return 0 } - neg := uint64(-offset) - if neg > baseDelta { - return 0 - } - return baseDelta - neg + return currentUsed - baseDelta } -func addOffset(base, delta int64) int64 { - const maxInt64 = int64(^uint64(0) >> 1) - const minInt64 = -maxInt64 - 1 - - if delta > 0 && base > maxInt64-delta { - return maxInt64 - } - if delta < 0 && base < minInt64-delta { - return minInt64 - } - return base + delta +func applyOffset(baseDelta, offset uint64) uint64 { + return baseDelta + offset } // TrafficStats 流量统计信息 diff --git a/web/src/admin/pages/Agents/TrafficStats.tsx b/web/src/admin/pages/Agents/TrafficStats.tsx index aad6e34..8c26d9a 100644 --- a/web/src/admin/pages/Agents/TrafficStats.tsx +++ b/web/src/admin/pages/Agents/TrafficStats.tsx @@ -16,7 +16,6 @@ const TrafficStats: React.FC = ({agentId}) => { const [form] = Form.useForm(); const queryClient = useQueryClient(); const [enabled, setEnabled] = useState(false); - const [editingUsed, setEditingUsed] = useState(false); // 获取流量统计 const {data: stats, isLoading} = useQuery({ @@ -33,18 +32,15 @@ const TrafficStats: React.FC = ({agentId}) => { mutationFn: async () => { const values = form.getFieldsValue(); const limitBytes = enabled ? (values.trafficLimit || 0) * 1024 * 1024 * 1024 : 0; - const usedBytes = editingUsed && values.trafficUsed ? values.trafficUsed * 1024 * 1024 * 1024 : 0; return updateTrafficConfig(agentId, { enabled: enabled, type: values.trafficType || 'recv', limit: limitBytes, - used: usedBytes, resetDay: enabled ? (values.trafficResetDay || 0) : 0, }); }, onSuccess: () => { message.success('配置已保存'); - setEditingUsed(false); queryClient.invalidateQueries({queryKey: ['trafficStats', agentId]}); }, onError: (error: unknown) => { @@ -279,20 +275,9 @@ const TrafficStats: React.FC = ({agentId}) => { - 已使用流量 - - - } + label="已使用流量" name="trafficUsed" - extra={editingUsed ? "手动设置已使用的流量大小(GB)" : "当前已使用的流量(只读)"} + extra="当前已使用的流量(只读)" > = ({agentId}) => { placeholder="已使用流量(GB)" style={{width: '100%'}} addonAfter="GB" - disabled={!editingUsed} + disabled /> diff --git a/web/src/types/index.ts b/web/src/types/index.ts index a2bc33f..d7980bd 100644 --- a/web/src/types/index.ts +++ b/web/src/types/index.ts @@ -531,7 +531,6 @@ export interface UpdateTrafficConfigRequest { enabled: boolean; // 是否启用 type: string; // 统计类型: recv进站/send出站/both全部 limit: number; // 流量限额(字节), 0表示不限制 - used?: number; // 手动设置已使用流量(可选) resetDay: number; // 流量重置日期(1-31), 0表示不自动重置 }