initial
This commit is contained in:
0
app/routers/__init__.py
Normal file
0
app/routers/__init__.py
Normal file
42
app/routers/configs.py
Normal file
42
app/routers/configs.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import bson
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import configs
|
||||
|
||||
|
||||
class RequestPostBody(pydantic.BaseModel):
|
||||
name: str
|
||||
stage: str
|
||||
project: str
|
||||
|
||||
|
||||
class RequestPutBody(pydantic.BaseModel):
|
||||
id: str
|
||||
value: dict
|
||||
|
||||
|
||||
class RequestDeleteBody(pydantic.BaseModel):
|
||||
id: str
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/configs', status_code=fastapi.status.HTTP_202_ACCEPTED)
|
||||
async def post(body: RequestPostBody):
|
||||
await configs.create(configs.Config(name=body.name, project=body.project, stage=body.stage, value={}))
|
||||
|
||||
|
||||
@router.put('/api/v1/configs', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={404: {'description': 'Not found'}})
|
||||
async def put(body: RequestPutBody):
|
||||
changed = await configs.update_data(id=bson.ObjectId(body.id), value=body.value)
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
|
||||
|
||||
@router.delete('/api/v1/configs', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={404: {'description': 'Not found'}})
|
||||
async def delete(body: RequestDeleteBody):
|
||||
changed = await configs.delete(id=bson.ObjectId(body.id))
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
40
app/routers/experiments.py
Normal file
40
app/routers/experiments.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import experiments
|
||||
|
||||
|
||||
class RequestPostBody(pydantic.BaseModel):
|
||||
name: str
|
||||
stage: str
|
||||
project: str
|
||||
|
||||
|
||||
class RequestPutBody(pydantic.BaseModel):
|
||||
name: str
|
||||
stage: str
|
||||
project: str
|
||||
enabled: bool
|
||||
condition: str
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/experiments', status_code=fastapi.status.HTTP_202_ACCEPTED)
|
||||
async def post(body: RequestPostBody):
|
||||
await experiments.create(experiments.Experiment(name=body.name, project=body.project, stage=body.stage, enabled=False, condition='False'))
|
||||
|
||||
|
||||
@router.put('/api/v1/experiments', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={404: {'description': 'Not found'}})
|
||||
async def put(body: RequestPutBody):
|
||||
changed = await experiments.update(project=body.project, stage=body.stage, name=body.name, enabled=body.enabled, condition=body.condition)
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
|
||||
|
||||
@router.delete('/api/v1/experiments', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={404: {'description': 'Not found'}})
|
||||
async def delete(body: RequestPostBody):
|
||||
changed = await experiments.delete(project=body.project, stage=body.stage, name=body.name)
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
57
app/routers/fetch.py
Normal file
57
app/routers/fetch.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import asyncio
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import configs
|
||||
from app.storage.mongo import experiments
|
||||
from app.storage.mongo import staff
|
||||
|
||||
|
||||
class ExperimentData(pydantic.BaseModel):
|
||||
enabled: bool
|
||||
condition: str
|
||||
|
||||
|
||||
class PlatformStaff(pydantic.BaseModel):
|
||||
vk_id: list[int]
|
||||
yandex_id: list[int]
|
||||
telegram_id: list[int]
|
||||
email: list[str]
|
||||
|
||||
|
||||
class ResponseBody(pydantic.BaseModel):
|
||||
configs: dict[str, dict]
|
||||
experiments: dict[str, ExperimentData]
|
||||
platform_staff: PlatformStaff
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/fetch')
|
||||
async def execute(stage: str, project: str):
|
||||
confs, exps, staffs = await asyncio.gather(
|
||||
configs.get(project=project, stage=stage),
|
||||
experiments.get(project=project, stage=stage),
|
||||
staff.get(),
|
||||
)
|
||||
platform_staff = PlatformStaff(
|
||||
vk_id=[],
|
||||
yandex_id=[],
|
||||
telegram_id=[],
|
||||
email=[],
|
||||
)
|
||||
for user in staffs:
|
||||
if user.vk_id:
|
||||
platform_staff.vk_id.append(user.vk_id)
|
||||
if user.yandex_id:
|
||||
platform_staff.yandex_id.append(user.yandex_id)
|
||||
if user.telegram_id:
|
||||
platform_staff.telegram_id.append(user.telegram_id)
|
||||
if user.email:
|
||||
platform_staff.email.append(user.email)
|
||||
return ResponseBody(
|
||||
configs={conf.name: conf.value for conf in confs},
|
||||
experiments={exp.name: ExperimentData(enabled=exp.enabled, condition=exp.condition) for exp in exps},
|
||||
platform_staff=platform_staff,
|
||||
)
|
||||
43
app/routers/staff.py
Normal file
43
app/routers/staff.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import staff
|
||||
|
||||
|
||||
class RequestPutBody(pydantic.BaseModel):
|
||||
platform_id: int
|
||||
vk_id: int|None
|
||||
yandex_id: int|None
|
||||
telegram_id: int|None
|
||||
email: str|None
|
||||
|
||||
|
||||
class RequestPostBody(pydantic.BaseModel):
|
||||
platform_id: int
|
||||
email: str|None
|
||||
|
||||
|
||||
class RequestDeleteBody(pydantic.BaseModel):
|
||||
platform_id: int
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/staff', status_code=fastapi.status.HTTP_202_ACCEPTED)
|
||||
async def post(body: RequestPostBody):
|
||||
await staff.create(staff=staff.Staff(platform_id=body.platform_id, email=body.email))
|
||||
|
||||
|
||||
@router.put('/api/v1/staff', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={404: {'description': 'Not found'}})
|
||||
async def put(body: RequestPutBody):
|
||||
changed = await staff.update(platform_id=body.platform_id, email=body.email, vk_id=body.vk_id, yandex_id=body.yandex_id, telegram_id=body.telegram_id)
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
|
||||
|
||||
@router.delete('/api/v1/staff', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={404: {'description': 'Not found'}})
|
||||
async def delete(body: RequestDeleteBody):
|
||||
changed = await staff.delete(platform_id=body.platform_id)
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
Reference in New Issue
Block a user