File size: 14,497 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""Admin batch operations + SSE progress streaming.

Performance notes:
  - Uses ``run_batch`` for bounded-concurrency parallel execution
    (replaces old sequential for-loop)
  - Async mode: background task with SSE fan-out via AsyncTask
  - Sync mode: concurrent execution, single JSON response
"""

import asyncio
from typing import TYPE_CHECKING, Any, Callable, Awaitable

import orjson
from fastapi import APIRouter, Depends, Query, Request
from fastapi.responses import Response, StreamingResponse
from pydantic import BaseModel

from app.platform.config.snapshot import get_config
from app.platform.errors import AppError, ErrorKind, UpstreamError, ValidationError
from app.platform.logging.logger import logger
from app.platform.runtime.batch import run_batch
from app.platform.runtime.task import create_task, expire_task, get_task
from app.control.account.commands import AccountPatch, ListAccountsQuery
from app.control.account.state_machine import is_manageable

if TYPE_CHECKING:
    from app.control.account.refresh import AccountRefreshService
    from app.control.account.repository import AccountRepository

from . import get_refresh_svc, get_repo

router = APIRouter(prefix="/batch", tags=["Admin - Batch"])
_MAX_BATCH_CONCURRENCY = 80

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _concurrency(override: int | None, config_key: str, fallback: int = 50) -> int:
    """Resolve effective concurrency: query-param → config → fallback."""
    if override is not None:
        return min(max(1, override), _MAX_BATCH_CONCURRENCY)
    v = get_config(config_key, fallback)
    try:
        resolved = int(v)
    except (TypeError, ValueError):
        resolved = fallback
    return min(max(1, resolved), _MAX_BATCH_CONCURRENCY)


def _mask(token: str) -> str:
    return f"{token[:8]}...{token[-8:]}" if len(token) > 20 else token


async def _list_all_tokens(repo: "AccountRepository") -> list[str]:
    page_num, tokens = 1, []
    while True:
        page = await repo.list_accounts(ListAccountsQuery(page=page_num, page_size=2000))
        tokens.extend(r.token for r in page.items if is_manageable(r))
        if page_num >= page.total_pages or not page.items:
            break
        page_num += 1
    return tokens


async def _filter_manageable_tokens(repo: "AccountRepository", tokens: list[str]) -> list[str]:
    unique_tokens = list(dict.fromkeys(tokens))
    records = await repo.get_accounts(unique_tokens)
    by_token = {r.token: r for r in records}
    return [token for token in unique_tokens if (record := by_token.get(token)) and is_manageable(record)]


def _json(data: Any, status_code: int = 200) -> Response:
    return Response(content=orjson.dumps(data), media_type="application/json", status_code=status_code)


class BatchRequest(BaseModel):
    tokens: list[str] = []


# ---------------------------------------------------------------------------
# Dispatch engine — sync (run_batch) or async (background task + SSE)
# ---------------------------------------------------------------------------

async def _dispatch(
    tokens: list[str],
    handler: Callable[[str], Awaitable[dict]],
    *,
    use_async: bool,
    concurrency: int = 10,
    repo: "AccountRepository | None" = None,
) -> Response:
    if use_async:
        return await _dispatch_async(tokens, handler, concurrency)
    return await _dispatch_sync(tokens, handler, concurrency, repo)


async def _dispatch_sync(
    tokens: list[str],
    handler: Callable[[str], Awaitable[dict]],
    concurrency: int,
    repo: "AccountRepository | None" = None,
) -> Response:
    """Concurrent execution, collect all results, return at once."""
    results: dict[str, Any] = {}
    ok_c = fail_c = 0
    failed_tokens: list[str] = []

    async def _wrapped(token: str) -> tuple[str, dict | None, str | None]:
        try:
            data = await handler(token)
            return token, data, None
        except Exception as exc:
            return token, None, str(exc)

    raw = await run_batch(tokens, _wrapped, concurrency=concurrency)
    for token, data, err in raw:
        key = _mask(token)
        if err is None:
            ok_c += 1
            results[key] = data
        else:
            fail_c += 1
            failed_tokens.append(token)
            results[key] = {"error": err}

    # 批量查询失败 token 的状态,统计凭证失效数量
    expired_c = 0
    if repo and failed_tokens:
        from app.control.account.enums import AccountStatus
        records = await repo.get_accounts(failed_tokens)
        expired_c = sum(1 for r in records if r.status == AccountStatus.EXPIRED)

    return _json({
        "status": "success",
        "summary": {
            "total": len(tokens),
            "ok": ok_c,
            "fail": fail_c,
            "expired": expired_c,
            "transient": fail_c - expired_c,
        },
        "results": results,
    })


async def _dispatch_async(
    tokens: list[str],
    handler: Callable[[str], Awaitable[dict]],
    concurrency: int,
) -> Response:
    """Background task with per-item progress via AsyncTask SSE."""
    task = create_task(len(tokens))

    async def _run() -> None:
        try:
            results: dict[str, Any] = {}
            ok_c = fail_c = 0

            async def _one(token: str) -> None:
                nonlocal ok_c, fail_c
                if task.cancelled:
                    return
                masked = _mask(token)
                try:
                    if task.cancelled:
                        return
                    data = await handler(token)
                    ok_c += 1
                    results[masked] = data
                    task.record(True, item=masked, detail=data)
                except Exception as exc:
                    fail_c += 1
                    results[masked] = {"error": str(exc)}
                    task.record(False, item=masked, error=str(exc))

            await run_batch(tokens, _one, concurrency=concurrency)

            if task.cancelled:
                task.finish_cancelled()
            else:
                task.finish({
                    "status": "success",
                    "summary": {"total": len(tokens), "ok": ok_c, "fail": fail_c},
                    "results": results,
                })
        except Exception as exc:
            task.fail_task(str(exc))
        finally:
            asyncio.create_task(expire_task(task.id, 300))

    asyncio.create_task(_run())
    return _json({"status": "success", "task_id": task.id, "total": len(tokens)})


# ---------------------------------------------------------------------------
# Per-token handlers
# ---------------------------------------------------------------------------

async def _nsfw_one(repo: "AccountRepository", token: str, enabled: bool) -> dict:
    from app.dataplane.reverse.protocol.xai_auth import nsfw_sequence, set_nsfw
    if enabled:
        await nsfw_sequence(token)
    else:
        await set_nsfw(token, enabled)
    patch = AccountPatch(token=token, add_tags=["nsfw"]) if enabled else AccountPatch(token=token, remove_tags=["nsfw"])
    await repo.patch_accounts([patch])
    return {"success": True, "tagged": enabled}


async def _cache_clear_one(repo: "AccountRepository", token: str) -> dict:
    from app.control.account.invalid_credentials import mark_account_invalid_credentials
    from app.dataplane.reverse.transport.assets import list_assets, delete_asset
    try:
        resp = await list_assets(token)
        items = resp.get("assets", resp.get("items", []))

        async def _delete_one(item: dict) -> int:
            asset_id = item.get("id") or item.get("assetId")
            if not asset_id:
                return 0
            await delete_asset(token, asset_id)
            return 1

        results = await asyncio.gather(*[_delete_one(item) for item in items], return_exceptions=True)
        for result in results:
            if not isinstance(result, Exception):
                continue
            if await mark_account_invalid_credentials(repo, token, result, source="asset batch clear"):
                raise result
        return {"deleted": sum(r for r in results if isinstance(r, int))}
    except Exception as exc:
        await mark_account_invalid_credentials(repo, token, exc, source="asset batch clear")
        raise


# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------

@router.post("/nsfw")
async def batch_nsfw(
    req: BatchRequest,
    async_mode: bool = Query(False, alias="async"),
    all_manageable: bool = Query(False),
    concurrency: int | None = Query(None, ge=1),
    enabled: bool = Query(True),
    repo: "AccountRepository" = Depends(get_repo),
):
    tokens = [t.strip() for t in req.tokens if t.strip()]
    if all_manageable and tokens:
        raise ValidationError("tokens must be empty when all_manageable=true", param="tokens")
    if all_manageable:
        tokens = await _list_all_tokens(repo)
    else:
        if not tokens:
            raise ValidationError("No tokens provided", param="tokens")
        # Explicit refresh follows NSFW maintenance: only manageable accounts are operator-maintainable.
        requested_count = len(tokens)
        tokens = await _filter_manageable_tokens(repo, tokens)
        skipped_count = requested_count - len(tokens)
        if skipped_count:
            logger.info("admin batch nsfw skipped non-manageable tokens: skipped_count={}", skipped_count)
    if not tokens:
        raise ValidationError("No manageable tokens available", param="tokens")

    async def _nsfw_and_tag(token: str) -> dict:
        return await _nsfw_one(repo, token, enabled)

    c = _concurrency(concurrency, "batch.nsfw_concurrency")
    if all_manageable:
        logger.info("admin batch nsfw all manageable: token_count={} concurrency={}", len(tokens), c)
    return await _dispatch(tokens, _nsfw_and_tag, use_async=async_mode, concurrency=c)


@router.post("/refresh")
async def batch_refresh(
    req: BatchRequest,
    async_mode: bool = Query(False, alias="async"),
    all_manageable: bool = Query(False),
    concurrency: int | None = Query(None, ge=1),
    repo: "AccountRepository" = Depends(get_repo),
    refresh_svc: "AccountRefreshService" = Depends(get_refresh_svc),
):
    tokens = [t.strip() for t in req.tokens if t.strip()]
    if all_manageable and tokens:
        raise ValidationError("tokens must be empty when all_manageable=true", param="tokens")
    if all_manageable:
        tokens = await _list_all_tokens(repo)
    else:
        if not tokens:
            raise ValidationError("No tokens provided", param="tokens")
        requested_count = len(tokens)
        tokens = await _filter_manageable_tokens(repo, tokens)
        skipped_count = requested_count - len(tokens)
        if skipped_count:
            logger.info("admin batch refresh skipped non-manageable tokens: skipped_count={}", skipped_count)
    if not tokens:
        raise ValidationError("No manageable tokens available", param="tokens")

    async def _refresh_one(token: str) -> dict:
        result = await refresh_svc.refresh_tokens([token])
        if not result.refreshed:
            raise UpstreamError("未获取到真实配额数据")
        return {"refreshed": result.refreshed}

    c = _concurrency(concurrency, "batch.refresh_concurrency")
    if all_manageable:
        logger.info("admin batch refresh all manageable: token_count={} concurrency={}", len(tokens), c)
    return await _dispatch(tokens, _refresh_one, use_async=async_mode, concurrency=c, repo=repo)


@router.post("/cache-clear")
async def batch_cache_clear(
    req: BatchRequest,
    async_mode: bool = Query(False, alias="async"),
    concurrency: int | None = Query(None, ge=1),
    repo: "AccountRepository" = Depends(get_repo),
):
    tokens = [t.strip() for t in req.tokens if t.strip()]
    if not tokens:
        tokens = await _list_all_tokens(repo)
    if not tokens:
        raise ValidationError("No tokens available", param="tokens")

    async def _clear_one(token: str) -> dict:
        return await _cache_clear_one(repo, token)

    c = _concurrency(concurrency, "batch.asset_delete_concurrency")
    return await _dispatch(tokens, _clear_one, use_async=async_mode, concurrency=c)


# ---------------------------------------------------------------------------
# SSE stream + cancel
# ---------------------------------------------------------------------------

@router.get("/{task_id}/stream")
async def batch_stream(task_id: str, request: Request):
    # Auth is handled by the parent router's verify_admin_key dependency,
    # which accepts both Bearer header and ?app_key= query param (for EventSource).
    task = get_task(task_id)
    if not task:
        raise AppError(
            "Task not found",
            kind=ErrorKind.VALIDATION,
            code="task_not_found",
            status=404,
        )

    async def _stream():
        queue = task.attach()
        try:
            yield f"data: {orjson.dumps({'type': 'snapshot', **task.snapshot()}).decode()}\n\n"

            final = task.final_event()
            if final:
                yield f"data: {orjson.dumps(final).decode()}\n\n"
                return

            while True:
                try:
                    event = await asyncio.wait_for(queue.get(), timeout=15)
                except asyncio.TimeoutError:
                    yield ": ping\n\n"
                    final = task.final_event()
                    if final:
                        yield f"data: {orjson.dumps(final).decode()}\n\n"
                        return
                    continue

                yield f"data: {orjson.dumps(event).decode()}\n\n"
                if event.get("type") in ("done", "error", "cancelled"):
                    return
        finally:
            task.detach(queue)

    return StreamingResponse(_stream(), media_type="text/event-stream")


@router.post("/{task_id}/cancel")
async def batch_cancel(task_id: str):
    task = get_task(task_id)
    if not task:
        raise AppError(
            "Task not found",
            kind=ErrorKind.VALIDATION,
            code="task_not_found",
            status=404,
        )
    task.cancel()
    return {"status": "success"}