diff --git a/core/account_folder.py b/core/account_folder.py index 83a1ef2..0450d5d 100644 --- a/core/account_folder.py +++ b/core/account_folder.py @@ -143,7 +143,8 @@ def _skip_name(name): def list_accounts(prof=None): """根目录下的账号列表(一个子目录 = 一个账号)+ 素材数/待发数/上次运行。""" from core import video_plan as vp - from core.models import AeRun, VideoPlan + from core.models import AeRun, VideoPlan, AccountStatusLog + import datetime prof = merged(prof) root = account_root(prof) out = [] @@ -174,6 +175,22 @@ def list_accounts(prof=None): comment_counts = _comment.pending_counts() except Exception: comment_counts = {"by_id": {}, "by_name": {}} + # 近 7 天异常:一次查出所有异常检测记录,按 account_id/昵称取最近一条。 + now = datetime.datetime.now() + cutoff = (now - datetime.timedelta(days=7)).strftime("%Y-%m-%d %H:%M:%S") + try: + logs = (AccountStatusLog.query + .filter(AccountStatusLog.detected_at >= cutoff, + AccountStatusLog.status.like("%异常%")) + .order_by(AccountStatusLog.detected_at.desc()).all()) + except Exception: + logs = [] + anomaly_by_id, anomaly_by_name = {}, {} + for lg in logs: + if lg.account_id and lg.account_id not in anomaly_by_id: + anomaly_by_id[lg.account_id] = lg + if lg.nickname and lg.nickname not in anomaly_by_name: + anomaly_by_name[lg.nickname] = lg all_names = sorted(dirs | set(roster.keys())) for name in all_names: d = os.path.join(root, name) @@ -192,9 +209,39 @@ def list_accounts(prof=None): rr = roster.get(name) or {} cp = ((comment_counts.get("by_id") or {}).get(rr.get("id") or "") or (comment_counts.get("by_name") or {}).get(name) or 0) + # 近 7 天异常:自动检测日志 + 当前人工标记取较近的一条 + lg = (anomaly_by_id.get(rr.get("id") or "") + or anomaly_by_name.get(name)) + ab_at = (lg.detected_at or "") if lg else "" + ab_src = (lg.source or "") if lg else "" + manual_abn = "异常" in (rr.get("manual_status") or "") + if manual_abn: + mat = (rr.get("updated_at") or "").strip() + if not ab_at or (mat and mat > ab_at): + ab_at = mat or now.strftime("%Y-%m-%d %H:%M:%S") + ab_src = "人工标记" + ab_days = None + if ab_at: + try: + _dt = datetime.datetime.strptime(ab_at[:19], "%Y-%m-%d %H:%M:%S") + ab_days = max(0, (now - _dt).days) + except Exception: + ab_days = 0 + ab_text = "无异常" + if ab_at: + ab_text = ("今天异常" if ab_days == 0 + else ("%d天前异常" % ab_days)) + if manual_abn and (not lg or ab_src == "人工标记"): + ab_text += "(人工)" out.append({"name": name, "media": media, "pending": pending, "done": done, "total": total, "need_text": need_text, "comment_pending": int(cp or 0), + "douyin_id": rr.get("douyin_id") or "", + "status_7d_abnormal": bool(ab_at), + "status_7d_at": ab_at, + "status_7d_days": ab_days, + "status_7d_text": ab_text, + "status_7d_source": ab_src, "unscanned": max(0, media - total), "has_dir": has_dir, "device_name": rr.get("device_name") or "", @@ -232,8 +279,9 @@ def export_accounts_csv(prof=None): out = io.StringIO() w = csv.writer(out) w.writerow(["设备号", "账号昵称", "抖音号", "手机号", "在线", "自动状态", - "人工标记", "有效状态", "检测时间", "可发视频", "备注", - "素材入库", "未发布", "已发布", "最近发布"]) + "人工标记", "有效状态", "检测时间", "近7天状态", "最近异常时间", + "异常来源", "可发视频", "备注", "素材入库", "未发布", "已发布", + "最近发布"]) for a in rows: name = a.get("name") or "" rr = roster.get(name) or {} @@ -247,6 +295,9 @@ def export_accounts_csv(prof=None): a.get("manual_status") or "", a.get("effective_status") or a.get("account_status") or "", a.get("status_checked_at") or "", + a.get("status_7d_text") or "", + a.get("status_7d_at") or "", + a.get("status_7d_source") or "", "是" if rr.get("can_post_video") else "否", rr.get("note") or "", a.get("total") or 0, diff --git a/doc/ACCOUNT_FOLDER.md b/doc/ACCOUNT_FOLDER.md index beb0292..3c65850 100644 --- a/doc/ACCOUNT_FOLDER.md +++ b/doc/ACCOUNT_FOLDER.md @@ -322,17 +322,19 @@ App 从每段的动作组/任务带出来(列表里显示"动作组 · App"或 | 列 | 内容 | 可编辑 | |---|---|---| -| 账号 | 昵称(+ 抖音号小字);不在账号库的会标「账号库没有」 | 编辑走账号库 | +| 账号 | 昵称;不在账号库的会标「账号库没有」 | 编辑走账号库 | +| **抖音号** | `device_account.douyin_id`(账号库里的抖音号) | 编辑走账号库 | | **所属手机** | `A01` / 未登记 / 未入池;下拉直接**改 `device_account.device_name`** | ✅ 换机就在这里改 | | **账号状态(自动)** | 任务自动检测到的 `账号正常` / `账号异常` / `作品异常` / `账号安全待提升`…(`device_account.account_status`)+ **最近检测时间**(`status_checked_at`) | ✅ 点「改」手填;点「记录」看历史(时间/状态/设备/来源);任务「读取文字」写变量 `account_status` 时自动回写(读到什么写什么,读不到不写) | | **人工标记** | 人工选择 `未标记` / `账号异常` / `作品异常` / `账号限流` / `账号正常`(`device_account.manual_status`) | ✅ 下拉直接选;**优先于自动状态**(筛选、通知、账号池 API 都按“有效状态”= 人工标记优先);任务后续自动检测**不会覆盖**人工判断;选「未标记」= 清除,回落自动状态 | +| **近7天状态** | 计算列:近 7 天 `account_status_log` 里最近一条含「异常」的记录(或当前人工标记异常)。无异常显示「无异常」;有异常显示「N天前异常」/「今天异常(人工)」 | —(悬停看最近异常时间/来源) | | 素材库 | `入库 N · 未发布 N · 已发布 N`(+ 文件夹里还没入库的个数) | 点开进二级页 | | 最近发布 | 该账号最后一条的 `published_at` | — | | 操作 | 扫描 · 编辑账号 · **上传素材**(单个文件,见下)· **删除**(见下) | — | **导出**:工具栏「⬇ 导出账号列表」下载 CSV(UTF-8 BOM,Excel 直接打开)。列: 设备号 / 账号昵称 / 抖音号 / 手机号 / 在线 / 自动状态 / 人工标记 / 有效状态 / 检测时间 / -可发视频 / 备注 / 素材入库 / 未发布 / 已发布 / 最近发布。 +**近7天状态 / 最近异常时间 / 异常来源** / 可发视频 / 备注 / 素材入库 / 未发布 / 已发布 / 最近发布。 **状态筛选与汇总通知**:表格上方可按「账号状态」筛选(全部 / 只看异常 / 具体状态 / 未设置), **按有效状态筛**(人工标记优先,所以人工标了异常的号也会进「只看异常」)。 diff --git a/doc/API.md b/doc/API.md index 476715a..6ef6ca6 100644 --- a/doc/API.md +++ b/doc/API.md @@ -1093,7 +1093,7 @@ multipart 上传 `.zip` 或 `.db`(字段 `file`)→ 校验并暂存: | POST | `/api/af/scan` | 扫账号目录入库(`dry_run` 只预览) | | POST | `/api/af/upload` | 上传素材到账号文件夹(form-data,传完自动扫描) | | GET | `/api/af/items` | 条目列表(`account`/`date`/`status`/`category`/`display_mode`/`q`) | -| GET | `/api/af/accounts/export` | **导出账号列表 + 状态 CSV**(UTF-8 BOM;设备/昵称/抖音号/手机号/在线/自动状态/人工标记/有效状态/检测时间/可发视频/备注/素材计数) | +| GET | `/api/af/accounts/export` | **导出账号列表 + 状态 CSV**(UTF-8 BOM;设备/昵称/抖音号/手机号/在线/自动状态/人工标记/有效状态/检测时间/**近7天状态/最近异常时间/异常来源**/可发视频/备注/素材计数) | | GET | `/api/af/assets/template` | **下载素材模板 CSV**(UTF-8 BOM) | | POST | `/api/af/assets/import` | **模板批量上传**(两项校验 → 分拣落盘 → 入库;`dry_run` 只报告不写盘) | | DELETE | `/api/af/account/` | **删除账号**(`delete_roster` / `archive_dir` / `delete_records` 三项勾选;文件夹只归档不永久删) | diff --git a/doc/CHANGES_2026-10.md b/doc/CHANGES_2026-10.md index a9090e3..e54714c 100644 --- a/doc/CHANGES_2026-10.md +++ b/doc/CHANGES_2026-10.md @@ -1138,3 +1138,19 @@ A02 一度被记成 20.65(实际是 A11 的手机),自动认领把 A11 迁 - 测试:`test_comment.py` 覆盖表情解析、入库、`emoji_rows` 计数。 - 生产上 10:30 那次旧代码跑出的 `done/unknown` 评论行会在部署后统一退回 `pending`, 重新导入带表情的 UTF-8 数据后再跑。 + +## 44. 2026-10-31 新增:资产表「抖音号」列 + 「近7天状态」计算列 + +用户:"资产管理加一列显示抖音号,然后再加一列计算列,计算 7 天内账号状态: +没有异常显示无异常,有异常显示异常的时间几天前异常。" + +- 账号总表新增 **抖音号** 列(`device_account.douyin_id`,账号库为空时显示—)。 +- 新增 **近7天状态** 计算列: + - 数据源 = `account_status_log` 近 7 天内**状态含「异常」**的记录,取最近一条; + - 当前 `manual_status` 含「异常」也算(按人工标记的 `updated_at`;没有则按今天),文案带「(人工)」; + - 无异常 → **「无异常」**;有异常 → **「N天前异常」** / **「今天异常」**; + - 悬停显示最近异常时间与来源(计划运行/任务运行/手动修改/人工标记)。 +- `list_accounts` 一次查出 7 天内异常日志(按 account_id/昵称索引),不做 N+1; + 导出 CSV 同步增加「近7天状态 / 最近异常时间 / 异常来源」。 +- 测试:`test_roster.py` ⑪(自动日志 3 天前、无异常、人工异常今天、抖音号)。 +- 无数据库迁移(全部是计算列)。 diff --git a/scripts/tests/test_roster.py b/scripts/tests/test_roster.py index c8592fa..23c402b 100644 --- a/scripts/tests/test_roster.py +++ b/scripts/tests/test_roster.py @@ -304,7 +304,32 @@ check("清除人工标记后回落到自动状态", row.effective_status, "账 check("账号库里没有的号 → 不新建行", af.set_manual_status("没这个号", "账号异常"), False) -print("▶ ⑪ 导出账号列表 + 状态") +print("▶ ⑪ 近7天异常计算(自动日志 + 人工标记)") +import datetime # noqa: E402 +_now = datetime.datetime.now() +with app.app_context(): + db.session.add(DeviceAccount(id="r7d", device_name="A01", nickname="七天测试号", + douyin_id="9001")) + db.session.add(DeviceAccount(id="rno", device_name="A02", nickname="无异常测试号", + douyin_id="9002")) + db.session.add(DeviceAccount(id="rman", device_name="A03", nickname="人工异常测试号", + douyin_id="9003", manual_status="账号异常", + updated_at=_now.strftime("%Y-%m-%d %H:%M:%S"))) + db.session.add(AccountStatusLog( + id="log7d", account_id="r7d", nickname="七天测试号", status="账号异常", + source="计划运行", + detected_at=(_now - datetime.timedelta(days=3)).strftime("%Y-%m-%d %H:%M:%S"), + created_at=(_now - datetime.timedelta(days=3)).strftime("%Y-%m-%d %H:%M:%S"))) + db.session.commit() +_accts = {a["name"]: a for a in af.list_accounts()} +check("7天内有异常标记", _accts["七天测试号"]["status_7d_abnormal"], True) +check("7天异常天数", _accts["七天测试号"]["status_7d_days"], 3) +check("7天异常文案", _accts["七天测试号"]["status_7d_text"], "3天前异常") +check("无异常文案", _accts["无异常测试号"]["status_7d_text"], "无异常") +check("人工异常按今天显示", _accts["人工异常测试号"]["status_7d_text"], "今天异常(人工)") +check("抖音号列", _accts["七天测试号"]["douyin_id"], "9001") + +print("▶ ⑫ 导出账号列表 + 状态") _csv = af.export_accounts_csv().decode("utf-8-sig") check("CSV 有表头", "账号昵称" in _csv and "有效状态" in _csv, True) check("CSV 带账号", "周周苏州吃喝玩乐" in _csv, True) diff --git a/static/admin/account_folder.js b/static/admin/account_folder.js index 5eb8587..fb5cbc4 100644 --- a/static/admin/account_folder.js +++ b/static/admin/account_folder.js @@ -329,6 +329,8 @@ function afHtmlAsset() { '' + esc(a.name) + '' + (a.in_roster ? '' : ' 账号库没有') + (a.has_dir ? '' : '
还没有素材文件夹
') + '' + + '' + + (a.douyin_id ? esc(a.douyin_id) : '—') + '' + '' + devSel + '' + '' + (a.account_status @@ -346,6 +348,13 @@ function afHtmlAsset() { esc(a.status_checked_at) + '' : '') + '' + '' + afManualStatusHtml(a) + '' + + '' + + '' + + esc(a.status_7d_text || '无异常') + '' + '