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,
|
||||
)
|
||||
|
||||
@@ -402,6 +402,9 @@ class LoginTaskOut(BaseModel):
|
||||
cookie: str = ""
|
||||
message: str = ""
|
||||
created_by: int
|
||||
created_by_username: str = ""
|
||||
trigger_source: str = ""
|
||||
category: str = ""
|
||||
created_at: datetime | None = None
|
||||
finished_at: datetime | None = None
|
||||
|
||||
@@ -425,6 +428,9 @@ class LoginTaskOut(BaseModel):
|
||||
"cookie": self.cookie,
|
||||
"message": self.message,
|
||||
"created_by": self.created_by,
|
||||
"created_by_username": self.created_by_username,
|
||||
"trigger_source": self.trigger_source,
|
||||
"category": self.category,
|
||||
"created_at": _isoformat(self.created_at),
|
||||
"finished_at": _isoformat(self.finished_at),
|
||||
}
|
||||
|
||||
@@ -486,6 +486,9 @@ export interface LoginTaskItem {
|
||||
cookie: string;
|
||||
message: string;
|
||||
created_by: number;
|
||||
created_by_username: string;
|
||||
trigger_source: string;
|
||||
category: string;
|
||||
created_at: string | null;
|
||||
finished_at: string | null;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,31 @@ const STATUS_LABELS: Record<string, string> = {
|
||||
account_auth_unknown: '认证状态未知',
|
||||
};
|
||||
|
||||
const TRIGGER_SOURCE_LABELS: Record<string, string> = {
|
||||
automation: '自动化',
|
||||
manual: '人工',
|
||||
};
|
||||
|
||||
const LOGIN_CATEGORY_LABELS: Record<string, string> = {
|
||||
onboarding: '首次登录',
|
||||
relogin_high_value: '高价值重登',
|
||||
relogin_ordinary: '普通重登',
|
||||
manual: '人工登录',
|
||||
account_check: '账号检测',
|
||||
};
|
||||
|
||||
function loginSourceLabel(task: LoginTaskItem) {
|
||||
const source = TRIGGER_SOURCE_LABELS[task.trigger_source] || '未知';
|
||||
const category = LOGIN_CATEGORY_LABELS[task.category] || '';
|
||||
return category ? `${source} · ${category}` : source;
|
||||
}
|
||||
|
||||
function loginSourceColor(task: LoginTaskItem) {
|
||||
if (task.trigger_source === 'automation') return 'blue';
|
||||
if (task.trigger_source === 'manual') return 'green';
|
||||
return 'default';
|
||||
}
|
||||
|
||||
const ACCOUNT_STATUS_LABELS: Record<string, string> = {
|
||||
imported: '未登录',
|
||||
active: '正常',
|
||||
@@ -590,6 +615,21 @@ export default function LoginTasksPage() {
|
||||
width: 130,
|
||||
render: (name: string) => name || <Text type="secondary">未分配</Text>,
|
||||
},
|
||||
{
|
||||
title: '触发方式',
|
||||
dataIndex: 'trigger_source',
|
||||
width: 170,
|
||||
render: (_: string, record: LoginTaskItem) => (
|
||||
<Tag color={loginSourceColor(record)}>{loginSourceLabel(record)}</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '执行人',
|
||||
dataIndex: 'created_by_username',
|
||||
width: 110,
|
||||
render: (name: string, record: LoginTaskItem) =>
|
||||
name || `#${record.created_by}`,
|
||||
},
|
||||
{
|
||||
title: 'CK 状态',
|
||||
dataIndex: 'ck_check_status',
|
||||
|
||||
Reference in New Issue
Block a user