feat(资产): 抖音号列 + 近7天异常计算列

- 账号总表新增抖音号列
- 近7天状态:account_status_log 近7天异常 + 人工异常,显示 无异常/N天前异常/今天异常(人工)
- 导出 CSV 同步增加近7天状态/最近异常时间/异常来源
- test_roster 覆盖计算规则
This commit is contained in:
2026-10-31 11:14:08 +08:00
parent aba4583641
commit fb32d17877
6 changed files with 116 additions and 11 deletions
+54 -3
View File
@@ -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,