feat(通知): 账号状态汇总(检测账号跑完 + 每日 09:30)
- 新事件 account.status.summary:总数/正常/异常/状态分布/异常账号清单 - 计划收尾检测 read_text(account_status) 才发,普通发布计划不刷屏 - 每日 09:30 全量汇总;ACCOUNT_STATUS_DAILY/ACCOUNT_STATUS_SUMMARY_AT 可配 - 生产企微 webhook 已订阅该事件;测试与文档同步
This commit is contained in:
@@ -19,8 +19,10 @@
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from collections import Counter
|
||||
|
||||
from core.logger import get_logger
|
||||
|
||||
@@ -1165,6 +1167,81 @@ def set_account_status(name, status=""):
|
||||
return True
|
||||
|
||||
|
||||
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 = []
|
||||
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]
|
||||
]
|
||||
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"))
|
||||
return True
|
||||
except Exception as e:
|
||||
_log.warning(f"账号状态汇总通知失败(不影响主流程): {e}")
|
||||
return False
|
||||
|
||||
|
||||
_DAILY_STATUS_STARTED = False
|
||||
|
||||
|
||||
def start_daily_status_summary(at="09:30"):
|
||||
"""启动每日账号状态汇总(默认 09:30;`ACCOUNT_STATUS_DAILY=0` 关闭)。"""
|
||||
global _DAILY_STATUS_STARTED
|
||||
if _DAILY_STATUS_STARTED:
|
||||
return False
|
||||
if (os.environ.get("ACCOUNT_STATUS_DAILY", "1") or "1").strip() == "0":
|
||||
_log.info("账号状态每日汇总未启动(ACCOUNT_STATUS_DAILY=0)")
|
||||
return False
|
||||
_DAILY_STATUS_STARTED = True
|
||||
threading.Thread(target=_daily_status_loop, args=(at,),
|
||||
name="account-status-daily", daemon=True).start()
|
||||
_log.info(f"账号状态每日汇总已启动:每天 {at}")
|
||||
return True
|
||||
|
||||
|
||||
def _daily_status_loop(at):
|
||||
while True:
|
||||
try:
|
||||
hh, mm = [int(x) for x in str(at).split(":")[:2]]
|
||||
except Exception:
|
||||
hh, mm = 9, 30
|
||||
now = time.localtime()
|
||||
target = time.mktime((now.tm_year, now.tm_mon, now.tm_mday,
|
||||
hh, mm, 0, 0, 0, -1))
|
||||
if target <= time.time():
|
||||
target += 86400
|
||||
while time.time() < target:
|
||||
time.sleep(min(300, max(1, target - time.time())))
|
||||
notify_account_status_summary(job_name="每日账号状态汇总", source="每日定时")
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
def device_of_name(name, rows=None):
|
||||
"""账号名 → 设备信息。**精确匹配名字**;名字对不上时再拿抖音号试一次。
|
||||
|
||||
|
||||
@@ -340,6 +340,8 @@ _FIELD_LABELS = {
|
||||
"title": "标题", "message": "内容", "patrol_name": "巡检",
|
||||
"check_label": "检查", "action_label": "动作", "detail": "结果",
|
||||
"stopped": "停止", "failed_devices": "失败设备",
|
||||
"normal": "正常", "abnormal": "异常", "status_counts": "状态分布",
|
||||
"abnormal_accounts": "异常账号", "source": "来源",
|
||||
"failed_items": "失败明细", "filename": "文件", "size": "大小(字节)",
|
||||
"tables": "表数", "include_apk": "含APK", "user": "操作人", "operator": "操作人",
|
||||
"env": "环境", "db_target": "数据库", "device_count": "设备数",
|
||||
|
||||
@@ -125,6 +125,12 @@ 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"],
|
||||
"账号状态分布 + 异常账号清单:每次「检测账号」运行结束发一次,"
|
||||
"另外每天定时发一次全量汇总(可在通知设置里关)",
|
||||
agg_window=0, recommend=True),
|
||||
_e("task.video.published", "视频已发布", "业务",
|
||||
["serial", "device_name", "model", "phone", "nickname", "release_date", "seq",
|
||||
"title", "share_url"],
|
||||
|
||||
@@ -442,6 +442,21 @@ def inline_task_steps(steps):
|
||||
return out
|
||||
|
||||
|
||||
def _has_account_status_read(steps):
|
||||
"""这组步骤里有没有「读取文字」写 `account_status`(决定跑完要不要发状态汇总)。"""
|
||||
for st in steps or []:
|
||||
if not isinstance(st, dict):
|
||||
continue
|
||||
p = st.get("params") or {}
|
||||
if (st.get("type") == "read_text"
|
||||
and (p.get("var") or "").strip() == "account_status"):
|
||||
return True
|
||||
for k in ("children", "then", "else"):
|
||||
if _has_account_status_read(p.get(k)):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _app_id_for_steps(steps):
|
||||
"""从任务步骤里的 `open_app`/`stop_app` 的包名反查 App 注册表(任务段没有 app_id)。"""
|
||||
from core.models import AeApp
|
||||
@@ -760,6 +775,10 @@ def _make_tracker(plan, prepared, plan_run_id, date, app_d, act_d):
|
||||
job = prepared[0][0] if prepared else None
|
||||
devices = [dev for _, dev in prepared]
|
||||
total = len(devices)
|
||||
# 这次运行会不会写「账号状态」:有 read_text(account_status) 才在收尾发汇总,
|
||||
# 普通发布计划不刷这个通知。
|
||||
status_watch = any(_has_account_status_read((j.params or {}).get("steps"))
|
||||
for j, _ in prepared)
|
||||
|
||||
_segs = plan.get("segments") or []
|
||||
# 老计划(没有 segments)默认按发布流程统计;编排里所有段都不发布素材时,
|
||||
@@ -807,6 +826,9 @@ def _make_tracker(plan, prepared, plan_run_id, date, app_d, act_d):
|
||||
for a in dev["accounts"]:
|
||||
mapping[a] = st
|
||||
_finish_plan_run(plan_run_id, per_account=mapping)
|
||||
if status_watch:
|
||||
af.notify_account_status_summary(
|
||||
job_name=plan.get("name") or "", source="计划运行")
|
||||
except Exception as e:
|
||||
_log.warning("计划批次收尾失败(%s):%s", plan_run_id, e)
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user