From abaf65737e7f8566c6277929b8ecad72f3f3d70e Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Wed, 21 Oct 2026 16:44:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=80=9A=E7=9F=A5):=20=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=8C=89=E7=B1=BB=E5=9E=8B=E5=88=86=E5=88=AB?= =?UTF-8?q?=E5=8F=91=E9=80=81=EF=BC=88=E4=B8=8D=E5=81=9A=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/account_folder.py | 59 +++++++++++++++++++----------------- core/notifier.py | 5 +-- core/notify_events.py | 10 +++--- doc/ACCOUNT_FOLDER.md | 8 +++-- doc/CHANGES_2026-10.md | 17 ++++++----- doc/NOTIFY.md | 2 +- scripts/tests/test_roster.py | 26 ++++++++++------ 7 files changed, 73 insertions(+), 54 deletions(-) diff --git a/core/account_folder.py b/core/account_folder.py index fe35163..6cea354 100644 --- a/core/account_folder.py +++ b/core/account_folder.py @@ -22,7 +22,6 @@ import re import threading import time import uuid -from collections import Counter from core.logger import get_logger @@ -1170,40 +1169,46 @@ def set_account_status(name, status=""): def notify_account_status_summary(job_name="", source="计划运行"): """汇总账号库的「账号状态」并发通知(webhook 事件 `account.status.summary`)。 - 统计口径:`正常` = 状态恰好是「正常」;`异常` = 非空且不是「正常」 - (作品异常/账号异常/限流…);空状态算「未设置」,不算异常。 - 通知内容:总数 / 正常数 / 异常数 / 状态分布 / 异常账号清单(含设备)。 + 用户口径:**不做统计,按状态类型分别发送** —— 每个状态一条消息, + 标题是「账号状态:作品异常」,正文列出这个类型的账号(昵称 + 设备)。 + 空状态(未设置)不单独发;没有可发的类型时不发。 """ try: with _ctx(): rows = roster_rows() - if not rows: - return False - counts = Counter() - abnormal_rows = [] + groups = {} for r in rows: - st = (r.get("account_status") or "").strip() or "未设置" - counts[st] += 1 - if st not in ("正常", "未设置"): - abnormal_rows.append(r) - status_counts = [f"{k} {v}" for k, v in counts.most_common()] - abnormal_accounts = [ - "%s(%s · %s)" % (r.get("nickname") or "?", - r.get("account_status") or "未设置", - r.get("device_name") or "未登记") - for r in abnormal_rows[:20] - ] + st = (r.get("account_status") or "").strip() + if not st or st == "未设置": + continue + groups.setdefault(st, []).append(r) + if not groups: + return False + + def _prio(st): + if "异常" in st: + return 0 + if "正常" in st: + return 2 + return 1 + from core import notifier - notifier.notify( - "account.status.summary", - job_name=job_name or "全部账号", source=source, - total=len(rows), normal=counts.get("正常", 0), - abnormal=len(abnormal_rows), status_counts=status_counts, - abnormal_accounts=abnormal_accounts, - level=("warning" if abnormal_rows else "success")) + for st in sorted(groups, key=_prio): + rs = groups[st] + accounts = "、".join( + "%s(%s)" % (r.get("nickname") or "?", + r.get("device_name") or "未登记") + for r in rs) + level = ("warning" if ("异常" in st or "待提升" in st) + else ("success" if "正常" in st else "info")) + notifier.notify( + "account.status.summary", + title="账号状态:%s" % st, + job_name=job_name or "全部账号", source=source, + status_type=st, accounts=accounts, level=level) return True except Exception as e: - _log.warning(f"账号状态汇总通知失败(不影响主流程): {e}") + _log.warning(f"账号状态分类通知失败(不影响主流程): {e}") return False diff --git a/core/notifier.py b/core/notifier.py index cd835e2..6d49ef2 100644 --- a/core/notifier.py +++ b/core/notifier.py @@ -340,8 +340,9 @@ _FIELD_LABELS = { "title": "标题", "message": "内容", "patrol_name": "巡检", "check_label": "检查", "action_label": "动作", "detail": "结果", "stopped": "停止", "failed_devices": "失败设备", - "normal": "正常", "abnormal": "异常", "status_counts": "状态分布", - "abnormal_accounts": "异常账号", "source": "来源", + "normal": "正常", "abnormal": "异常", "attention": "待关注", + "status_counts": "状态分布", "status_type": "状态类型", "accounts": "账号", + "abnormal_accounts": "异常/待关注账号", "source": "来源", "failed_items": "失败明细", "filename": "文件", "size": "大小(字节)", "tables": "表数", "include_apk": "含APK", "user": "操作人", "operator": "操作人", "env": "环境", "db_target": "数据库", "device_count": "设备数", diff --git a/core/notify_events.py b/core/notify_events.py index 583c210..1af8ede 100644 --- a/core/notify_events.py +++ b/core/notify_events.py @@ -125,11 +125,11 @@ EVENTS = [ ["serial", "device_name", "model", "want", "msg"], "抖音停在登录页或要验证码 —— **需要人工登录**(平台绝不代替登录/过滑块)," "本设备已停止本轮任务", agg_window=0, agg_key=_BY_SERIAL, recommend=True), - _e("account.status.summary", "账号状态汇总", "业务", - ["job_name", "source", "total", "normal", "abnormal", - "status_counts", "abnormal_accounts"], - "账号状态分布 + 异常账号清单:每次「检测账号」运行结束发一次," - "另外每天定时发一次全量汇总(可在通知设置里关)", + _e("account.status.summary", "账号状态分类", "业务", + ["title", "job_name", "source", "status_type", "accounts"], + "按账号状态**类型分别发送**:每个类型一条消息,列出该状态的账号" + "(不做总数/正常/异常统计)。每次「检测账号」运行结束发一次," + "另外每天定时发一次", agg_window=0, recommend=True), _e("task.video.published", "视频已发布", "业务", ["serial", "device_name", "model", "phone", "nickname", "release_date", "seq", diff --git a/doc/ACCOUNT_FOLDER.md b/doc/ACCOUNT_FOLDER.md index 4cad506..03e8c90 100644 --- a/doc/ACCOUNT_FOLDER.md +++ b/doc/ACCOUNT_FOLDER.md @@ -306,9 +306,11 @@ App 从每段的动作组/任务带出来(列表里显示"动作组 · App"或 | 操作 | 扫描 · 编辑账号 · **上传素材**(单个文件,见下)· **删除**(见下) | — | **状态筛选与汇总通知**:表格上方可按「账号状态」筛选(全部 / 只看异常 / 具体状态 / 未设置)。 -每次「检测账号」计划跑完会自动发一条 `account.status.summary` 到已勾选该事件的 webhook -(总数/正常/异常/状态分布/异常账号清单);另外每天 09:30 发一次全量汇总 -(`ACCOUNT_STATUS_DAILY=0` 关每日,`ACCOUNT_STATUS_SUMMARY_AT` 改时间)。见 [NOTIFY.md](NOTIFY.md)。 +每次「检测账号」计划跑完会自动发 `account.status.summary` 到已勾选该事件的 webhook: +**按状态类型分别发送**(每个类型一条消息,标题如「账号状态:作品异常」,正文列出该类型的账号, +不做总数统计);另外每天 09:30 再发一次。 +(`ACCOUNT_STATUS_DAILY=0` 关每日,`ACCOUNT_STATUS_SUMMARY_AT=HH:MM` 改时间——这两个是服务器 +`.env` 配置,不是页面开关)。见 [NOTIFY.md](NOTIFY.md)。 **删除账号**(行内「删除」):账号表 = 账号库 ∪ 素材目录,所以弹窗分三项勾选: diff --git a/doc/CHANGES_2026-10.md b/doc/CHANGES_2026-10.md index 9f581ca..3497ad9 100644 --- a/doc/CHANGES_2026-10.md +++ b/doc/CHANGES_2026-10.md @@ -934,22 +934,25 @@ data/accounts/周周苏州探店日记、周周苏州约会好去处/_日志/202 - 与「所属手机」「昵称搜索」**叠加**生效;「清除筛选」一并还原;计数显示 `共 N / 筛出 M`。 - 纯前端(`static/admin/account_folder.js`),刷新页面即可用(强刷加载新 JS)。 -## 34. 2026-10-21 新增:账号状态汇总通知(webhook) +## 34. 2026-10-21 新增:账号状态分类通知(webhook) 用户:"汇总账号状态情况的通知发送到我的 webhook;每次运行完发还是什么时候发,你看一下。" +随后更正:"你不用统计,你就按照类型分类发送就行了。" -**触发时机(两个,互补)**: +**触发时机(两个)**: 1. **每次「检测账号」计划跑完发一次**:构建出的计划任务里含 `read_text(var=account_status)` 才触发(普通发布计划不发,避免刷屏)。 -2. **每天 09:30 发一次全量汇总**:服务器重启后按下一个 09:30 算; - `ACCOUNT_STATUS_DAILY=0` 关闭,`ACCOUNT_STATUS_SUMMARY_AT=HH:MM` 改时间。 +2. **每天 09:30 再发一次**:`ACCOUNT_STATUS_DAILY=0` 关闭, + `ACCOUNT_STATUS_SUMMARY_AT=HH:MM` 改时间(服务器 `.env`,不是页面开关)。 -**内容**(事件 `account.status.summary`):总数 / 正常数 / 异常数 / 状态分布 / -异常账号清单(昵称 + 状态 + 设备);有异常时消息级别为 warning。 +**内容**(事件 `account.status.summary`):**按状态类型分别发送**—— +每个类型一条消息,标题如「账号状态:作品异常」,正文列出该类型的账号(昵称 + 设备), +**不做总数/正常/异常统计**。异常类型消息级别 warning,正常类型 success。 - 账号状态数据来自 `device_account.account_status`(资产管理那份)。 - 生产企微 webhook 的订阅列表已加上该事件(旧配置 37 → 38 个事件)。 - 新增 `notify_events` 事件与字段中文标签;`account_folder.notify_account_status_summary()`; 计划收尾按 `status_watch` 触发;`web_server.py` 启动每日汇总线程。 -- 单测:`test_roster.py` ⑧(事件/总数/正常/异常/清单)、`test_plan.py`(识别 read_text 状态步骤)。 +- 单测:`test_roster.py` ⑧(按 3 个类型分别发、标题/账号清单、无统计字段)、 + `test_plan.py`(识别 read_text 状态步骤)。 diff --git a/doc/NOTIFY.md b/doc/NOTIFY.md index 90760e8..c262d85 100644 --- a/doc/NOTIFY.md +++ b/doc/NOTIFY.md @@ -55,7 +55,7 @@ daemon 线程。所以: | 业务 | `task.video.published`(视频发布成功,带作品分享链接)/ `task.video.failed`(发布失败**或结果未知**——结果未知意味着可能已经发出去了,要到计划页人工确认,平台不会自动重发) | | 业务 | `task.page.mismatch`(「校验页面」没等到该出现的特征元素 —— **上一步没生效**,默认跳过本轮剩余步骤,避免在错的页面上继续盲点;见 TASK_DEV §4.7)/ `task.account.switch_failed`(切号失败:账号列表里没这个名字,或切完回读还是别人 —— **本轮不会发这个号的内容**,要人工到设备上确认号还在不在)/ `task.account.need_login`(抖音停在登录/验证码页,**需要人工登录**,平台绝不代替登录/过滑块;本设备已停止本轮任务)—— 都由「切换账号」步骤发出 | | 业务·任务自己发 | `task.notify.custom`(步骤「发通知」)/ `task.patrol.hit`(任务「公共巡检」命中)——**标题正文由任务自己写**,见下 | -| 业务·账号状态 | `account.status.summary`(**每次「检测账号」计划跑完自动发一次**(该计划包含 `read_text` 写 `account_status`);另外每天 **09:30** 发一次全量汇总。内容:总数 / 正常 / 异常 / 状态分布 / 异常账号清单。`ACCOUNT_STATUS_DAILY=0` 关每日,`ACCOUNT_STATUS_SUMMARY_AT=HH:MM` 改时间) | +| 业务·账号状态 | `account.status.summary`(**按状态类型分别发送**:每个类型一条消息,标题「账号状态:作品异常」,正文列出该类型的账号(昵称 + 设备),**不做总数/正常/异常统计**。每次「检测账号」计划跑完自动发一次(该计划包含 `read_text` 写 `account_status`);另外每天 **09:30** 再发一次。`ACCOUNT_STATUS_DAILY=0` 关每日,`ACCOUNT_STATUS_SUMMARY_AT=HH:MM` 改时间——这两个是服务器 `.env` 配置,不是页面上的开关) | | Worker | `worker.connected` · `.attempt.done` · `.attempt.error`(单次尝试级,噪音大,默认没人勾) | | 设备 | `device.online` · `.offline` · `.discovered` · `.claimed` · `device.heartbeat_timeout` · `device.battery.low` · `.battery.recovered` | | 安装 | `apk.install.started` · `.finished` | diff --git a/scripts/tests/test_roster.py b/scripts/tests/test_roster.py index 3283e98..abdaa25 100644 --- a/scripts/tests/test_roster.py +++ b/scripts/tests/test_roster.py @@ -218,10 +218,13 @@ print("▶ ⑧ 账号状态汇总通知(事件 account.status.summary)") with app.app_context(): r = DeviceAccount.query.filter_by(nickname="锋哥苏州百业探访").first() if r: - r.account_status = "正常" + r.account_status = "账号正常" r2 = DeviceAccount.query.filter_by(nickname="周周苏州吃喝玩乐").first() if r2: r2.account_status = "账号异常" + r3 = DeviceAccount.query.filter_by(nickname="锋哥苏州甲探访").first() + if r3: + r3.account_status = "账号安全待提升" db.session.commit() notes = [] @@ -232,14 +235,19 @@ try: ok = af.notify_account_status_summary(job_name="检测账号", source="计划运行") finally: _notifier.notify = _orig_notify -check("汇总函数返回 True", ok, True) -check("事件名", notes[0][0] if notes else "", "account.status.summary") -kw = notes[0][1] if notes else {} -check("总数等于账号库", kw.get("total"), len(af.roster_rows())) -check("有异常账号", (kw.get("abnormal") or 0) >= 1, True) -check("有正常账号", (kw.get("normal") or 0) >= 1, True) -check("异常清单包含具体账号", - any("周周苏州吃喝玩乐" in x for x in (kw.get("abnormal_accounts") or [])), True) +check("分类通知函数返回 True", ok, True) +check("按类型分别发送(3 类)", len(notes), 3) +check("事件名统一", {ev for ev, _ in notes}, {"account.status.summary"}) +by_type = {kw.get("status_type"): kw for _, kw in notes} +ab = by_type.get("账号异常") or {} +check("账号异常标题", "账号异常" in (ab.get("title") or ""), True) +check("账号异常列表", "周周苏州吃喝玩乐" in (ab.get("accounts") or ""), True) +att = by_type.get("账号安全待提升") or {} +check("待提升列表", "锋哥苏州甲探访" in (att.get("accounts") or ""), True) +nrm = by_type.get("账号正常") or {} +check("账号正常列表", "锋哥苏州百业探访" in (nrm.get("accounts") or ""), True) +check("没有统计字段(按类型发,不做汇总)", + ("total" in ab or "normal" in ab or "abnormal" in ab), False) print() if fails: