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,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>
)
}