Download app/routers/admin/sync.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 3.73 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/app/routers/admin/sync.py
- Command line
-
hf download hf://spaces/binhugg2/api/app/routers/admin/sync.py
-
curl -L -o sync.py https://huggingface.co/spaces/binhugg2/api/resolve/main/app/routers/admin/sync.py
3.73 kB
| import asyncio | |
| from fastapi import APIRouter, Depends, Query | |
| from sqlalchemy import select, func | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.database import async_session, get_db | |
| from app.dependencies import get_current_admin, require_permission | |
| from app.models.admin import Admin | |
| from app.models.sync_log import SyncLog | |
| from app.models.source_site import SourceSite | |
| from app.schemas.common import ApiResponse, PaginatedData | |
| from app.services.collector import collector | |
| from app.services.sync import get_sync_logs, create_sync_log, finish_sync_log | |
| router = APIRouter() | |
| async def list_sync_logs( | |
| page: int = Query(1, ge=1), | |
| page_size: int = Query(20, ge=1, le=100), | |
| source_id: int | None = Query(None), | |
| status: str | None = Query(None), | |
| admin: Admin = Depends(require_permission("sync:list")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| logs, total = await get_sync_logs(db, source_id=source_id, status=status, page=page, page_size=page_size) | |
| if logs: | |
| source_ids = list({l.source_id for l in logs}) | |
| source_result = await db.execute(select(SourceSite).where(SourceSite.id.in_(source_ids))) | |
| source_map = {s.id: s for s in source_result.scalars().all()} | |
| else: | |
| source_map = {} | |
| items = [] | |
| for l in logs: | |
| source = source_map.get(l.source_id) | |
| items.append({ | |
| "id": l.id, | |
| "source_id": l.source_id, | |
| "source_name": source.name if source else "Unknown", | |
| "range_type": l.range_type, | |
| "status": l.status, | |
| "total_fetched": l.total_fetched, | |
| "new_count": l.new_count, | |
| "update_count": l.update_count, | |
| "error_message": l.error_message, | |
| "started_at": str(l.started_at) if l.started_at else None, | |
| "finished_at": str(l.finished_at) if l.finished_at else None, | |
| }) | |
| return ApiResponse.ok( | |
| PaginatedData(list=items, total=total, page=page, page_size=page_size).model_dump() | |
| ) | |
| async def trigger_sync( | |
| range_type: str = Query("daily", pattern="^(all|monthly|weekly|daily)$"), | |
| source_id: int | None = Query(None), | |
| admin: Admin = Depends(require_permission("sync:btn:trigger")), | |
| ): | |
| if collector.is_running: | |
| return ApiResponse.fail(40003, "同步任务正在执行中,请稍后再试") | |
| if source_id: | |
| async with async_session() as db: | |
| result = await db.execute(select(SourceSite).where(SourceSite.id == source_id)) | |
| source = result.scalar_one_or_none() | |
| if not source: | |
| return ApiResponse.fail(40401, "源站不存在") | |
| if not source.is_active: | |
| return ApiResponse.fail(40004, "源站未启用") | |
| from datetime import datetime, timezone | |
| from app.models.sync_log import SyncLog | |
| log = SyncLog( | |
| source_id=source_id, | |
| range_type=range_type, | |
| status="running", | |
| started_at=datetime.now(timezone.utc), | |
| ) | |
| async with async_session() as db: | |
| db.add(log) | |
| await db.commit() | |
| log_id = log.id | |
| result = await collector.sync_source(source, range_type) | |
| await finish_sync_log(log_id, result) | |
| else: | |
| results = await collector.sync_all(range_type) | |
| result = results[-1] if results else {} | |
| return ApiResponse.ok(result) | |
| async def sync_status( | |
| admin: Admin = Depends(require_permission("sync:list")), | |
| ): | |
| return ApiResponse.ok({ | |
| "is_running": collector.is_running, | |
| }) |