187 lines
7.2 KiB
React
187 lines
7.2 KiB
React
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
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 (
|
|
<div className='quality-card'>
|
|
<div className='quality-card-head'>
|
|
<span className='quality-card-title'>
|
|
<Grid3x3 size={16} />
|
|
{t('渠道故障热力图')}
|
|
<span className='quality-card-sub'>
|
|
{t('流量前 20 的渠道 · 颜色越深上游故障率越高 · 点击格子下钻')}
|
|
</span>
|
|
</span>
|
|
<div className='quality-heatmap-legend'>
|
|
<span>0%</span>
|
|
{[0, 25, 50, 75, 100].map((lvl) => (
|
|
<span
|
|
key={lvl}
|
|
className='swatch quality-heatmap-cell'
|
|
style={{ '--cell-level': `${lvl}%`, cursor: 'default' }}
|
|
/>
|
|
))}
|
|
<span>≥50%</span>
|
|
</div>
|
|
</div>
|
|
<div className='quality-card-body'>
|
|
{data?.meta && (
|
|
<QualityMetaNotice meta={data.meta} scope='platform' t={t} />
|
|
)}
|
|
<Spin spinning={loading}>
|
|
{error ? (
|
|
<div className='quality-empty'>{qualityErrorText(t, error)}</div>
|
|
) : rows.length === 0 ? (
|
|
<div className='quality-empty'>{t('暂无渠道调用数据')}</div>
|
|
) : (
|
|
<div className='quality-heatmap app-scrollbar mt-2'>
|
|
<table className='quality-heatmap-table'>
|
|
<tbody>
|
|
{rows.map((row) => {
|
|
const byTs = {};
|
|
(row.cells || []).forEach((c) => {
|
|
byTs[c.ts] = c;
|
|
});
|
|
const name = names[row.channel_id] || `#${row.channel_id}`;
|
|
return (
|
|
<tr key={row.channel_id}>
|
|
<th title={name}>
|
|
<button
|
|
type='button'
|
|
className='truncate max-w-[170px] text-left'
|
|
onClick={() =>
|
|
setFilters({ channel_id: row.channel_id })
|
|
}
|
|
>
|
|
{name}
|
|
</button>
|
|
</th>
|
|
{timeline.map((ts) => {
|
|
const cell = byTs[ts];
|
|
if (!cell || !cell.total) {
|
|
return (
|
|
<td key={ts}>
|
|
<span
|
|
className='quality-heatmap-cell block'
|
|
data-empty='true'
|
|
/>
|
|
</td>
|
|
);
|
|
}
|
|
// 50% 及以上即满色,低故障率也保持可见的层次
|
|
const level =
|
|
Math.min(1, cell.upstream_fault_rate / 0.5) * 100;
|
|
return (
|
|
<td key={ts}>
|
|
<Tooltip
|
|
content={
|
|
<div className='text-xs'>
|
|
<div>{name}</div>
|
|
<div>{fmtTs(ts)}</div>
|
|
<div>
|
|
{t('请求数')} {fmtInt(cell.total)} ·{' '}
|
|
{t('上游故障')}{' '}
|
|
{fmtInt(cell.upstream_fault)} (
|
|
{pct(cell.upstream_fault_rate)})
|
|
</div>
|
|
</div>
|
|
}
|
|
>
|
|
<button
|
|
type='button'
|
|
aria-label={`${name} ${fmtTs(ts)}`}
|
|
className='quality-heatmap-cell block'
|
|
style={{ '--cell-level': `${level}%` }}
|
|
onClick={() =>
|
|
setFilters({
|
|
channel_id: row.channel_id,
|
|
range: 'custom',
|
|
start: String(ts),
|
|
end: String(ts + gran),
|
|
granularity: 'auto',
|
|
})
|
|
}
|
|
/>
|
|
</Tooltip>
|
|
</td>
|
|
);
|
|
})}
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
{timeline.length > 0 && (
|
|
<div className='mt-2 flex justify-between text-xs text-[var(--text-secondary)] quality-num'>
|
|
<span>{fmtTs(timeline[0])}</span>
|
|
<span>{fmtTs(timeline[timeline.length - 1])}</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</Spin>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default QualityHeatmap;
|