feat(report): 格子悬停显示当天点赞/评论/收藏/分享;报表可按任务过滤
两件事: 1. **格子悬停提示**(matrix.html) 每个有数据的格子挂一个自定义浮层,显示「日期 · 博主 / 条数 / 点赞 / 评论 / 收藏 / 分享 / 标签」。浮层 `pointer-events: none`,否则它会盖住格子触发 mouseleave,自己闪个不停;表格滚动时主动收起。内容用 DOM 拼而不是 innerHTML —— 昵称来自平台,不能当 HTML 解释。 2. **任务过滤**(/api/monitor/matrix 增加 task_id) 平台下拉旁边加「任务」下拉,数据来自现有的 /api/monitor/tasks。 `task_id` 会先校验平台归属,传错平台宁可返回空,也不给出别的平台的数。 选择会写进地址栏,可以直接收藏某一组条件。 指标口径:每篇作品取**最近一轮**快照(run_id 最大)再按格子求和。未知是 None 不是 0 —— 提示里显示成「—」,避免把「没读到」说成「没有互动」。 测试:10 条(新增指标求和取最新一轮、未知为 None、任务过滤、跨平台任务被拒)。
This commit is contained in:
+169
-26
@@ -18,9 +18,7 @@
|
||||
--text: #e6f1ff;
|
||||
--muted: #7b8ba3;
|
||||
--cyan: #22d3ee;
|
||||
--green: #34d399;
|
||||
--pink: #f472b6;
|
||||
--orange: #fb923c;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
@@ -59,6 +57,7 @@
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
}
|
||||
select { max-width: 200px; }
|
||||
select:hover, button:hover, a.btn:hover { border-color: var(--cyan); }
|
||||
#status { color: var(--muted); font-size: 12px; }
|
||||
#status.error { color: var(--pink); }
|
||||
@@ -99,7 +98,6 @@
|
||||
text-align: left;
|
||||
white-space: nowrap;
|
||||
font-weight: 500;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
tbody td {
|
||||
border-right: 1px solid var(--line);
|
||||
@@ -107,7 +105,7 @@
|
||||
padding: 5px 8px;
|
||||
white-space: nowrap;
|
||||
vertical-align: top;
|
||||
min-width: 88px;
|
||||
min-width: 92px;
|
||||
}
|
||||
tbody tr:hover th, tbody tr:hover td { background: #131c28; }
|
||||
|
||||
@@ -157,6 +155,29 @@
|
||||
color: var(--muted);
|
||||
}
|
||||
#login a { color: var(--cyan); }
|
||||
|
||||
/* 悬停提示。跟着格子走,但不吃鼠标事件 —— 否则它一出现就会盖住格子、
|
||||
触发 mouseleave,然后自己消失,来回闪。 */
|
||||
#tip {
|
||||
display: none;
|
||||
position: fixed;
|
||||
z-index: 100;
|
||||
pointer-events: none;
|
||||
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); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -168,6 +189,11 @@
|
||||
<option value="xhs">小红书</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>任务
|
||||
<select id="task">
|
||||
<option value="">全部任务</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>范围
|
||||
<select id="days">
|
||||
<option value="7">最近 7 天</option>
|
||||
@@ -186,30 +212,94 @@
|
||||
</div>
|
||||
|
||||
<div class="wrap" id="wrap"></div>
|
||||
<div id="tip"></div>
|
||||
<div class="legend">
|
||||
<span><b>N</b> = 当天发出的作品条数</span>
|
||||
<span><b>无标签</b> = 发了但没打标签</span>
|
||||
<span><b>空白</b> = 当天没发作品</span>
|
||||
<span>标签按词表里的顺序排列</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');
|
||||
|
||||
platformSel.value = params.get('platform') || 'dy';
|
||||
daysSel.value = params.get('days') || '14';
|
||||
const tip = document.getElementById('tip');
|
||||
|
||||
function setStatus(text, isError) {
|
||||
statusEl.textContent = text;
|
||||
statusEl.className = isError ? 'error' : '';
|
||||
}
|
||||
|
||||
function cellNode(cell) {
|
||||
function fmt(value) {
|
||||
if (value === null || value === undefined) return '—';
|
||||
return Number(value).toLocaleString('zh-CN');
|
||||
}
|
||||
|
||||
function hideTip() {
|
||||
tip.style.display = 'none';
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function showTip(event) {
|
||||
const td = event.currentTarget;
|
||||
const cell = td._cell;
|
||||
if (!cell) return;
|
||||
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');
|
||||
@@ -228,14 +318,20 @@
|
||||
none.className = 'untagged';
|
||||
none.textContent = '无标签';
|
||||
td.appendChild(none);
|
||||
return td;
|
||||
}
|
||||
for (const tag of cell.tags) {
|
||||
const chip = document.createElement('span');
|
||||
chip.className = 'tag';
|
||||
chip.textContent = tag;
|
||||
td.appendChild(chip);
|
||||
} else {
|
||||
for (const tag of cell.tags) {
|
||||
const chip = document.createElement('span');
|
||||
chip.className = 'tag';
|
||||
chip.textContent = tag;
|
||||
td.appendChild(chip);
|
||||
}
|
||||
}
|
||||
|
||||
td._cell = cell;
|
||||
td.dataset.day = day;
|
||||
td.dataset.creator = creatorName;
|
||||
td.addEventListener('mouseenter', showTip);
|
||||
td.addEventListener('mouseleave', hideTip);
|
||||
return td;
|
||||
}
|
||||
|
||||
@@ -245,7 +341,11 @@
|
||||
setStatus('这个范围内没有作品');
|
||||
return;
|
||||
}
|
||||
setStatus(`${data.dates.length} 天 · ${data.creators.length} 个博主`);
|
||||
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');
|
||||
@@ -271,7 +371,7 @@
|
||||
tr.appendChild(th);
|
||||
for (const day of data.dates) {
|
||||
const rowCells = data.cells[day] || {};
|
||||
tr.appendChild(cellNode(rowCells[creator.hash]));
|
||||
tr.appendChild(cellNode(rowCells[creator.hash], day, creator.name));
|
||||
}
|
||||
tbody.appendChild(tr);
|
||||
}
|
||||
@@ -279,41 +379,84 @@
|
||||
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 response = await fetch(
|
||||
`/api/monitor/matrix?platform=${encodeURIComponent(platform)}&days=${days}`,
|
||||
{ credentials: 'same-origin' },
|
||||
);
|
||||
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}`);
|
||||
throw new Error('HTTP ' + response.status);
|
||||
}
|
||||
render(await response.json());
|
||||
} catch (err) {
|
||||
setStatus(`加载失败:${err.message}`, true);
|
||||
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;
|
||||
}
|
||||
await load();
|
||||
}
|
||||
|
||||
document.getElementById('refresh').addEventListener('click', load);
|
||||
platformSel.addEventListener('change', load);
|
||||
platformSel.addEventListener('change', async () => {
|
||||
await loadTasks();
|
||||
await load();
|
||||
});
|
||||
taskSel.addEventListener('change', load);
|
||||
daysSel.addEventListener('change', load);
|
||||
load();
|
||||
// 表格滚动时把提示收起来,否则它会停在原地、和格子脱节。
|
||||
wrap.addEventListener('scroll', hideTip);
|
||||
init();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user