feat(aiops): 长窗口(>1h)分析必须指定应用

全命名空间 6h/24h 聚合数据量过大会拖垮 Loki:前端时间范围旁新增应用范围选择器
(labelValues 候选),>1h 未选应用禁用开始分析并提示;后端同样强制 400(闸门在服务端);
信号采集选择器按 app 收窄后 histogram/stats 从全量变为单应用流,秒级返回。
This commit is contained in:
opsx committed 2026-10-31 22:51:55 +08:00
1 parent 1610988c2e
commit c090ba0564
7 files changed
+66 -14

No files matched your search

+1 -1
View File
@@ -84,7 +84,7 @@ export const aiApi = {
saveConfig: (data: Partial<AIConfig>) => 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<AIAnalyzeResponse>('/ai/analyze', data, { timeout: 180000 }),
runs: (page: number, pageSize: number) =>
http.get<{ list: AIRun[]; total: number; page: number; pageSize: number }>(
+4
View File
@@ -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)',
+4
View File
@@ -9,6 +9,10 @@ export default {
},
analyze: {
appScope: '应用范围',
allApps: '全部应用',
appRequired: '必选(超过 1 小时)',
appScopeHint: '超过 1 小时的分析必须指定应用,避免全命名空间聚合过慢',
source: '数据源',
range: '时间范围',
question: '补充问题(可选)',
+39 -6
View File
@@ -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<LogSource[]>([])
const [sourceId, setSourceId] = useState(0)
const [rangeKey, setRangeKey] = useState<(typeof RANGE_PRESETS)[number]['key']>('15m')
const [apps, setApps] = useState<string[]>([])
const [app, setApp] = useState('')
const [question, setQuestion] = useState('')
const [running, setRunning] = useState(false)
const [outcome, setOutcome] = useState<AIAnalyzeResponse | null>(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<AIConfig | null>(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() {
))}
</div>
</div>
<div className="space-y-1">
<Label className="text-xs">
{t('analyze.appScope')}
{needApp && <span className="ml-1 text-destructive">*</span>}
</Label>
<select
aria-label={t('analyze.appScope')}
className="h-9 min-w-40 rounded-md border bg-background px-3 text-sm"
value={app}
onChange={e => setApp(e.target.value)}
>
<option value="">{needApp ? t('analyze.appRequired') : t('analyze.allApps')}</option>
{apps.map(a => <option key={a} value={a}>{a}</option>)}
</select>
{needApp && <p className="text-[11px] text-warning">{t('analyze.appScopeHint')}</p>}
</div>
<div className="min-w-64 flex-1 space-y-1">
<Label className="text-xs">{t('analyze.question')}</Label>
<Input
@@ -264,7 +297,7 @@ export default function AiopsPage() {
className="h-9"
/>
</div>
<Button onClick={runAnalysis} disabled={running || !enabled} className="h-9">
<Button onClick={runAnalysis} disabled={running || !enabled || (needApp && !app)} className="h-9">
{running ? <Loader2 className="mr-1.5 h-4 w-4 animate-spin" /> : <Play className="mr-1.5 h-4 w-4" />}
{running ? t('analyze.running') : t('analyze.run')}
</Button>