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/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