From c090ba0564ad3ea22d6954c86ed43b4a5ffe23fd Mon Sep 17 00:00:00 2001 From: opsx Date: Sat, 31 Oct 2026 22:51:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(aiops):=20=E9=95=BF=E7=AA=97=E5=8F=A3(>1h)?= =?UTF-8?q?=E5=88=86=E6=9E=90=E5=BF=85=E9=A1=BB=E6=8C=87=E5=AE=9A=E5=BA=94?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 全命名空间 6h/24h 聚合数据量过大会拖垮 Loki:前端时间范围旁新增应用范围选择器 (labelValues 候选),>1h 未选应用禁用开始分析并提示;后端同样强制 400(闸门在服务端); 信号采集选择器按 app 收窄后 histogram/stats 从全量变为单应用流,秒级返回。 --- backend/controllers/ai.go | 10 ++++-- backend/services/ai_analyze.go | 4 +-- backend/services/ai_signal.go | 11 +++++-- frontend/src/api/ai/index.ts | 2 +- frontend/src/i18n/locales/en/aiops.ts | 4 +++ frontend/src/i18n/locales/zh/aiops.ts | 4 +++ frontend/src/pages/aiops/index.tsx | 45 +++++++++++++++++++++++---- 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/backend/controllers/ai.go b/backend/controllers/ai.go index 26de2e8..6adfc6f 100644 --- a/backend/controllers/ai.go +++ b/backend/controllers/ai.go @@ -22,7 +22,7 @@ func (ctrl *AIController) GetConfig(c *gin.Context) { userID := c.GetUint("userId") cfg := services.MaskedAIConfig() utils.Success(c, gin.H{ - "config": cfg, + "config": cfg, "tokenUsedToday": services.AITokenUsedToday(userID), }) } @@ -91,6 +91,7 @@ func (ctrl *AIController) Analyze(c *gin.Context) { Start int64 `json:"start" binding:"required"` End int64 `json:"end" binding:"required"` Question string `json:"question"` + App string `json:"app"` } if !utils.BindAndValidate(c, &req) { return @@ -110,13 +111,18 @@ func (ctrl *AIController) Analyze(c *gin.Context) { utils.BadRequest(c, fmt.Sprintf("时间范围过大(%d 分钟):AI 分析最多支持 24 小时,建议用 15~60 分钟", int(to.Sub(from).Minutes()))) return } + // 长窗口必须指定应用:全命名空间聚合在 6h/24h 下数据量过大,查询会拖垮 Loki + if to.Sub(from) > time.Hour && req.App == "" { + utils.BadRequest(c, "超过 1 小时的分析必须指定应用(app):全命名空间长窗口聚合过慢,请先在应用排行里选一个 app 缩小范围") + return + } userID := c.GetUint("userId") username := c.GetString("username") admin := c.GetString("role") == "admin" cfg := services.GetAIConfig() - outcome, err := services.RunAIAnalysis(c.Request.Context(), cfg, src, userID, username, admin, from, to, req.Question) + outcome, err := services.RunAIAnalysis(c.Request.Context(), cfg, src, userID, username, admin, from, to, req.Question, req.App) if err != nil { // 未配置模型 / 配额用尽 / 上游报错:都给出具体原因,不包装成 500 utils.Error(c, 400, err.Error()) diff --git a/backend/services/ai_analyze.go b/backend/services/ai_analyze.go index 28e856e..1fb88c4 100644 --- a/backend/services/ai_analyze.go +++ b/backend/services/ai_analyze.go @@ -59,7 +59,7 @@ JSON 结构: }` // RunAIAnalysis 执行一次分析:采集信号 → 脱敏 → 调模型 → 解析 → 落审计。 -func RunAIAnalysis(ctx context.Context, cfg models.AIConfig, src *models.LogSource, userID uint, username string, admin bool, from, to time.Time, question string) (*AIAnalysisOutcome, error) { +func RunAIAnalysis(ctx context.Context, cfg models.AIConfig, src *models.LogSource, userID uint, username string, admin bool, from, to time.Time, question, app string) (*AIAnalysisOutcome, error) { if !cfg.Enabled { return nil, fmt.Errorf("AI 分析未启用:请先在「智能运维 → 模型配置」中开启并保存") } @@ -79,7 +79,7 @@ func RunAIAnalysis(ctx context.Context, cfg models.AIConfig, src *models.LogSour } started := time.Now() - signals, err := CollectAISignals(ctx, src, userID, admin, from, to) + signals, err := CollectAISignals(ctx, src, userID, admin, from, to, app) if err != nil { return nil, err } diff --git a/backend/services/ai_signal.go b/backend/services/ai_signal.go index 284e820..98afa0a 100644 --- a/backend/services/ai_signal.go +++ b/backend/services/ai_signal.go @@ -92,15 +92,20 @@ type AIAlert struct { } // CollectAISignals 采集一次分析所需的全部信号。 -func CollectAISignals(ctx context.Context, src *models.LogSource, userID uint, admin bool, from, to time.Time) (*AISignalBundle, error) { +func CollectAISignals(ctx context.Context, src *models.LogSource, userID uint, admin bool, from, to time.Time, app string) (*AISignalBundle, error) { if src == nil { return nil, fmt.Errorf("数据源不存在") } if !to.After(from) { return nil, fmt.Errorf("时间范围无效") } - // 复用查询授权:AI 能看到的数据范围必须与当前用户一致,不多不少 - selector, err := AuthorizedLogQuery(src, userID, admin, `{namespace=~".+"}`) + // 复用查询授权:AI 能看到的数据范围必须与当前用户一致,不多不少。 + // 长窗口由上层强制指定 app(缩小聚合范围);15m/1h 允许全命名空间 + baseSelector := `{namespace=~".+"}` + if app != "" { + baseSelector = `{namespace=~".+", app="` + app + `"}` + } + selector, err := AuthorizedLogQuery(src, userID, admin, baseSelector) if err != nil { return nil, err } diff --git a/frontend/src/api/ai/index.ts b/frontend/src/api/ai/index.ts index 6a8f0df..562a918 100644 --- a/frontend/src/api/ai/index.ts +++ b/frontend/src/api/ai/index.ts @@ -84,7 +84,7 @@ export const aiApi = { saveConfig: (data: Partial) => http.put<{ config: AIConfig }>('/ai/config', data), testConfig: (data: { baseUrl?: string; model?: string; apiKey?: string }) => http.post<{ reply: string; model: string }>('/ai/config/test', data, { timeout: 45000 }), - analyze: (data: { sourceId: number; start: number; end: number; question?: string }) => + analyze: (data: { sourceId: number; start: number; end: number; question?: string; app?: string }) => http.post('/ai/analyze', data, { timeout: 180000 }), runs: (page: number, pageSize: number) => http.get<{ list: AIRun[]; total: number; page: number; pageSize: number }>( diff --git a/frontend/src/i18n/locales/en/aiops.ts b/frontend/src/i18n/locales/en/aiops.ts index 03cbb74..b825f85 100644 --- a/frontend/src/i18n/locales/en/aiops.ts +++ b/frontend/src/i18n/locales/en/aiops.ts @@ -9,6 +9,10 @@ export default { }, analyze: { + appScope: 'App scope', + allApps: 'All apps', + appRequired: 'Required (over 1h)', + appScopeHint: 'Analysis over 1 hour requires a specific app to keep aggregation fast', source: 'Data source', range: 'Time range', question: 'Extra question (optional)', diff --git a/frontend/src/i18n/locales/zh/aiops.ts b/frontend/src/i18n/locales/zh/aiops.ts index a5614e4..472ab77 100644 --- a/frontend/src/i18n/locales/zh/aiops.ts +++ b/frontend/src/i18n/locales/zh/aiops.ts @@ -9,6 +9,10 @@ export default { }, analyze: { + appScope: '应用范围', + allApps: '全部应用', + appRequired: '必选(超过 1 小时)', + appScopeHint: '超过 1 小时的分析必须指定应用,避免全命名空间聚合过慢', source: '数据源', range: '时间范围', question: '补充问题(可选)', diff --git a/frontend/src/pages/aiops/index.tsx b/frontend/src/pages/aiops/index.tsx index 4ec7170..857fb44 100644 --- a/frontend/src/pages/aiops/index.tsx +++ b/frontend/src/pages/aiops/index.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useMemo, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { LLM_PROVIDERS, matchProvider } from './llmProviders' import { Loader2, Play, Save, Sparkles, PlugZap, History, ShieldCheck } from 'lucide-react' @@ -50,12 +50,28 @@ export default function AiopsPage() { const [sources, setSources] = useState([]) const [sourceId, setSourceId] = useState(0) const [rangeKey, setRangeKey] = useState<(typeof RANGE_PRESETS)[number]['key']>('15m') + const [apps, setApps] = useState([]) + const [app, setApp] = useState('') const [question, setQuestion] = useState('') const [running, setRunning] = useState(false) const [outcome, setOutcome] = useState(null) const [promptOpen, setPromptOpen] = useState(false) const [enabled, setEnabled] = useState(false) + // app 候选:长窗口(>1h)必须指定应用收窄聚合范围 + useEffect(() => { + setApp('') + setApps([]) + if (!sourceId) return + let active = true + logsApi.labelValues(sourceId, 'app') + .then(list => { if (active) setApps(Array.isArray(list) ? list : []) }) + .catch(() => { if (active) setApps([]) }) + return () => { active = false } + }, [sourceId]) + const rangeMinutes = RANGE_PRESETS.find(r => r.key === rangeKey)?.minutes ?? 15 + const needApp = rangeMinutes > 60 + // ── 配置 ── const [config, setConfig] = useState(null) const [apiKeyInput, setApiKeyInput] = useState('') @@ -120,16 +136,16 @@ export default function AiopsPage() { if (tab === 'history') loadRuns(runPage) }, [tab, runPage, loadRuns]) - const rangeMinutes = useMemo( - () => RANGE_PRESETS.find(r => r.key === rangeKey)?.minutes ?? 15, - [rangeKey], - ) const runAnalysis = async () => { if (!sourceId) { toast.error('请先选择数据源') return } + if (needApp && !app) { + toast.error('超过 1 小时的分析必须指定应用:全命名空间长窗口聚合过慢') + return + } const end = Date.now() const start = end - rangeMinutes * 60 * 1000 setRunning(true) @@ -140,6 +156,7 @@ export default function AiopsPage() { start: start * 1e6, end: end * 1e6, question: question.trim() || undefined, + app: app || undefined, }) setOutcome(res) loadConfig() @@ -255,6 +272,22 @@ export default function AiopsPage() { ))} +
+ + + {needApp &&

{t('analyze.appScopeHint')}

} +
-