/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ // 调用质量看板的枚举与文案。枚举值与后端 service/monitor/outcome.go 保持一致。 export const OUTCOMES = [ { value: 1, key: 'c1', label: '直接成功', tone: 'success' }, { value: 2, key: 'c2', label: '兜底成功', tone: 'primary' }, { value: 3, key: 'c3', label: '兜底失败', tone: 'danger' }, { value: 4, key: 'c4', label: '失败(未兜底)', tone: 'warning' }, { value: 5, key: 'c5', label: '客户端中断', tone: 'tertiary' }, { value: 6, key: 'c6', label: '内容安全拦截', tone: 'purple' }, ]; export const FAULTS = [ { value: 1, key: 'upstream', label: '上游故障', orgLabel: '服务侧故障' }, { value: 2, key: 'platform', label: '平台故障', orgLabel: '服务侧故障' }, { value: 3, key: 'client', label: '请求参数问题', orgLabel: '请求参数问题' }, { value: 4, key: 'quota', label: '额度 / 限流', orgLabel: '额度 / 限流' }, { value: 5, key: 'abort', label: '客户端中断', orgLabel: '客户端中断' }, ]; export const SOURCES = [ { value: 1, label: 'API 调用' }, { value: 2, label: '异步任务' }, { value: 3, label: '任务提交' }, { value: 4, label: '体验中心' }, { value: 5, label: '内部调用' }, { value: 6, label: '未鉴权请求' }, ]; export const TIME_RANGES = [ { value: '1h', label: '近 1 小时', seconds: 3600 }, { value: '6h', label: '近 6 小时', seconds: 6 * 3600 }, { value: '24h', label: '近 24 小时', seconds: 24 * 3600 }, { value: '7d', label: '近 7 天', seconds: 7 * 86400 }, { value: '30d', label: '近 30 天', seconds: 30 * 86400 }, ]; export const PLATFORM_DIMS = [ { value: 'model', label: '模型', filterKey: 'model' }, { value: 'channel', label: '渠道', filterKey: 'channel_id' }, { value: 'org', label: '企业', filterKey: 'org_id' }, { value: 'group', label: '分组', filterKey: 'group' }, { value: 'error', label: '错误码', filterKey: 'error_code' }, { value: 'path', label: '接口', filterKey: 'path' }, { value: 'user', label: '用户', filterKey: 'user_id' }, { value: 'token', label: '密钥', filterKey: 'token_id' }, ]; export const ORG_DIMS = [ { value: 'model', label: '模型', filterKey: 'model' }, { value: 'dept', label: '部门', filterKey: 'dept_id' }, { value: 'project', label: '项目', filterKey: 'project_id' }, { value: 'user', label: '成员', filterKey: 'user_id' }, { value: 'token', label: '密钥', filterKey: 'token_id' }, { value: 'error', label: '错误码', filterKey: 'error_code' }, ]; export const ORDER_OPTIONS = [ { value: 'total', label: '按调用量' }, { value: 'success_rate', label: '按成功率(低→高)' }, { value: 'fallback_failed', label: '按兜底失败数' }, { value: 'failed', label: '按失败数' }, { value: 'upstream_fault_rate', label: '按上游故障率' }, { value: 'p95', label: '按 P95 耗时' }, ]; export const ALERT_METRICS = [ { value: 'success_rate', label: '成功率(%)', unit: '%' }, { value: 'fallback_failed', label: '兜底失败次数', unit: '' }, { value: 'upstream_fault_pct', label: '上游故障率(%)', unit: '%' }, { value: 'p95_ms', label: 'P95 耗时(毫秒)', unit: 'ms' }, ]; export const ALERT_DIM_TYPES = [ { value: 1, label: '全平台' }, { value: 2, label: '模型' }, { value: 3, label: '渠道' }, { value: 5, label: '企业' }, ]; // 后端错误码 → 中文文案 key(前端经 t() 翻译) export const QUALITY_ERROR_TEXT = { window_invalid: '时间范围无效,请重新选择', window_too_wide: '按用户 / 密钥或交叉条件筛选时,时间范围最长 7 天,请缩小范围', detail_window_too_wide: '明细查询时间范围最长 31 天,请缩小范围', invalid_dimension: '不支持的分析维度', unsupported_dimension: '当前筛选条件下不支持该维度', query_failed: '查询失败,请稍后重试', invalid_source: '调用来源参数无效', invalid_outcome: '结局参数无效', invalid_fault_side: '责任方参数无效', invalid_model: '模型参数过长', invalid_group: '分组参数过长', invalid_path: '接口参数过长', invalid_error_code: '错误码参数过长', org_required: '缺少企业上下文,请重新进入企业中心', token_not_in_org: '该密钥不属于本企业', invalid_keyword: '搜索关键词过长', invalid_type: '搜索类型无效', request_trace_params_required: '缺少请求 ID,无法查看链路', invalid_body: '提交内容格式错误', invalid_name: '请填写规则名称(最长 64 字)', invalid_dim_type: '请选择监控范围', invalid_dim_key: '请填写监控对象', invalid_metric: '请选择监控指标', invalid_comparator: '请选择比较方式', invalid_threshold: '阈值不能为负数', invalid_window: '统计窗口需在 5~1440 分钟之间', invalid_min_samples: '最小样本量需大于 0', invalid_silence: '静默期需在 5 分钟 ~ 7 天之间', rule_not_found: '规则不存在或已被删除', forbidden: '无权查看调用质量,请联系企业所有者开通「调用质量」权限', feature_disabled: '企业未开通「调用质量」功能,请联系平台开通', logs_query_busy: '当前直查日志的查询较多,请稍后重试或缩小时间范围', query_timeout: '查询超时,请缩小时间范围或减少筛选条件后重试', }; export const DEGRADE_TEXT = { user_or_token_filter: '按用户 / 密钥筛选,已改为直接统计调用日志', user_or_token_dimension: '按用户 / 密钥维度分析,已改为直接统计调用日志', cross_dimension: '当前筛选组合超出预聚合范围,已改为直接统计调用日志', path_ignored: '直查日志时不支持按接口筛选,接口条件未生效', heatmap_requires_single_dimension: '热力图只支持按时间与渠道查看,当前筛选条件下不展示', }; // 错误展示:已知错误码翻译;未知码若是中文文案(如企业中间件返回的业务提示)原样展示; // 其余(英文内部错误、数据库报错)统一显示通用文案,不把内部信息直显给用户 export const qualityErrorText = (t, message) => { const key = QUALITY_ERROR_TEXT[message]; if (key) return t(key); if (message && /[一-鿿]/.test(message)) return t(message); return t('查询失败,请稍后重试'); }; // 图表需要具体色值:从主题 token 读取,保证随亮 / 暗色与品牌色切换 export const readCssVar = (name, fallback) => { if (typeof window === 'undefined') return fallback; const value = getComputedStyle(document.body).getPropertyValue(name).trim(); return value || fallback; }; const toneVar = { success: ['--ui-color-success', '#3bb346'], primary: ['--ui-color-primary', '#0064fa'], danger: ['--ui-color-danger', '#f93920'], warning: ['--ui-color-warning', '#fc8800'], tertiary: ['--ui-color-tertiary', '#6b7075'], purple: ['--ui-color-secondary', '#8b5cf6'], }; export const outcomeColors = () => OUTCOMES.map((o) => readCssVar(toneVar[o.tone][0], toneVar[o.tone][1])); export const toneColor = (tone) => readCssVar(toneVar[tone][0], toneVar[tone][1]); export const outcomeTagColor = { 1: 'green', 2: 'blue', 3: 'red', 4: 'orange', 5: 'grey', 6: 'purple', }; export const pct = (v, digits = 1) => { const number = Number(v); return v === null || v === undefined || !Number.isFinite(number) ? '-' : `${(number * 100).toFixed(digits)}%`; }; export const fmtInt = (v) => Number(v || 0).toLocaleString(); export const fmtMs = (v) => { const n = Number(v || 0); if (n <= 0) return '-'; if (n < 1000) return `${n} ms`; return `${(n / 1000).toFixed(n < 10000 ? 2 : 1)} s`; }; export const fmtTs = (ts, withSeconds = false) => { if (!ts) return '-'; const d = new Date(ts * 1000); const pad = (n) => String(n).padStart(2, '0'); const base = `${d.getMonth() + 1}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`; return withSeconds ? `${base}:${pad(d.getSeconds())}` : base; };