feat(login): 登录任务展示触发方式与执行人
- 后端按批次/账号关联 LoginAttempt,返回 trigger_source/category/操作人 - 登录任务页增加“触发方式”“执行人”列 - 区分自动化/人工、首次登录/高价值重登/普通重登/账号检测 - 补充登录任务来源回归测试
This commit is contained in:
@@ -19,7 +19,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import WorkerSessionLocal, get_db
|
||||
from ..deps import authenticate_websocket, get_current_user, require_permission
|
||||
from ..models import Account, ExecutionJob, LoginTask, User
|
||||
from ..models import Account, ExecutionJob, LoginAttempt, LoginTask, User
|
||||
from ..permissions import user_has_permission
|
||||
from ..schemas import LOGIN_BATCH_MAX_ACCOUNTS, LoginBatchRequest, LoginTaskOut
|
||||
from ..services.audit_service import record_audit
|
||||
@@ -276,10 +276,45 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
||||
):
|
||||
accounts_map[int(row[0])] = row
|
||||
|
||||
# 登录任务记录本身不带来源;来源在 LoginAttempt(人工/自动化/首次登录/重登)。
|
||||
batch_ids = {str(t.attempt_batch_id or t.batch_id or "") for t in rows}
|
||||
batch_ids.discard("")
|
||||
attempt_map: dict[tuple[str, int], LoginAttempt] = {}
|
||||
if batch_ids and account_ids:
|
||||
for attempt in (
|
||||
db.query(LoginAttempt)
|
||||
.filter(
|
||||
LoginAttempt.batch_id.in_(batch_ids),
|
||||
LoginAttempt.account_id.in_(account_ids),
|
||||
)
|
||||
.order_by(LoginAttempt.id.asc())
|
||||
.all()
|
||||
):
|
||||
attempt_map[(str(attempt.batch_id or ""), int(attempt.account_id))] = attempt
|
||||
creator_ids = {
|
||||
int(value)
|
||||
for value in [
|
||||
*(t.created_by for t in rows),
|
||||
*(attempt.created_by for attempt in attempt_map.values()),
|
||||
]
|
||||
if value
|
||||
}
|
||||
creator_map = (
|
||||
{
|
||||
int(user_id): str(username or "")
|
||||
for user_id, username in db.query(User.id, User.username)
|
||||
.filter(User.id.in_(creator_ids))
|
||||
.all()
|
||||
}
|
||||
if creator_ids
|
||||
else {}
|
||||
)
|
||||
result = []
|
||||
for t in rows:
|
||||
attempt_status = t.attempt_status or ""
|
||||
account = accounts_map.get(int(t.account_id))
|
||||
batch_id = str(t.attempt_batch_id or t.batch_id or "")
|
||||
attempt = attempt_map.get((batch_id, int(t.account_id)))
|
||||
result.append(
|
||||
LoginTaskOut(
|
||||
id=t.id,
|
||||
@@ -299,6 +334,9 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
||||
or t.message
|
||||
or "",
|
||||
created_by=t.created_by,
|
||||
created_by_username=creator_map.get(int(t.created_by), ""),
|
||||
trigger_source=(str(attempt.trigger_source or "") if attempt else ""),
|
||||
category=(str(attempt.category or "") if attempt else ""),
|
||||
created_at=t.created_at,
|
||||
finished_at=t.attempt_at or t.finished_at,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user