feat(login): 登录任务展示触发方式与执行人
- 后端按批次/账号关联 LoginAttempt,返回 trigger_source/category/操作人 - 登录任务页增加“触发方式”“执行人”列 - 区分自动化/人工、首次登录/高价值重登/普通重登/账号检测 - 补充登录任务来源回归测试
This commit is contained in:
@@ -12,12 +12,14 @@
|
|||||||
走 ``/stop/{batch_id}``,不是删除。
|
走 ``/stop/{batch_id}``,不是删除。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
import web.backend.database as database_module
|
import web.backend.database as database_module
|
||||||
from tests.support.db import get_test_engine, make_test_session
|
from tests.support.db import get_test_engine, make_test_session
|
||||||
from web.backend.models import Account, LoginTask, User
|
from web.backend.models import Account, LoginAttempt, LoginTask, User
|
||||||
from web.backend.routers.login import delete_task, delete_tasks, list_tasks
|
from web.backend.routers.login import delete_task, delete_tasks, list_tasks
|
||||||
|
|
||||||
|
|
||||||
@@ -53,6 +55,17 @@ class TestTaskPagination:
|
|||||||
self.session.add(task)
|
self.session.add(task)
|
||||||
self.session.flush()
|
self.session.flush()
|
||||||
self.task_ids.append(task.id)
|
self.task_ids.append(task.id)
|
||||||
|
self.session.add(
|
||||||
|
LoginAttempt(
|
||||||
|
account_id=self.account.id,
|
||||||
|
batch_id="paged-batch",
|
||||||
|
category="relogin_ordinary",
|
||||||
|
trigger_source="automation",
|
||||||
|
created_by=self.user.id,
|
||||||
|
status="success",
|
||||||
|
quota_day=datetime.now(UTC).date(),
|
||||||
|
)
|
||||||
|
)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
||||||
def teardown_method(self):
|
def teardown_method(self):
|
||||||
@@ -116,6 +129,18 @@ class TestTaskPagination:
|
|||||||
assert data["total"] == 12
|
assert data["total"] == 12
|
||||||
|
|
||||||
|
|
||||||
|
def test_login_task_exposes_trigger_source_and_operator(self):
|
||||||
|
db = self._fresh()
|
||||||
|
try:
|
||||||
|
data = list_tasks(page=1, page_size=20, db=db, current=self.user)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
item = data["items"][0]
|
||||||
|
assert item.trigger_source == "automation"
|
||||||
|
assert item.category == "relogin_ordinary"
|
||||||
|
assert item.created_by_username == "admin"
|
||||||
|
|
||||||
|
|
||||||
class TestBulkDelete:
|
class TestBulkDelete:
|
||||||
def setup_method(self):
|
def setup_method(self):
|
||||||
self.engine = get_test_engine()
|
self.engine = get_test_engine()
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from sqlalchemy.orm import Session
|
|||||||
|
|
||||||
from ..database import WorkerSessionLocal, get_db
|
from ..database import WorkerSessionLocal, get_db
|
||||||
from ..deps import authenticate_websocket, get_current_user, require_permission
|
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 ..permissions import user_has_permission
|
||||||
from ..schemas import LOGIN_BATCH_MAX_ACCOUNTS, LoginBatchRequest, LoginTaskOut
|
from ..schemas import LOGIN_BATCH_MAX_ACCOUNTS, LoginBatchRequest, LoginTaskOut
|
||||||
from ..services.audit_service import record_audit
|
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
|
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 = []
|
result = []
|
||||||
for t in rows:
|
for t in rows:
|
||||||
attempt_status = t.attempt_status or ""
|
attempt_status = t.attempt_status or ""
|
||||||
account = accounts_map.get(int(t.account_id))
|
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(
|
result.append(
|
||||||
LoginTaskOut(
|
LoginTaskOut(
|
||||||
id=t.id,
|
id=t.id,
|
||||||
@@ -299,6 +334,9 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]:
|
|||||||
or t.message
|
or t.message
|
||||||
or "",
|
or "",
|
||||||
created_by=t.created_by,
|
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,
|
created_at=t.created_at,
|
||||||
finished_at=t.attempt_at or t.finished_at,
|
finished_at=t.attempt_at or t.finished_at,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -402,6 +402,9 @@ class LoginTaskOut(BaseModel):
|
|||||||
cookie: str = ""
|
cookie: str = ""
|
||||||
message: str = ""
|
message: str = ""
|
||||||
created_by: int
|
created_by: int
|
||||||
|
created_by_username: str = ""
|
||||||
|
trigger_source: str = ""
|
||||||
|
category: str = ""
|
||||||
created_at: datetime | None = None
|
created_at: datetime | None = None
|
||||||
finished_at: datetime | None = None
|
finished_at: datetime | None = None
|
||||||
|
|
||||||
@@ -425,6 +428,9 @@ class LoginTaskOut(BaseModel):
|
|||||||
"cookie": self.cookie,
|
"cookie": self.cookie,
|
||||||
"message": self.message,
|
"message": self.message,
|
||||||
"created_by": self.created_by,
|
"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),
|
"created_at": _isoformat(self.created_at),
|
||||||
"finished_at": _isoformat(self.finished_at),
|
"finished_at": _isoformat(self.finished_at),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -486,6 +486,9 @@ export interface LoginTaskItem {
|
|||||||
cookie: string;
|
cookie: string;
|
||||||
message: string;
|
message: string;
|
||||||
created_by: number;
|
created_by: number;
|
||||||
|
created_by_username: string;
|
||||||
|
trigger_source: string;
|
||||||
|
category: string;
|
||||||
created_at: string | null;
|
created_at: string | null;
|
||||||
finished_at: string | null;
|
finished_at: string | null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,31 @@ const STATUS_LABELS: Record<string, string> = {
|
|||||||
account_auth_unknown: '认证状态未知',
|
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> = {
|
const ACCOUNT_STATUS_LABELS: Record<string, string> = {
|
||||||
imported: '未登录',
|
imported: '未登录',
|
||||||
active: '正常',
|
active: '正常',
|
||||||
@@ -590,6 +615,21 @@ export default function LoginTasksPage() {
|
|||||||
width: 130,
|
width: 130,
|
||||||
render: (name: string) => name || <Text type="secondary">未分配</Text>,
|
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 状态',
|
title: 'CK 状态',
|
||||||
dataIndex: 'ck_check_status',
|
dataIndex: 'ck_check_status',
|
||||||
|
|||||||
Reference in New Issue
Block a user