perf(api): optimize bulk user URI fetching in webpanel

Refactored the web panel's user link generation to resolve a major performance bottleneck. Previously, fetching links for N users would trigger N separate script executions, causing significant delays from process startup overhead.

- Introduced a new bulk API endpoint (`/api/v1/users/uri/bulk`) that accepts a list of usernames and calls the backend script only once.
- Updated the frontend JavaScript in `users.html` to use this new endpoint, replacing N parallel API calls with a single one.
- Cleaned up the `wrapper_uri.py` script for better readability and maintainability.
This commit is contained in:
Whispering Wind
2025-08-27 17:22:04 +03:30
committed by GitHub
parent c494250f9f
commit 18d3a1029b
4 changed files with 227 additions and 698 deletions

View File

@ -18,6 +18,8 @@ class UserInfoResponse(BaseModel):
class UserListResponse(RootModel):
root: dict[str, UserInfoResponse]
class UsernamesRequest(BaseModel):
usernames: List[str]
class AddUserInputBody(BaseModel):
username: str

View File

@ -1,7 +1,15 @@
import json
from typing import List
from fastapi import APIRouter, HTTPException
from .schema.user import UserListResponse, UserInfoResponse, AddUserInputBody, EditUserInputBody, UserUriResponse, AddBulkUsersInputBody
from .schema.user import (
UserListResponse,
UserInfoResponse,
AddUserInputBody,
EditUserInputBody,
UserUriResponse,
AddBulkUsersInputBody,
UsernamesRequest
)
from .schema.response import DetailResponse
import cli_api
@ -79,6 +87,29 @@ async def add_bulk_users_api(body: AddBulkUsersInputBody):
raise HTTPException(status_code=500,
detail=f"An unexpected error occurred while adding bulk users: {str(e)}")
@router.post('/uri/bulk', response_model=List[UserUriResponse])
async def show_multiple_user_uris_api(request: UsernamesRequest):
"""
Get URI information for multiple users in a single request for efficiency.
"""
if not request.usernames:
return []
try:
uri_data_list = cli_api.show_user_uri_json(request.usernames)
if not uri_data_list:
raise HTTPException(status_code=404, detail='No URI data found for the provided users.')
valid_responses = [data for data in uri_data_list if not data.get('error')]
return valid_responses
except cli_api.ScriptNotFoundError as e:
raise HTTPException(status_code=500, detail=f'Server script error: {str(e)}')
except cli_api.CommandExecutionError as e:
raise HTTPException(status_code=400, detail=f'Error executing script: {str(e)}')
except Exception as e:
raise HTTPException(status_code=400, detail=f'Unexpected error: {str(e)}')
@router.get('/{username}', response_model=UserInfoResponse)
async def get_user_api(username: str):