/* 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 */ import React, { useEffect, useMemo, useRef, useState } from 'react'; import { Spin, Tooltip } from '@douyinfe/semi-ui'; import { Grid3x3 } from 'lucide-react'; import { fmtInt, fmtTs, pct, qualityErrorText } from './qualityConstants'; import { QualityMetaNotice } from './QualityOverview'; /** * 渠道 × 时间 上游故障率热力图(仅平台侧)。 * 色阶由 --semi-color-danger 按故障率混入,跟随主题;点击格子下钻到该渠道该时段。 */ const QualityHeatmap = ({ quality, t }) => { const { get, params, setFilters } = quality; const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const seq = useRef(0); useEffect(() => { const current = ++seq.current; setLoading(true); setError(''); get('/heatmap', { ...params, top: 20 }) .then((d) => current === seq.current && setData(d)) .catch((e) => { if (current !== seq.current) return; setData(null); setError(e.code || 'query_failed'); }) .finally(() => current === seq.current && setLoading(false)); }, [get, params]); const gran = data?.meta?.granularity || 3600; const timeline = useMemo(() => { const set = new Set(); (data?.rows || []).forEach((row) => (row.cells || []).forEach((c) => set.add(c.ts)), ); return Array.from(set).sort((a, b) => a - b); }, [data]); const rows = data?.rows || []; const names = data?.channel_names || {}; return (
{t('渠道故障热力图')} {t('流量前 20 的渠道 · 颜色越深上游故障率越高 · 点击格子下钻')}
0% {[0, 25, 50, 75, 100].map((lvl) => ( ))} ≥50%
{data?.meta && ( )} {error ? (
{qualityErrorText(t, error)}
) : rows.length === 0 ? (
{t('暂无渠道调用数据')}
) : (
{rows.map((row) => { const byTs = {}; (row.cells || []).forEach((c) => { byTs[c.ts] = c; }); const name = names[row.channel_id] || `#${row.channel_id}`; return ( {timeline.map((ts) => { const cell = byTs[ts]; if (!cell || !cell.total) { return ( ); } // 50% 及以上即满色,低故障率也保持可见的层次 const level = Math.min(1, cell.upstream_fault_rate / 0.5) * 100; return ( ); })} ); })}
{name}
{fmtTs(ts)}
{t('请求数')} {fmtInt(cell.total)} ·{' '} {t('上游故障')}{' '} {fmtInt(cell.upstream_fault)} ( {pct(cell.upstream_fault_rate)})
} >
{timeline.length > 0 && (
{fmtTs(timeline[0])} {fmtTs(timeline[timeline.length - 1])}
)}
)}
); }; export default QualityHeatmap;