diff --git a/api/api_v1/authz/authz.py b/api/api_v1/authz/authz.py index 8acad29..90e808a 100644 --- a/api/api_v1/authz/authz.py +++ b/api/api_v1/authz/authz.py @@ -226,3 +226,34 @@ async def edit_role( """ res = await crud.role.edit_role(db, date_in) return schemas.Msg(code=0, msg='ok', data=res.matched_count) + + + +@router.get("/update_api_list") +async def update_api_list( + request: Request, + db: AsyncIOMotorDatabase = Depends(get_database), + current_user: schemas.UserDB = Depends(deps.get_current_user), +): + """更新 api 列表""" + app = request.app + data = {} + for r in app.routes: + title = r.tags[0] if hasattr(r, 'description') else None + if not title: + continue + data.setdefault(title, {'list': []}) + path = r.path + name = r.description if hasattr(r, 'description') else r.name + data[title]['list'].append({'api': path, 'title': name}) + + data = [{'title': k, 'list': v['list']} for k, v in data.items()] + for item in data: + title = item['title'] + for l in item['list']: + api = l['api'] + name = l['title'] + add_data = schemas.UpdateApi(path=api, name=name) + await crud.api_list.update_api(db, add_data) + + return schemas.Msg(code=0, msg='ok', data=1) \ No newline at end of file diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py index cb933a1..40794d1 100644 --- a/api/api_v1/endpoints/query.py +++ b/api/api_v1/endpoints/query.py @@ -239,7 +239,7 @@ async def retention_model(request: Request, sql = res['sql'] df = await ckdb.query_dataframe(sql) if len(df) == 0: - return schemas.Msg(code=-1, msg='无数据', data=None) + return schemas.Msg(code=0, msg='无数据', data=None) title = f'用户数' date_range = res['date_range'] diff --git a/crud/crud_api_list.py b/crud/crud_api_list.py index b7667c5..1445fe3 100644 --- a/crud/crud_api_list.py +++ b/crud/crud_api_list.py @@ -13,6 +13,12 @@ class CRUDApiList(CRUDBase): return await self.update_one(db, where, data, upsert=True) + async def update_api(self, db: AsyncIOMotorDatabase, data_in: schemas.UpdateApi): + where = {'path': data_in.path} + data = {'$set': data_in.dict()} + + return await self.update_one(db, where, data, upsert=True) + async def edit_api(self, db: AsyncIOMotorDatabase, data_in: schemas.EditApi): where = {'_id': data_in.id} data = {'$set': data_in.dict(exclude={'id'})} diff --git a/schemas/api_list.py b/schemas/api_list.py index 4f3b261..014c893 100644 --- a/schemas/api_list.py +++ b/schemas/api_list.py @@ -12,13 +12,15 @@ class ApiBase(BaseModel): desc: str = None - - - class AddApi(ApiBase): path: str name: str - desc: str + desc: str = None + + +class UpdateApi(BaseModel): + path: str + name: str class AddApiDB(DBBase, AddApi):