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):
|
||||
"""账号名 → 设备信息。**精确匹配名字**;名字对不上时再拿抖音号试一次。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user