Files
MediaCrawler/api/reports/matrix.html
T
butubb c06848ae98
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s
fix(monitor): 卡死的一轮不再堵住调度循环;矩阵补总获赞和首条视频日期
「抓不到最新的」有两个叠加的原因:

1. 采集卡死时 `_run_due_task` 是内联 await,execute_task 里的 wait_for 又在等
   一个不响应取消的协程 —— 整个调度循环(连同自动追加、每日推送、其他任务)
   一起停 65 分钟。现在采集丢到后台任务里跑,循环照常 tick,reap_stale_runs
   也能按时回收。实测任务 25 最近 8 次运行有 4 次这样。
2. 超级编导自动追加在 10-25 因密码错误被停到 11-24(防锁号),新作品进不来。
   已把 25 号的调度钟拨回现在,补进了 10-31 那条。

标签矩阵:博主名字左边在粉丝数之后加「主页总获赞」和「第一条视频发布日期」
(查全库最早的作品时间,不受 7/14/30 天窗口限制)。
2026-11-02 10:24:36 +08:00

757 lines
28 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>标签矩阵 · 综合采集平台</title>
<!--
独立的报表页,不走 Vite 构建:这样它不会被前端产物覆盖,也不用为它加一条路由。
数据来自 /api/monitor/matrix(同一套鉴权,靠同源 cookie)。
转置在前端做 —— MySQL 5.7 没有 PIVOT,列数又随博主变化,SQL 里拼不出来。
-->
<style>
:root {
--bg: #0a0e14;
--bg-soft: #111823;
--line: #1e2a3a;
--text: #e6f1ff;
--muted: #7b8ba3;
--cyan: #22d3ee;
--pink: #f472b6;
}
* { box-sizing: border-box; }
body {
margin: 0;
background: var(--bg);
color: var(--text);
font: 13px/1.5 system-ui, -apple-system, "Segoe UI", "PingFang SC",
"Microsoft YaHei", sans-serif;
}
header {
position: sticky;
top: 0;
z-index: 20;
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
padding: 12px 16px;
/* 刻意不用 backdrop-filter:这个表头是 sticky 的,滚动时模糊要每帧重算,
在没有 GPU 加速的机器上很贵。纯色就够。 */
background: #0b1118;
border-bottom: 1px solid var(--line);
}
h1 { font-size: 15px; font-weight: 600; margin: 0; }
h1 span { color: var(--cyan); }
.spacer { flex: 1; }
label { color: var(--muted); font-size: 12px; }
select, input, button, a.btn {
background: var(--bg-soft);
color: var(--text);
border: 1px solid var(--line);
border-radius: 6px;
padding: 5px 9px;
font: inherit;
font-size: 12px;
cursor: pointer;
text-decoration: none;
}
select { max-width: 200px; }
input[type="number"] { width: 78px; cursor: text; }
select:hover, input:hover, button:hover, a.btn:hover { border-color: var(--cyan); }
input:focus { outline: none; border-color: var(--cyan); }
#status { color: var(--muted); font-size: 12px; }
#status.error { color: var(--pink); }
.wrap { overflow: auto; max-height: calc(100vh - 58px); }
table {
border-collapse: separate;
border-spacing: 0;
font-size: 12px;
}
thead th {
position: sticky;
top: 0;
z-index: 10;
background: #0d131c;
border-bottom: 1px solid var(--line);
border-right: 1px solid var(--line);
padding: 8px 10px;
text-align: left;
white-space: nowrap;
font-weight: 500;
color: var(--muted);
}
/* 左上角那一格要同时钉住两个方向 */
thead th:first-child {
left: 0;
z-index: 15;
color: var(--cyan);
}
tbody th {
position: sticky;
left: 0;
z-index: 5;
background: #0d131c;
border-right: 1px solid var(--line);
border-bottom: 1px solid var(--line);
padding: 7px 10px;
text-align: left;
white-space: nowrap;
font-weight: 500;
}
tbody td {
position: relative;
border-right: 1px solid var(--line);
border-bottom: 1px solid var(--line);
/* 底部留一条给左下角的作品条数,别让它压到标签上。 */
padding: 4px 8px 15px;
white-space: nowrap;
vertical-align: top;
min-width: 112px;
}
tbody tr:hover th, tbody tr:hover td { background: #131c28; }
/* 一格的主角:点赞 / 评论。标签是分类,数字才是「这条发得怎么样」。 */
.cell-metrics {
display: flex;
gap: 8px;
margin-bottom: 2px;
font-variant-numeric: tabular-nums;
line-height: 1.4;
}
.cell-metrics span { font-size: 12px; }
.cell-metrics b {
margin-right: 2px;
color: var(--muted);
font-weight: 400;
font-size: 10px;
}
.cell-metrics .like { color: var(--pink); }
.cell-metrics .comment { color: var(--cyan); }
/* 左下角的作品条数:是参照信息,压暗、贴角,不参与主视觉。 */
.cell-count {
position: absolute;
left: 5px;
bottom: 1px;
min-width: 14px;
padding: 0 3px;
border-radius: 3px;
background: rgba(123, 139, 163, 0.14);
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
line-height: 1.5;
}
.tag {
display: inline-block;
margin: 1px 3px 1px 0;
padding: 1px 6px;
border-radius: 999px;
border: 1px solid rgba(34, 211, 238, 0.35);
background: rgba(34, 211, 238, 0.1);
color: var(--cyan);
font-size: 11px;
}
.tag:nth-of-type(2n) {
border-color: rgba(244, 114, 182, 0.35);
background: rgba(244, 114, 182, 0.1);
color: var(--pink);
}
.untagged { color: var(--muted); font-size: 11px; }
.empty { color: #2a3646; }
/* 博主名字左边的参照信息:粉丝 / 总获赞 / 第一条视频日期。
都压暗、贴边,不该和名字抢视线。 */
.fans, .likes, .first {
display: inline-block;
min-width: 36px;
margin-right: 6px;
padding: 0 4px;
border-radius: 4px;
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
text-align: right;
}
.fans { background: rgba(34, 211, 238, 0.08); }
.likes { background: rgba(244, 114, 182, 0.08); }
.first {
min-width: 70px;
background: rgba(123, 139, 163, 0.10);
text-align: center;
font-size: 10px;
}
/* 条件格式命中的格子。用 inset 描边而不是 box-shadow 扩散:不改变布局,
也不会触发整行重排。 */
tbody td.hit {
background: rgba(244, 114, 182, 0.16);
box-shadow: inset 0 0 0 1px rgba(244, 114, 182, 0.55);
}
tbody tr:hover td.hit { background: rgba(244, 114, 182, 0.24); }
.legend {
display: flex;
gap: 14px;
flex-wrap: wrap;
padding: 8px 16px 14px;
color: var(--muted);
font-size: 11px;
border-top: 1px solid var(--line);
}
.legend b { color: var(--text); font-weight: 500; }
#login {
display: none;
margin: 40px auto;
max-width: 360px;
padding: 20px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--bg-soft);
text-align: center;
color: var(--muted);
}
#login a { color: var(--cyan); }
/* 悬停提示。跟着格子走。**可交互**:里面的标题是链接,要能点。
防闪烁靠「离开格子后延迟 250ms 再收」+ 进入提示就取消(见 scheduleHide)。 */
#tip {
display: none;
position: fixed;
z-index: 100;
pointer-events: auto;
max-width: 320px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: #0f1620;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
font-size: 11px;
line-height: 1.7;
color: var(--text);
}
#tip .tip-head { color: var(--cyan); font-weight: 600; margin-bottom: 2px; }
#tip .tip-metrics { font-variant-numeric: tabular-nums; }
#tip .tip-metrics span { margin-right: 10px; white-space: nowrap; }
#tip .tip-metrics b { color: var(--muted); font-weight: 400; }
#tip .tip-tags { color: var(--muted); }
/* 逐条明细:并集看不出「哪个标签属于哪条视频」,这里补上。 */
#tip .tip-notes {
margin-top: 5px;
padding-top: 5px;
border-top: 1px solid var(--line);
white-space: normal;
}
#tip .tip-note { margin-bottom: 4px; line-height: 1.55; }
#tip .tip-note-title { color: var(--text); }
/* 标题就是作品链接,点一下在抖音打开。 */
#tip a.tip-note-title {
color: var(--cyan);
text-decoration: none;
cursor: pointer;
}
#tip a.tip-note-title:hover { text-decoration: underline; }
/* 每条作品自己的指标。缩进到标题文字下面,一眼对得上是哪条。 */
#tip .tip-note-meta {
padding-left: 14px;
color: var(--muted);
font-variant-numeric: tabular-nums;
}
#tip .tip-note-metrics { margin-right: 8px; }
#tip .tip-note-tags { color: var(--pink); }
#tip .tip-note-more { color: var(--muted); }
</style>
</head>
<body>
<header>
<h1>标签<span>矩阵</span></h1>
<label>平台
<select id="platform">
<option value="dy">抖音</option>
<option value="xhs">小红书</option>
</select>
</label>
<label>任务
<select id="task">
<option value="">全部任务</option>
</select>
</label>
<label>范围
<select id="days">
<option value="7">最近 7 天</option>
<option value="14">最近 14 天</option>
<option value="30">最近 30 天</option>
</select>
</label>
<label title="满足条件的格子会高亮">高亮
<select id="cfMetric">
<option value="likes">点赞</option>
<option value="comments">评论</option>
<option value="collected">收藏</option>
<option value="shares">分享</option>
<option value="count">条数</option>
</select>
≥ <input id="cfValue" type="number" min="0" step="1" placeholder="数值" />
</label>
<button id="refresh">刷新</button>
<span id="status"></span>
<span class="spacer"></span>
<a class="btn" href="/">返回控制台</a>
</header>
<div id="login">
请先在<a href="/">控制台</a>登录,再回到这个页面。
</div>
<div class="wrap" id="wrap"></div>
<div id="tip"></div>
<div class="legend">
<span><b>名字左边</b> = 粉丝数 / 主页总获赞 / 第一条视频发布日期</span>
<span><b>赞 / 评</b> = 当天这些作品的点赞 / 评论</span>
<span><b>左下角数字</b> = 当天作品条数</span>
<span><b>悬停后点标题</b> = 在抖音打开这条作品</span>
<span><b>无标签</b> = 发了但没打标签</span>
<span><b>空白</b> = 当天没发作品</span>
<span>鼠标停到格子上可以看当天的点赞 / 评论 / 收藏 / 分享</span>
</div>
<script>
const params = new URLSearchParams(location.search);
const platformSel = document.getElementById('platform');
const taskSel = document.getElementById('task');
const daysSel = document.getElementById('days');
const statusEl = document.getElementById('status');
const wrap = document.getElementById('wrap');
const login = document.getElementById('login');
const tip = document.getElementById('tip');
const cfMetricSel = document.getElementById('cfMetric');
const cfValueInput = document.getElementById('cfValue');
// 条件格式只在本地重渲染,不重新请求 —— 指标已经随格子一起返回了。
let lastData = null;
// 离开格子之后延迟收起提示的计时器:提示里的标题是链接,鼠标要从格子挪进去点。
let hideTimer = null;
function setStatus(text, isError) {
statusEl.textContent = text;
statusEl.className = isError ? 'error' : '';
}
function fmt(value) {
if (value === null || value === undefined) return '—';
return Number(value).toLocaleString('zh-CN');
}
/** 格子里的数字要短:一万以上用「万」,否则一行放不下赞和评两个数。 */
function compact(value) {
if (value === null || value === undefined) return '—';
const number = Number(value);
if (!Number.isFinite(number)) return '—';
if (Math.abs(number) >= 100000000) {
return (number / 100000000).toFixed(1).replace(/\.0$/, '') + '亿';
}
if (Math.abs(number) >= 10000) {
return (number / 10000).toFixed(1).replace(/\.0$/, '') + '万';
}
return number.toLocaleString('zh-CN');
}
function dateParts(ms) {
const date = new Date(ms);
const pad = (value) => String(value).padStart(2, '0');
return {
date: date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()),
time: pad(date.getHours()) + ':' + pad(date.getMinutes()),
};
}
function metricNode(label, kind, value) {
const span = document.createElement('span');
span.className = kind;
const name = document.createElement('b');
name.textContent = label;
span.appendChild(name);
span.appendChild(document.createTextNode(compact(value)));
return span;
}
function hideTip() {
clearTimeout(hideTimer);
tip.style.display = 'none';
}
/** 离开格子时不立刻收 —— 留一点时间让鼠标移进提示里点标题。 */
function scheduleHide() {
clearTimeout(hideTimer);
hideTimer = setTimeout(hideTip, 250);
}
function cfThreshold() {
const raw = cfValueInput.value.trim();
if (raw === '') return null;
const value = Number(raw);
return Number.isFinite(value) ? value : null;
}
function cfHit(cell) {
if (!cell) return false;
const threshold = cfThreshold();
if (threshold === null) return false;
const value = cell[cfMetricSel.value];
return value !== null && value !== undefined && value >= threshold;
}
function syncCfUrl() {
const url = new URL(location.href);
const threshold = cfThreshold();
if (threshold === null) {
url.searchParams.delete('cf');
} else {
url.searchParams.set('cf', cfMetricSel.value + ':' + threshold);
}
history.replaceState(null, '', url);
}
function buildTip(cell, day, creatorName) {
// 用 DOM 拼,不用 innerHTML —— 昵称是平台来的文本,不能当 HTML 解释。
tip.textContent = '';
const head = document.createElement('div');
head.className = 'tip-head';
head.textContent = day + ' · ' + creatorName;
tip.appendChild(head);
const metrics = document.createElement('div');
metrics.className = 'tip-metrics';
const pairs = [
['条数', cell.count],
['点赞', cell.likes],
['评论', cell.comments],
['收藏', cell.collected],
['分享', cell.shares],
];
for (const [label, value] of pairs) {
const span = document.createElement('span');
const name = document.createElement('b');
name.textContent = label;
span.appendChild(name);
span.appendChild(document.createTextNode(' ' + fmt(value)));
metrics.appendChild(span);
}
tip.appendChild(metrics);
const tags = document.createElement('div');
tags.className = 'tip-tags';
tags.textContent =
'标签:' + (cell.tags && cell.tags.length ? cell.tags.join(' / ') : '无标签');
tip.appendChild(tags);
// 逐条列出作品标题和它自己的标签。并集看不出「哪个标签属于哪条视频」,
// 一条作品时也一样 —— 标题本身就是要看的东西,不能只在两条以上时才给。
const details = cell.details || [];
if (details.length > 0) {
const list = document.createElement('div');
list.className = 'tip-notes';
const limit = 12;
for (let index = 0; index < Math.min(details.length, limit); index += 1) {
const item = details[index];
const row = document.createElement('div');
row.className = 'tip-note';
// 标题就是链接:点一下跳去抖音看原视频。没有地址的老数据就还是纯文字。
const titleText = index + 1 + '. ' + item.title;
let title;
if (item.url) {
title = document.createElement('a');
title.href = item.url;
title.target = '_blank';
title.rel = 'noopener noreferrer';
title.title = '在抖音打开';
} else {
title = document.createElement('div');
}
title.className = 'tip-note-title';
title.textContent = titleText;
row.appendChild(title);
const meta = document.createElement('div');
meta.className = 'tip-note-meta';
const metrics = document.createElement('span');
metrics.className = 'tip-note-metrics';
metrics.textContent =
'赞 ' + compact(item.likes) +
' · 评 ' + compact(item.comments) +
' · 藏 ' + compact(item.collected) +
' · 转 ' + compact(item.shares);
meta.appendChild(metrics);
const noteTags = document.createElement('span');
noteTags.className = 'tip-note-tags';
noteTags.textContent =
item.tags && item.tags.length ? item.tags.join(' / ') : '无标签';
meta.appendChild(noteTags);
row.appendChild(meta);
list.appendChild(row);
}
if (details.length > limit) {
const more = document.createElement('div');
more.className = 'tip-note-more';
more.textContent = '…还有 ' + (details.length - limit) + ' 条';
list.appendChild(more);
}
tip.appendChild(list);
}
}
function showTip(event) {
const td = event.currentTarget;
const cell = td._cell;
if (!cell) return;
clearTimeout(hideTimer);
buildTip(cell, td.dataset.day, td.dataset.creator);
tip.style.display = 'block';
// 先显示再量尺寸,否则 offsetWidth 是 0。
const rect = td.getBoundingClientRect();
const width = tip.offsetWidth;
const height = tip.offsetHeight;
let left = rect.left + rect.width / 2 - width / 2;
left = Math.max(8, Math.min(left, window.innerWidth - width - 8));
let top = rect.bottom + 8;
if (top + height > window.innerHeight - 8) {
top = rect.top - height - 8;
}
tip.style.left = left + 'px';
tip.style.top = Math.max(8, top) + 'px';
}
function cellNode(cell, day, creatorName) {
const td = document.createElement('td');
if (!cell) {
const dot = document.createElement('span');
dot.className = 'empty';
dot.textContent = '·';
td.appendChild(dot);
return td;
}
// 主角:点赞 / 评论。数字未知时显示「—」,不是 0。
const metrics = document.createElement('div');
metrics.className = 'cell-metrics';
metrics.appendChild(metricNode('赞', 'like', cell.likes));
metrics.appendChild(metricNode('评', 'comment', cell.comments));
td.appendChild(metrics);
if (!cell.tags || cell.tags.length === 0) {
const none = document.createElement('span');
none.className = 'untagged';
none.textContent = '无标签';
td.appendChild(none);
} else {
for (const tag of cell.tags) {
const chip = document.createElement('span');
chip.className = 'tag';
chip.textContent = tag;
td.appendChild(chip);
}
}
// 作品条数放左下角。
const count = document.createElement('span');
count.className = 'cell-count';
count.textContent = cell.count;
count.title = '当天共 ' + cell.count + ' 条作品';
td.appendChild(count);
td._cell = cell;
td.dataset.day = day;
td.dataset.creator = creatorName;
if (cfHit(cell)) td.classList.add('hit');
td.addEventListener('mouseenter', showTip);
td.addEventListener('mouseleave', scheduleHide);
return td;
}
function render(data) {
lastData = data;
wrap.innerHTML = '';
if (!data.dates.length) {
setStatus('这个范围内没有作品');
return;
}
const scope =
data.task_id && taskSel.selectedOptions.length
? ' · ' + taskSel.selectedOptions[0].textContent
: '';
setStatus(data.dates.length + ' 天 · ' + data.creators.length + ' 个博主' + scope);
const table = document.createElement('table');
const thead = document.createElement('thead');
const headRow = document.createElement('tr');
// 左上角那一格钉住两个方向 —— 横着滚日期、竖着滚博主都要能看到它。
const corner = document.createElement('th');
corner.textContent = '博主 \\ 日期';
headRow.appendChild(corner);
for (const day of data.dates) {
const th = document.createElement('th');
th.textContent = day;
headRow.appendChild(th);
}
thead.appendChild(headRow);
table.appendChild(thead);
const tbody = document.createElement('tbody');
for (const creator of data.creators) {
const tr = document.createElement('tr');
const th = document.createElement('th');
// 账号池给的状态挂在名字后面的括号里,例如「周周逛苏州(账号异常)」。
const label = creator.account_status
? creator.name + '(' + creator.account_status + ')'
: creator.name;
// 粉丝数放在**名字左边**,一眼能看出这个号多大。没有快照的博主
// (作品模式刚导入还没跑过一轮)就不画 —— 不写 0,那会被读成「没有粉丝」。
const fansKnown = creator.fans !== null && creator.fans !== undefined;
const likesKnown =
creator.total_favorited !== null && creator.total_favorited !== undefined;
const firstAt = creator.first_published_at;
const titleParts = [];
if (fansKnown) {
const fans = document.createElement('span');
fans.className = 'fans';
fans.textContent = compact(creator.fans);
fans.title = '粉丝数';
th.appendChild(fans);
titleParts.push(fmt(creator.fans) + ' 粉丝');
}
if (likesKnown) {
// 主页上显示的那个「获赞总数」。
const likes = document.createElement('span');
likes.className = 'likes';
likes.textContent = compact(creator.total_favorited);
likes.title = '主页总获赞';
th.appendChild(likes);
titleParts.push(fmt(creator.total_favorited) + ' 总获赞');
}
if (firstAt) {
const parts = dateParts(firstAt);
const first = document.createElement('span');
first.className = 'first';
first.textContent = parts.date;
first.title = '第一条视频发布于 ' + parts.date + ' ' + parts.time;
th.appendChild(first);
titleParts.push('第一条视频 ' + parts.date);
}
const nameSpan = document.createElement('span');
nameSpan.textContent = label;
th.appendChild(nameSpan);
titleParts.push(label);
th.title = titleParts.join(' · ');
tr.appendChild(th);
for (const day of data.dates) {
const rowCells = data.cells[day] || {};
tr.appendChild(cellNode(rowCells[creator.hash], day, label));
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
wrap.appendChild(table);
}
async function loadTasks() {
taskSel.innerHTML = '<option value="">全部任务</option>';
try {
const response = await fetch(
'/api/monitor/tasks?platform=' + encodeURIComponent(platformSel.value),
{ credentials: 'same-origin' },
);
if (!response.ok) return;
const data = await response.json();
for (const task of data.tasks || []) {
const option = document.createElement('option');
option.value = String(task.id);
option.textContent = task.name;
taskSel.appendChild(option);
}
} catch (err) {
// 任务列表拉不到不该挡住看矩阵,保持「全部任务」即可。
}
}
async function load() {
const platform = platformSel.value;
const days = daysSel.value;
const taskId = taskSel.value;
// 把当前选择写进地址栏,方便直接收藏/分享到某一组条件
const url = new URL(location.href);
url.searchParams.set('platform', platform);
url.searchParams.set('days', days);
if (taskId) url.searchParams.set('task_id', taskId);
else url.searchParams.delete('task_id');
history.replaceState(null, '', url);
setStatus('加载中…');
wrap.innerHTML = '';
hideTip();
login.style.display = 'none';
try {
const query = new URLSearchParams({ platform: platform, days: days });
if (taskId) query.set('task_id', taskId);
const response = await fetch('/api/monitor/matrix?' + query.toString(), {
credentials: 'same-origin',
});
if (response.status === 401) {
login.style.display = 'block';
setStatus('未登录', true);
return;
}
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
render(await response.json());
} catch (err) {
setStatus('加载失败:' + err.message, true);
}
}
async function init() {
platformSel.value = params.get('platform') || 'dy';
daysSel.value = params.get('days') || '14';
await loadTasks();
const wanted = params.get('task_id');
if (wanted && Array.from(taskSel.options).some((o) => o.value === wanted)) {
taskSel.value = wanted;
}
const cf = params.get('cf');
if (cf && cf.includes(':')) {
const parts = cf.split(':');
const allowed = ['likes', 'comments', 'collected', 'shares', 'count'];
if (allowed.includes(parts[0])) cfMetricSel.value = parts[0];
cfValueInput.value = parts[1];
}
await load();
}
document.getElementById('refresh').addEventListener('click', load);
platformSel.addEventListener('change', async () => {
await loadTasks();
await load();
});
taskSel.addEventListener('change', load);
daysSel.addEventListener('change', load);
// 条件格式只重画表格,不重新请求数据。
const rerender = () => {
if (lastData) render(lastData);
syncCfUrl();
};
cfMetricSel.addEventListener('change', rerender);
cfValueInput.addEventListener('input', rerender);
// 表格滚动时把提示收起来,否则它会停在原地、和格子脱节。
wrap.addEventListener('scroll', hideTip);
// 鼠标移进提示里(点标题)就取消收起;移出去再收。
tip.addEventListener('mouseenter', () => clearTimeout(hideTimer));
tip.addEventListener('mouseleave', hideTip);
init();
</script>
</body>
</html>