实时监控 LeetCode 平台上 Two-Sum 题目的在线刷题人数,支持国际区(US)和中国区(CN)数据对比。
- ⏱️ 实时采集:每分钟自动采集一次数据
- 📊 双 Y 轴图表:完美展示不同数量级的数据对比
- 🔍 多时间范围:支持 1 小时到 7 天的历史数据查询
- 🎨 精美可视化:基于 ECharts 的交互式图表
- 💰 零成本部署:完全基于 Cloudflare 免费套餐
- ⚡ 高性能:全球 CDN 加速,毫秒级响应
- 后端:Cloudflare Workers(定时任务 + REST API)
- 数据库:Cloudflare D1(SQLite)
- 前端:纯静态页面(ECharts)
- 部署:Cloudflare Pages
two-sum/
├── src/
│ └── index.js # Worker 主逻辑(Cron + API)
├── public/
│ └── index.html # 前端页面
├── schema.sql # 数据库表结构
├── wrangler.toml # Cloudflare 配置
├── package.json # 项目依赖
└── README.md # 本文件
- Node.js 16+
- npm 或 yarn
- Cloudflare 账号(免费)
npm installnpm run db:create复制输出的 database_id,并更新 wrangler.toml 中的配置:
[[d1_databases]]
binding = "DB"
database_name = "two-sum-db"
database_id = "粘贴你的_database_id" # 替换这里本地环境:
npm run db:init生产环境(稍后部署时运行):
npm run db:init:remote启动 Worker 开发服务器:
npm run dev访问 http://localhost:8787 查看 API 端点:
http://localhost:8787/api/data?hours=24- 获取历史数据http://localhost:8787/api/latest- 获取最新数据http://localhost:8787/api/stats- 获取统计信息http://localhost:8787/api/collect- 手动触发采集
在另一个终端启动前端服务器:
npm run pages:dev访问 http://localhost:3000 查看可视化页面。
public/index.html 中的 API_URL 配置:
const API_URL = 'http://localhost:8787'; // 本地开发时使用在浏览器访问:
http://localhost:8787/api/collect
然后查询数据库验证:
npx wrangler d1 execute two-sum-db --local --command "SELECT * FROM records ORDER BY timestamp DESC LIMIT 10"npx wrangler loginnpm run deploy部署成功后会输出 Worker URL,例如:
https://two-sum.your-subdomain.workers.dev
npm run db:init:remote由于 Cron 是每分钟执行,等待 1-2 分钟后查询数据库:
npx wrangler d1 execute two-sum-db --remote --command "SELECT COUNT(*) as count FROM records"或者手动触发采集:
curl https://two-sum.your-subdomain.workers.dev/api/collect方式一:通过 CLI 部署
修改 public/index.html 中的 API_URL:
const API_URL = 'https://two-sum.your-subdomain.workers.dev';然后部署:
npm run pages:deploy方式二:通过 GitHub 自动部署(推荐)
- 将代码推送到 GitHub
- 登录 Cloudflare Dashboard → Pages
- 点击 "Create a project" → "Connect to Git"
- 选择你的仓库
- 配置构建设置:
- Build command: (留空)
- Build output directory:
public
- 点击 "Save and Deploy"
部署完成后,你会得到一个 .pages.dev 域名。
在 Cloudflare Dashboard 中:
-
Worker 绑定自定义域名:
- Workers & Pages → two-sum → Settings → Triggers
- 添加自定义域名(需要域名托管在 Cloudflare)
-
Pages 绑定自定义域名:
- Pages → two-sum-web → Custom domains
- 添加你的域名
本地环境:
npx wrangler d1 execute two-sum-db --local --command "SELECT * FROM records LIMIT 10"生产环境:
npx wrangler d1 execute two-sum-db --remote --command "SELECT * FROM records LIMIT 10"npx wrangler d1 execute two-sum-db --remote --command "
SELECT
region,
COUNT(*) as total,
MIN(count) as min,
MAX(count) as max,
AVG(count) as avg
FROM records
GROUP BY region
"保留最近 7 天的数据:
npx wrangler d1 execute two-sum-db --remote --command "
DELETE FROM records
WHERE timestamp < strftime('%s', 'now', '-7 days') * 1000
"npx wrangler tail实时查看定时任务和 API 请求的日志输出。
修改 wrangler.toml:
# 暂停:注释掉 crons 配置
# [triggers]
# crons = ["* * * * *"]
# 恢复:取消注释
[triggers]
crons = ["* * * * *"]然后重新部署:
npm run deployLeetCode 的 WebSocket 协议可能需要调整。建议:
- 在浏览器开发者工具中观察真实的 WebSocket 消息格式
- 修改
src/index.js中的getOnlineCount()函数 - 调整消息解析逻辑
检查 wrangler.toml 中的 database_id 是否正确:
npx wrangler d1 list检查 CORS 配置和 API URL:
- 确保
public/index.html中的API_URL指向正确的 Worker 地址 - 在浏览器控制台查看网络请求错误
# 查看实时日志
npx wrangler tail
# 手动触发测试
curl https://your-worker.workers.dev/api/collect获取历史数据。
查询参数:
hours(可选):返回最近 N 小时的数据,默认 24
响应示例:
[
{
"region": "US",
"count": 1234,
"timestamp": 1707580800000
},
{
"region": "CN",
"count": 567,
"timestamp": 1707580800000
}
]获取最新数据。
响应示例:
{
"US": {
"region": "US",
"count": 1234,
"timestamp": 1707580800000
},
"CN": {
"region": "CN",
"count": 567,
"timestamp": 1707580860000
},
"updated_at": "2024-02-10T12:34:56.789Z"
}获取统计信息。
响应示例:
{
"total_records": 2880,
"by_region": [
{
"region": "US",
"count": 1440,
"min": 800,
"max": 2000,
"avg": 1234.5
},
{
"region": "CN",
"count": 1440,
"min": 300,
"max": 800,
"avg": 567.8
}
]
}- 数据聚合:对于长时间范围(7天+),可以在 API 中按小时聚合数据
- 缓存策略:添加 Cloudflare Cache API 缓存历史数据
- 告警通知:当人数异常时发送通知(Telegram/Email)
- 多题目支持:扩展到监控更多热门题目
MIT License
欢迎提交 Issue 和 Pull Request!
Made with ❤️ using Cloudflare Workers