chore: 项目定名为 workbuddy-portal,容器化并补齐文档体系
## 项目定名 - 目录 wb_usage_portal → workbuddy-portal - Python 包 wb_usage → workbuddy_portal(含 session cookie 名) - 界面品牌统一为 WorkBuddy Portal;项目标识收敛到 config 单一来源 ## 容器化 - Dockerfile:多阶段构建,依赖层与源码解耦;非 root(uid 1000);内置健康检查 - docker-compose.yml:单服务 + 绑定挂载 data/logs + 日志轮转 + TZ - docker/entrypoint.sh:幂等初始化 → exec serve(LF 行尾,已由 .gitattributes 锁定) - docker/healthcheck.py:纯标准库探活 /login(slim 镜像无 curl) - .dockerignore / .env.example;数据目录可用 WB_DATA_DIR 等环境变量覆盖 ## 文档 - docs/USER-GUIDE.md 用户使用手册(含 9 张真实界面截图) - docs/DEPLOYMENT.md 部署运维(Docker / 裸机 / 反代 / 备份 / 推 Gitea 注册表) - docs/ARCHITECTURE.md 架构与设计说明(含已知坑与红线、验证体系) - docs/API.md 接口参考(路径 / 参数 / 返回结构 / 错误码) - docs/FAQ.md 常见问题;docs/CHANGELOG.md 变更日志 ## 修复缺陷(8) 1. /records/export 必然 500:生成器在请求上下文销毁后才迭代,改用自建连接 2. 大屏页图表全白:相对路径把 echarts.min.js 解析成 /vendor/... → 404 3. /users 500:路由已注册但模板缺失 4. 明细页日期筛选失效:视图传 f.frm、模板读 f.from 5. 配置页维护按钮全死:调用了不存在的 WBU.bindMaint() 6. 审计只能看最近 40 条:LIMIT 写死 7. 明细页多跑一条无用 SELECT:day_list() 取了没人用 8. 登录页锁定阈值未从配置注入 ## 安全加固 - 新增 safe_next():拒绝 //evil.com 等协议相对 URL 的开放重定向 - 缺 CSRF 的写请求统一 400 - 默认开启云端 HTTPS 证书校验(ssl_verify=1);Cookie 是账号凭证 - 登录失败计数表加上限与 TTL - /logout 拆分为 POST(执行) + GET(仅提示),防 <img src=/logout> 静默退出 - settings 内部簿记键 slot:* 读写两侧过滤,不再从 /api/settings 泄漏 ## 内部质量与工具 - 设置项写时校验 + 读时兜底,杜绝「一个手滑的数字让采集整个跑不起来」 - 全局 ValueError → 400:手写 query string 不再暴露 500 页面 - CSV 导出改 csv.writer 流式写入(原手工拼串,字段含逗号会串列) - bundle 明细加 20000 上限并回传 recordsTotal/recordsTruncated,不静默丢数据 - tools/smoke.py 离线回归 99 项;tools/check_live.py 真实 HTTP 56 项 - tools/shots.py Playwright 逐页截图 + JS 报错收集 ## 验证 - compileall 通过;smoke 99/99;对容器实例 check_live 56/56;截图 0 JS 报错 - 容器内采集实测成功(trigger=startup 补跑:新增 11 条)
这个提交包含在:
@@ -0,0 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Web 层包:蓝图注册。"""
|
||||
from . import api, views
|
||||
|
||||
|
||||
def register(app):
|
||||
app.register_blueprint(views.bp)
|
||||
app.register_blueprint(api.bp)
|
||||
@@ -0,0 +1,443 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""JSON API —— ECharts 大屏与后台页面的数据入口。
|
||||
|
||||
约定:
|
||||
* 全部需要登录;POST 另需 CSRF(security.check_csrf 统一拦截)
|
||||
* 参数 from/to 为 'YYYY-MM-DD';缺省则不限(即全量)
|
||||
* 列表类接口默认不返回 prompt 全文(占传输量约 80%),只有 /api/top 与
|
||||
/api/records/<request_id> 会带
|
||||
"""
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from .. import collect, config, db, query, scheduler
|
||||
from ..security import admin_required, current_user, login_required
|
||||
|
||||
bp = Blueprint("api", __name__, url_prefix="/api")
|
||||
|
||||
|
||||
def _arg(name, default=None):
|
||||
v = request.args.get(name)
|
||||
return v if v not in (None, "") else default
|
||||
|
||||
|
||||
class BadParam(ValueError):
|
||||
"""查询参数不合法 -> 由 __init__ 的 ValueError 处理器统一转成 400。"""
|
||||
|
||||
|
||||
def _win(required=False):
|
||||
"""解析 from/to,返回归一化后的 (from, to)。
|
||||
|
||||
归一化放在这里,视图与聚合层就不必再各自防御:
|
||||
无法识别的写法('abc'、'2026-09-08 12:00:00' 里的空格等)此前会一路冒到
|
||||
`datetime.strptime` 变成 HTTP 500,现在统一 400 并带上人话说明。
|
||||
"""
|
||||
raw_f, raw_t = _arg("from"), _arg("to")
|
||||
f, t = query.norm_day(raw_f), query.norm_day(raw_t)
|
||||
if raw_f and not f:
|
||||
raise BadParam("参数 from 不是合法日期:%s(正确写法 2026-09-08)" % raw_f)
|
||||
if raw_t and not t:
|
||||
raise BadParam("参数 to 不是合法日期:%s(正确写法 2026-09-08)" % raw_t)
|
||||
if f and t and f > t:
|
||||
f, t = t, f
|
||||
if required and (not f or not t):
|
||||
raise BadParam("需要同时提供 from 与 to(YYYY-MM-DD)")
|
||||
return f, t
|
||||
|
||||
|
||||
def _int(name, default, lo=1, hi=2000):
|
||||
try:
|
||||
return max(lo, min(hi, int(request.args.get(name, default))))
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
@bp.get("/manifest")
|
||||
@login_required
|
||||
def api_manifest():
|
||||
return jsonify(query.manifest(db.get_db()))
|
||||
|
||||
|
||||
@bp.get("/bundle")
|
||||
@login_required
|
||||
def api_bundle():
|
||||
"""大屏页一次拿齐:全量 daily + 窗口 dims/top/records。"""
|
||||
frm, to = _win()
|
||||
return jsonify(query.bundle(db.get_db(), frm, to, top_n=_int("topN", query.DEFAULT_TOP_N, 1, 1000)))
|
||||
|
||||
|
||||
@bp.get("/summary")
|
||||
@login_required
|
||||
def api_summary():
|
||||
conn = db.get_db()
|
||||
frm, to = _win()
|
||||
if not frm or not to:
|
||||
t = query.totals(conn)
|
||||
frm, to = t["firstDay"], t["lastDay"]
|
||||
return jsonify(query.summary(conn, frm, to))
|
||||
|
||||
|
||||
@bp.get("/daily")
|
||||
@login_required
|
||||
def api_daily():
|
||||
return jsonify({"days": query.daily(db.get_db(), *_win())})
|
||||
|
||||
|
||||
@bp.get("/dims")
|
||||
@login_required
|
||||
def api_dims():
|
||||
conn = db.get_db()
|
||||
d = query.dims(conn, *_win())
|
||||
dim = _arg("dim")
|
||||
if dim in d:
|
||||
return jsonify({dim: d[dim]})
|
||||
return jsonify(d)
|
||||
|
||||
|
||||
@bp.get("/top")
|
||||
@login_required
|
||||
def api_top():
|
||||
conn = db.get_db()
|
||||
return jsonify(query.top(conn, *_win(), n=_int("n", 50, 1, 1000)))
|
||||
|
||||
|
||||
@bp.get("/records")
|
||||
@login_required
|
||||
def api_records():
|
||||
conn = db.get_db()
|
||||
frm, to = _win()
|
||||
page = _int("page", 1, 1, 100000)
|
||||
size = _int("size", 50, 1, 500)
|
||||
r = query.records_page(conn, frm, to, model=_arg("model"), client=_arg("client"),
|
||||
q=_arg("q"), page=page, size=size, order=_arg("order", "ts_desc"),
|
||||
with_prompt=False if _arg("lean") == "1" else True)
|
||||
return jsonify(r)
|
||||
|
||||
|
||||
@bp.get("/records/<request_id>")
|
||||
@login_required
|
||||
def api_record(request_id):
|
||||
row = db.get_db().execute(
|
||||
"SELECT * FROM usage_records WHERE request_id=?", (request_id,)).fetchone()
|
||||
if row is None:
|
||||
return jsonify({"ok": False, "message": "记录不存在"}), 404
|
||||
return jsonify(dict(row))
|
||||
|
||||
|
||||
@bp.get("/runs")
|
||||
@login_required
|
||||
def api_runs():
|
||||
conn = db.get_db()
|
||||
rows = conn.execute("SELECT id,trigger,status,started_at,finished_at,duration_ms,win_from,"
|
||||
"win_to,fetched,added,dup,total,conflicts,exit_code,message"
|
||||
" FROM collect_runs ORDER BY id DESC LIMIT ?", (_int("limit", 50, 1, 500),))
|
||||
return jsonify({"items": [dict(r) for r in rows]})
|
||||
|
||||
|
||||
@bp.get("/runs/<int:run_id>")
|
||||
@login_required
|
||||
def api_run(run_id):
|
||||
row = db.get_db().execute("SELECT * FROM collect_runs WHERE id=?", (run_id,)).fetchone()
|
||||
if row is None:
|
||||
return jsonify({"ok": False, "message": "运行记录不存在"}), 404
|
||||
return jsonify(dict(row))
|
||||
|
||||
|
||||
@bp.get("/status")
|
||||
@login_required
|
||||
def api_status():
|
||||
conn = db.get_db()
|
||||
sch = scheduler.get_scheduler()
|
||||
nxt = scheduler.next_run_at(conn)
|
||||
last = conn.execute("SELECT * FROM collect_runs ORDER BY id DESC LIMIT 1").fetchone()
|
||||
running = conn.execute("SELECT COUNT(*) FROM collect_runs WHERE status='running'").fetchone()[0]
|
||||
return jsonify({
|
||||
"server_time": db.now_str(),
|
||||
"scheduler": {
|
||||
"running": sch.running,
|
||||
"enabled": db.get_bool(conn, "schedule_enabled", True),
|
||||
"times": scheduler.slots(conn),
|
||||
"next_run": nxt.strftime("%Y-%m-%d %H:%M:%S") if nxt else None,
|
||||
"catch_up": db.get_bool(conn, "catch_up", True),
|
||||
"lockfile": os.path.exists(collect.LOCK_PATH),
|
||||
},
|
||||
"running_runs": running,
|
||||
"last_run": dict(last) if last else None,
|
||||
"cookie_set": bool((db.get_setting(conn, "cookie") or "").strip()),
|
||||
})
|
||||
|
||||
|
||||
@bp.post("/collect")
|
||||
@login_required
|
||||
def api_collect():
|
||||
"""手动触发一次采集(后台线程之外同步执行,页面等待结果)。"""
|
||||
body = request.get_json(silent=True) or {}
|
||||
if not isinstance(body, dict):
|
||||
return jsonify({"ok": False, "message": "请求体必须是对象"}), 400
|
||||
frm, to = body.get("from"), body.get("to")
|
||||
try:
|
||||
kw = {}
|
||||
if frm:
|
||||
d = query.norm_day(frm)
|
||||
if not d:
|
||||
raise BadParam("起始日期不合法:%s(正确写法 2026-09-08)" % frm)
|
||||
kw["from_dt"] = datetime.strptime(d, "%Y-%m-%d")
|
||||
if to:
|
||||
d = query.norm_day(to)
|
||||
if not d:
|
||||
raise BadParam("结束日期不合法:%s(正确写法 2026-09-08)" % to)
|
||||
kw["to_dt"] = datetime.strptime(d, "%Y-%m-%d").replace(hour=23, minute=59, second=59)
|
||||
if kw.get("from_dt") and kw.get("to_dt") and kw["from_dt"] > kw["to_dt"]:
|
||||
raise BadParam("起始日期不能晚于结束日期")
|
||||
r = collect.run_sync(trigger="manual", **kw)
|
||||
except BadParam as e:
|
||||
return jsonify({"ok": False, "error": "bad_request", "message": str(e)}), 400
|
||||
except collect.Busy as e:
|
||||
return jsonify({"ok": False, "error": "busy", "message": str(e)}), 409
|
||||
except collect.ApiError as e:
|
||||
code = 401 if e.cookie_expired else 502
|
||||
return jsonify({"ok": False, "error": "cookie_expired" if e.cookie_expired else "api",
|
||||
"message": str(e)}), code
|
||||
except Exception as e: # noqa: BLE001
|
||||
return jsonify({"ok": False, "error": "internal", "message": str(e)}), 500
|
||||
db.audit(db.get_db(), "collect", (current_user() or {}).get("username"), r["message"],
|
||||
request.remote_addr)
|
||||
return jsonify({"ok": True, "result": r})
|
||||
|
||||
|
||||
@bp.get("/audit")
|
||||
@login_required
|
||||
def api_audit():
|
||||
"""操作审计分页(日志管理页用;原来只能看最近 40 条)。"""
|
||||
conn = db.get_db()
|
||||
action = _arg("action")
|
||||
page = _int("page", 1, 1, 100000)
|
||||
size = _int("size", 50, 1, 500)
|
||||
w, p = "", []
|
||||
if action:
|
||||
w, p = "WHERE action = ?", [action]
|
||||
total = conn.execute("SELECT COUNT(*) FROM audit_log %s" % w, p).fetchone()[0]
|
||||
rows = conn.execute("SELECT * FROM audit_log %s ORDER BY id DESC LIMIT ? OFFSET ?" % w,
|
||||
p + [size, (page - 1) * size])
|
||||
actions = [r[0] for r in conn.execute(
|
||||
"SELECT DISTINCT action FROM audit_log ORDER BY action")]
|
||||
return jsonify({"total": total, "page": page, "size": size,
|
||||
"pages": max(1, (total + size - 1) // size),
|
||||
"actions": actions,
|
||||
"items": [dict(r) for r in rows]})
|
||||
|
||||
|
||||
# ---------------- 维护动作(原来只有 CLI 能做) ----------------
|
||||
@bp.post("/maintenance/<action>")
|
||||
@login_required
|
||||
def api_maintenance(action):
|
||||
"""把 CLI 里的维护动作搬到页面上:补全 prompt / VACUUM / 导出 CSV。"""
|
||||
conn = db.get_db()
|
||||
user = (current_user() or {}).get("username")
|
||||
try:
|
||||
if action == "fill-prompt":
|
||||
try:
|
||||
n = collect.fill_prompt(conn, log=lambda m: None)
|
||||
except collect.ApiError as e:
|
||||
return jsonify({"ok": False, "error": "api", "message": str(e)}), 502
|
||||
msg = "补全 %d 条 User Prompt" % n
|
||||
elif action == "vacuum":
|
||||
before = os.path.getsize(config.SQLITE_PATH) if os.path.exists(config.SQLITE_PATH) else 0
|
||||
conn.execute("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
conn.execute("VACUUM")
|
||||
after = os.path.getsize(config.SQLITE_PATH) if os.path.exists(config.SQLITE_PATH) else 0
|
||||
msg = "数据库整理完成:%s → %s" % (_human(before), _human(after))
|
||||
elif action == "export-csv":
|
||||
path, n = collect.export_csv(conn)
|
||||
msg = "已导出 %d 条到 %s" % (n, os.path.relpath(path, config.BASE_DIR))
|
||||
elif action == "recount":
|
||||
n = collect.record_count(conn)
|
||||
msg = "存档当前 %d 条记录" % n
|
||||
else:
|
||||
return jsonify({"ok": False, "error": "unknown", "message": "未知维护动作"}), 404
|
||||
except Exception as e: # noqa: BLE001
|
||||
return jsonify({"ok": False, "error": "internal", "message": str(e)}), 500
|
||||
db.audit(conn, "maintenance:" + action, user, msg, request.remote_addr)
|
||||
return jsonify({"ok": True, "message": msg})
|
||||
|
||||
|
||||
def _human(n):
|
||||
for unit in ("B", "KB", "MB", "GB"):
|
||||
if n < 1024 or unit == "GB":
|
||||
return "%.1f %s" % (n, unit) if unit != "B" else "%d B" % n
|
||||
n /= 1024.0
|
||||
|
||||
|
||||
@bp.get("/settings")
|
||||
@login_required
|
||||
def api_settings_get():
|
||||
conn = db.get_db()
|
||||
s = db.get_settings(conn)
|
||||
if (s.get("cookie") or "").strip():
|
||||
s["cookie_hint"] = "%d 字符,…%s" % (len(s["cookie"]), s["cookie"][-12:])
|
||||
else:
|
||||
s["cookie_hint"] = ""
|
||||
s.pop("cookie", None) # 不回传明文凭证
|
||||
# 内部簿记键(slot:09:00 这类调度槽位标记)不属于配置项,绝不外泄
|
||||
for k in [k for k in list(s) if config.is_internal_key(k)]:
|
||||
s.pop(k, None)
|
||||
return jsonify(s)
|
||||
|
||||
|
||||
@bp.post("/settings")
|
||||
@login_required
|
||||
def api_settings_post():
|
||||
conn = db.get_db()
|
||||
body = request.get_json(silent=True) or {}
|
||||
if not isinstance(body, dict):
|
||||
return jsonify({"ok": False, "message": "请求体必须是对象"}), 400
|
||||
changed, errors, ignored = [], [], []
|
||||
for k, v in body.items():
|
||||
if config.is_internal_key(k):
|
||||
ignored.append(k)
|
||||
continue # slot:* 是调度簿记,不允许前台写
|
||||
if k == "cookie":
|
||||
if not str(v).strip():
|
||||
continue # 空值不动,避免误清
|
||||
if str(v).strip().lower() in ("__clear__", "-"):
|
||||
db.set_setting(conn, "cookie", "")
|
||||
changed.append(k)
|
||||
continue
|
||||
val, err = config.normalize_setting(k, v)
|
||||
if err:
|
||||
errors.append(err)
|
||||
continue
|
||||
db.set_setting(conn, k, val)
|
||||
changed.append(k)
|
||||
if errors:
|
||||
db.audit(conn, "settings_rejected", (current_user() or {}).get("username"),
|
||||
";".join(errors)[:500], request.remote_addr)
|
||||
return jsonify({"ok": False, "error": "invalid", "message": ";".join(errors),
|
||||
"errors": errors, "changed": sorted(changed)}), 400
|
||||
# 调整调度配置后清掉槽位标记,让新时刻立即生效
|
||||
if {"schedule_times", "schedule_enabled"} & set(changed):
|
||||
conn.execute("DELETE FROM settings WHERE key LIKE ?", (scheduler.SLOT_PREFIX + "%",))
|
||||
db.audit(conn, "settings", (current_user() or {}).get("username"),
|
||||
"修改:" + (",".join(sorted(changed)) or "(无变化)"), request.remote_addr)
|
||||
return jsonify({"ok": True, "changed": sorted(changed), "ignored": sorted(ignored)})
|
||||
|
||||
|
||||
@bp.post("/password")
|
||||
@login_required
|
||||
def api_password():
|
||||
from ..security import hash_password, verify_password
|
||||
conn = db.get_db()
|
||||
body = request.get_json(silent=True) or {}
|
||||
u = current_user()
|
||||
row = conn.execute("SELECT * FROM users WHERE id=?", (u["id"],)).fetchone()
|
||||
if row is None or not verify_password(row["password_hash"], body.get("old") or ""):
|
||||
return jsonify({"ok": False, "message": "原密码不正确"}), 400
|
||||
new = (body.get("new") or "").strip()
|
||||
err = _check_password(new, body.get("new2"))
|
||||
if err:
|
||||
return jsonify({"ok": False, "message": err}), 400
|
||||
conn.execute("UPDATE users SET password_hash=? WHERE id=?", (hash_password(new), u["id"]))
|
||||
db.audit(conn, "password", u["username"], "修改登录密码", request.remote_addr)
|
||||
return jsonify({"ok": True, "message": "密码已更新"})
|
||||
|
||||
|
||||
# ---------------- 用户管理(原来只有 CLI passwd) ----------------
|
||||
def _check_password(new, new2=None):
|
||||
if len(new or "") < 6:
|
||||
return "密码至少 6 位"
|
||||
if len(new) > 128:
|
||||
return "密码过长(上限 128 位)"
|
||||
if new2 is not None and new2 != new:
|
||||
return "两次输入的新密码不一致"
|
||||
return None
|
||||
|
||||
|
||||
@bp.get("/users")
|
||||
@admin_required
|
||||
def api_users():
|
||||
rows = db.get_db().execute(
|
||||
"SELECT id,username,display_name,is_admin,created_at,last_login_at,login_count"
|
||||
" FROM users ORDER BY id").fetchall()
|
||||
return jsonify({"items": [dict(r) for r in rows]})
|
||||
|
||||
|
||||
@bp.post("/users")
|
||||
@admin_required
|
||||
def api_user_create():
|
||||
from ..security import hash_password
|
||||
conn = db.get_db()
|
||||
body = request.get_json(silent=True) or {}
|
||||
name = (body.get("username") or "").strip()
|
||||
pwd = (body.get("password") or "").strip()
|
||||
if not name or len(name) > 32:
|
||||
return jsonify({"ok": False, "message": "用户名必填且不超过 32 字符"}), 400
|
||||
err = _check_password(pwd, body.get("password2"))
|
||||
if err:
|
||||
return jsonify({"ok": False, "message": err}), 400
|
||||
exist = conn.execute("SELECT id FROM users WHERE username=?", (name,)).fetchone()
|
||||
if exist:
|
||||
return jsonify({"ok": False, "message": "用户名已存在"}), 400
|
||||
conn.execute("INSERT INTO users(username,password_hash,display_name,is_admin,created_at)"
|
||||
" VALUES(?,?,?,?,?)",
|
||||
(name, hash_password(pwd), (body.get("display_name") or name).strip()[:64],
|
||||
1 if str(body.get("is_admin", "1")) in ("1", "true", "on") else 0,
|
||||
db.now_str()))
|
||||
db.audit(conn, "user_create", (current_user() or {}).get("username"), "新建用户 " + name,
|
||||
request.remote_addr)
|
||||
return jsonify({"ok": True, "message": "已创建用户 " + name})
|
||||
|
||||
|
||||
@bp.post("/users/<int:uid>")
|
||||
@admin_required
|
||||
def api_user_update(uid):
|
||||
from ..security import hash_password
|
||||
conn = db.get_db()
|
||||
row = conn.execute("SELECT * FROM users WHERE id=?", (uid,)).fetchone()
|
||||
if row is None:
|
||||
return jsonify({"ok": False, "message": "用户不存在"}), 404
|
||||
body = request.get_json(silent=True) or {}
|
||||
me = current_user()
|
||||
changed = []
|
||||
if "display_name" in body:
|
||||
conn.execute("UPDATE users SET display_name=? WHERE id=?",
|
||||
((body.get("display_name") or "").strip()[:64], uid))
|
||||
changed.append("显示名")
|
||||
if "is_admin" in body:
|
||||
v = 1 if str(body.get("is_admin")) in ("1", "true", "on") else 0
|
||||
if uid == me["id"] and not v:
|
||||
return jsonify({"ok": False, "message": "不能取消自己的管理员身份"}), 400
|
||||
conn.execute("UPDATE users SET is_admin=? WHERE id=?", (v, uid))
|
||||
changed.append("管理员")
|
||||
pwd = (body.get("password") or "").strip()
|
||||
if pwd:
|
||||
err = _check_password(pwd, body.get("password2"))
|
||||
if err:
|
||||
return jsonify({"ok": False, "message": err}), 400
|
||||
conn.execute("UPDATE users SET password_hash=? WHERE id=?", (hash_password(pwd), uid))
|
||||
changed.append("密码")
|
||||
if not changed:
|
||||
return jsonify({"ok": False, "message": "没有要修改的内容"}), 400
|
||||
db.audit(conn, "user_update", me["username"],
|
||||
"修改用户 %s:%s" % (row["username"], "、".join(changed)), request.remote_addr)
|
||||
return jsonify({"ok": True, "message": "已更新:" + "、".join(changed)})
|
||||
|
||||
|
||||
@bp.post("/users/<int:uid>/delete")
|
||||
@admin_required
|
||||
def api_user_delete(uid):
|
||||
conn = db.get_db()
|
||||
me = current_user()
|
||||
row = conn.execute("SELECT * FROM users WHERE id=?", (uid,)).fetchone()
|
||||
if row is None:
|
||||
return jsonify({"ok": False, "message": "用户不存在"}), 404
|
||||
if uid == me["id"]:
|
||||
return jsonify({"ok": False, "message": "不能删除当前登录的自己"}), 400
|
||||
n = conn.execute("SELECT COUNT(*) FROM users").fetchone()[0]
|
||||
if n <= 1:
|
||||
return jsonify({"ok": False, "message": "至少要保留一个账号"}), 400
|
||||
conn.execute("DELETE FROM users WHERE id=?", (uid,))
|
||||
db.audit(conn, "user_delete", me["username"], "删除用户 " + row["username"],
|
||||
request.remote_addr)
|
||||
return jsonify({"ok": True, "message": "已删除 " + row["username"]})
|
||||
@@ -0,0 +1,417 @@
|
||||
/* ============================================================
|
||||
WorkBuddy Portal —— 统一视觉系统
|
||||
设计令牌与大屏页(static/dashboard/index.html)保持一致,
|
||||
两处用同一套色板/圆角/间距,避免后台与大屏像两个产品。
|
||||
============================================================ */
|
||||
:root {
|
||||
/* 底色与层次 */
|
||||
--bg: #080d1a;
|
||||
--panel: rgba(255, 255, 255, .045);
|
||||
--panel-hi: rgba(255, 255, 255, .07);
|
||||
--panel-dim: rgba(0, 0, 0, .30);
|
||||
--line: rgba(255, 255, 255, .09);
|
||||
--line-soft: rgba(255, 255, 255, .06);
|
||||
|
||||
/* 文本 */
|
||||
--text: #e8edf7;
|
||||
--sub: #8b9bb4;
|
||||
--dim: #64748b;
|
||||
|
||||
/* 主色板(与大屏 ECharts PALETTE 同源) */
|
||||
--cyan: #22d3ee;
|
||||
--violet: #a78bfa;
|
||||
--amber: #fbbf24;
|
||||
--green: #34d399;
|
||||
--red: #f87171;
|
||||
--blue: #60a5fa;
|
||||
--pink: #f472b6;
|
||||
|
||||
/* 语义色(用量口径:升用琥珀、降用青,刻意不用红绿) */
|
||||
--up: var(--amber);
|
||||
--down: var(--cyan);
|
||||
|
||||
/* 形状与节奏 */
|
||||
--r-card: 14px;
|
||||
--r-ctl: 9px;
|
||||
--r-pill: 6px;
|
||||
--gap: 16px;
|
||||
--shadow: 0 12px 34px rgba(0, 0, 0, .42);
|
||||
|
||||
color-scheme: dark;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
html, body { margin: 0; padding: 0; }
|
||||
|
||||
body {
|
||||
background:
|
||||
radial-gradient(1100px 600px at 12% -10%, rgba(34, 211, 238, .10), transparent 60%),
|
||||
radial-gradient(900px 500px at 100% 0%, rgba(167, 139, 250, .10), transparent 55%),
|
||||
var(--bg);
|
||||
color: var(--text);
|
||||
font: 13.5px/1.65 "Microsoft YaHei", "PingFang SC", system-ui, -apple-system,
|
||||
"Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
min-height: 100vh;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
a { color: var(--cyan); text-decoration: none; transition: color .15s; }
|
||||
a:hover { text-decoration: underline; }
|
||||
/* 表单控件必须显式继承字体:曾经写过 `font: 13px/1.5 inherit`,
|
||||
那是非法声明(简写里不能出现 inherit 作为字族),整条被浏览器丢弃,
|
||||
结果输入框用回浏览器默认字体,与页面格格不入。 */
|
||||
button, input, select, textarea { font-family: inherit; font-size: 13px; }
|
||||
|
||||
code {
|
||||
background: rgba(255, 255, 255, .07); padding: 1px 5px; border-radius: 4px;
|
||||
font: 12px/1.5 ui-monospace, Consolas, "Cascadia Mono", monospace;
|
||||
color: var(--cyan);
|
||||
}
|
||||
.mono, .logbox, .fullprompt {
|
||||
font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
|
||||
}
|
||||
.mono { font-size: 12px; }
|
||||
.sm { font-size: 11.5px; }
|
||||
.nowrap { white-space: nowrap; }
|
||||
.num, .tabular { font-variant-numeric: tabular-nums; }
|
||||
.muted { color: var(--sub); }
|
||||
.right { text-align: right; }
|
||||
/* 页面标题里嵌的关键数字(如总积分),给一点强调但不喧宾夺主 */
|
||||
.hl { color: var(--cyan); font-variant-numeric: tabular-nums; }
|
||||
/* 0 积分的记录整行数字变暗,扫一眼就能跳过长尾 */
|
||||
.tbl td.zero { color: var(--dim); }
|
||||
|
||||
/* 可访问性:键盘焦点一定要看得见 */
|
||||
:focus-visible { outline: 2px solid rgba(34, 211, 238, .65); outline-offset: 2px; }
|
||||
|
||||
/* ---------------- 顶栏 ---------------- */
|
||||
.topbar {
|
||||
display: flex; align-items: center; gap: 18px;
|
||||
padding: 0 22px; height: 58px;
|
||||
background: rgba(10, 16, 28, .86);
|
||||
border-bottom: 1px solid var(--line);
|
||||
backdrop-filter: blur(10px);
|
||||
position: sticky; top: 0; z-index: 50;
|
||||
}
|
||||
.brand { display: flex; align-items: center; gap: 9px; font-size: 14px; letter-spacing: .2px; }
|
||||
.brand a { color: var(--text); }
|
||||
.brand a:hover { text-decoration: none; color: var(--cyan); }
|
||||
.brand .dot {
|
||||
width: 9px; height: 9px; border-radius: 50%; background: var(--cyan);
|
||||
box-shadow: 0 0 12px var(--cyan); flex: 0 0 auto;
|
||||
}
|
||||
.brand .ver { color: var(--dim); font-size: 11px; }
|
||||
|
||||
.topbar nav { display: flex; gap: 3px; margin-left: 6px; overflow-x: auto; }
|
||||
.topbar nav a {
|
||||
padding: 6px 13px; border-radius: var(--r-ctl); color: var(--sub);
|
||||
font-size: 13px; white-space: nowrap; position: relative;
|
||||
}
|
||||
.topbar nav a:hover { background: var(--panel-hi); color: var(--text); text-decoration: none; }
|
||||
.topbar nav a.on {
|
||||
background: linear-gradient(135deg, rgba(34, 211, 238, .22), rgba(167, 139, 250, .22));
|
||||
color: #fff; font-weight: 600;
|
||||
}
|
||||
.topbar .me { margin-left: auto; display: flex; align-items: center; gap: 10px; flex: 0 0 auto; }
|
||||
.topbar .who { color: var(--sub); font-size: 12.5px; }
|
||||
.topbar .who b { color: var(--text); font-weight: 600; }
|
||||
|
||||
/* ---------------- 布局 ---------------- */
|
||||
.wrap { max-width: 1480px; margin: 0 auto; padding: 20px 22px 60px; }
|
||||
.pagehead {
|
||||
display: flex; align-items: flex-end; justify-content: space-between;
|
||||
gap: 14px; flex-wrap: wrap; margin-bottom: var(--gap);
|
||||
}
|
||||
.pagehead h1 {
|
||||
font-size: 20px; margin: 0; font-weight: 700; letter-spacing: .3px;
|
||||
display: flex; align-items: center; gap: 10px;
|
||||
}
|
||||
.pagehead h1::before {
|
||||
content: ""; width: 4px; height: 19px; border-radius: 2px;
|
||||
background: linear-gradient(180deg, var(--cyan), var(--violet)); flex: 0 0 auto;
|
||||
}
|
||||
.pagehead .lead { color: var(--sub); font-size: 12.5px; margin: 4px 0 0 14px; }
|
||||
.pagehead .actions { display: flex; gap: 10px; flex-wrap: wrap; }
|
||||
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); margin-bottom: var(--gap); }
|
||||
.grid3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: var(--gap); margin-bottom: var(--gap); }
|
||||
|
||||
/* ---------------- 卡片 ---------------- */
|
||||
.card {
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--line); border-radius: var(--r-card);
|
||||
padding: 16px 18px; margin-bottom: var(--gap);
|
||||
}
|
||||
.card > h2, .card > .cardhead > h2 {
|
||||
font-size: 14px; margin: 0 0 12px; font-weight: 600; letter-spacing: .2px;
|
||||
display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
|
||||
}
|
||||
.card > .cardhead > h2 { margin-bottom: 0; }
|
||||
.card > h2::before, .card > .cardhead > h2::before {
|
||||
content: ""; width: 3px; height: 13px; border-radius: 2px;
|
||||
background: var(--cyan); flex: 0 0 auto;
|
||||
}
|
||||
.cardhead {
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
gap: 12px; flex-wrap: wrap; margin-bottom: 12px;
|
||||
}
|
||||
.cardhead > h2 { margin-bottom: 0; }
|
||||
.card h3 { font-size: 13px; margin: 18px 0 10px; font-weight: 600; color: var(--text); }
|
||||
.card h3:first-child { margin-top: 0; }
|
||||
.hint { color: var(--sub); font-size: 12px; line-height: 1.75; }
|
||||
.hint a { text-decoration: underline; text-decoration-color: rgba(34, 211, 238, .4); }
|
||||
.card > .hint:last-child { margin-bottom: 0; }
|
||||
.sect-divider { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }
|
||||
|
||||
/* ---------------- KPI ---------------- */
|
||||
.kpis {
|
||||
display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: 14px; margin-bottom: var(--gap);
|
||||
}
|
||||
.kpi {
|
||||
background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-card);
|
||||
padding: 14px 16px; position: relative; overflow: hidden;
|
||||
}
|
||||
.kpi::after {
|
||||
content: ""; position: absolute; left: 0; top: 0; width: 3px; height: 100%;
|
||||
background: var(--c, var(--cyan));
|
||||
}
|
||||
.kpi > span { color: var(--sub); font-size: 12px; display: block; letter-spacing: .2px; }
|
||||
.kpi > b {
|
||||
display: block; font-size: 23px; margin: 7px 0 4px; font-weight: 700;
|
||||
font-variant-numeric: tabular-nums; letter-spacing: .3px;
|
||||
}
|
||||
.kpi > i { color: var(--sub); font-size: 11.5px; font-style: normal; display: block; }
|
||||
.kpi > i .delta { color: var(--up); font-variant-numeric: tabular-nums; }
|
||||
.kpi > i .delta.dn { color: var(--down); }
|
||||
|
||||
/* ---------------- 表格 ---------------- */
|
||||
/* 表格必须包在 .tablewrap 里:窄屏时横向滚动,而不是把卡片撑破 */
|
||||
.tablewrap { overflow-x: auto; margin: 0 -2px; }
|
||||
.tablewrap::-webkit-scrollbar { height: 8px; }
|
||||
.tablewrap::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, .14); border-radius: 4px; }
|
||||
.tbl { width: 100%; border-collapse: collapse; font-size: 12.5px; }
|
||||
.tbl th, .tbl td {
|
||||
padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--line-soft);
|
||||
vertical-align: top;
|
||||
}
|
||||
.tbl th {
|
||||
color: var(--sub); font-weight: 500; font-size: 11.5px;
|
||||
white-space: nowrap; letter-spacing: .3px;
|
||||
position: sticky; top: 0; background: rgba(12, 18, 32, .96);
|
||||
backdrop-filter: blur(4px); z-index: 1;
|
||||
}
|
||||
.tbl tbody tr:hover { background: rgba(255, 255, 255, .028); }
|
||||
.tbl tbody tr:last-child td { border-bottom: 0; }
|
||||
.tbl td.num, .tbl th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
|
||||
.tbl td.empty, .tbl td.empty:hover { text-align: center; color: var(--sub); padding: 28px; }
|
||||
.tbl td.px { max-width: 460px; min-width: 220px; }
|
||||
.tbl details summary { cursor: pointer; color: var(--sub); }
|
||||
.tbl details summary:hover { color: var(--text); }
|
||||
.tbl details[open] summary { color: var(--cyan); }
|
||||
|
||||
.scroll-y { max-height: 340px; overflow: auto; }
|
||||
|
||||
.kv { width: 100%; border-collapse: collapse; font-size: 12.5px; }
|
||||
.kv th {
|
||||
width: 118px; color: var(--sub); text-align: left; font-weight: 500;
|
||||
padding: 7px 10px 7px 0; vertical-align: top; white-space: nowrap;
|
||||
}
|
||||
.kv td { padding: 7px 0; }
|
||||
|
||||
.fullprompt {
|
||||
margin-top: 9px; padding: 11px 13px; background: var(--panel-dim);
|
||||
border: 1px solid var(--line); border-radius: 8px;
|
||||
white-space: pre-wrap; word-break: break-word; color: var(--text);
|
||||
font-size: 12px; line-height: 1.75; max-height: 320px; overflow: auto;
|
||||
}
|
||||
|
||||
/* ---------------- 标签 / 徽章 ---------------- */
|
||||
.tag {
|
||||
display: inline-block; padding: 1px 7px; border-radius: var(--r-pill);
|
||||
font-size: 11px; line-height: 17px; white-space: nowrap;
|
||||
background: rgba(255, 255, 255, .08); color: var(--sub); vertical-align: 1px;
|
||||
}
|
||||
.tag.ok { background: rgba(52, 211, 153, .16); color: var(--green); }
|
||||
.tag.warn { background: rgba(251, 191, 36, .16); color: var(--amber); }
|
||||
.tag.bad { background: rgba(248, 113, 113, .16); color: var(--red); }
|
||||
.tag.info { background: rgba(96, 165, 250, .16); color: var(--blue); }
|
||||
.tag.accent { background: rgba(167, 139, 250, .18); color: var(--violet); }
|
||||
.tag.mute { background: rgba(255, 255, 255, .06); color: var(--dim); }
|
||||
|
||||
.kbd {
|
||||
display: inline-block; padding: 0 6px; border-radius: 4px;
|
||||
border: 1px solid var(--line); background: var(--panel-dim);
|
||||
font: 11.5px/18px ui-monospace, Consolas, monospace; color: var(--sub);
|
||||
}
|
||||
|
||||
/* ---------------- 按钮 ---------------- */
|
||||
.btn {
|
||||
display: inline-flex; align-items: center; justify-content: center; gap: 6px;
|
||||
padding: 7px 14px; border-radius: var(--r-ctl); border: 1px solid var(--line);
|
||||
background: var(--panel-hi); color: var(--text); font-size: 12.5px;
|
||||
cursor: pointer; transition: background .15s, border-color .15s, box-shadow .15s, filter .15s;
|
||||
white-space: nowrap; text-decoration: none;
|
||||
}
|
||||
.btn:hover { background: rgba(255, 255, 255, .11); text-decoration: none; }
|
||||
.btn.primary {
|
||||
background: linear-gradient(135deg, rgba(34, 211, 238, .26), rgba(167, 139, 250, .26));
|
||||
border-color: rgba(34, 211, 238, .42); color: #fff; font-weight: 600;
|
||||
}
|
||||
.btn.primary:hover { border-color: var(--cyan); box-shadow: 0 0 14px rgba(34, 211, 238, .22); }
|
||||
.btn.ghost { background: transparent; color: var(--sub); }
|
||||
.btn.ghost:hover { color: var(--text); background: var(--panel-hi); }
|
||||
.btn.danger { border-color: rgba(248, 113, 113, .34); color: var(--red); background: rgba(248, 113, 113, .10); }
|
||||
.btn.danger:hover { background: rgba(248, 113, 113, .18); }
|
||||
.btn.sm { padding: 4px 10px; font-size: 12px; }
|
||||
.btn[disabled], .btn[aria-disabled=true] { opacity: .5; cursor: not-allowed; }
|
||||
.btnrow { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
|
||||
form .btn { margin-top: 8px; }
|
||||
|
||||
/* ---------------- 表单 ---------------- */
|
||||
label { display: block; color: var(--sub); font-size: 12px; }
|
||||
label input, label select, label textarea, .inp {
|
||||
width: 100%; margin-top: 5px; padding: 8px 10px; border-radius: 8px;
|
||||
background: var(--panel-dim); border: 1px solid var(--line); color: var(--text);
|
||||
font-size: 13px; line-height: 1.5; outline: none; transition: border-color .15s, background .15s;
|
||||
}
|
||||
label input:hover, label select:hover, label textarea:hover { border-color: rgba(255, 255, 255, .16); }
|
||||
label input:focus, label select:focus, label textarea:focus {
|
||||
border-color: var(--cyan); background: rgba(0, 0, 0, .42);
|
||||
}
|
||||
label textarea {
|
||||
font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
|
||||
font-size: 12px; line-height: 1.6; resize: vertical;
|
||||
}
|
||||
label.col { margin-bottom: 13px; }
|
||||
label.row { display: flex; align-items: center; gap: 12px; margin-bottom: 11px; }
|
||||
label.row > span { flex: 0 0 132px; }
|
||||
label.row input, label.row select { margin-top: 0; }
|
||||
label.row.inline { display: inline-flex; margin-bottom: 0; }
|
||||
label.row.inline > span { flex: 0 0 auto; }
|
||||
label .unit { color: var(--dim); font-size: 11.5px; margin-left: 8px; flex: 0 0 auto; }
|
||||
.field-err { color: var(--red); font-size: 11.5px; margin-top: 4px; }
|
||||
select option { background: #111a2e; color: var(--text); }
|
||||
|
||||
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
|
||||
.filters label { flex: 1 1 152px; }
|
||||
.filters label input, .filters label select { min-width: 110px; }
|
||||
.filters .btn { margin-top: 0; }
|
||||
|
||||
/* 快捷区间按钮组:卡片标题右侧、筛选条里都可能出现,所以不限定父级 */
|
||||
.quick { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
|
||||
|
||||
/* 行内小输入框(用户列表里直接改显示名/权限) */
|
||||
.inp-sm { padding: 4px 8px; font-size: 12px; margin-top: 0; width: auto; min-width: 96px; }
|
||||
.tbl select.inp-sm { min-width: 84px; }
|
||||
|
||||
/* 分段控件(与大屏页 .seg 一致) */
|
||||
.seg {
|
||||
display: inline-flex; background: var(--panel-hi); border: 1px solid var(--line);
|
||||
border-radius: var(--r-ctl); overflow: hidden;
|
||||
}
|
||||
.seg a, .seg button {
|
||||
background: transparent; border: 0; color: var(--sub); padding: 5px 12px;
|
||||
font-size: 12.5px; cursor: pointer; transition: .15s; font-family: inherit;
|
||||
text-decoration: none; white-space: nowrap;
|
||||
}
|
||||
.seg a:hover, .seg button:hover { color: var(--text); background: rgba(255, 255, 255, .06); text-decoration: none; }
|
||||
.seg a.on, .seg button.on {
|
||||
background: linear-gradient(135deg, rgba(34, 211, 238, .28), rgba(167, 139, 250, .28));
|
||||
color: #fff; font-weight: 600;
|
||||
}
|
||||
|
||||
/* ---------------- 分页 ---------------- */
|
||||
.pager { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; align-items: center; }
|
||||
.pager a, .pager .cur, .pager .gap {
|
||||
padding: 4px 11px; border-radius: 7px; border: 1px solid var(--line);
|
||||
font-size: 12px; color: var(--sub); min-width: 34px; text-align: center;
|
||||
}
|
||||
.pager a:hover { color: var(--text); border-color: rgba(34, 211, 238, .45); text-decoration: none; }
|
||||
.pager .cur {
|
||||
background: linear-gradient(135deg, rgba(34, 211, 238, .24), rgba(167, 139, 250, .24));
|
||||
color: #fff; border-color: transparent; font-weight: 600;
|
||||
}
|
||||
.pager .gap { border-color: transparent; color: var(--dim); }
|
||||
.pager .meta { margin-left: auto; color: var(--sub); font-size: 11.5px; }
|
||||
|
||||
/* ---------------- 提示 / 日志 ---------------- */
|
||||
.flashes { margin-bottom: var(--gap); }
|
||||
.flash {
|
||||
padding: 11px 14px; border-radius: var(--r-ctl); font-size: 12.5px;
|
||||
margin-bottom: 8px; border: 1px solid var(--line);
|
||||
background: rgba(34, 211, 238, .10); color: var(--text);
|
||||
line-height: 1.7;
|
||||
}
|
||||
.flash.error { background: rgba(248, 113, 113, .12); border-color: rgba(248, 113, 113, .3); }
|
||||
.flash.warn { background: rgba(251, 191, 36, .12); border-color: rgba(251, 191, 36, .3); }
|
||||
.flash.ok { background: rgba(52, 211, 153, .12); border-color: rgba(52, 211, 153, .3); }
|
||||
.flash:last-child { margin-bottom: 0; }
|
||||
.logbox {
|
||||
background: rgba(0, 0, 0, .40); border: 1px solid var(--line); border-radius: var(--r-ctl);
|
||||
padding: 12px 14px; max-height: 430px; overflow: auto; margin: 0;
|
||||
font-size: 11.5px; line-height: 1.75; white-space: pre-wrap;
|
||||
word-break: break-word; color: #c6d3e6;
|
||||
}
|
||||
.logbox:empty::before { content: "(暂无内容)"; color: var(--dim); }
|
||||
.empty-state {
|
||||
padding: 34px 16px; text-align: center; color: var(--sub); font-size: 12.5px;
|
||||
border: 1px dashed var(--line); border-radius: var(--r-ctl);
|
||||
}
|
||||
.empty-state b { display: block; color: var(--text); font-size: 13.5px; margin-bottom: 6px; font-weight: 600; }
|
||||
|
||||
/* ---------------- 登录 ---------------- */
|
||||
.loginwrap {
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
min-height: calc(100vh - 140px); padding: 24px 16px;
|
||||
}
|
||||
.login { width: 100%; max-width: 400px; padding: 28px 28px 22px; margin: 0; }
|
||||
.login .logo {
|
||||
width: 44px; height: 44px; border-radius: 13px; margin-bottom: 14px;
|
||||
background: linear-gradient(135deg, var(--cyan), var(--violet));
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
font-size: 20px; font-weight: 800; color: #061019;
|
||||
}
|
||||
.login h1 { font-size: 19px; margin: 0 0 5px; }
|
||||
.login .hint { margin: 0 0 20px; }
|
||||
.login label { margin-bottom: 14px; }
|
||||
.login .btn { width: 100%; padding: 10px; margin-top: 4px; }
|
||||
.login .foot-note {
|
||||
margin: 16px 0 0; padding-top: 14px; border-top: 1px solid var(--line);
|
||||
color: var(--dim); font-size: 11.5px; line-height: 1.7;
|
||||
}
|
||||
|
||||
/* ---------------- 错误页 ---------------- */
|
||||
.errpage { text-align: center; padding: 66px 24px; }
|
||||
.errpage .code {
|
||||
font-size: 52px; font-weight: 800; line-height: 1; margin: 0 0 12px;
|
||||
background: linear-gradient(135deg, var(--cyan), var(--violet));
|
||||
-webkit-background-clip: text; background-clip: text; color: transparent;
|
||||
}
|
||||
.errpage p { color: var(--sub); margin: 0 0 20px; }
|
||||
|
||||
.foot {
|
||||
text-align: center; color: var(--dim); font-size: 11.5px;
|
||||
padding: 18px 22px 30px; line-height: 1.9;
|
||||
}
|
||||
.foot code { font-size: 11px; }
|
||||
|
||||
/* ---------------- 响应式 ---------------- */
|
||||
@media (max-width: 1100px) {
|
||||
.grid2 { grid-template-columns: 1fr; }
|
||||
}
|
||||
@media (max-width: 820px) {
|
||||
.topbar { gap: 10px; padding: 0 12px; height: auto; flex-wrap: wrap; padding-bottom: 8px; }
|
||||
.topbar nav { order: 3; width: 100%; margin-left: 0; padding-bottom: 2px; }
|
||||
.topbar .me { margin-left: auto; }
|
||||
.wrap { padding: 14px 12px 48px; }
|
||||
.pagehead { align-items: flex-start; }
|
||||
.pagehead h1 { font-size: 18px; }
|
||||
.kpi > b { font-size: 20px; }
|
||||
label.row { flex-direction: column; align-items: stretch; gap: 4px; }
|
||||
label.row > span { flex: 0 0 auto; }
|
||||
.card { padding: 14px; }
|
||||
.login { padding: 22px 18px 18px; }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
* { transition: none !important; animation: none !important; }
|
||||
}
|
||||
文件差异内容过多而无法显示
加载差异
文件差异因一行或多行过长而隐藏
@@ -0,0 +1,16 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
|
||||
<defs>
|
||||
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0" stop-color="#22d3ee"/>
|
||||
<stop offset="1" stop-color="#a78bfa"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<rect x="2" y="2" width="60" height="60" rx="16" fill="#0b1220"/>
|
||||
<rect x="2.75" y="2.75" width="58.5" height="58.5" rx="15.25" fill="none"
|
||||
stroke="url(#g)" stroke-width="1.5" opacity=".55"/>
|
||||
<g fill="url(#g)">
|
||||
<rect x="14" y="34" width="8" height="16" rx="2.5"/>
|
||||
<rect x="28" y="22" width="8" height="28" rx="2.5"/>
|
||||
<rect x="42" y="14" width="8" height="36" rx="2.5"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
之后 宽度: | 高度: | 大小: 660 B |
@@ -0,0 +1,193 @@
|
||||
/* 门户页面的轻量交互:统一带 CSRF 的请求、表单提交、日志尾部加载、维护动作 */
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
function token() {
|
||||
return window.WB_CSRF || "";
|
||||
}
|
||||
|
||||
// 所有提示都先转义再插入:服务端 message 里可能带用户输入(用户名、文件名),
|
||||
// 直接 innerHTML 等于给它一个 XSS 入口。
|
||||
function esc(s) {
|
||||
return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) {
|
||||
return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c];
|
||||
});
|
||||
}
|
||||
|
||||
function flashBox() {
|
||||
var box = document.getElementById("collectMsg");
|
||||
if (!box) {
|
||||
box = document.createElement("div");
|
||||
box.id = "collectMsg";
|
||||
box.className = "flash";
|
||||
var head = document.querySelector(".pagehead");
|
||||
(head ? head.parentNode : document.body).insertBefore(box, head ? head.nextSibling : null);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
|
||||
// say(文本, 类别):文本按纯文本渲染,\n 转成换行
|
||||
function say(msg, cat) {
|
||||
var box = flashBox();
|
||||
box.style.display = "block";
|
||||
box.className = "flash " + (cat || "");
|
||||
box.innerHTML = esc(msg).replace(/\n/g, "<br>");
|
||||
if (cat !== "warn") {
|
||||
window.clearTimeout(say._t);
|
||||
say._t = window.setTimeout(function () { box.style.display = "none"; }, 12000);
|
||||
}
|
||||
}
|
||||
|
||||
function post(url, data, opts) {
|
||||
opts = opts || {};
|
||||
return fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-CSRF-Token": token() },
|
||||
body: JSON.stringify(data || {})
|
||||
}).then(function (r) {
|
||||
return r.json().catch(function () { return { ok: false, message: "响应不是 JSON(HTTP " + r.status + ")" }; })
|
||||
.then(function (j) { return { status: r.status, body: j }; });
|
||||
}).then(function (res) {
|
||||
if (res.status === 401) {
|
||||
say("登录已失效,即将跳转登录页…", "warn");
|
||||
window.setTimeout(function () { location.href = "/login?next=" + encodeURIComponent(location.pathname); }, 1200);
|
||||
throw new Error("unauthorized");
|
||||
}
|
||||
return res.body;
|
||||
});
|
||||
}
|
||||
|
||||
function formData(form) {
|
||||
var out = {};
|
||||
Array.prototype.forEach.call(form.elements, function (el) {
|
||||
if (!el.name || el.type === "submit" || el.name === "_csrf") return;
|
||||
if (el.type === "checkbox") { out[el.name] = el.checked ? "1" : "0"; return; }
|
||||
if (el.type === "radio") { if (el.checked) out[el.name] = el.value; return; }
|
||||
out[el.name] = el.value;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
// 失败提示的公共尾巴:把「下一步怎么办」直接告诉用户
|
||||
function hintOf(j) {
|
||||
if (!j) return "";
|
||||
if (j.error === "cookie_expired") return "\n请到「配置管理」更新 Cookie 与 User-Agent(两者须取自同一次浏览器请求)。";
|
||||
if (j.error === "busy") return "\n已有采集在进行,可稍后重试,或到「日志管理」查看进度。";
|
||||
if (j.errors && j.errors.length) return "\n" + j.errors.join("\n");
|
||||
return "";
|
||||
}
|
||||
|
||||
// 通用表单绑定:提交到指定 API,成功后按 reload 与否决定刷新
|
||||
function bindForm(sel, url, opts) {
|
||||
opts = opts || {};
|
||||
var form = document.querySelector(sel);
|
||||
if (!form) return;
|
||||
form.addEventListener("submit", function (e) {
|
||||
e.preventDefault();
|
||||
var d = formData(form);
|
||||
if (opts.validate) {
|
||||
var err = opts.validate(d);
|
||||
if (err) { say(err, "warn"); return; }
|
||||
}
|
||||
if (opts.confirmText && !window.confirm(opts.confirmText)) return;
|
||||
var btn = form.querySelector("button[type=submit]");
|
||||
if (btn) { btn.disabled = true; btn.dataset.t = btn.textContent; btn.textContent = "处理中…"; }
|
||||
var isCollect = url.indexOf("/collect") >= 0;
|
||||
var req = isCollect ? post(url, { from: d.from || null, to: d.to || null }) : post(url, d);
|
||||
req.then(function (j) {
|
||||
if (j.ok === false) { say((j.message || "操作失败") + hintOf(j), "error"); return; }
|
||||
if (isCollect && j.result) {
|
||||
var r = j.result;
|
||||
say("采集完成:云端返回 " + r.fetched + " 条,新增 " + r.added + " 条,重复 " + r.dup +
|
||||
" 条,存档共 " + r.total + " 条。" + (r.conflicts ? "(有 " + r.conflicts + " 条时间漂移警告)" : ""),
|
||||
r.conflicts ? "warn" : "ok");
|
||||
} else {
|
||||
say("已保存" + (j.changed && j.changed.length ? ":" + j.changed.join("、") : ""), "ok");
|
||||
}
|
||||
if (opts.reload !== false) window.setTimeout(function () { location.reload(); }, 900);
|
||||
}).catch(function (e) {
|
||||
if (String(e.message) !== "unauthorized") say("请求失败:" + e.message, "error");
|
||||
}).finally(function () {
|
||||
if (btn) { btn.disabled = false; btn.textContent = btn.dataset.t; }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function bindTail(btnSel, url, boxSel, metaSel) {
|
||||
var btn = document.querySelector(btnSel);
|
||||
if (!btn) return;
|
||||
function load() {
|
||||
btn.disabled = true;
|
||||
fetch(url + "?lines=400", { headers: { "X-Requested-With": "fetch" } })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (j) {
|
||||
var box = document.querySelector(boxSel);
|
||||
box.textContent = (j.lines && j.lines.length) ? j.lines.join("\n") : "(日志为空)";
|
||||
box.scrollTop = box.scrollHeight;
|
||||
var meta = document.querySelector(metaSel);
|
||||
if (meta) meta.textContent = j.path + " · " + (j.size / 1024).toFixed(1) + " KB";
|
||||
})
|
||||
.catch(function (e) { say("日志加载失败:" + e.message, "error"); })
|
||||
.finally(function () { btn.disabled = false; });
|
||||
}
|
||||
btn.addEventListener("click", load);
|
||||
load();
|
||||
}
|
||||
|
||||
// 顶栏/页头的「立即采集一次」按钮
|
||||
function bindCollect(sel) {
|
||||
var btn = document.querySelector(sel || "#btnCollect");
|
||||
if (!btn) return;
|
||||
btn.addEventListener("click", function () {
|
||||
btn.disabled = true;
|
||||
var old = btn.textContent;
|
||||
btn.textContent = "采集中…";
|
||||
post("/api/collect", {})
|
||||
.then(function (j) {
|
||||
if (j.ok === false) { say((j.message || "采集失败") + hintOf(j), "error"); return; }
|
||||
var r = j.result;
|
||||
say("采集完成:云端返回 " + r.fetched + " 条,新增 " + r.added + " 条,重复 " + r.dup +
|
||||
" 条,存档共 " + r.total + " 条。" + (r.conflicts ? "(" + r.conflicts + " 条时间漂移警告)" : ""),
|
||||
r.conflicts ? "warn" : "ok");
|
||||
window.setTimeout(function () { location.reload(); }, 1100);
|
||||
})
|
||||
.catch(function (e) { if (String(e.message) !== "unauthorized") say("请求失败:" + e.message, "error"); })
|
||||
.finally(function () { btn.disabled = false; btn.textContent = old; });
|
||||
});
|
||||
}
|
||||
|
||||
// 维护动作:补全 Prompt / 导出全量 CSV / 整理数据库
|
||||
var MAINT = {
|
||||
"fill-prompt": { label: "补全缺失 Prompt", confirm: "补全 Prompt 需要联网并逐天重拉云端,可能耗时较久。继续?" },
|
||||
"export-csv": { label: "导出全量 CSV" },
|
||||
"vacuum": { label: "整理数据库", confirm: "将执行 checkpoint + VACUUM,期间数据库会短暂锁定。继续?" },
|
||||
"recount": { label: "重新计数" }
|
||||
};
|
||||
|
||||
function bindMaint(sel) {
|
||||
Array.prototype.forEach.call(document.querySelectorAll(sel || "[data-maint]"), function (btn) {
|
||||
btn.addEventListener("click", function () {
|
||||
var act = btn.dataset.maint;
|
||||
var meta = MAINT[act] || { label: act };
|
||||
if (meta.confirm && !window.confirm(meta.confirm)) return;
|
||||
btn.disabled = true;
|
||||
var old = btn.textContent;
|
||||
btn.textContent = "处理中…";
|
||||
post("/api/maintenance/" + encodeURIComponent(act), {})
|
||||
.then(function (j) {
|
||||
if (j.ok === false) { say((j.message || (meta.label + " 失败")) + hintOf(j), "error"); return; }
|
||||
say(j.message || (meta.label + " 完成"), "ok");
|
||||
window.setTimeout(function () { location.reload(); }, 1500);
|
||||
})
|
||||
.catch(function (e) { if (String(e.message) !== "unauthorized") say("请求失败:" + e.message, "error"); })
|
||||
.finally(function () { btn.disabled = false; btn.textContent = old; });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
window.WBU = {
|
||||
post: post, bindForm: bindForm, bindTail: bindTail,
|
||||
bindCollect: bindCollect, bindMaint: bindMaint, say: say, esc: esc
|
||||
};
|
||||
document.addEventListener("DOMContentLoaded", function () { bindCollect("#btnCollect"); });
|
||||
})();
|
||||
@@ -0,0 +1,64 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="color-scheme" content="dark">
|
||||
<title>{% block title %}{{ project_title }}{% endblock %}</title>
|
||||
<link rel="icon" href="{{ url_for('static', filename='favicon.svg') }}">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/app.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
{% set nav = active|default('') %}
|
||||
{% set on_dash = request.path.startswith('/dashboard') %}
|
||||
{% if current_user() %}
|
||||
<header class="topbar">
|
||||
<div class="brand">
|
||||
<span class="dot"></span>
|
||||
<a href="{{ url_for('views.overview') }}"><b>{{ project_title }}</b></a>
|
||||
<span class="ver">v{{ app_version }}</span>
|
||||
</div>
|
||||
<nav>
|
||||
<a href="{{ url_for('views.overview') }}" class="{{ 'on' if nav=='overview' }}">概览</a>
|
||||
<a href="{{ url_for('views.dashboard') }}" class="{{ 'on' if on_dash }}">用量大屏</a>
|
||||
<a href="{{ url_for('views.records') }}" class="{{ 'on' if nav=='records' }}">数据明细</a>
|
||||
<a href="{{ url_for('views.tasks') }}" class="{{ 'on' if nav=='tasks' }}">任务管理</a>
|
||||
<a href="{{ url_for('views.config_page') }}" class="{{ 'on' if nav=='config' }}">配置管理</a>
|
||||
<a href="{{ url_for('views.logs') }}" class="{{ 'on' if nav=='logs' }}">日志管理</a>
|
||||
{% if current_user().is_admin %}
|
||||
<a href="{{ url_for('views.users_page') }}" class="{{ 'on' if nav=='users' }}">用户管理</a>
|
||||
{% endif %}
|
||||
</nav>
|
||||
<div class="me">
|
||||
<span class="who"><b>{{ current_user().display_name }}</b>{% if current_user().is_admin %} <span class="tag accent">管理员</span>{% endif %}</span>
|
||||
{# 退出用 POST + CSRF:GET 型退出会被 <img src="/logout"> 这类请求静默触发 #}
|
||||
<form method="post" action="{{ url_for('views.logout_post') }}" style="margin:0">
|
||||
<input type="hidden" name="_csrf" value="{{ csrf_token() }}">
|
||||
<button class="btn ghost sm" type="submit">退出</button>
|
||||
</form>
|
||||
</div>
|
||||
</header>
|
||||
{% endif %}
|
||||
|
||||
<main class="wrap">
|
||||
{% with msgs = get_flashed_messages(with_categories=true) %}
|
||||
{% if msgs %}
|
||||
<div class="flashes">
|
||||
{% for cat, m in msgs %}<div class="flash {{ cat }}">{{ m }}</div>{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% block body %}{% endblock %}
|
||||
</main>
|
||||
|
||||
<footer class="foot">
|
||||
<b>{{ project_title }}</b> · {{ project_name }} · 采集 / 存储 / 呈现三合一 · 数据正本 <code>data/usage.sqlite</code><br>
|
||||
采集在 Web 进程内按配置时刻执行,无需外部计划任务
|
||||
</footer>
|
||||
|
||||
<script>
|
||||
window.WB_CSRF = "{{ csrf_token() }}";
|
||||
</script>
|
||||
{% block scripts %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,109 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}配置管理 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
|
||||
<div class="pagehead">
|
||||
<div>
|
||||
<h1>配置管理</h1>
|
||||
<p class="lead">凭证、采集参数、维护动作都在这里;所有配置存在数据库,改完立即生效</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>云端凭证</h2>
|
||||
<span class="tag {{ 'ok' if s.cookie_hint else 'bad' }}">{{ '已配置' if s.cookie_hint else '未配置' }}</span>
|
||||
</div>
|
||||
<p class="hint">
|
||||
{% if s.cookie_hint %}当前 Cookie:{{ s.cookie_hint }}(页面与接口都不回传明文){% endif %}
|
||||
<br>获取方式:Chrome 打开 <code>https://www.workbuddy.cn/profile/plans-usage</code> → F12 → Network →
|
||||
任选一个 <code>billing</code> 请求 → 复制 Request Headers 里的 <code>cookie</code> 与 <code>user-agent</code>
|
||||
(<b>两者必须取自同一次请求</b>),粘贴到下面。
|
||||
</p>
|
||||
<form id="formCred">
|
||||
<label class="col">Cookie
|
||||
<textarea name="cookie" rows="4" placeholder="留空表示不修改;填 - 表示清空已保存的 Cookie" spellcheck="false"></textarea>
|
||||
</label>
|
||||
<label class="col">User-Agent
|
||||
<textarea name="user_agent" rows="2" spellcheck="false">{{ s.user_agent }}</textarea>
|
||||
</label>
|
||||
<button class="btn primary" type="submit">保存凭证</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<div class="grid2">
|
||||
<section class="card">
|
||||
<h2>采集参数</h2>
|
||||
<form id="formCollect">
|
||||
<label class="row"><span>接口基址</span><input name="api_base" value="{{ s.api_base }}" spellcheck="false"></label>
|
||||
<label class="row"><span>接口路径</span><input name="api_path" value="{{ s.api_path }}" spellcheck="false"></label>
|
||||
{% set b = num_settings %}
|
||||
<label class="row"><span>分页大小</span>
|
||||
<input name="page_size" type="number" min="{{ b.page_size[0] }}" max="{{ b.page_size[1] }}" value="{{ s.page_size }}">
|
||||
<em class="unit">{{ b.page_size[0] }}~{{ b.page_size[1] }} 条/页</em></label>
|
||||
<label class="row"><span>断点回退</span>
|
||||
<input name="rewind_minutes" type="number" min="{{ b.rewind_minutes[0] }}" max="{{ b.rewind_minutes[1] }}" value="{{ s.rewind_minutes }}">
|
||||
<em class="unit">分钟</em></label>
|
||||
<label class="row"><span>时间漂移容差</span>
|
||||
<input name="drift_tolerance_minutes" type="number" min="{{ b.drift_tolerance_minutes[0] }}" max="{{ b.drift_tolerance_minutes[1] }}" value="{{ s.drift_tolerance_minutes }}">
|
||||
<em class="unit">分钟</em></label>
|
||||
<label class="row"><span>Prompt 截断</span>
|
||||
<input name="max_prompt" type="number" min="{{ b.max_prompt[0] }}" max="{{ b.max_prompt[1] }}" value="{{ s.max_prompt }}">
|
||||
<em class="unit">字符(0 = 不截断)</em></label>
|
||||
<label class="row"><span>整日校验天数</span>
|
||||
<input name="verify_days" type="number" min="{{ b.verify_days[0] }}" max="{{ b.verify_days[1] }}" value="{{ s.verify_days }}">
|
||||
<em class="unit">天</em></label>
|
||||
<label class="row"><span>请求超时</span>
|
||||
<input name="timeout" type="number" min="{{ b.timeout[0] }}" max="{{ b.timeout[1] }}" value="{{ s.timeout }}">
|
||||
<em class="unit">秒</em></label>
|
||||
<label class="row"><span>校验 TLS 证书</span>
|
||||
<select name="ssl_verify">
|
||||
<option value="1" {{ 'selected' if s.ssl_verify != '0' }}>校验(推荐)</option>
|
||||
<option value="0" {{ 'selected' if s.ssl_verify == '0' }}>不校验(仅自签/企业代理时用)</option>
|
||||
</select>
|
||||
</label>
|
||||
<p class="hint">Cookie 就是账号凭证,关掉证书校验等于把它暴露给中间人,非必要不要关。</p>
|
||||
<button class="btn primary" type="submit">保存采集参数</button>
|
||||
<p class="hint">整日校验会按天重新拉云端 total 与本地比对,发现缺记录自动补入;设为 0 表示关闭(日常够用)。</p>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2>修改登录密码</h2>
|
||||
<form id="formPwd">
|
||||
<label class="col">原密码<input name="old" type="password" autocomplete="current-password"></label>
|
||||
<label class="col">新密码<input name="new" type="password" autocomplete="new-password"></label>
|
||||
<label class="col">确认新密码<input name="new2" type="password" autocomplete="new-password"></label>
|
||||
<button class="btn primary" type="submit">修改密码</button>
|
||||
<p class="hint">至少 6 位。修改成功后当前会话仍有效,不必重新登录。</p>
|
||||
</form>
|
||||
|
||||
<hr class="sect-divider">
|
||||
<h3>维护动作</h3>
|
||||
<div class="btnrow">
|
||||
<button class="btn" type="button" data-maint="fill-prompt"
|
||||
title="把云端仍保留、但本地为空的 User Prompt 补回来">补全缺失 Prompt</button>
|
||||
<button class="btn" type="button" data-maint="export-csv"
|
||||
title="导出与官网 xlsx 同构的全量 CSV 到 data/exports/">导出全量 CSV</button>
|
||||
<button class="btn" type="button" data-maint="vacuum"
|
||||
title="checkpoint + VACUUM,回收删除后的空闲页">整理数据库</button>
|
||||
</div>
|
||||
<p class="hint">补全 Prompt 需要联网并逐天重拉云端;导出与整理只动本地数据。</p>
|
||||
<div class="btnrow" style="margin-top:14px">
|
||||
<a class="btn ghost" href="{{ url_for('views.records_export') }}">按当前明细页筛选导出</a>
|
||||
{% if current_user().is_admin %}<a class="btn ghost" href="{{ url_for('views.users_page') }}">用户管理</a>{% endif %}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{{ url_for('static', filename='js/app.js') }}"></script>
|
||||
<script>
|
||||
WBU.bindForm('#formCred', '/api/settings');
|
||||
WBU.bindForm('#formCollect', '/api/settings');
|
||||
WBU.bindForm('#formPwd', '/api/password', {validate: d => d.new === d.new2 ? null : '两次输入的新密码不一致'});
|
||||
WBU.bindMaint('[data-maint]');
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,16 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}{{ code }} · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
<div class="card errpage">
|
||||
<div class="code">{{ code }}</div>
|
||||
<p>{{ message }}</p>
|
||||
<div class="btnrow" style="justify-content:center">
|
||||
{% if current_user() %}
|
||||
<a class="btn primary" href="{{ url_for('views.overview') }}">回到概览</a>
|
||||
<a class="btn ghost" href="javascript:history.back()">返回上一页</a>
|
||||
{% else %}
|
||||
<a class="btn primary" href="{{ url_for('views.login') }}">去登录</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,25 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}登录 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
<div class="loginwrap">
|
||||
<form class="card login" method="post" action="{{ url_for('views.login') }}">
|
||||
<div class="logo">W</div>
|
||||
<h1>{{ project_title }}</h1>
|
||||
<p class="hint">{{ project_desc }}</p>
|
||||
<input type="hidden" name="_csrf" value="{{ csrf_token() }}">
|
||||
<input type="hidden" name="next" value="{{ next_url or '' }}">
|
||||
<label>用户名
|
||||
<input name="username" value="{{ username or '' }}" autocomplete="username"
|
||||
autofocus required maxlength="32">
|
||||
</label>
|
||||
<label>密码
|
||||
<input name="password" type="password" autocomplete="current-password" required>
|
||||
</label>
|
||||
<button class="btn primary" type="submit">登 录</button>
|
||||
<p class="foot-note">
|
||||
首次部署默认账号 <code>admin</code> / <code>admin123</code>,登录后请立即到「配置管理」修改密码。<br>
|
||||
连续输错 {{ max_fails }} 次将锁定 {{ lock_minutes }} 分钟;登录状态保持 {{ session_hours }} 小时。
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,155 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}日志管理 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
|
||||
<div class="pagehead">
|
||||
<div>
|
||||
<h1>日志管理</h1>
|
||||
<p class="lead">采集逐次日志、应用运行日志与操作审计</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button class="btn" id="btnTail" type="button">刷新应用日志</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if detail %}
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>运行 #{{ detail.id }} 详情</h2>
|
||||
<a class="btn ghost sm" href="{{ url_for('views.logs') }}">收起</a>
|
||||
</div>
|
||||
<table class="kv">
|
||||
<tr><th>触发方式</th><td>{{ detail.trigger }}</td><th>状态</th>
|
||||
<td>
|
||||
{% if detail.status=='ok' %}<span class="tag ok">成功</span>
|
||||
{% elif detail.status=='warn' %}<span class="tag warn">有警告</span>
|
||||
{% elif detail.status=='running' %}<span class="tag info">进行中</span>
|
||||
{% else %}<span class="tag bad">失败</span>{% endif %}
|
||||
{% if detail.exit_code is not none %}<span class="tag mute">exit {{ detail.exit_code }}</span>{% endif %}
|
||||
</td></tr>
|
||||
<tr><th>开始</th><td class="mono">{{ detail.started_at }}</td><th>结束</th><td class="mono">{{ detail.finished_at or '—' }}</td></tr>
|
||||
<tr><th>同步区间</th><td colspan="3" class="mono">{{ detail.win_from or '—' }} ~ {{ detail.win_to or '' }}</td></tr>
|
||||
<tr><th>云端返回</th><td>{{ detail.fetched }}</td><th>新增 / 重复</th><td>{{ detail.added }} / {{ detail.dup }}</td></tr>
|
||||
<tr><th>入库总数</th><td>{{ detail.total }}</td><th>时间冲突</th><td>{{ detail.conflicts }}</td></tr>
|
||||
<tr><th>结论</th><td colspan="3">{{ detail.message or '—' }}</td></tr>
|
||||
</table>
|
||||
<pre class="logbox" style="margin-top:12px">{{ detail.detail or '(无日志)' }}</pre>
|
||||
</section>
|
||||
{% endif %}
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>采集运行日志</h2>
|
||||
<div class="seg">
|
||||
<a href="{{ url_for('views.logs') }}" class="{{ 'on' if not status }}">全部</a>
|
||||
<a href="{{ url_for('views.logs', status='ok') }}" class="{{ 'on' if status=='ok' }}">成功</a>
|
||||
<a href="{{ url_for('views.logs', status='warn') }}" class="{{ 'on' if status=='warn' }}">警告</a>
|
||||
<a href="{{ url_for('views.logs', status='error') }}" class="{{ 'on' if status=='error' }}">失败</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tablewrap">
|
||||
<table class="tbl">
|
||||
<thead><tr><th>#</th><th>开始</th><th>触发</th><th>状态</th><th class="num">耗时</th>
|
||||
<th class="num">新增</th><th class="num">重复</th><th class="num">冲突</th><th>结论</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
{% for r in runs %}
|
||||
<tr>
|
||||
<td>{{ r.id }}</td>
|
||||
<td class="mono nowrap">{{ r.started_at[5:] if r.started_at else '—' }}</td>
|
||||
<td><span class="tag {{ 'info' if r.trigger=='schedule' else ('accent' if r.trigger=='startup' else 'mute') }}">{{ r.trigger }}</span></td>
|
||||
<td class="nowrap">
|
||||
{% if r.status=='ok' %}<span class="tag ok">成功</span>
|
||||
{% elif r.status=='warn' %}<span class="tag warn">有警告</span>
|
||||
{% elif r.status=='running' %}<span class="tag info">进行中</span>
|
||||
{% else %}<span class="tag bad">失败</span>{% endif %}
|
||||
</td>
|
||||
<td class="num">{{ ((r.duration_ms or 0) / 1000) | round(1) }}s</td>
|
||||
<td class="num">{{ r.added }}</td><td class="num">{{ r.dup }}</td>
|
||||
<td class="num">{% if r.conflicts %}<span class="tag warn">{{ r.conflicts }}</span>{% else %}0{% endif %}</td>
|
||||
<td>{{ r.message or '—' }}</td>
|
||||
<td><a href="{{ url_for('views.logs', run=r.id, status=status or none) }}">详情</a></td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr><td colspan="10" class="empty">暂无采集日志</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% if pages > 1 %}
|
||||
{# 翻页时保留 status/act,否则一翻页筛选条件就丢了 #}
|
||||
<div class="pager">
|
||||
{% if page > 1 %}<a href="{{ url_for('views.logs', page=page-1, status=status or none, act=act or none) }}">‹</a>{% endif %}
|
||||
{% for p in page_window %}
|
||||
{% if p == page %}<span class="cur">{{ p }}</span>
|
||||
{% else %}<a href="{{ url_for('views.logs', page=p, status=status or none, act=act or none) }}">{{ p }}</a>{% endif %}
|
||||
{% endfor %}
|
||||
{% if page < pages %}<a href="{{ url_for('views.logs', page=page+1, status=status or none, act=act or none) }}">›</a>{% endif %}
|
||||
<span class="meta">第 {{ page }} / {{ pages }} 页 · 共 {{ '{:,}'.format(total) }} 次</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
|
||||
<div class="grid2">
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>应用日志</h2>
|
||||
<span class="hint" id="tailMeta"></span>
|
||||
</div>
|
||||
<pre class="logbox" id="appLog">正在加载 logs/app.log …</pre>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>操作审计</h2>
|
||||
<span class="hint">共 {{ '{:,}'.format(atotal) }} 条 · 第 {{ apage }} / {{ apages }} 页</span>
|
||||
</div>
|
||||
{% if actions %}
|
||||
<div class="seg" style="margin-bottom:10px">
|
||||
<a href="{{ url_for('views.logs', status=status or none) }}" class="{{ 'on' if not act }}">全部</a>
|
||||
{% for a in actions[:6] %}
|
||||
<a href="{{ url_for('views.logs', act=a[0], status=status or none) }}"
|
||||
class="{{ 'on' if act==a[0] }}">{{ a[0] }} <span class="muted">{{ a[1] }}</span></a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="tablewrap scroll-y">
|
||||
<table class="tbl" id="auditTable">
|
||||
<thead><tr><th>时间</th><th>用户</th><th>动作</th><th>说明</th><th>IP</th></tr></thead>
|
||||
<tbody>
|
||||
{% for a in audits %}
|
||||
<tr><td class="mono nowrap">{{ a.at[5:] if a.at else '' }}</td>
|
||||
<td>{{ a.actor or '—' }}</td>
|
||||
<td><span class="tag mute">{{ a.action }}</span></td>
|
||||
<td>{{ a.detail or '—' }}</td><td class="mono">{{ a.ip or '' }}</td></tr>
|
||||
{% else %}
|
||||
<tr><td colspan="5" class="empty">暂无审计记录</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% if apages > 1 %}
|
||||
<div class="pager">
|
||||
{% if apage > 1 %}
|
||||
<a href="{{ url_for('views.logs', apage=apage-1, act=act or none, status=status or none) }}">‹</a>
|
||||
{% endif %}
|
||||
{% for p in apage_window %}
|
||||
{% if p == apage %}<span class="cur">{{ p }}</span>
|
||||
{% else %}<a href="{{ url_for('views.logs', apage=p, act=act or none, status=status or none) }}">{{ p }}</a>{% endif %}
|
||||
{% endfor %}
|
||||
{% if apage < apages %}
|
||||
<a href="{{ url_for('views.logs', apage=apage+1, act=act or none, status=status or none) }}">›</a>
|
||||
{% endif %}
|
||||
<span class="meta">共 {{ '{:,}'.format(atotal) }} 条</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{{ url_for('static', filename='js/app.js') }}"></script>
|
||||
<script>
|
||||
WBU.bindTail('#btnTail', '/logs/tail', '#appLog', '#tailMeta');
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,124 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}概览 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
|
||||
<div class="pagehead">
|
||||
<div>
|
||||
<h1>概览</h1>
|
||||
<p class="lead">存档 {{ '{:,}'.format(totals.records) }} 条记录,覆盖 {{ totals.firstDay }} ~ {{ totals.lastDay }}</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<a class="btn" href="{{ url_for('views.dashboard') }}">打开用量大屏</a>
|
||||
<button class="btn primary" id="btnCollect" type="button">立即采集一次</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="collectMsg" class="flash" style="display:none"></div>
|
||||
|
||||
<div class="kpis">
|
||||
<div class="kpi" style="--c:var(--cyan)">
|
||||
<span>累计积分</span><b>{{ '%.2f'|format(totals.credits) }}</b>
|
||||
<i>{{ totals.days }} 个活跃日 · 单次均价 {{ '%.2f'|format(totals.credits / totals.calls if totals.calls else 0) }}</i>
|
||||
</div>
|
||||
<div class="kpi" style="--c:var(--violet)">
|
||||
<span>调用次数</span><b>{{ '{:,}'.format(totals.calls) }}</b>
|
||||
<i>计费 {{ '{:,}'.format(totals.billableCalls) }} · 免费 {{ '{:,}'.format(totals.freeCalls) }}
|
||||
({{ '%.1f'|format(totals.freeCalls / totals.calls * 100 if totals.calls else 0) }}%)</i>
|
||||
</div>
|
||||
<div class="kpi" style="--c:var(--green)">
|
||||
<span>今日积分</span><b>{{ '%.2f'|format(today_stat.credits) }}</b>
|
||||
<i>{{ today_stat.calls }} 次调用{% if today_stat.partial %} · 残日截至 {{ today_stat.partial.hhmm }}{% endif %}
|
||||
{% if yesterday.credits %}
|
||||
· 昨日整日 {{ '%.2f'|format(yesterday.credits) }}
|
||||
{% endif %}</i>
|
||||
</div>
|
||||
<div class="kpi" style="--c:var(--amber)">
|
||||
<span>近 30 天积分</span><b>{{ '%.2f'|format(stat30.credits) }}</b>
|
||||
<i>{% if stat30.delta %}<span class="delta {{ 'dn' if stat30.delta.credits < 0 }}">环比 {{ '%+.1f'|format(stat30.delta.credits) }}%</span>
|
||||
· 前一区间 {{ '%.0f'|format(stat30.prev.credits) }}{% else %}无可比区间{% endif %}</i>
|
||||
</div>
|
||||
<div class="kpi" style="--c:var(--blue)">
|
||||
<span>模型 / 客户端</span><b>{{ totals.models }} / {{ totals.clients }}</b>
|
||||
<i>按积分排序见下表</i>
|
||||
</div>
|
||||
<div class="kpi" style="--c:{{ 'var(--green)' if mf.health.cookie else 'var(--red)' }}">
|
||||
<span>采集健康</span><b>{{ '正常' if mf.health.cookie else '缺 Cookie' }}</b>
|
||||
<i>{% if mf.health.lastRunAt %}最近采集 {{ mf.health.lastRunAt[5:16] }}{% else %}尚无采集记录{% endif %}</i>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid2">
|
||||
<section class="card">
|
||||
<h2>调度状态</h2>
|
||||
<table class="kv">
|
||||
<tr><th>调度线程</th><td>{% if sch.running %}<span class="tag ok">运行中</span> 每 {{ sch.interval }}s 轮询{% else %}<span class="tag bad">未运行</span>{% endif %}</td></tr>
|
||||
<tr><th>调度开关</th><td>{% if sch.enabled %}<span class="tag ok">已启用</span>{% else %}<span class="tag bad">已停用</span>{% endif %}</td></tr>
|
||||
<tr><th>每日时刻</th><td>{{ sch.times | join(' · ') if sch.times else '—' }}{% if sch.catch_up %} <span class="tag">含启动补跑</span>{% endif %}</td></tr>
|
||||
<tr><th>下次执行</th><td class="mono">{{ sch.next_run or '—' }}</td></tr>
|
||||
<tr><th>采集互斥锁</th><td>{% if sch.lock %}<span class="tag warn">采集中</span>{% else %}<span class="tag ok">空闲</span>{% endif %}</td></tr>
|
||||
<tr><th>Cookie</th><td>{% if mf.health.cookie %}<span class="tag ok">已配置</span>{% else %}<span class="tag bad">未配置</span> <a href="{{ url_for('views.config_page') }}">去配置</a>{% endif %}</td></tr>
|
||||
<tr><th>服务器时间</th><td class="mono">{{ sch.now }}</td></tr>
|
||||
</table>
|
||||
<p class="hint">调度在 Web 进程内运行,不再需要计划任务或外部自动化。所有时刻与开关都在
|
||||
<a href="{{ url_for('views.tasks') }}">任务管理</a>里改。</p>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>模型消耗 TOP</h2>
|
||||
<span class="hint">按积分降序,取前 {{ models|length }} 名</span>
|
||||
</div>
|
||||
<div class="tablewrap">
|
||||
<table class="tbl">
|
||||
<thead><tr><th>模型</th><th class="num">调用</th><th class="num">积分</th><th class="num">单次均价</th><th class="num">免费占比</th></tr></thead>
|
||||
<tbody>
|
||||
{% for m in models %}
|
||||
<tr><td>{{ m.name }}</td><td class="num">{{ m.calls }}</td>
|
||||
<td class="num">{{ '%.2f'|format(m.credits) }}</td>
|
||||
<td class="num">{{ '%.2f'|format(m.avgPerCall) }}</td>
|
||||
<td class="num">{{ '%.0f%%'|format(m.freeRate * 100) }}</td></tr>
|
||||
{% else %}
|
||||
<tr><td colspan="5" class="empty">还没有数据,先点右上角「立即采集一次」</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p class="hint">客户端构成:{% for c in clients %}{{ c.name }} {{ '%.1f%%'|format(c.credits / totals.credits * 100 if totals.credits else 0) }}{% if not loop.last %} · {% endif %}{% else %}—{% endfor %}</p>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>最近采集</h2>
|
||||
<a class="btn ghost sm" href="{{ url_for('views.tasks') }}">查看全部</a>
|
||||
</div>
|
||||
<div class="tablewrap">
|
||||
<table class="tbl">
|
||||
<thead><tr><th>#</th><th>触发</th><th>开始</th><th class="num">耗时</th><th class="num">云端</th>
|
||||
<th class="num">新增</th><th class="num">重复</th><th class="num">冲突</th><th class="num">总数</th><th>结论</th></tr></thead>
|
||||
<tbody>
|
||||
{% for r in runs %}
|
||||
<tr>
|
||||
<td><a href="{{ url_for('views.logs', run=r.id) }}">{{ r.id }}</a></td>
|
||||
<td><span class="tag {{ 'info' if r.trigger=='schedule' else ('accent' if r.trigger=='startup' else 'mute') }}">{{ r.trigger }}</span></td>
|
||||
<td class="mono">{{ r.started_at[5:] if r.started_at else '—' }}</td>
|
||||
<td class="num">{{ ((r.duration_ms or 0) / 1000) | round(1) }}s</td>
|
||||
<td class="num">{{ r.fetched if r.fetched is not none else '—' }}</td>
|
||||
<td class="num">{{ r.added }}</td>
|
||||
<td class="num">{{ r.dup }}</td>
|
||||
<td class="num">{% if r.conflicts %}<span class="tag warn">{{ r.conflicts }}</span>{% else %}0{% endif %}</td>
|
||||
<td class="num">{{ r.total }}</td>
|
||||
<td>{{ r.message or '—' }}</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr><td colspan="10" class="empty">还没有采集记录,点右上角「立即采集一次」</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{{ url_for('static', filename='js/app.js') }}"></script>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,169 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}数据明细 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
|
||||
{# 注意:模板里统一用 frm 而不是 from,避免与 Python 关键字混淆;
|
||||
拼回 URL 时必须换成 HTTP 规范参数名 from/to,否则导出/翻页会丢筛选条件。 #}
|
||||
{% set qs = {'from': f.frm, 'to': f.to, 'model': f.model, 'client': f.client,
|
||||
'q': f.q, 'order': f.order, 'size': f.size} %}
|
||||
{% set has_filter = f.frm or f.to or f.model or f.client or f.q %}
|
||||
|
||||
<div class="pagehead">
|
||||
<div>
|
||||
<h1>数据明细</h1>
|
||||
<p class="lead">
|
||||
共 {{ '{:,}'.format(data.total) }} 条匹配记录 · 合计
|
||||
<b class="hl">{{ '%.2f'|format(data.credits) }}</b> 积分
|
||||
</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<a class="btn ghost" href="{{ url_for('views.dashboard') }}">用量大屏</a>
|
||||
<a class="btn primary" href="{{ url_for('views.records_export', **qs) }}">导出当前筛选 CSV</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>筛选</h2>
|
||||
<div class="quick">
|
||||
<button class="btn sm ghost" type="button" data-range="today">今日</button>
|
||||
<button class="btn sm ghost" type="button" data-range="7d">近 7 天</button>
|
||||
<button class="btn sm ghost" type="button" data-range="30d">近 30 天</button>
|
||||
<button class="btn sm ghost" type="button" data-range="all">全部</button>
|
||||
</div>
|
||||
</div>
|
||||
<form class="filters" id="formFilter" method="get" action="{{ url_for('views.records') }}">
|
||||
<label>开始日期<input type="date" name="from" id="fFrom" value="{{ f.frm }}"></label>
|
||||
<label>结束日期<input type="date" name="to" id="fTo" value="{{ f.to }}"></label>
|
||||
<label>模型
|
||||
<select name="model">
|
||||
<option value="">全部</option>
|
||||
{% for m in models %}<option value="{{ m }}" {{ 'selected' if f.model==m }}>{{ m }}</option>{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
<label>客户端
|
||||
<select name="client">
|
||||
<option value="">全部</option>
|
||||
{% for c in clients %}<option value="{{ c }}" {{ 'selected' if f.client==c }}>{{ c }}</option>{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
<label>关键词
|
||||
<input name="q" value="{{ f.q }}" placeholder="搜 Prompt / RequestID">
|
||||
</label>
|
||||
<label>排序
|
||||
<select name="order">
|
||||
<option value="ts_desc" {{ 'selected' if f.order=='ts_desc' }}>时间倒序</option>
|
||||
<option value="ts" {{ 'selected' if f.order=='ts' }}>时间正序</option>
|
||||
<option value="credits_desc" {{ 'selected' if f.order=='credits_desc' }}>积分从高到低</option>
|
||||
<option value="credits" {{ 'selected' if f.order=='credits' }}>积分从低到高</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>每页
|
||||
<select name="size">
|
||||
{% for n in [20, 50, 100, 200, 500] %}
|
||||
<option value="{{ n }}" {{ 'selected' if f.size==n }}>{{ n }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
<button class="btn primary" type="submit">查询</button>
|
||||
{% if has_filter %}<a class="btn ghost" href="{{ url_for('views.records') }}">重置</a>{% endif %}
|
||||
</form>
|
||||
<p class="hint">
|
||||
日期留空表示不限。有关键词时会同时匹配 User Prompt 与 RequestID(按天全表扫描,区间越大越慢)。
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>记录列表</h2>
|
||||
<span class="hint">
|
||||
第 {{ data.page }} / {{ data.pages }} 页 · 每页 {{ data.size }} 条
|
||||
{%- if data.total %} · 本页 {{ rows|length }} 条{% endif %}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{% if rows %}
|
||||
<div class="tablewrap">
|
||||
<table class="tbl">
|
||||
<thead>
|
||||
<tr><th class="num">#</th><th>时间</th><th class="num">积分</th><th>模型</th>
|
||||
<th>客户端</th><th>RequestID</th><th>User Prompt</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for r in rows %}
|
||||
<tr>
|
||||
<td class="num muted">{{ (data.page - 1) * data.size + loop.index }}</td>
|
||||
<td class="mono nowrap">{{ r.ts }}</td>
|
||||
<td class="num {{ 'zero' if not r.credits }}">{{ '%.2f'|format(r.credits) }}</td>
|
||||
<td class="nowrap">{{ r.model }}</td>
|
||||
<td class="nowrap"><span class="tag mute">{{ r.client }}</span></td>
|
||||
<td class="mono sm nowrap" title="{{ r.request_id }}">{{ r.request_id[:16] }}…</td>
|
||||
<td class="px">
|
||||
{% if r.prompt %}
|
||||
<details>
|
||||
<summary>{{ r.prompt[:70] }}{% if r.prompt|length > 70 %}…{% endif %}</summary>
|
||||
<div class="fullprompt">{{ r.prompt }}</div>
|
||||
</details>
|
||||
{% else %}
|
||||
<span class="hint">(空)</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="empty-state">
|
||||
<b>没有匹配的记录</b>
|
||||
{% if has_filter %}当前筛选条件过窄,试试放宽日期区间或点「重置」。{% else %}存档里还没有数据,到「任务管理」手动采集一次。{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if data.pages > 1 %}
|
||||
<div class="pager">
|
||||
{% if data.page > 1 %}
|
||||
<a href="{{ url_for('views.records', page=1, **qs) }}">«</a>
|
||||
<a href="{{ url_for('views.records', page=data.page-1, **qs) }}">‹ 上一页</a>
|
||||
{% endif %}
|
||||
{% for p in page_window %}
|
||||
{% if p == data.page %}<span class="cur">{{ p }}</span>
|
||||
{% else %}<a href="{{ url_for('views.records', page=p, **qs) }}">{{ p }}</a>{% endif %}
|
||||
{% endfor %}
|
||||
{% if data.page < data.pages %}
|
||||
<a href="{{ url_for('views.records', page=data.page+1, **qs) }}">下一页 ›</a>
|
||||
<a href="{{ url_for('views.records', page=data.pages, **qs) }}">»</a>
|
||||
{% endif %}
|
||||
<span class="meta">第 {{ data.page }} / {{ data.pages }} 页 · 共 {{ '{:,}'.format(data.total) }} 条</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script>
|
||||
// 快捷区间:只填表单里的两个日期输入框,真正的提交交给「查询」按钮,
|
||||
// 避免按一下就把当前排序/每页设置冲掉。
|
||||
(function () {
|
||||
function ymd(d) { // 必须用本地字段拼串:toISOString 走 UTC,GMT+8 下会少一天
|
||||
var m = d.getMonth() + 1, day = d.getDate();
|
||||
return d.getFullYear() + "-" + (m < 10 ? "0" : "") + m + "-" + (day < 10 ? "0" : "") + day;
|
||||
}
|
||||
var from = document.getElementById("fFrom"), to = document.getElementById("fTo");
|
||||
Array.prototype.forEach.call(document.querySelectorAll("[data-range]"), function (btn) {
|
||||
btn.addEventListener("click", function () {
|
||||
var v = btn.dataset.range, now = new Date();
|
||||
if (v === "all") { from.value = ""; to.value = ""; }
|
||||
else if (v === "today") { from.value = to.value = ymd(now); }
|
||||
else {
|
||||
var n = v === "7d" ? 6 : 29; // 含今天,所以是 N-1
|
||||
from.value = ymd(new Date(now.getTime() - n * 86400000));
|
||||
to.value = ymd(now);
|
||||
}
|
||||
from.focus();
|
||||
});
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,128 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}任务管理 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
|
||||
<div class="pagehead">
|
||||
<div>
|
||||
<h1>任务管理</h1>
|
||||
<p class="lead">调度在 Web 进程内执行,采集互斥由文件锁保证;这里也能手动触发与按区间回填</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button class="btn primary" id="btnCollect" type="button">立即采集一次</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="collectMsg" class="flash" style="display:none"></div>
|
||||
|
||||
<div class="grid2">
|
||||
<section class="card">
|
||||
<h2>采集调度</h2>
|
||||
<form id="formTask">
|
||||
<label class="row"><span>启用调度</span>
|
||||
<select name="schedule_enabled">
|
||||
<option value="1" {{ 'selected' if sch.enabled }}>启用</option>
|
||||
<option value="0" {{ 'selected' if not sch.enabled }}>停用</option>
|
||||
</select>
|
||||
</label>
|
||||
<label class="row"><span>每日时刻</span>
|
||||
<input name="schedule_times" value="{{ s_times }}" placeholder="09:00,17:00" spellcheck="false">
|
||||
</label>
|
||||
<p class="hint">本地时区,逗号分隔,支持 <code>HH:MM</code>(也可只写 <code>9</code>)。保存后立即生效,
|
||||
并会清空当天已执行的槽位标记以便新时刻接管。</p>
|
||||
<label class="row"><span>启动补跑</span>
|
||||
<select name="catch_up">
|
||||
<option value="1" {{ 'selected' if sch.catch_up }}>开启(错过的时刻在宽限期内补跑)</option>
|
||||
<option value="0" {{ 'selected' if not sch.catch_up }}>关闭(只在到点时执行)</option>
|
||||
</select>
|
||||
</label>
|
||||
<label class="row"><span>补跑宽限</span>
|
||||
<input name="catch_up_grace_hours" value="{{ s_grace }}" type="number" min="1" max="168">
|
||||
<em class="unit">小时(超过就不补,避免开机狂刷)</em>
|
||||
</label>
|
||||
<button class="btn primary" type="submit">保存调度配置</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2>运行状态</h2>
|
||||
<table class="kv">
|
||||
<tr><th>服务器时间</th><td class="mono">{{ sch.now }}</td></tr>
|
||||
<tr><th>调度线程</th><td>{% if sch.running %}<span class="tag ok">运行中</span>{% else %}<span class="tag bad">未运行</span>{% endif %}
|
||||
<span class="hint">({{ sch.interval }}s 轮询)</span></td></tr>
|
||||
<tr><th>下次执行</th><td class="mono">{{ sch.next_run or '—' }}</td></tr>
|
||||
<tr><th>互斥锁</th><td>{% if sch.lock %}<span class="tag warn">有采集在跑</span>{% else %}<span class="tag ok">空闲</span>{% endif %}</td></tr>
|
||||
<tr><th>最近采集</th><td>{% if sch.last %}<span class="tag {{ 'ok' if sch.last.status=='ok' else ('warn' if sch.last.status=='warn' else 'bad') }}">#{{ sch.last.id }} {{ sch.last.status }}</span>
|
||||
<span class="hint">{{ sch.last.message or '' }}</span>{% else %}—{% endif %}</td></tr>
|
||||
</table>
|
||||
|
||||
<hr class="sect-divider">
|
||||
<h3>历史回填</h3>
|
||||
<form id="formBackfill">
|
||||
<label class="row"><span>起始日期</span><input type="date" name="from" max="{{ sch.now[:10] }}"></label>
|
||||
<label class="row"><span>结束日期</span><input type="date" name="to" value="{{ sch.now[:10] }}" max="{{ sch.now[:10] }}"></label>
|
||||
<button class="btn" type="submit">按区间补采</button>
|
||||
<p class="hint">指定区间重新拉取云端明细,已存在的记录按 <code>RequestID</code> 去重,不会重复计入。
|
||||
区间越大耗时越长(云端按天分页拉取)。</p>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>采集运行历史</h2>
|
||||
<span class="hint">共 {{ '{:,}'.format(total) }} 次</span>
|
||||
</div>
|
||||
<div class="tablewrap">
|
||||
<table class="tbl">
|
||||
<thead><tr><th>#</th><th>触发</th><th>状态</th><th>开始</th><th class="num">耗时</th>
|
||||
<th>同步区间</th><th class="num">云端</th><th class="num">新增</th><th class="num">重复</th>
|
||||
<th class="num">冲突</th><th>结论</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
{% for r in runs %}
|
||||
<tr>
|
||||
<td>{{ r.id }}</td>
|
||||
<td><span class="tag {{ 'info' if r.trigger=='schedule' else ('accent' if r.trigger=='startup' else 'mute') }}">{{ r.trigger }}</span></td>
|
||||
<td class="nowrap">
|
||||
{% if r.status=='ok' %}<span class="tag ok">成功</span>
|
||||
{% elif r.status=='warn' %}<span class="tag warn">有警告</span>
|
||||
{% elif r.status=='running' %}<span class="tag info">进行中</span>
|
||||
{% else %}<span class="tag bad">失败</span>{% endif %}
|
||||
{% if r.exit_code %}<span class="tag mute">exit {{ r.exit_code }}</span>{% endif %}
|
||||
</td>
|
||||
<td class="mono nowrap">{{ r.started_at[5:] if r.started_at else '—' }}</td>
|
||||
<td class="num">{{ ((r.duration_ms or 0) / 1000) | round(1) }}s</td>
|
||||
<td class="mono">{{ r.win_from or '—' }}<br>{{ r.win_to or '' }}</td>
|
||||
<td class="num">{{ r.fetched }}</td>
|
||||
<td class="num">{{ r.added }}</td>
|
||||
<td class="num">{{ r.dup }}</td>
|
||||
<td class="num">{% if r.conflicts %}<span class="tag warn">{{ r.conflicts }}</span>{% else %}0{% endif %}</td>
|
||||
<td>{{ r.message or '—' }}</td>
|
||||
<td><a href="{{ url_for('views.logs', run=r.id) }}">日志</a></td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr><td colspan="12" class="empty">暂无运行记录</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% if pages > 1 %}
|
||||
<div class="pager">
|
||||
{% if page > 1 %}<a href="{{ url_for('views.tasks', page=page-1) }}">‹</a>{% endif %}
|
||||
{% for p in page_window %}
|
||||
{% if p == page %}<span class="cur">{{ p }}</span>
|
||||
{% else %}<a href="{{ url_for('views.tasks', page=p) }}">{{ p }}</a>{% endif %}
|
||||
{% endfor %}
|
||||
{% if page < pages %}<a href="{{ url_for('views.tasks', page=page+1) }}">›</a>{% endif %}
|
||||
<span class="meta">第 {{ page }} / {{ pages }} 页</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{{ url_for('static', filename='js/app.js') }}"></script>
|
||||
<script>
|
||||
WBU.bindForm('#formTask', '/api/settings');
|
||||
WBU.bindForm('#formBackfill', '/api/collect', {confirmText: '确定按该区间重新采集吗?区间较大时会耗时较久。'});
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,162 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}用户管理 · {{ project_title }}{% endblock %}
|
||||
{% block body %}
|
||||
|
||||
<div class="pagehead">
|
||||
<div>
|
||||
<h1>用户管理</h1>
|
||||
<p class="lead">门户在局域网可访问,因此必须靠账号隔离;这里维护账号、管理员身份与密码</p>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<span class="tag accent">仅管理员可见</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid2">
|
||||
<section class="card">
|
||||
<h2>新建账号</h2>
|
||||
<form id="formNewUser">
|
||||
<label class="row"><span>用户名</span>
|
||||
<input name="username" maxlength="32" placeholder="登录名(≤32 字符)" autocomplete="off" spellcheck="false"></label>
|
||||
<label class="row"><span>显示名</span>
|
||||
<input name="display_name" maxlength="64" placeholder="留空则与用户名相同"></label>
|
||||
<label class="row"><span>密码</span>
|
||||
<input name="password" type="password" autocomplete="new-password"></label>
|
||||
<label class="row"><span>确认密码</span>
|
||||
<input name="password2" type="password" autocomplete="new-password"></label>
|
||||
<label class="row"><span>权限</span>
|
||||
<select name="is_admin">
|
||||
<option value="1">管理员(可管理用户)</option>
|
||||
<option value="0">普通账号(只读数据与日志)</option>
|
||||
</select>
|
||||
</label>
|
||||
<button class="btn primary" type="submit">创建账号</button>
|
||||
<p class="hint">密码至少 6 位、最多 128 位。普通账号不能用本页,也调不动用户管理接口。</p>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>账号列表</h2>
|
||||
<span class="hint">共 {{ users|length }} 个</span>
|
||||
</div>
|
||||
<div class="tablewrap">
|
||||
<table class="tbl" id="userTable">
|
||||
<thead><tr><th class="num">ID</th><th>用户名</th><th>显示名</th><th>权限</th>
|
||||
<th>最后登录</th><th class="num">次数</th><th>操作</th></tr></thead>
|
||||
<tbody>
|
||||
{% for u in users %}
|
||||
<tr data-uid="{{ u.id }}" data-name="{{ u.username }}">
|
||||
<td class="num muted">{{ u.id }}</td>
|
||||
<td class="nowrap"><b>{{ u.username }}</b>
|
||||
{% if u.id == me.id %}<span class="tag accent">当前</span>{% endif %}</td>
|
||||
<td><input class="inp inp-sm" name="display_name" maxlength="64"
|
||||
value="{{ u.display_name or '' }}" spellcheck="false"></td>
|
||||
<td>
|
||||
{# 不能取消自己的管理员身份,所以本人的下拉直接禁用(服务端也会再拦一次) #}
|
||||
<select class="inp inp-sm" name="is_admin" {{ 'disabled' if u.id == me.id }}>
|
||||
<option value="1" {{ 'selected' if u.is_admin }}>管理员</option>
|
||||
<option value="0" {{ 'selected' if not u.is_admin }}>普通</option>
|
||||
</select>
|
||||
</td>
|
||||
<td class="mono sm nowrap">{{ u.last_login_at or '—' }}</td>
|
||||
<td class="num">{{ u.login_count }}</td>
|
||||
<td class="nowrap">
|
||||
<button class="btn sm" type="button" data-act="save">保存</button>
|
||||
<button class="btn sm ghost" type="button" data-act="pwd">改密</button>
|
||||
{% if u.id != me.id %}
|
||||
<button class="btn sm danger" type="button" data-act="del">删除</button>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr><td colspan="7" class="empty">还没有账号</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p class="hint">「改密」会依次询问新密码与确认;管理员不能取消自己的管理员身份,任何人也不能删除自己。</p>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<section class="card">
|
||||
<div class="cardhead">
|
||||
<h2>用户操作审计</h2>
|
||||
<span class="hint">最近 20 条</span>
|
||||
</div>
|
||||
<div class="tablewrap scroll-y">
|
||||
<table class="tbl">
|
||||
<thead><tr><th>时间</th><th>操作者</th><th>动作</th><th>说明</th><th>IP</th></tr></thead>
|
||||
<tbody>
|
||||
{% for a in audits %}
|
||||
<tr>
|
||||
<td class="mono sm nowrap">{{ a.at[5:] if a.at else '' }}</td>
|
||||
<td>{{ a.actor or '—' }}</td>
|
||||
<td><span class="tag mute">{{ a.action }}</span></td>
|
||||
<td>{{ a.detail or '—' }}</td>
|
||||
<td class="mono sm">{{ a.ip or '' }}</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr><td colspan="5" class="empty">暂无用户操作记录</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{{ url_for('static', filename='js/app.js') }}"></script>
|
||||
<script>
|
||||
WBU.bindForm('#formNewUser', '/api/users', {
|
||||
validate: function (d) {
|
||||
if (!d.username) return '用户名必填';
|
||||
return d.password === d.password2 ? null : '两次输入的密码不一致';
|
||||
}
|
||||
});
|
||||
|
||||
// 行内操作走事件委托:表格动态渲染也不会丢绑定
|
||||
document.getElementById('userTable').addEventListener('click', function (e) {
|
||||
var btn = e.target.closest('button[data-act]');
|
||||
if (!btn) return;
|
||||
var row = btn.closest('tr'), uid = row.dataset.uid, name = row.dataset.name;
|
||||
var act = btn.dataset.act;
|
||||
var old = btn.textContent;
|
||||
btn.disabled = true;
|
||||
btn.textContent = '…';
|
||||
|
||||
function done(p) {
|
||||
p.then(function (j) {
|
||||
if (j.ok === false) { WBU.say(j.message || '操作失败', 'error'); return; }
|
||||
WBU.say(j.message || '已保存', 'ok');
|
||||
window.setTimeout(function () { location.reload(); }, 900);
|
||||
}).catch(function (err) {
|
||||
if (String(err.message) !== 'unauthorized') WBU.say('请求失败:' + err.message, 'error');
|
||||
}).finally(function () { btn.disabled = false; btn.textContent = old; });
|
||||
}
|
||||
|
||||
if (act === 'save') {
|
||||
var fields = {};
|
||||
fields.display_name = row.querySelector('[name=display_name]').value;
|
||||
var sel = row.querySelector('[name=is_admin]');
|
||||
if (sel && !sel.disabled) fields.is_admin = sel.value;
|
||||
done(WBU.post('/api/users/' + uid, fields));
|
||||
} else if (act === 'pwd') {
|
||||
var p1 = window.prompt('为用户「' + name + '」设置新密码(至少 6 位)');
|
||||
if (p1 === null) { btn.disabled = false; btn.textContent = old; return; }
|
||||
var p2 = window.prompt('再输入一次新密码以确认');
|
||||
if (p2 === null) { btn.disabled = false; btn.textContent = old; return; }
|
||||
if (p1 !== p2) { WBU.say('两次输入的密码不一致', 'warn'); btn.disabled = false; btn.textContent = old; return; }
|
||||
done(WBU.post('/api/users/' + uid, { password: p1, password2: p2 }));
|
||||
} else if (act === 'del') {
|
||||
if (!window.confirm('确定删除用户「' + name + '」?该操作不可撤销(其历史审计记录会保留)。')) {
|
||||
btn.disabled = false; btn.textContent = old; return;
|
||||
}
|
||||
done(WBU.post('/api/users/' + uid + '/delete', {}));
|
||||
} else {
|
||||
btn.disabled = false; btn.textContent = old;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,361 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""页面路由(Jinja 模板)。
|
||||
|
||||
分工:
|
||||
/ 概览(KPI + 入口)
|
||||
/dashboard ECharts 交互大屏(独立静态页,登录后可达,数据走 /api/bundle)
|
||||
/tasks 任务管理:调度开关/时刻、手动触发、运行历史
|
||||
/config 配置管理:Cookie / UA / 采集参数 / 改密码
|
||||
/logs 日志管理:采集逐次明细 + 应用日志尾部
|
||||
/records 数据明细:分页、筛选、搜索、导出
|
||||
"""
|
||||
import csv
|
||||
import io
|
||||
import os
|
||||
import sqlite3
|
||||
|
||||
from flask import (Blueprint, current_app, flash, jsonify, redirect, render_template,
|
||||
request, send_from_directory, url_for)
|
||||
|
||||
from .. import collect, config, db, query, scheduler
|
||||
from ..security import (admin_required, clear_fail, current_user, is_locked, lock_left,
|
||||
login_ok, login_required, login_session, logout_session,
|
||||
note_fail, safe_next)
|
||||
|
||||
bp = Blueprint("views", __name__)
|
||||
|
||||
|
||||
def _ip():
|
||||
return request.headers.get("X-Forwarded-For", request.remote_addr or "").split(",")[0].strip()
|
||||
|
||||
|
||||
# ---------------- 登录 ----------------
|
||||
def _login_ctx(**kw):
|
||||
"""登录页共用的上下文:锁定阈值/会话时长都从配置读,避免模板里写死数字。"""
|
||||
kw.setdefault("max_fails", config.MAX_LOGIN_FAILS)
|
||||
kw.setdefault("lock_minutes", config.LOGIN_LOCK_MINUTES)
|
||||
kw.setdefault("session_hours", config.SESSION_HOURS)
|
||||
return kw
|
||||
|
||||
|
||||
@bp.route("/login", methods=["GET", "POST"])
|
||||
def login():
|
||||
nxt = request.values.get("next") or ""
|
||||
if request.method == "POST":
|
||||
ip = _ip()
|
||||
if is_locked(ip):
|
||||
n = lock_left(ip)
|
||||
flash("登录失败次数过多,请 %d 秒后再试" % n, "error")
|
||||
return render_template("login.html", **_login_ctx(next_url=nxt)), 429
|
||||
username = (request.form.get("username") or "").strip()
|
||||
pwd = request.form.get("password") or ""
|
||||
conn = db.get_db()
|
||||
user = login_ok(conn, username, pwd)
|
||||
if user is None:
|
||||
n = note_fail(ip)
|
||||
db.audit(conn, "login_failed", username, "第 %d 次失败" % n, ip)
|
||||
flash("用户名或密码不正确(剩余尝试 %d 次)" % max(0, config.MAX_LOGIN_FAILS - n), "error")
|
||||
# 必须把 next 显式回填:失败后 request.args 为空,
|
||||
# 若模板从 request.args 取值会导致跳转目标丢失(历史 bug)。
|
||||
return render_template("login.html", **_login_ctx(username=username, next_url=nxt)), 401
|
||||
clear_fail(ip)
|
||||
login_session(user)
|
||||
db.audit(conn, "login", username, "登录成功", ip)
|
||||
return redirect(safe_next(nxt, url_for("views.overview")))
|
||||
if current_user():
|
||||
return redirect(url_for("views.overview"))
|
||||
return render_template("login.html", **_login_ctx(next_url=nxt))
|
||||
|
||||
|
||||
@bp.post("/logout")
|
||||
@login_required
|
||||
def logout_post():
|
||||
"""退出登录改为 POST + CSRF:GET 型退出会被 <img src> 这类请求静默触发。"""
|
||||
u = current_user()
|
||||
if u:
|
||||
db.audit(db.get_db(), "logout", u["username"], "", _ip())
|
||||
logout_session()
|
||||
flash("已退出登录", "ok")
|
||||
return redirect(url_for("views.login"))
|
||||
|
||||
|
||||
@bp.get("/logout")
|
||||
def logout():
|
||||
"""保留 GET 入口:老书签/旧页面不会 405,但只做跳转不执行退出。"""
|
||||
if current_user():
|
||||
flash("为安全起见,退出登录请点页面右上角的「退出」按钮", "warn")
|
||||
return redirect(url_for("views.overview"))
|
||||
return redirect(url_for("views.login"))
|
||||
|
||||
|
||||
# ---------------- 概览 ----------------
|
||||
@bp.get("/")
|
||||
@login_required
|
||||
def overview():
|
||||
conn = db.get_db()
|
||||
mf = query.manifest(conn)
|
||||
t = query.totals(conn)
|
||||
today = db.now_str()[:10]
|
||||
st = query.summary(conn, today, today)
|
||||
d30 = query.summary(conn, _shift(-29), today)
|
||||
# 昨日对比:昨日整日 vs 今日(残日),让「今天偏少」有参照
|
||||
y = _shift(-1)
|
||||
yest = query.summary(conn, y, y)
|
||||
dims = query.dims(conn)
|
||||
# 注意:这里的 SQL 必须把模板用到的列都选出来(模板渲染 r.fetched,
|
||||
# 少选一列并不会报错,只会静默渲染成空白 —— 历史 bug)。
|
||||
runs = conn.execute(
|
||||
"SELECT id,trigger,status,started_at,duration_ms,fetched,added,dup,total,conflicts,message"
|
||||
" FROM collect_runs ORDER BY id DESC LIMIT 8").fetchall()
|
||||
return render_template("overview.html", mf=mf, totals=t, today_stat=st, stat30=d30,
|
||||
yesterday=yest, yday=y,
|
||||
models=dims["model"][:8], clients=dims["client"],
|
||||
runs=runs, sch=_sch_info(conn), active="overview")
|
||||
|
||||
|
||||
def _shift(days):
|
||||
from datetime import datetime, timedelta
|
||||
return (datetime.now() + timedelta(days=days)).strftime("%Y-%m-%d")
|
||||
|
||||
|
||||
# ---------------- 大屏(独立 ECharts 页)----------------
|
||||
@bp.get("/dashboard")
|
||||
@bp.get("/dashboard/")
|
||||
@login_required
|
||||
def dashboard():
|
||||
# 大屏是独立静态页,但它也是导航的一项 —— 用 redirect 到一个带 active 的
|
||||
# 路由做不到(静态页由 send_from_directory 直接吐文件),所以这里只负责发文件;
|
||||
# 导航高亮改为让大屏页自己提供「返回后台」入口(见 index.html 的 topbar)。
|
||||
return send_from_directory(os.path.join(current_app.static_folder, "dashboard"), "index.html")
|
||||
|
||||
|
||||
# ---------------- 任务管理 ----------------
|
||||
def _sch_info(conn):
|
||||
sch = scheduler.get_scheduler()
|
||||
nxt = scheduler.next_run_at(conn)
|
||||
last = conn.execute("SELECT * FROM collect_runs ORDER BY id DESC LIMIT 1").fetchone()
|
||||
return {
|
||||
"running": sch.running,
|
||||
"enabled": db.get_bool(conn, "schedule_enabled", True),
|
||||
"times": scheduler.slots(conn),
|
||||
"next_run": nxt.strftime("%Y-%m-%d %H:%M:%S") if nxt else None,
|
||||
"catch_up": db.get_bool(conn, "catch_up", True),
|
||||
"interval": sch.interval,
|
||||
"last": dict(last) if last else None,
|
||||
"lock": os.path.exists(collect.LOCK_PATH),
|
||||
"now": db.now_str(),
|
||||
}
|
||||
|
||||
|
||||
@bp.get("/tasks")
|
||||
@login_required
|
||||
def tasks():
|
||||
conn = db.get_db()
|
||||
page = _int_arg("page", 1, 1, 10 ** 6)
|
||||
size = 20
|
||||
total = conn.execute("SELECT COUNT(*) FROM collect_runs").fetchone()[0]
|
||||
runs = conn.execute("SELECT * FROM collect_runs ORDER BY id DESC LIMIT ? OFFSET ?",
|
||||
(size, (page - 1) * size)).fetchall()
|
||||
s = db.get_settings(conn)
|
||||
pages = max(1, (total + size - 1) // size)
|
||||
return render_template("tasks.html", runs=runs, sch=_sch_info(conn),
|
||||
s_times=s.get("schedule_times") or "",
|
||||
s_grace=s.get("catch_up_grace_hours") or "12",
|
||||
page=page, pages=pages, total=total,
|
||||
page_window=_page_window(page, pages),
|
||||
active="tasks")
|
||||
|
||||
|
||||
def _int_arg(name, default, lo, hi):
|
||||
"""健壮地取整数查询参数:非法值回落到 default,不抛异常(历史 500 来源)。"""
|
||||
try:
|
||||
return max(lo, min(hi, int(request.args.get(name) or default)))
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _page_window(page, pages, span=9):
|
||||
"""分页器只渲染当前页附近的一段,避免几百个页码链接把页面撑长。"""
|
||||
if pages <= span:
|
||||
return list(range(1, pages + 1))
|
||||
half = span // 2
|
||||
lo = max(1, min(page - half, pages - span + 1))
|
||||
return list(range(lo, lo + span))
|
||||
|
||||
|
||||
# ---------------- 配置管理 ----------------
|
||||
@bp.get("/config")
|
||||
@login_required
|
||||
def config_page():
|
||||
conn = db.get_db()
|
||||
s = db.get_settings(conn)
|
||||
for k in [k for k in list(s) if config.is_internal_key(k)]:
|
||||
s.pop(k, None)
|
||||
cookie = (s.pop("cookie", "") or "")
|
||||
s["cookie_hint"] = ("%d 字符,结尾 …%s" % (len(cookie), cookie[-16:])) if cookie else ""
|
||||
return render_template("config.html", s=s, sch=_sch_info(conn),
|
||||
secret_keys=config.SECRET_KEYS,
|
||||
num_settings=config.NUM_SETTINGS,
|
||||
active="config")
|
||||
|
||||
|
||||
# ---------------- 用户管理 ----------------
|
||||
@bp.get("/users")
|
||||
@admin_required
|
||||
def users_page():
|
||||
conn = db.get_db()
|
||||
users = conn.execute(
|
||||
"SELECT id,username,display_name,is_admin,created_at,last_login_at,login_count"
|
||||
" FROM users ORDER BY id").fetchall()
|
||||
audits = conn.execute("SELECT * FROM audit_log WHERE action LIKE 'user%'"
|
||||
" ORDER BY id DESC LIMIT 20").fetchall()
|
||||
return render_template("users.html", users=users, audits=audits,
|
||||
me=current_user(), active="users")
|
||||
|
||||
|
||||
# ---------------- 日志管理 ----------------
|
||||
@bp.get("/logs")
|
||||
@login_required
|
||||
def logs():
|
||||
conn = db.get_db()
|
||||
run_id = request.args.get("run")
|
||||
detail = None
|
||||
if run_id and str(run_id).isdigit():
|
||||
detail = conn.execute("SELECT * FROM collect_runs WHERE id=?", (int(run_id),)).fetchone()
|
||||
status = request.args.get("status") or ""
|
||||
w, p = ("WHERE status = ?", [status]) if status in ("ok", "warn", "error", "running") else ("", [])
|
||||
# 操作审计:按动作筛选 + 分页(原来只能看最近 40 条,等于不可查)
|
||||
act = request.args.get("act") or ""
|
||||
aw, ap = ("WHERE action = ?", [act]) if act else ("", [])
|
||||
apage = _int_arg("apage", 1, 1, 10 ** 6)
|
||||
asize = 20
|
||||
atotal = conn.execute("SELECT COUNT(*) FROM audit_log %s" % aw, ap).fetchone()[0]
|
||||
audits = conn.execute("SELECT * FROM audit_log %s ORDER BY id DESC LIMIT ? OFFSET ?" % aw,
|
||||
ap + [asize, (apage - 1) * asize]).fetchall()
|
||||
# 注意传的是 sqlite3.Row 列表而不是纯字符串列表:模板要用 a[0]=动作、a[1]=次数,
|
||||
# 若在这里就用推导式取 r[0],模板里的 a[0] 会变成「字符串的第一个字符」。
|
||||
actions = conn.execute(
|
||||
"SELECT action, COUNT(*) n FROM audit_log GROUP BY action ORDER BY n DESC, action").fetchall()
|
||||
page = _int_arg("page", 1, 1, 10 ** 6)
|
||||
size = 30
|
||||
total = conn.execute("SELECT COUNT(*) FROM collect_runs %s" % w, p).fetchone()[0]
|
||||
runs = conn.execute("SELECT id,trigger,status,started_at,duration_ms,fetched,added,dup,total,"
|
||||
"conflicts,exit_code,message FROM collect_runs %s"
|
||||
" ORDER BY id DESC LIMIT ? OFFSET ?" % w, p + [size, (page - 1) * size]).fetchall()
|
||||
apages = max(1, (atotal + asize - 1) // asize)
|
||||
return render_template("logs.html", runs=runs, detail=detail, audits=audits,
|
||||
actions=actions, act=act, apage=apage, apages=apages, atotal=atotal,
|
||||
apage_window=_page_window(apage, apages, span=7),
|
||||
page=page, pages=max(1, (total + size - 1) // size), total=total,
|
||||
page_window=_page_window(page, max(1, (total + size - 1) // size)),
|
||||
status=status,
|
||||
active="logs")
|
||||
|
||||
|
||||
@bp.get("/logs/tail")
|
||||
@login_required
|
||||
def logs_tail():
|
||||
n = _int_arg("lines", 200, 10, 2000)
|
||||
path = config.APP_LOG
|
||||
if not os.path.exists(path):
|
||||
return jsonify({"lines": [], "path": path, "size": 0})
|
||||
# 只保留尾部 n 行,不把整个日志文件读进内存
|
||||
from collections import deque
|
||||
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
||||
lines = list(deque(f, maxlen=n))
|
||||
return jsonify({"lines": [l.rstrip("\n") for l in lines], "path": path,
|
||||
"size": os.path.getsize(path)})
|
||||
|
||||
|
||||
# ---------------- 数据明细 ----------------
|
||||
def _day_args():
|
||||
"""取明细页的日期区间。
|
||||
|
||||
规范参数名是 from / to,但模板内部为了避免与 Python 关键字混淆用的是
|
||||
frm,历史上也出现过 `?frm=` 的链接,这里一并接受,避免导出/翻页丢筛选条件。
|
||||
"""
|
||||
a = request.args
|
||||
frm = query.norm_day(a.get("from") or a.get("frm"))
|
||||
to = query.norm_day(a.get("to"))
|
||||
if frm and to and frm > to:
|
||||
frm, to = to, frm
|
||||
return frm, to
|
||||
|
||||
|
||||
@bp.get("/records")
|
||||
@login_required
|
||||
def records():
|
||||
conn = db.get_db()
|
||||
frm, to = _day_args()
|
||||
model = request.args.get("model") or None
|
||||
client = request.args.get("client") or None
|
||||
q = request.args.get("q") or None
|
||||
order = request.args.get("order") or "ts_desc"
|
||||
page = _int_arg("page", 1, 1, 10 ** 6)
|
||||
size = _int_arg("size", 50, 10, query.MAX_PAGE_SIZE)
|
||||
data = query.records_page(conn, frm, to, model=model, client=client, q=q,
|
||||
page=page, size=size, order=order)
|
||||
# 只算一次 dims:query.dims() 内部有 3 条 GROUP BY,重复调用纯属浪费
|
||||
d = query.dims(conn)
|
||||
models = [r["name"] for r in d["model"]]
|
||||
clients = [r["name"] for r in d["client"]]
|
||||
# 注意:不要把含 "items" 键的 dict 直接交给模板——Jinja 的属性查找会先命中
|
||||
# dict.items 这个方法而不是数据,所以这里拆出独立变量。
|
||||
return render_template("records.html", data=data, rows=data["items"], models=models,
|
||||
clients=clients,
|
||||
page_window=_page_window(page, data["pages"]),
|
||||
f=dict(frm=frm or "", to=to or "", model=model or "",
|
||||
client=client or "", q=q or "", order=order, size=size),
|
||||
active="records")
|
||||
|
||||
|
||||
@bp.get("/records/export")
|
||||
@login_required
|
||||
def records_export():
|
||||
"""按当前筛选条件导出 CSV(与官网 xlsx 同构的列)。
|
||||
|
||||
用 csv 模块逐行写(原来手工拼字符串,model/client 含逗号或引号时会串列),
|
||||
数据用 query.iter_records 流式取,不把整个结果集读进内存。
|
||||
"""
|
||||
from flask import Response
|
||||
frm, to = _day_args()
|
||||
model = request.args.get("model") or None
|
||||
client = request.args.get("client") or None
|
||||
q = request.args.get("q") or None
|
||||
order = request.args.get("order") or "ts_desc"
|
||||
|
||||
def gen():
|
||||
yield "\ufeff" # UTF-8 BOM:Excel 直接双击不乱码
|
||||
buf = io.StringIO()
|
||||
w = csv.writer(buf, lineterminator="\r\n")
|
||||
w.writerow(collect.FIELDS)
|
||||
yield buf.getvalue()
|
||||
# 重要:流式响应的 body 是在视图返回、请求上下文被 pop(teardown 里
|
||||
# close_db 已把 g.db 关掉)之后才被 WSGI 服务器逐块拉取的,
|
||||
# 所以这里绝不能复用 db.get_db() 的连接——那会直接
|
||||
# `sqlite3.ProgrammingError: Cannot operate on a closed database`。
|
||||
# 自己开一条连接,并在流结束时关掉。
|
||||
own = db.connect()
|
||||
try:
|
||||
for r in query.iter_records(own, frm, to, model=model, client=client,
|
||||
q=q, order=order):
|
||||
buf.seek(0)
|
||||
buf.truncate(0)
|
||||
w.writerow([r["request_id"], "%.2f" % r["credits"], r["prompt"] or "",
|
||||
r["model"], r["client"], r["ts"]])
|
||||
yield buf.getvalue()
|
||||
finally:
|
||||
own.close()
|
||||
|
||||
name = "usage_%s_%s.csv" % (frm or "all", to or db.now_str()[:10])
|
||||
resp = Response(gen(), mimetype="text/csv; charset=utf-8",
|
||||
headers={"Content-Disposition": 'attachment; filename="%s"' % name})
|
||||
# 导出可能很慢,避免 nginx 之类的前置代理先缓冲整个响应体
|
||||
resp.headers["X-Accel-Buffering"] = "no"
|
||||
return resp
|
||||
|
||||
|
||||
# ---------------- 兼容旧地址 ----------------
|
||||
@bp.get("/index.html")
|
||||
def legacy_index():
|
||||
return redirect(url_for("views.dashboard"))
|
||||
在新工单中引用
屏蔽一个用户