feat: 监控面板 / 登录鉴权 / 多平台切换 / MySQL
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

在上游 MediaCrawler 之上新增一层:

- 监控层 api/monitor/ —— 多博主/多笔记的定时采集、指标快照差分、报表、
  企业微信通知。每轮采集写入独立目录,差分才成立。
- WebUI 登录鉴权 api/auth.py —— PBKDF2 口令 + 服务端会话,/api 全接口防护。
  WebSocket 单独加依赖:BaseHTTPMiddleware 对 ws 作用域直接放行,覆盖不到。
- 全局平台切换 + 能力矩阵 —— 如实区分「爬虫模块支持」与「监控层已接线」,
  未接通的平台直接拒绝建任务,而不是静默跑空。
- 监控库改用 MySQL 5.7(可回退 SQLite 供测试):逐表强制 utf8mb4
  (服务端与库默认都是 latin1),启动校验所连 schema 以防写错库,
  连接池 recycle + pre_ping 应对 MySQL 的 8 小时空闲断连。

修复上游缺陷:

- xhs/core.py: 主页抓取失败会跳掉整个博主,导致一条作品都抓不到,
  而那份资料只喂给一个空函数。改为尽力而为,失败不中断。
- xhs/login.py: cookie 登录只注入 web_session,冷启动签名会失败。
  新增 INJECT_ALL_COOKIES 开关(默认关闭,原有行为不变)。
- requirements.txt: 补上 websockets。它在上游 pyproject.toml 里有声明、
  这里漏了,导致 uvicorn 没有 WebSocket 能力,实时日志流从未工作。

改动过的上游文件清单及合并方式见 UPSTREAM.md。

测试:492 passed(另有 1 个既有的 Windows/gbk 上游测试失败,与本改动无关)
This commit is contained in:
2026-10-07 09:58:40 +08:00
parent 5d547f4586
commit 4e60524f37
88 changed files with 13224 additions and 436 deletions
@@ -0,0 +1,305 @@
import { useMemo, useState } from 'react'
import {
ChevronDown,
ChevronRight,
Download,
ExternalLink,
LayoutList,
Rows3,
ThumbsUp,
} from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import {
useCommentNoteOptions,
useMonitorComments,
useMonitorCommentsGrouped,
} from '@/hooks/useMonitor'
import { monitorApi } from '@/lib/api'
import { formatRelative } from '@/lib/monitorFormat'
import type { CommentBucket, MonitorComment } from '@/types/monitor'
interface CommentsFeedProps {
taskId: number | null
}
const ALL_NOTES = '__all__'
/** Cover + truncated title + link, identifying the work a comment sits under. */
function NoteBadge({
title,
cover,
url,
noteId,
compact = false,
}: {
title: string
cover: string
url: string
noteId: string
compact?: boolean
}) {
return (
<div className="flex items-center gap-2 min-w-0">
{cover ? (
<img
src={cover}
alt=""
loading="lazy"
className={`rounded object-cover bg-cyber-bg-tertiary flex-shrink-0 ${
compact ? 'w-6 h-8' : 'w-8 h-10'
}`}
/>
) : (
<div
className={`rounded bg-cyber-bg-tertiary flex-shrink-0 ${
compact ? 'w-6 h-8' : 'w-8 h-10'
}`}
/>
)}
<span className="truncate text-[10px] font-mono text-cyber-text-secondary" title={title || noteId}>
{title || noteId}
</span>
{url && (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
className="text-cyber-text-muted hover:text-cyber-neon-cyan flex-shrink-0"
title="打开原文"
>
<ExternalLink className="w-3 h-3" />
</a>
)}
</div>
)
}
function CommentRow({ comment, showNote }: { comment: MonitorComment; showNote: boolean }) {
return (
<div className="rounded-md border border-cyber-border-subtle bg-cyber-bg-tertiary/40 px-3 py-2">
<div className="flex items-center justify-between gap-2 text-[10px] font-mono">
<span className="text-cyber-text-secondary truncate">{comment.nickname || '匿名'}</span>
<span className="flex items-center gap-2 flex-shrink-0 text-cyber-text-muted">
{comment.like_count !== null && (
<span className="flex items-center gap-1">
<ThumbsUp className="w-3 h-3" />
{comment.like_count}
</span>
)}
{comment.sub_comment_count > 0 && <span>{comment.sub_comment_count} 条回复</span>}
<span>{formatRelative(comment.first_seen_at)}</span>
</span>
</div>
<p className="mt-1 text-[11px] font-mono text-cyber-text-primary break-words">
{comment.content}
</p>
{/* Only in the flat view: in a grouped view the header already says which
work these belong to, so repeating it on every row is noise. */}
{showNote && (
<div className="mt-1.5 pt-1.5 border-t border-cyber-border-subtle">
<NoteBadge
title={comment.note_title}
cover={comment.note_cover}
url={comment.note_url}
noteId={comment.note_id}
compact
/>
</div>
)}
</div>
)
}
function CollapsibleGroup({
bucket,
open,
onToggle,
}: {
bucket: CommentBucket
open: boolean
onToggle: () => void
}) {
return (
<div className="rounded-lg border border-cyber-border-subtle overflow-hidden">
<button
onClick={onToggle}
className="w-full flex items-center gap-2 px-3 py-2 bg-cyber-bg-tertiary/60 hover:bg-cyber-bg-elevated/60 transition-colors text-left"
>
{open ? (
<ChevronDown className="w-3.5 h-3.5 text-cyber-text-muted flex-shrink-0" />
) : (
<ChevronRight className="w-3.5 h-3.5 text-cyber-text-muted flex-shrink-0" />
)}
<div className="flex-1 min-w-0">
<NoteBadge
title={bucket.note_title}
cover={bucket.note_cover}
url={bucket.note_url}
noteId={bucket.note_id}
/>
</div>
<Badge variant="outline" className="text-[10px] px-1.5 py-0 flex-shrink-0">
{bucket.comments.length} 条
</Badge>
</button>
{open && (
<div className="p-2 space-y-1.5">
{bucket.comments.map((comment) => (
<CommentRow
key={comment.comment_id}
comment={comment}
showNote={false}
/>
))}
</div>
)}
</div>
)
}
export function CommentsFeed({ taskId }: CommentsFeedProps) {
const [grouped, setGrouped] = useState(true)
const [noteFilter, setNoteFilter] = useState<string>(ALL_NOTES)
const [openNotes, setOpenNotes] = useState<Set<string> | null>(null)
const activeNote = noteFilter === ALL_NOTES ? null : noteFilter
const { data: noteOptions } = useCommentNoteOptions(taskId)
// Both hooks always run; react-query caches, so the inactive one costs one
// request and no rerender churn when toggling views.
const { data: groups, isLoading: loadingGroups } = useMonitorCommentsGrouped(taskId, activeNote)
const { data: flat, isLoading: loadingFlat } = useMonitorComments(taskId, activeNote)
// Expand the newest group only, so the panel does not open as a wall of text.
const expanded = useMemo(() => {
if (openNotes !== null) return openNotes
return new Set(groups && groups.length ? [groups[0].note_id] : [])
}, [openNotes, groups])
const toggleGroup = (noteId: string) => {
const next = new Set(expanded)
if (next.has(noteId)) next.delete(noteId)
else next.add(noteId)
setOpenNotes(next)
}
const total = grouped ? (groups ?? []).reduce((sum, g) => sum + g.comments.length, 0) : (flat ?? []).length
const isLoading = grouped ? loadingGroups : loadingFlat
if (taskId === null) {
return (
<p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">
请先选择一个监控任务
</p>
)
}
return (
<div className="space-y-2">
{/* Toolbar */}
<div className="flex items-center gap-2 flex-wrap">
<Button
variant={grouped ? 'default' : 'outline'}
size="sm"
onClick={() => {
setGrouped(true)
// Re-derive the default expansion for the new view.
setOpenNotes(null)
}}
>
<Rows3 className="w-3 h-3 mr-1" />
按作品分组
</Button>
<Button
variant={grouped ? 'outline' : 'default'}
size="sm"
onClick={() => setGrouped(false)}
>
<LayoutList className="w-3 h-3 mr-1" />
平铺
</Button>
<Select value={noteFilter} onValueChange={setNoteFilter}>
<SelectTrigger className="h-8 text-[11px] w-[240px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value={ALL_NOTES}>全部作品</SelectItem>
{noteOptions?.map((note) => (
<SelectItem key={note.note_id} value={note.note_id}>
{`${note.note_title || note.note_id} (${note.comment_count})`}
</SelectItem>
))}
</SelectContent>
</Select>
<span className="text-[10px] font-mono text-cyber-text-muted">共 {total} 条</span>
<Button
variant="outline"
size="sm"
className="ml-auto"
onClick={() =>
window.open(
monitorApi.getExportUrl({
kind: 'comments',
taskId: taskId ?? undefined,
noteId: activeNote ?? undefined,
}),
'_blank',
)
}
>
<Download className="w-3 h-3 mr-1" />
导出 CSV
</Button>
</div>
{/* Honest about what is NOT here. */}
<p className="text-[10px] font-mono text-cyber-neon-orange">
评论接口没有时间排序,这里只有平台默认排序下抓取到的前 N 条,
<span className="font-bold">沉底的新评论不会出现</span>。可在任务里调大「每篇评论抓取条数」。
</p>
{isLoading ? (
<p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">加载中…</p>
) : total === 0 ? (
<p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">
还没有采集到评论
</p>
) : grouped ? (
<div className="space-y-2">
{groups?.map((bucket) => (
<CollapsibleGroup
key={bucket.note_id}
bucket={bucket}
open={expanded.has(bucket.note_id)}
onToggle={() => toggleGroup(bucket.note_id)}
/>
))}
</div>
) : (
<div className="space-y-1.5">
{flat?.map((comment) => (
<CommentRow
key={`${comment.note_id}:${comment.comment_id}`}
comment={comment}
showNote
/>
))}
</div>
)}
</div>
)
}
@@ -0,0 +1,133 @@
import { useState } from 'react'
import { AlertTriangle, CheckCircle2, KeyRound, Trash2 } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { useClearCookie, useCookieStatus, useSetCookie } from '@/hooks/useMonitor'
import { useCurrentPlatform } from '@/hooks/usePlatform'
import { formatDateTime, formatRelative } from '@/lib/monitorFormat'
/**
* Cookie health for unattended runs.
*
* The expiry signal that matters is `last_ok_at` (the last run that actually
* authenticated), not `updated_at`: a cookie can be saved long ago and still be
* working, or be fresh and already rejected.
*/
export function CookiePanel() {
const { capability, platform } = useCurrentPlatform()
// Scoped to the selected platform: each keeps its own login.
const { data: status } = useCookieStatus()
const setCookie = useSetCookie()
const clearCookie = useClearCookie()
const [draft, setDraft] = useState('')
const [editing, setEditing] = useState(false)
const showForm = !status?.present || editing
const label = capability?.label ?? platform
return (
<div className="rounded-lg glass-panel float-panel p-4 space-y-3">
<div className="flex items-center justify-between gap-3">
<div className="flex items-center gap-2">
<KeyRound className="w-4 h-4 text-cyber-neon-cyan" />
<span className="font-mono text-xs tracking-wider text-cyber-text-primary">
{label}登录态
</span>
{status?.present ? (
<Badge variant="success" className="text-[10px]">
已配置
</Badge>
) : (
<Badge variant="warning" className="text-[10px]">
未配置
</Badge>
)}
</div>
<div className="flex items-center gap-2">
{status?.present && !editing && (
<>
<Button variant="outline" size="sm" onClick={() => setEditing(true)}>
替换
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => clearCookie.mutate()}
disabled={clearCookie.isPending}
>
<Trash2 className="w-3 h-3" />
</Button>
</>
)}
</div>
</div>
{status?.present && !editing && (
<div className="flex flex-wrap items-center gap-x-6 gap-y-1 text-[11px] font-mono text-cyber-text-secondary">
<span>
长度 <span className="text-cyber-neon-cyan">{status.length}</span>
</span>
<span>
保存于 <span className="text-cyber-text-primary">{formatDateTime(status.updated_at)}</span>
</span>
<span className="flex items-center gap-1">
最近成功
{status.last_ok_at ? (
<>
<CheckCircle2 className="w-3 h-3 text-cyber-neon-green" />
<span className="text-cyber-neon-green">{formatRelative(status.last_ok_at)}</span>
</>
) : (
<>
<AlertTriangle className="w-3 h-3 text-cyber-neon-orange" />
<span className="text-cyber-neon-orange">尚无成功记录</span>
</>
)}
</span>
</div>
)}
{showForm && (
<div className="space-y-2">
<p className="text-[11px] font-mono text-cyber-text-muted leading-relaxed">
定时监控需要长期有效的登录态。在浏览器登录{label}后,从开发者工具复制完整 Cookie
粘贴到此处(需包含 <span className="text-cyber-neon-cyan">web_session</span>、
<span className="text-cyber-neon-cyan">a1</span>、
<span className="text-cyber-neon-cyan">webId</span>)。
Cookie 通常数周后失效,届时任务会提示重新粘贴。该 Cookie 只用于{label}。
</p>
<textarea
value={draft}
onChange={(event) => setDraft(event.target.value)}
placeholder="web_session=xxxx; a1=xxxx; webId=xxxx; ..."
rows={4}
className="w-full rounded-md border border-cyber-border-DEFAULT bg-cyber-bg-tertiary p-2 font-mono text-[11px] text-cyber-text-primary outline-none focus-visible:border-cyber-neon-cyan/50"
/>
<div className="flex items-center gap-2">
<Button
size="sm"
disabled={!draft.trim() || setCookie.isPending}
onClick={() =>
setCookie.mutate(draft.trim(), {
onSuccess: () => {
setDraft('')
setEditing(false)
},
})
}
>
保存
</Button>
{status?.present && (
<Button variant="ghost" size="sm" onClick={() => setEditing(false)}>
取消
</Button>
)}
</div>
</div>
)}
</div>
)
}
+110
View File
@@ -0,0 +1,110 @@
import { CheckCheck, FilePlus2, MessageCirclePlus, TrendingUp, TriangleAlert } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { useMarkEventsRead, useMonitorEvents } from '@/hooks/useMonitor'
import { formatRelative } from '@/lib/monitorFormat'
import type { EventType } from '@/types/monitor'
interface EventsFeedProps {
taskId: number | null
}
const EVENT_META: Record<
EventType,
{ label: string; icon: typeof FilePlus2; variant: 'success' | 'warning' | 'destructive' | 'default' }
> = {
// A comment that genuinely appeared since the last run.
new_comment_posted: { label: '新评论', icon: MessageCirclePlus, variant: 'success' },
// One that only just entered the visible top-N window - flagged differently
// so the distinction is never blurred.
new_comment_seen: { label: '新出现评论', icon: MessageCirclePlus, variant: 'default' },
new_note: { label: '新作品', icon: FilePlus2, variant: 'success' },
metric_delta: { label: '数据变化', icon: TrendingUp, variant: 'default' },
run_failed: { label: '采集失败', icon: TriangleAlert, variant: 'destructive' },
suspected_auth_failure: { label: '登录失效', icon: TriangleAlert, variant: 'warning' },
// Distinct from an auth failure: the login is fine, this target is the issue.
no_data_found: { label: '未抓到数据', icon: TriangleAlert, variant: 'warning' },
}
export function EventsFeed({ taskId }: EventsFeedProps) {
const { data: events, isLoading } = useMonitorEvents(taskId)
const markRead = useMarkEventsRead()
if (isLoading) {
return <p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">加载中…</p>
}
const unread = (events ?? []).filter((event) => !event.is_read).length
return (
<div className="space-y-2">
<div className="flex items-center justify-between">
<span className="text-[10px] font-mono text-cyber-text-muted">
{unread > 0 ? `${unread} 条未读` : '全部已读'}
</span>
{unread > 0 && (
<Button
variant="ghost"
size="sm"
onClick={() => markRead.mutate(taskId ?? undefined)}
disabled={markRead.isPending}
>
<CheckCheck className="w-3 h-3 mr-1" />
全部标为已读
</Button>
)}
</div>
{!events || events.length === 0 ? (
<p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">
{taskId === null ? '请先选择一个监控任务' : '暂无变化事件'}
</p>
) : (
<div className="space-y-1">
{events.map((event) => {
const meta = EVENT_META[event.type] ?? {
label: event.type,
icon: TrendingUp,
variant: 'default' as const,
}
const Icon = meta.icon
return (
<div
key={event.id}
className={`flex items-start gap-2 rounded-md border px-3 py-2 ${
event.is_read
? 'border-cyber-border-subtle bg-cyber-bg-tertiary/30'
: 'border-cyber-neon-cyan/30 bg-cyber-neon-cyan/5'
}`}
>
<Icon
className={`w-3.5 h-3.5 mt-0.5 flex-shrink-0 ${
meta.variant === 'destructive'
? 'text-cyber-neon-pink'
: meta.variant === 'warning'
? 'text-cyber-neon-orange'
: 'text-cyber-neon-cyan'
}`}
/>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2 flex-wrap">
<Badge variant={meta.variant} className="text-[9px] px-1 py-0">
{meta.label}
</Badge>
<span className="text-[11px] font-mono text-cyber-text-primary break-words">
{event.title}
</span>
</div>
<div className="mt-0.5 text-[9px] font-mono text-cyber-text-muted">
任务 #{event.task_id} · {formatRelative(event.created_at)}
</div>
</div>
</div>
)
})}
</div>
)}
</div>
)
}
@@ -0,0 +1,198 @@
import { useState } from 'react'
import { Activity, BellRing, FileText, MessageSquare, Plus } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { useMonitorOverview, useMonitorTasks } from '@/hooks/useMonitor'
import type { MonitorTask } from '@/types/monitor'
import { useCookieStatus, useWebhookStatus } from '@/hooks/useMonitor'
import { useCurrentPlatform } from '@/hooks/usePlatform'
import { UnwiredPlatformNotice } from '@/components/layout/UnwiredPlatformNotice'
import { CommentsFeed } from './CommentsFeed'
import { EventsFeed } from './EventsFeed'
import { NotesTable } from './NotesTable'
import { RunHistory } from './RunHistory'
import { StatTile } from './StatTile'
import { TaskCard } from './TaskCard'
import { TaskEditorDialog } from './TaskEditorDialog'
export function MonitorDashboard() {
const { data: overview } = useMonitorOverview()
const { data: tasks, isLoading } = useMonitorTasks()
const { data: cookieStatus } = useCookieStatus()
const { data: webhookStatus } = useWebhookStatus()
const cookieOk = cookieStatus?.present ?? false
const webhookOk = webhookStatus?.configured ?? false
const { isWired, isLoading: platformLoading } = useCurrentPlatform()
const [selectedTaskId, setSelectedTaskId] = useState<number | null>(null)
const [editorOpen, setEditorOpen] = useState(false)
const [editingTask, setEditingTask] = useState<MonitorTask | null>(null)
const [onlyNew, setOnlyNew] = useState(false)
// Fall back to the first task so the detail panes are never empty for no
// reason; keep an explicit selection once the user makes one.
const selectedTask =
tasks?.find((task) => task.id === selectedTaskId) ?? (tasks?.length ? tasks[0] : null)
const openCreate = () => {
setEditingTask(null)
setEditorOpen(true)
}
const openEdit = (task: MonitorTask) => {
setEditingTask(task)
setEditorOpen(true)
}
// An unwired platform has no monitoring to show; an explanatory panel beats an
// empty dashboard that looks broken.
if (!platformLoading && !isWired) {
return <UnwiredPlatformNotice area="监控" />
}
return (
<div className="flex-1 flex flex-col gap-3 overflow-hidden min-h-0 relative z-10">
{/* KPI row - single numbers are tiles, not charts. */}
<div className="grid grid-cols-2 lg:grid-cols-4 gap-2 flex-shrink-0">
<StatTile
icon={Activity}
label="监控任务"
value={overview?.tasks ?? 0}
hint={`${overview?.enabled_tasks ?? 0} 个启用中`}
/>
<StatTile icon={FileText} label="已追踪作品" value={overview?.notes ?? 0} />
<StatTile icon={MessageSquare} label="已追踪评论" value={overview?.comments ?? 0} />
<StatTile
icon={BellRing}
label="未读变化"
value={overview?.unread_events ?? 0}
hint={`24h 内 ${overview?.events_24h ?? 0} 条`}
accent={(overview?.unread_events ?? 0) > 0}
/>
</div>
<div className="flex-1 flex gap-3 overflow-hidden min-h-0">
{/* Left: tasks + cookie */}
<div className="w-[340px] flex-shrink-0 flex flex-col gap-3 overflow-hidden">
<div className="flex items-center justify-between flex-shrink-0">
<span className="font-mono text-xs tracking-wider text-cyber-text-primary">
监控任务
</span>
<Button size="sm" onClick={openCreate}>
<Plus className="w-3 h-3 mr-1" />
新建
</Button>
</div>
<div className="flex-1 overflow-y-auto terminal-scroll space-y-2 pr-1">
{isLoading && (
<p className="py-6 text-center text-[11px] font-mono text-cyber-text-muted">
加载中…
</p>
)}
{tasks?.length === 0 && (
<p className="py-6 text-center text-[11px] font-mono text-cyber-text-muted">
还没有监控任务,点击「新建」开始
</p>
)}
{tasks?.map((task) => (
<TaskCard
key={task.id}
task={task}
selected={selectedTask?.id === task.id}
onSelect={() => setSelectedTaskId(task.id)}
onEdit={() => openEdit(task)}
/>
))}
</div>
<div className="flex-shrink-0">
{/* The panels themselves moved to the Settings page; this only keeps
the "is anything unconfigured?" signal where tasks are run. */}
<div className="rounded-lg glass-panel float-panel px-3 py-2 space-y-1">
<div className="flex items-center gap-2 text-[10px] font-mono">
<span className={cookieOk ? 'status-dot status-dot-online' : 'status-dot status-dot-warning'} />
<span className="text-cyber-text-secondary">
登录态 {cookieOk ? '已配置' : '未配置'}
</span>
<span className="mx-1 text-cyber-text-muted">·</span>
<span className={webhookOk ? 'status-dot status-dot-online' : 'status-dot status-dot-offline'} />
<span className="text-cyber-text-secondary">
通知 {webhookOk ? '已配置' : '未配置'}
</span>
</div>
<p className="text-[10px] font-mono text-cyber-text-muted">
在「设置」页配置登录态、通知与采集策略
</p>
</div>
</div>
</div>
{/* Right: detail panes for the selected task */}
<div className="flex-1 flex flex-col overflow-hidden min-w-0 rounded-lg glass-panel float-panel">
{selectedTask ? (
<Tabs defaultValue="notes" className="flex-1 flex flex-col overflow-hidden">
<div className="px-3 pt-3 flex items-center justify-between gap-3 flex-shrink-0">
<TabsList>
<TabsTrigger value="notes">作品</TabsTrigger>
<TabsTrigger value="comments">评论</TabsTrigger>
<TabsTrigger value="events">
变化
{selectedTask.unread_events > 0 && (
<Badge variant="destructive" className="ml-1 text-[9px] px-1 py-0">
{selectedTask.unread_events}
</Badge>
)}
</TabsTrigger>
<TabsTrigger value="runs">运行历史</TabsTrigger>
</TabsList>
{selectedTask.mode === 'creator' && (
<span className="text-[10px] font-mono text-cyber-text-muted">
仅显示每个博主最新的 {selectedTask.max_notes_count} 条作品
</span>
)}
</div>
<TabsContent value="notes" className="flex-1 overflow-auto terminal-scroll px-3 pb-3 mt-2">
<div className="flex items-center gap-2 mb-2">
<Button
variant={onlyNew ? 'default' : 'outline'}
size="sm"
onClick={() => setOnlyNew((value) => !value)}
>
只看新增
</Button>
</div>
<NotesTable taskId={selectedTask.id} onlyNew={onlyNew} />
</TabsContent>
<TabsContent value="comments" className="flex-1 overflow-auto terminal-scroll px-3 pb-3 mt-2">
<CommentsFeed taskId={selectedTask.id} />
</TabsContent>
<TabsContent value="events" className="flex-1 overflow-auto terminal-scroll px-3 pb-3 mt-2">
<EventsFeed taskId={selectedTask.id} />
</TabsContent>
<TabsContent value="runs" className="flex-1 overflow-auto terminal-scroll px-3 pb-3 mt-2">
<RunHistory taskId={selectedTask.id} />
</TabsContent>
</Tabs>
) : (
<div className="flex-1 flex items-center justify-center">
<p className="text-[11px] font-mono text-cyber-text-muted">
创建或选择一个监控任务以查看数据
</p>
</div>
)}
</div>
</div>
<TaskEditorDialog open={editorOpen} onOpenChange={setEditorOpen} task={editingTask} />
</div>
)
}
@@ -0,0 +1,209 @@
import { useMemo, useState } from 'react'
import { useNoteSeries } from '@/hooks/useMonitor'
import { formatCount, formatDateTime } from '@/lib/monitorFormat'
type MetricKey = 'liked_count' | 'comment_count' | 'collected_count' | 'share_count'
const METRICS: Array<{ key: MetricKey; label: string }> = [
{ key: 'liked_count', label: '点赞' },
{ key: 'comment_count', label: '评论' },
{ key: 'collected_count', label: '收藏' },
{ key: 'share_count', label: '分享' },
]
const VIEW_W = 600
const VIEW_H = 160
const PAD_LEFT = 10
const PAD_RIGHT = 56
const PAD_TOP = 18
const PAD_BOTTOM = 22
interface NoteTrendChartProps {
noteId: string
taskId: number | null
noteTitle: string
}
/**
* Metric history for a single note.
*
* Deliberately one metric at a time rather than several overlaid: like and
* comment counts differ by orders of magnitude, so a shared axis would invent a
* misleading relationship between them. One series also means no legend is
* needed -- the picker names it.
*/
export function NoteTrendChart({ noteId, taskId, noteTitle }: NoteTrendChartProps) {
const [metric, setMetric] = useState<MetricKey>('liked_count')
const [hoverIndex, setHoverIndex] = useState<number | null>(null)
const { data: series, isLoading } = useNoteSeries(noteId, taskId)
// A point with no parsed value is a gap, not a zero.
const points = useMemo(
() => (series ?? []).filter((point) => point[metric] !== null),
[series, metric],
)
const geometry = useMemo(() => {
if (points.length < 2) return null
const values = points.map((point) => point[metric] as number)
const min = Math.min(...values)
const max = Math.max(...values)
// A flat series would divide by zero; give it a nominal band.
const span = max - min || 1
const innerW = VIEW_W - PAD_LEFT - PAD_RIGHT
const innerH = VIEW_H - PAD_TOP - PAD_BOTTOM
const xy = points.map((point, index) => {
const value = point[metric] as number
return {
x: PAD_LEFT + (index / (points.length - 1)) * innerW,
y: PAD_TOP + innerH - ((value - min) / span) * innerH,
value,
point,
}
})
return { xy, min, max }
}, [points, metric])
if (isLoading) {
return <div className="p-4 text-[11px] font-mono text-cyber-text-muted">加载中…</div>
}
return (
<div className="space-y-2">
<div className="flex items-center justify-between gap-3 flex-wrap">
<span className="font-mono text-xs text-cyber-text-primary">
指标趋势 · <span className="text-cyber-text-secondary">{noteTitle || noteId}</span>
</span>
<div className="flex items-center gap-1">
{METRICS.map((option) => (
<button
key={option.key}
onClick={() => setMetric(option.key)}
className={`px-2 py-1 rounded text-[10px] font-mono transition-colors ${
metric === option.key
? 'bg-cyber-neon-cyan/20 text-cyber-neon-cyan border border-cyber-neon-cyan/40'
: 'text-cyber-text-muted border border-transparent hover:text-cyber-text-secondary'
}`}
>
{option.label}
</button>
))}
</div>
</div>
{!geometry ? (
<p className="py-6 text-center text-[11px] font-mono text-cyber-text-muted">
至少需要两轮采集才能画出趋势(当前 {points.length} 个有效数据点)
</p>
) : (
<div className="relative">
<svg
viewBox={`0 0 ${VIEW_W} ${VIEW_H}`}
className="w-full h-40"
preserveAspectRatio="none"
onMouseLeave={() => setHoverIndex(null)}
>
{/* Recessive solid hairlines - never dashed. */}
{[0, 0.5, 1].map((ratio) => {
const y = PAD_TOP + (VIEW_H - PAD_TOP - PAD_BOTTOM) * ratio
return (
<line
key={ratio}
x1={PAD_LEFT}
x2={VIEW_W - PAD_RIGHT}
y1={y}
y2={y}
stroke="rgb(var(--cyber-text-muted) / 0.25)"
strokeWidth={1}
vectorEffect="non-scaling-stroke"
/>
)
})}
{hoverIndex !== null && geometry.xy[hoverIndex] && (
<line
x1={geometry.xy[hoverIndex].x}
x2={geometry.xy[hoverIndex].x}
y1={PAD_TOP}
y2={VIEW_H - PAD_BOTTOM}
stroke="rgb(var(--cyber-neon-cyan) / 0.5)"
strokeWidth={1}
vectorEffect="non-scaling-stroke"
/>
)}
<polyline
points={geometry.xy.map((node) => `${node.x},${node.y}`).join(' ')}
fill="none"
stroke="rgb(var(--cyber-neon-cyan))"
strokeWidth={2}
strokeLinejoin="round"
strokeLinecap="round"
vectorEffect="non-scaling-stroke"
/>
{/* Only the endpoint is labelled - a number on every point is noise. */}
<circle
cx={geometry.xy[geometry.xy.length - 1].x}
cy={geometry.xy[geometry.xy.length - 1].y}
r={4}
fill="rgb(var(--cyber-neon-cyan))"
stroke="rgb(var(--cyber-bg-primary))"
strokeWidth={2}
vectorEffect="non-scaling-stroke"
/>
{geometry.xy.map((node, index) => (
<rect
key={index}
x={node.x - 6}
y={PAD_TOP}
width={12}
height={VIEW_H - PAD_TOP - PAD_BOTTOM}
fill="transparent"
onMouseEnter={() => setHoverIndex(index)}
/>
))}
</svg>
{/* Axis extremes live in text tokens, never the series colour. */}
<span className="absolute left-0 top-0 text-[9px] font-mono text-cyber-text-muted">
{formatCount(geometry.max)}
</span>
<span className="absolute left-0 bottom-5 text-[9px] font-mono text-cyber-text-muted">
{formatCount(geometry.min)}
</span>
<span className="absolute right-0 top-1/2 -translate-y-1/2 text-[11px] font-mono text-cyber-text-primary">
{formatCount(geometry.xy[geometry.xy.length - 1].value)}
</span>
{hoverIndex !== null && geometry.xy[hoverIndex] && (
<div
className="absolute -top-1 px-2 py-1 rounded border border-cyber-border-DEFAULT bg-cyber-bg-elevated text-[10px] font-mono text-cyber-text-primary pointer-events-none whitespace-nowrap"
style={{
left: `${(geometry.xy[hoverIndex].x / VIEW_W) * 100}%`,
transform: 'translateX(-50%)',
}}
>
<div className="text-cyber-neon-cyan">
{formatCount(geometry.xy[hoverIndex].value)}
</div>
<div className="text-cyber-text-muted">
{formatDateTime(geometry.xy[hoverIndex].point.captured_at)}
</div>
</div>
)}
</div>
)}
<p className="text-[10px] font-mono text-cyber-text-muted">
共 {points.length} 个数据点,每轮采集记录一次快照
</p>
</div>
)
}
+146
View File
@@ -0,0 +1,146 @@
import { Fragment, useState } from 'react'
import { ChevronDown, ChevronRight, ExternalLink } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { useMonitorNotes } from '@/hooks/useMonitor'
import { formatCount, formatDelta, formatRelative } from '@/lib/monitorFormat'
import type { MonitorNote, NoteMetrics } from '@/types/monitor'
import { NoteTrendChart } from './NoteTrendChart'
interface NotesTableProps {
taskId: number | null
onlyNew: boolean
}
/** A metric with its change vs the previous run underneath. */
function MetricCell({ value, delta }: { value: number | null; delta: number | null }) {
const deltaText = formatDelta(delta)
return (
<div className="text-right">
<div className="font-mono text-xs text-cyber-text-primary">{formatCount(value)}</div>
{deltaText && (
<div
className={`font-mono text-[9px] ${
(delta ?? 0) > 0 ? 'text-cyber-neon-green' : 'text-cyber-neon-pink'
}`}
>
{deltaText}
</div>
)}
</div>
)
}
const METRIC_COLUMNS: Array<{ key: keyof NoteMetrics; label: string }> = [
{ key: 'liked_count', label: '点赞' },
{ key: 'comment_count', label: '评论' },
{ key: 'collected_count', label: '收藏' },
{ key: 'share_count', label: '分享' },
]
export function NotesTable({ taskId, onlyNew }: NotesTableProps) {
const { data: notes, isLoading } = useMonitorNotes(taskId, onlyNew)
const [expanded, setExpanded] = useState<string | null>(null)
if (isLoading) {
return <p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">加载中…</p>
}
if (!notes || notes.length === 0) {
return (
<p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">
{taskId === null
? '请先选择一个监控任务'
: onlyNew
? '最近一轮没有新增作品'
: '还没有采集到作品,运行一次任务后即可看到'}
</p>
)
}
const isExpanded = (note: MonitorNote) => expanded === `${note.task_id}:${note.note_id}`
return (
<div className="overflow-x-auto terminal-scroll">
<table className="w-full text-xs font-mono">
<thead className="sticky top-0 bg-cyber-bg-tertiary">
<tr className="text-cyber-text-secondary text-[10px]">
<th className="w-6" />
<th className="text-left font-normal py-2 px-2">作品</th>
{METRIC_COLUMNS.map((column) => (
<th key={column.key} className="text-right font-normal py-2 px-2">
{column.label}
</th>
))}
<th className="text-right font-normal py-2 px-2">首次发现</th>
<th className="w-8" />
</tr>
</thead>
<tbody>
{notes.map((note) => {
const open = isExpanded(note)
return (
<Fragment key={`${note.task_id}:${note.note_id}`}>
<tr
onClick={() => setExpanded(open ? null : `${note.task_id}:${note.note_id}`)}
className="border-t border-cyber-border-subtle hover:bg-cyber-bg-elevated/50 cursor-pointer"
>
<td className="py-2 px-1 text-cyber-text-muted">
{open ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
</td>
<td className="py-2 px-2 max-w-[320px]">
<div className="flex items-center gap-1.5">
{note.is_new && (
<Badge variant="success" className="text-[9px] px-1 py-0 flex-shrink-0">
NEW
</Badge>
)}
<span className="truncate text-cyber-text-primary" title={note.title}>
{note.title || note.note_id}
</span>
</div>
<div className="text-[9px] text-cyber-text-muted truncate">
{note.note_id} · {note.snapshot_count} 次快照
</div>
</td>
{METRIC_COLUMNS.map((column) => (
<td key={column.key} className="py-2 px-2">
<MetricCell value={note.metrics[column.key]} delta={note.deltas[column.key]} />
</td>
))}
<td className="py-2 px-2 text-right text-[10px] text-cyber-text-muted">
{formatRelative(note.first_seen_at)}
</td>
<td className="py-2 px-1">
{note.note_url && (
<a
href={note.note_url}
target="_blank"
rel="noopener noreferrer"
onClick={(event) => event.stopPropagation()}
className="text-cyber-text-muted hover:text-cyber-neon-cyan"
>
<ExternalLink className="w-3 h-3" />
</a>
)}
</td>
</tr>
{open && (
<tr className="bg-cyber-bg-secondary/40">
<td colSpan={METRIC_COLUMNS.length + 4} className="px-4 py-3">
<NoteTrendChart
noteId={note.note_id}
taskId={note.task_id}
noteTitle={note.title}
/>
</td>
</tr>
)}
</Fragment>
)
})}
</tbody>
</table>
</div>
)
}
+302
View File
@@ -0,0 +1,302 @@
import { useEffect, useMemo, useState } from 'react'
import { Download, FilePlus2, MessageSquarePlus, ThumbsUp, Star, TriangleAlert } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import { useMonitorTasks, useReport } from '@/hooks/useMonitor'
import { useCurrentPlatform } from '@/hooks/usePlatform'
import { UnwiredPlatformNotice } from '@/components/layout/UnwiredPlatformNotice'
import { monitorApi } from '@/lib/api'
import { formatCount } from '@/lib/monitorFormat'
import type { MetricKey, ReportRow } from '@/types/monitor'
import { StatTile } from './StatTile'
/** Local-date ISO string; toISOString() would shift across the UTC boundary. */
function toISODate(value: Date): string {
const pad = (n: number) => String(n).padStart(2, '0')
return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(value.getDate())}`
}
const METRIC_COLUMNS: Array<{ key: MetricKey; label: string }> = [
{ key: 'liked_count_delta', label: '点赞 Δ' },
{ key: 'comment_count_delta', label: '评论 Δ' },
{ key: 'collected_count_delta', label: '收藏 Δ' },
{ key: 'share_count_delta', label: '分享 Δ' },
]
/** Neutral for zero, since most rows are zero and colouring them all is noise. */
function DeltaCell({ value }: { value: number }) {
if (value === 0) return <span className="text-cyber-text-muted">—</span>
return (
<span className={value > 0 ? 'text-cyber-neon-green' : 'text-cyber-neon-pink'}>
{value > 0 ? '+' : ''}
{formatCount(value)}
</span>
)
}
export function ReportView() {
const { isWired, isLoading: platformLoading } = useCurrentPlatform()
const { data: tasks } = useMonitorTasks()
const [selected, setSelected] = useState<number[] | null>(null)
const [endDate, setEndDate] = useState(() => toISODate(new Date()))
const [startDate, setStartDate] = useState(() => {
const d = new Date()
d.setDate(d.getDate() - 6)
return toISODate(d)
})
// Default to every task once the list arrives; the user narrows from there.
useEffect(() => {
if (tasks && selected === null) {
setSelected(tasks.map((task) => task.id))
}
}, [tasks, selected])
const activeTaskIds = selected ?? []
const { data: report, isLoading } = useReport(activeTaskIds, startDate, endDate)
const allSelected = useMemo(
() => Boolean(tasks?.length) && activeTaskIds.length === tasks?.length,
[tasks, activeTaskIds],
)
const toggleTask = (id: number) => {
setSelected((current) => {
const base = current ?? []
return base.includes(id) ? base.filter((value) => value !== id) : [...base, id]
})
}
const applyPreset = (days: number) => {
const end = new Date()
const start = new Date()
start.setDate(start.getDate() - (days - 1))
setStartDate(toISODate(start))
setEndDate(toISODate(end))
}
if (!platformLoading && !isWired) {
return <UnwiredPlatformNotice area="报表" />
}
const totals = report?.totals
const partialLabels = (report?.partial_metrics ?? [])
.map((field) => report?.metric_labels?.[field] ?? field)
.join('、')
return (
<div className="flex-1 flex flex-col gap-3 overflow-hidden min-h-0 relative z-10">
{/* Filters */}
<div className="rounded-lg glass-panel float-panel p-3 flex-shrink-0 space-y-3">
<div className="flex items-center justify-between gap-3 flex-wrap">
<div className="flex items-center gap-2">
<span className="font-mono text-xs tracking-wider text-cyber-text-primary">
统计范围
</span>
<div className="flex items-center gap-1">
<input
type="date"
value={startDate}
onChange={(event) => setStartDate(event.target.value)}
className="rounded-md border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-2 py-1 font-mono text-[11px] text-cyber-text-primary outline-none focus-visible:border-cyber-neon-cyan/50"
/>
<span className="text-cyber-text-muted text-[11px] font-mono">至</span>
<input
type="date"
value={endDate}
onChange={(event) => setEndDate(event.target.value)}
className="rounded-md border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-2 py-1 font-mono text-[11px] text-cyber-text-primary outline-none focus-visible:border-cyber-neon-cyan/50"
/>
</div>
</div>
<div className="flex items-center gap-1">
{[7, 30, 90].map((days) => (
<Button key={days} variant="outline" size="sm" onClick={() => applyPreset(days)}>
近 {days} 天
</Button>
))}
<Button
variant="outline"
size="sm"
onClick={() =>
window.open(
monitorApi.getExportUrl({
kind: 'report',
format: 'csv',
taskIds: activeTaskIds,
startDate,
endDate,
}),
'_blank',
)
}
>
<Download className="w-3 h-3 mr-1" />
导出
</Button>
</div>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between">
<span className="text-[11px] font-mono text-cyber-text-secondary">
参与统计的任务(已选{' '}
<span className="text-cyber-neon-cyan">{activeTaskIds.length}</span> 个)
</span>
<Button
variant="ghost"
size="sm"
onClick={() =>
setSelected(allSelected ? [] : (tasks ?? []).map((task) => task.id))
}
disabled={!tasks?.length}
>
{allSelected ? '全不选' : '全选'}
</Button>
</div>
{tasks?.length ? (
<div className="flex flex-wrap gap-2">
{tasks.map((task) => {
const checked = activeTaskIds.includes(task.id)
return (
<label
key={task.id}
className={`flex items-center gap-1.5 px-2 py-1 rounded-md border cursor-pointer transition-colors ${
checked
? 'border-cyber-neon-cyan/40 bg-cyber-neon-cyan/10'
: 'border-cyber-border-subtle bg-cyber-bg-tertiary'
}`}
>
<Checkbox
checked={checked}
onCheckedChange={() => toggleTask(task.id)}
/>
<span
className={`text-[11px] font-mono ${
checked ? 'text-cyber-neon-cyan' : 'text-cyber-text-muted'
}`}
>
{task.name}
</span>
</label>
)
})}
</div>
) : (
<p className="text-[11px] font-mono text-cyber-text-muted">
还没有监控任务,先去「监控」页建一个。
</p>
)}
</div>
</div>
{/* Headline totals */}
<div className="grid grid-cols-2 lg:grid-cols-4 gap-2 flex-shrink-0">
<StatTile icon={FilePlus2} label="新增作品" value={formatCount(totals?.new_notes ?? 0)} />
<StatTile
icon={MessageSquarePlus}
label="新增评论"
value={formatCount(totals?.new_comments ?? 0)}
/>
<StatTile
icon={ThumbsUp}
label="点赞增量"
value={formatCount(totals?.liked_count_delta ?? 0)}
hint={`覆盖 ${report?.note_count ?? 0} 篇作品`}
/>
<StatTile
icon={Star}
label="收藏增量"
value={formatCount(totals?.collected_count_delta ?? 0)}
/>
</div>
{/* Daily breakdown */}
<div className="flex-1 overflow-auto terminal-scroll rounded-lg glass-panel float-panel">
{activeTaskIds.length === 0 ? (
<p className="py-10 text-center text-[11px] font-mono text-cyber-text-muted">
请至少选择一个任务
</p>
) : isLoading ? (
<p className="py-10 text-center text-[11px] font-mono text-cyber-text-muted">统计中…</p>
) : (
<table className="w-full text-xs font-mono">
<thead className="sticky top-0 bg-cyber-bg-tertiary">
<tr className="text-cyber-text-secondary text-[10px]">
<th className="text-left font-normal py-2 px-3">日期</th>
<th className="text-right font-normal py-2 px-3">新增作品</th>
<th className="text-right font-normal py-2 px-3">新增评论</th>
{METRIC_COLUMNS.map((column) => (
<th key={column.key} className="text-right font-normal py-2 px-3">
{column.label}
</th>
))}
</tr>
</thead>
<tbody>
{report?.rows.map((row: ReportRow) => (
<tr key={row.date} className="border-t border-cyber-border-subtle">
<td className="py-2 px-3 text-cyber-text-primary">{row.date}</td>
<td className="py-2 px-3 text-right">
<span className={row.new_notes > 0 ? 'text-cyber-neon-green' : 'text-cyber-text-muted'}>
{row.new_notes || '—'}
</span>
</td>
<td className="py-2 px-3 text-right">
<span
className={row.new_comments > 0 ? 'text-cyber-neon-green' : 'text-cyber-text-muted'}
>
{row.new_comments || '—'}
</span>
</td>
{METRIC_COLUMNS.map((column) => (
<td key={column.key} className="py-2 px-3 text-right">
<DeltaCell value={row[column.key]} />
</td>
))}
</tr>
))}
{report && (
<tr className="border-t-2 border-cyber-neon-cyan/30 bg-cyber-bg-tertiary/60">
<td className="py-2 px-3 text-cyber-neon-cyan">合计</td>
<td className="py-2 px-3 text-right text-cyber-neon-cyan">
{report.totals.new_notes}
</td>
<td className="py-2 px-3 text-right text-cyber-neon-cyan">
{report.totals.new_comments}
</td>
{METRIC_COLUMNS.map((column) => (
<td key={column.key} className="py-2 px-3 text-right text-cyber-neon-cyan">
{report.totals[column.key] > 0 ? '+' : ''}
{formatCount(report.totals[column.key])}
</td>
))}
</tr>
)}
</tbody>
</table>
)}
</div>
{/* Honest caveats, stated where the numbers are read. */}
<div className="flex-shrink-0 space-y-1">
{partialLabels && (
<p className="flex items-center gap-1.5 text-[10px] font-mono text-cyber-neon-orange">
<TriangleAlert className="w-3 h-3" />
{partialLabels} 存在无法解析的数值,这些增量未计入统计,实际增幅会更高。
</p>
)}
<p className="text-[10px] font-mono text-cyber-text-muted">
「增量」= 当日末值 − 当日之前最后一次采到的值;作品首次出现的那天从 0 起算,
所以新增作品的全部点赞都计在其首次发现日。<br />
「新增评论」受接口限制只覆盖平台默认排序的前 N 条,沉底的新评论不计入。
</p>
</div>
</div>
)
}
@@ -0,0 +1,96 @@
import { Badge } from '@/components/ui/badge'
import { useTaskRuns } from '@/hooks/useMonitor'
import { formatDateTime, formatRelative } from '@/lib/monitorFormat'
import type { RunStatus } from '@/types/monitor'
interface RunHistoryProps {
taskId: number | null
}
const STATUS_META: Record<RunStatus, { label: string; variant: 'success' | 'warning' | 'destructive' | 'running' | 'idle' }> = {
success: { label: '成功', variant: 'success' },
partial: { label: '部分失败', variant: 'warning' },
failed: { label: '失败', variant: 'destructive' },
timeout: { label: '超时', variant: 'destructive' },
interrupted: { label: '已中断', variant: 'warning' },
running: { label: '运行中', variant: 'running' },
pending: { label: '排队中', variant: 'idle' },
}
export function RunHistory({ taskId }: RunHistoryProps) {
const { data: runs, isLoading } = useTaskRuns(taskId)
if (isLoading) {
return <p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">加载中…</p>
}
if (!runs || runs.length === 0) {
return (
<p className="py-8 text-center text-[11px] font-mono text-cyber-text-muted">
{taskId === null ? '请先选择一个监控任务' : '还没有运行记录'}
</p>
)
}
return (
<div className="overflow-x-auto terminal-scroll">
<table className="w-full text-xs font-mono">
<thead className="sticky top-0 bg-cyber-bg-tertiary">
<tr className="text-cyber-text-secondary text-[10px]">
<th className="text-left font-normal py-2 px-2">状态</th>
<th className="text-left font-normal py-2 px-2">触发</th>
<th className="text-right font-normal py-2 px-2">抓到作品</th>
<th className="text-right font-normal py-2 px-2">新增作品</th>
<th className="text-right font-normal py-2 px-2">新增评论</th>
<th className="text-left font-normal py-2 px-2">开始时间</th>
<th className="text-left font-normal py-2 px-2">说明</th>
</tr>
</thead>
<tbody>
{runs.map((run) => {
const meta = STATUS_META[run.status] ?? STATUS_META.pending
return (
<tr key={run.id} className="border-t border-cyber-border-subtle">
<td className="py-2 px-2">
<div className="flex items-center gap-1.5">
<Badge variant={meta.variant} className="text-[9px] px-1 py-0">
{meta.label}
</Badge>
{run.is_baseline && (
<span className="text-[9px] text-cyber-text-muted" title="首次运行建立基线,不产生变化事件">
基线
</span>
)}
</div>
</td>
<td className="py-2 px-2 text-[10px] text-cyber-text-secondary">
{run.trigger === 'manual' ? '手动' : '定时'}
</td>
<td className="py-2 px-2 text-right text-cyber-text-primary">{run.notes_fetched}</td>
<td className="py-2 px-2 text-right">
<span className={run.new_notes > 0 ? 'text-cyber-neon-green' : 'text-cyber-text-muted'}>
{run.new_notes}
</span>
</td>
<td className="py-2 px-2 text-right">
<span
className={run.new_comments > 0 ? 'text-cyber-neon-green' : 'text-cyber-text-muted'}
>
{run.new_comments}
</span>
</td>
<td className="py-2 px-2 text-[10px] text-cyber-text-muted">
{formatDateTime(run.started_at)}
<span className="ml-1">({formatRelative(run.started_at)})</span>
</td>
<td className="py-2 px-2 text-[10px] text-cyber-neon-orange max-w-[240px] truncate">
{run.error_message ?? ''}
</td>
</tr>
)
})}
</tbody>
</table>
</div>
)
}
+48
View File
@@ -0,0 +1,48 @@
import type { LucideIcon } from 'lucide-react'
interface StatTileProps {
label: string
value: number | string
icon: LucideIcon
/** Draws attention when non-zero, e.g. unread change events. */
accent?: boolean
hint?: string
}
/**
* A single headline number.
*
* Deliberately a tile and not a chart: one number's job is to be read, and
* wrapping it in a plot (a one-bar bar chart, a two-slice pie) is a known way
* to hide the number rather than show it.
*/
export function StatTile({ label, value, icon: Icon, accent = false, hint }: StatTileProps) {
return (
<div className="rounded-lg glass-panel float-panel px-3 py-2.5 flex items-center gap-3">
<div
className={`p-1.5 rounded-md border ${
accent
? 'border-cyber-neon-pink/40 bg-cyber-neon-pink/10'
: 'border-cyber-neon-cyan/30 bg-cyber-neon-cyan/10'
}`}
>
<Icon
className={`w-3.5 h-3.5 ${accent ? 'text-cyber-neon-pink' : 'text-cyber-neon-cyan'}`}
/>
</div>
<div className="min-w-0">
<div
className={`font-mono text-lg leading-none ${
accent ? 'text-cyber-neon-pink' : 'text-cyber-text-primary'
}`}
>
{value}
</div>
<div className="text-[10px] font-mono text-cyber-text-muted mt-1 truncate">
{label}
{hint ? <span className="text-cyber-text-secondary"> · {hint}</span> : null}
</div>
</div>
</div>
)
}
+156
View File
@@ -0,0 +1,156 @@
import { Bell, CalendarClock, Pencil, Play, Trash2 } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { useDeleteTask, useRunTaskNow, useUpdateTask } from '@/hooks/useMonitor'
import { formatInterval, formatRelative } from '@/lib/monitorFormat'
import type { MonitorTask } from '@/types/monitor'
interface TaskCardProps {
task: MonitorTask
selected: boolean
onSelect: () => void
onEdit: () => void
}
/** Maps a task's last run status onto a badge variant. */
function statusVariant(status: string) {
switch (status) {
case 'success':
return 'success' as const
case 'running':
case 'pending':
return 'running' as const
case 'partial':
return 'warning' as const
case 'failed':
case 'timeout':
return 'destructive' as const
default:
return 'idle' as const
}
}
const STATUS_LABEL: Record<string, string> = {
idle: '未运行',
success: '成功',
partial: '部分失败',
failed: '失败',
timeout: '超时',
running: '运行中',
pending: '排队中',
interrupted: '已中断',
}
export function TaskCard({ task, selected, onSelect, onEdit }: TaskCardProps) {
const updateTask = useUpdateTask()
const deleteTask = useDeleteTask()
const runNow = useRunTaskNow()
// A suspected cookie failure surfaces here so it is visible without opening
// the event feed.
const authTrouble = task.last_status === 'partial'
return (
<div
onClick={onSelect}
className={`rounded-lg glass-panel float-panel p-3 cursor-pointer transition-all ${
selected
? 'border-cyber-neon-cyan/60 shadow-glow-cyan-sm'
: 'border-cyber-border-subtle hover:border-cyber-neon-cyan/30'
}`}
>
<div className="flex items-start justify-between gap-2">
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2 flex-wrap">
<span className="font-mono text-sm text-cyber-text-primary truncate">{task.name}</span>
<Badge variant="outline" className="text-[10px]">
{task.mode === 'creator' ? '博主' : '笔记'}
</Badge>
<Badge variant={statusVariant(task.last_status)} className="text-[10px]">
{STATUS_LABEL[task.last_status] ?? task.last_status}
</Badge>
{task.unread_events > 0 && (
<span className="flex items-center gap-1 text-[10px] font-mono text-cyber-neon-pink">
<Bell className="w-3 h-3" />
{task.unread_events}
</span>
)}
</div>
<div className="mt-1.5 flex flex-wrap items-center gap-x-4 gap-y-1 text-[11px] font-mono text-cyber-text-secondary">
<span>
目标 <span className="text-cyber-neon-cyan">{task.target_count}</span> 个
</span>
<span>间隔 {formatInterval(task.interval_minutes)}</span>
<span className="flex items-center gap-1">
<CalendarClock className="w-3 h-3" />
{task.enabled ? formatRelative(task.next_run_at) : '已暂停'}
</span>
<span className="text-cyber-text-muted">
上次 {formatRelative(task.last_run_at)}
</span>
</div>
{authTrouble && (
<p className="mt-1.5 text-[10px] font-mono text-cyber-neon-orange">
{task.last_error ?? '疑似登录态失效,请检查 Cookie'}
</p>
)}
</div>
<div className="flex items-center gap-1 flex-shrink-0">
<Button
variant="ghost"
size="icon"
title={task.enabled ? '暂停' : '启用'}
onClick={(event) => {
event.stopPropagation()
updateTask.mutate({ id: task.id, payload: { enabled: !task.enabled } })
}}
>
<span
className={`status-dot ${task.enabled ? 'status-dot-online' : 'status-dot-offline'}`}
/>
</Button>
<Button
variant="ghost"
size="icon"
title="立即运行"
disabled={runNow.isPending}
onClick={(event) => {
event.stopPropagation()
runNow.mutate(task.id)
}}
>
<Play className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
title="编辑"
onClick={(event) => {
event.stopPropagation()
onEdit()
}}
>
<Pencil className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="icon"
title="删除"
onClick={(event) => {
event.stopPropagation()
if (confirm(`确定删除监控任务「${task.name}」?已采集的历史数据也会一并删除。`)) {
deleteTask.mutate(task.id)
}
}}
>
<Trash2 className="w-4 h-4 text-cyber-neon-pink" />
</Button>
</div>
</div>
</div>
)
}
@@ -0,0 +1,281 @@
import { useEffect, useState } from 'react'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import { useCreateTask, useSettings, useUpdateTask } from '@/hooks/useMonitor'
import type { MonitorMode, MonitorTask, TaskCreatePayload } from '@/types/monitor'
interface TaskEditorDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
task?: MonitorTask | null
}
const TEXTAREA_CLASS =
'w-full rounded-md border border-cyber-border-DEFAULT bg-cyber-bg-tertiary p-2 font-mono text-[11px] text-cyber-text-primary outline-none focus-visible:border-cyber-neon-cyan/50'
// The backend enforces this floor too; every run launches a browser and hits
// XHS several times, so tight polling is what triggers rate limiting.
const INTERVAL_OPTIONS = [
{ value: '30', label: '30 分钟' },
{ value: '60', label: '1 小时' },
{ value: '120', label: '2 小时' },
{ value: '360', label: '6 小时' },
{ value: '720', label: '12 小时' },
{ value: '1440', label: '1 天' },
{ value: '4320', label: '3 天' },
{ value: '10080', label: '7 天' },
]
export function TaskEditorDialog({ open, onOpenChange, task }: TaskEditorDialogProps) {
const isEdit = Boolean(task)
const createTask = useCreateTask()
const updateTask = useUpdateTask()
const { data: settings } = useSettings()
const [name, setName] = useState('')
const [mode, setMode] = useState<MonitorMode>('creator')
const [intervalMinutes, setIntervalMinutes] = useState('360')
const [maxNotes, setMaxNotes] = useState('20')
const [enableComments, setEnableComments] = useState(true)
const [maxComments, setMaxComments] = useState('50')
const [notifyEnabled, setNotifyEnabled] = useState(false)
const [targets, setTargets] = useState('')
// Reset the form whenever the dialog is (re)opened. For a new task the
// starting values come from the Settings page, so those defaults actually
// govern rather than being overwritten by hardcoded numbers here.
useEffect(() => {
if (!open) return
setName(task?.name ?? '')
setMode(task?.mode ?? 'creator')
setIntervalMinutes(
String(task?.interval_minutes ?? settings?.values['collect.default_interval_minutes'] ?? 360),
)
setMaxNotes(
String(task?.max_notes_count ?? settings?.values['collect.default_max_notes'] ?? 20),
)
setEnableComments(task?.enable_comments ?? true)
setMaxComments(
String(task?.max_comments_count ?? settings?.values['collect.default_max_comments'] ?? 50),
)
setNotifyEnabled(task?.notify_enabled ?? false)
setTargets(task ? task.targets.map((t) => t.raw_value || t.external_id).join('\n') : '')
}, [open, task, settings])
const targetList = targets
.split(/[\n,]+/)
.map((value) => value.trim())
.filter(Boolean)
const pending = createTask.isPending || updateTask.isPending
// Note-mode and creator-mode targets are different shapes, so switching mode
// would silently mis-parse the list. The backend decides parsing from the
// task's stored mode, hence mode is fixed once created.
const canSubmit = name.trim().length > 0 && targetList.length > 0 && !pending
const handleSubmit = () => {
const payload: TaskCreatePayload = {
name: name.trim(),
mode,
interval_minutes: Number(intervalMinutes),
max_notes_count: Number(maxNotes),
enable_comments: enableComments,
max_comments_count: Number(maxComments),
run_timeout_seconds: 3600,
enabled: true,
notify_enabled: notifyEnabled,
targets: targetList,
}
const done = () => onOpenChange(false)
if (isEdit && task) {
updateTask.mutate({ id: task.id, payload }, { onSuccess: done })
} else {
createTask.mutate(payload, { onSuccess: done })
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-2xl max-h-[85vh] overflow-y-auto terminal-scroll">
<DialogHeader>
<DialogTitle className="font-mono">
{isEdit ? '编辑监控任务' : '新建监控任务'}
</DialogTitle>
<DialogDescription className="font-mono text-xs">
定时重复采集目标,并与上一次快照对比,产出新增作品、新增评论和互动数据变化。
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-2">
<div className="space-y-2">
<Label className="text-xs font-mono text-cyber-text-secondary">任务名称</Label>
<Input
value={name}
onChange={(event) => setName(event.target.value)}
placeholder="例如:竞品博主监控"
className="h-9 text-xs"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div className="space-y-2">
<Label className="text-xs font-mono text-cyber-text-secondary">监控类型</Label>
<Select
value={mode}
onValueChange={(value) => setMode(value as MonitorMode)}
disabled={isEdit}
>
<SelectTrigger className="h-9 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="creator">博主(监控其作品)</SelectItem>
<SelectItem value="note">笔记(批量监控指定内容)</SelectItem>
</SelectContent>
</Select>
{isEdit && (
<p className="text-[10px] font-mono text-cyber-text-muted">
类型创建后不可更改
</p>
)}
</div>
<div className="space-y-2">
<Label className="text-xs font-mono text-cyber-text-secondary">采集间隔</Label>
<Select value={intervalMinutes} onValueChange={setIntervalMinutes}>
<SelectTrigger className="h-9 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
{INTERVAL_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
<div className="space-y-2">
<Label className="text-xs font-mono text-cyber-text-secondary">
{mode === 'creator' ? '博主主页链接或 ID' : '笔记链接或 ID'}
</Label>
<textarea
value={targets}
onChange={(event) => setTargets(event.target.value)}
rows={5}
placeholder={
mode === 'creator'
? '每行一个,支持完整主页链接或纯 ID:\nhttps://www.xiaohongshu.com/user/profile/5f58bd99...\n5f58bd990000000001003753'
: '每行一个,支持完整笔记链接或纯 ID:\nhttps://www.xiaohongshu.com/explore/6aa3d827...'
}
className={TEXTAREA_CLASS}
/>
<p className="text-[10px] font-mono text-cyber-text-muted">
已识别 <span className="text-cyber-neon-cyan">{targetList.length}</span> 个目标。
<span className="text-cyber-neon-orange">建议只填纯 ID</span>
——链接里的 xsec_token 会过期,纯 ID 永久有效。
</p>
</div>
<div className="grid grid-cols-2 gap-3">
<div className="space-y-2">
<Label className="text-xs font-mono text-cyber-text-secondary">
每轮最多采集作品数
</Label>
<Input
type="number"
min={1}
value={maxNotes}
onChange={(event) => setMaxNotes(event.target.value)}
className="h-9 text-xs"
/>
<p className="text-[10px] font-mono text-cyber-text-muted">
只取最新的前 N 条,决定了"该博主的作品"覆盖范围
</p>
</div>
<div className="space-y-2">
<Label className="text-xs font-mono text-cyber-text-secondary">每篇评论抓取条数</Label>
<div className="flex items-center gap-2">
<Input
type="number"
min={1}
value={maxComments}
disabled={!enableComments}
onChange={(event) => setMaxComments(event.target.value)}
className="h-9 text-xs"
/>
<div className="flex items-center gap-1.5 flex-shrink-0">
<Checkbox
id="enable-comments"
checked={enableComments}
onCheckedChange={(checked) => setEnableComments(checked === true)}
/>
<label
htmlFor="enable-comments"
className="text-[10px] font-mono text-cyber-text-secondary cursor-pointer"
>
抓评论
</label>
</div>
</div>
<p className="text-[10px] font-mono text-cyber-neon-orange">
接口无时间排序,只能取平台默认排序的前 N 条做对比,N 越大越容易发现新评论
</p>
</div>
</div>
<div className="flex items-start gap-2 rounded-md border border-cyber-border-subtle bg-cyber-bg-tertiary/40 p-3">
<Checkbox
id="notify-enabled"
checked={notifyEnabled}
onCheckedChange={(checked) => setNotifyEnabled(checked === true)}
/>
<div className="space-y-0.5">
<label
htmlFor="notify-enabled"
className="text-xs font-mono text-cyber-text-primary cursor-pointer"
>
推送企业微信通知
</label>
<p className="text-[10px] font-mono text-cyber-text-muted">
仅在本任务**采集失败 / 登录态失效**或**发现新作品**时推送,
一轮只发一条汇总。需先在监控页配置 Webhook 地址。
</p>
</div>
</div>
</div>
<DialogFooter>
<Button variant="ghost" onClick={() => onOpenChange(false)}>
取消
</Button>
<Button onClick={handleSubmit} disabled={!canSubmit}>
{isEdit ? '保存' : '创建'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
@@ -0,0 +1,125 @@
import { useState } from 'react'
import { Send, Trash2, Webhook } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { useClearWebhook, useSetWebhook, useTestWebhook, useWebhookStatus } from '@/hooks/useMonitor'
/**
* 企业微信机器人 Webhook 配置。
*
* 地址里的 key 等同凭据,所以服务端只回传打码后的形式 —— 想换就得重新粘贴,
* 和 Cookie 的处理保持一致。
*/
export function WebhookPanel() {
const { data: status } = useWebhookStatus()
const setWebhook = useSetWebhook()
const clearWebhook = useClearWebhook()
const testWebhook = useTestWebhook()
const [draft, setDraft] = useState('')
const [editing, setEditing] = useState(false)
const showForm = !status?.configured || editing
return (
<div className="rounded-lg glass-panel float-panel p-4 space-y-3">
<div className="flex items-center justify-between gap-3">
<div className="flex items-center gap-2">
<Webhook className="w-4 h-4 text-cyber-neon-cyan" />
<span className="font-mono text-xs tracking-wider text-cyber-text-primary">
企业微信通知
</span>
{status?.configured ? (
<Badge variant="success" className="text-[10px]">
已配置
</Badge>
) : (
<Badge variant="idle" className="text-[10px]">
未配置
</Badge>
)}
</div>
{status?.configured && (
<div className="flex items-center gap-1">
<Button
variant="outline"
size="sm"
onClick={() => testWebhook.mutate(undefined)}
disabled={testWebhook.isPending}
>
<Send className="w-3 h-3 mr-1" />
发测试
</Button>
{!editing && (
<Button variant="ghost" size="sm" onClick={() => setEditing(true)}>
替换
</Button>
)}
<Button
variant="ghost"
size="sm"
onClick={() => clearWebhook.mutate()}
disabled={clearWebhook.isPending}
>
<Trash2 className="w-3 h-3" />
</Button>
</div>
)}
</div>
{status?.configured && !editing && (
<p className="text-[11px] font-mono text-cyber-text-secondary break-all">
{status.masked}
</p>
)}
{showForm && (
<div className="space-y-2">
<p className="text-[11px] font-mono text-cyber-text-muted leading-relaxed">
在企业微信群里「添加群机器人」后复制 Webhook 地址。
只有**开启了推送开关**的任务才会发消息,且**一轮只发一条汇总**,
不会刷屏。
</p>
<textarea
value={draft}
onChange={(event) => setDraft(event.target.value)}
placeholder="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx"
rows={2}
className="w-full rounded-md border border-cyber-border-DEFAULT bg-cyber-bg-tertiary p-2 font-mono text-[11px] text-cyber-text-primary outline-none focus-visible:border-cyber-neon-cyan/50"
/>
<div className="flex items-center gap-2">
<Button
size="sm"
disabled={!draft.trim() || setWebhook.isPending}
onClick={() =>
setWebhook.mutate(draft.trim(), {
onSuccess: () => {
setDraft('')
setEditing(false)
},
})
}
>
保存
</Button>
<Button
variant="outline"
size="sm"
disabled={!draft.trim() || testWebhook.isPending}
onClick={() => testWebhook.mutate(draft.trim())}
>
保存前先测
</Button>
{status?.configured && (
<Button variant="ghost" size="sm" onClick={() => setEditing(false)}>
取消
</Button>
)}
</div>
</div>
)}
</div>
)
}