This commit is contained in:
2024-11-22 21:44:04 +03:00
commit f4ca742f43
20 changed files with 633 additions and 0 deletions

0
app/__init__.py Normal file
View File

0
app/routers/__init__.py Normal file
View File

42
app/routers/configs.py Normal file
View 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)

View 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
View 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
View 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)

0
app/storage/__init__.py Normal file
View File

View File

@@ -0,0 +1,30 @@
import os
import motor
import motor.motor_asyncio
import pymongo
MONGO_HOST = os.getenv('MONGO_HOST', 'localhost')
MONGO_PASSWORD = os.getenv('MONGO_PASSWORD', 'password')
CONNECTION_STRING = f'mongodb://mongo:{MONGO_PASSWORD}@{MONGO_HOST}:27017/'
database: 'motor.MotorDatabase' = motor.motor_asyncio.AsyncIOMotorClient(CONNECTION_STRING).configurator
def create_indexes():
client = pymongo.MongoClient(CONNECTION_STRING)
database = client.get_database('configurator')
database.get_collection('configs').create_index([
('stage', 1),
('project', 1),
('name', 1)
])
database.get_collection('experiments').create_index([
('stage', 1),
('project', 1),
('name', 1)
])
database.get_collection('staff').create_index([
('platform_id', 1),
])

View File

@@ -0,0 +1,38 @@
import bson
import pydantic
from app.storage.mongo import database
from bson import codec_options
collection = database.get_collection("configs", codec_options=codec_options.CodecOptions(tz_aware=True))
class Config(pydantic.BaseModel):
name: str
project: str
stage: str
value: dict
_id: bson.ObjectId|None = None
async def create(config: Config) -> str:
result = await collection.insert_one(config.model_dump())
return result.inserted_id
async def update_data(project: str, stage: str, name: str, value: dict) -> bool:
result = await collection.update_one({'project': project, 'stage': stage, 'name': name}, {'$set': {'value': value}})
return result.modified_count != 0
async def delete(project: str, stage: str, name: str) -> bool:
result = await collection.delete_one({'project': project, 'stage': stage, 'name': name})
return result.deleted_count != 0
async def get(project: str, stage: str) -> list[Config]:
result = []
async for item in collection.find({'stage': stage, 'project': project}):
result.append(Config.model_validate(item))
return result

View File

@@ -0,0 +1,39 @@
import bson
import pydantic
from app.storage.mongo import database
from bson import codec_options
collection = database.get_collection("experiments", codec_options=codec_options.CodecOptions(tz_aware=True))
class Experiment(pydantic.BaseModel):
name: str
enabled: bool
condition: str
project: str
stage: str
_id: bson.ObjectId|None = None
async def create(experiment: Experiment) -> str:
result = await collection.insert_one(experiment.model_dump())
return result.inserted_id
async def update(project: str, stage: str, name: str, enabled: bool, condition: str) -> bool:
result = await collection.update_one({'project': project, 'stage': stage, 'name': name}, {'$set': {'enabled': enabled, 'condition': condition}})
return result.modified_count != 0
async def delete(project: str, stage: str, name: str) -> bool:
result = await collection.delete_one({'project': project, 'stage': stage, 'name': name})
return result.deleted_count != 0
async def get(project: str, stage: str) -> list[Experiment]:
result = []
async for item in collection.find({'stage': stage, 'project': project}):
result.append(Experiment.model_validate(item))
return result

View File

@@ -0,0 +1,37 @@
import pydantic
from app.storage.mongo import database
from bson import codec_options
collection = database.get_collection("staff", codec_options=codec_options.CodecOptions(tz_aware=True))
class Staff(pydantic.BaseModel):
platform_id: int
vk_id: int|None = None
yandex_id: int|None = None
telegram_id: int|None = None
email: str|None = None
async def create(staff: Staff) -> str:
result = await collection.insert_one(staff.model_dump())
return result.inserted_id
async def update(platform_id: int, vk_id: int|None, yandex_id: int|None, telegram_id: int|None, email: str|None) -> bool:
result = await collection.update_one({'platform_id': platform_id}, {'$set': {'vk_id': vk_id, 'yandex_id': yandex_id, 'telegram_id': telegram_id, 'email': email}})
return result.modified_count != 0
async def delete(platform_id: int) -> bool:
result = await collection.delete_one({'platform_id': platform_id})
return result.deleted_count != 0
async def get() -> list[Staff]:
result = []
async for item in collection.find({}):
result.append(Staff.model_validate(item))
return result

4
app/utils/time.py Normal file
View File

@@ -0,0 +1,4 @@
import datetime
now = lambda: datetime.datetime.now(datetime.UTC)