initial
This commit is contained in:
0
app/storage/__init__.py
Normal file
0
app/storage/__init__.py
Normal file
30
app/storage/mongo/__init__.py
Normal file
30
app/storage/mongo/__init__.py
Normal 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),
|
||||
])
|
||||
38
app/storage/mongo/configs.py
Normal file
38
app/storage/mongo/configs.py
Normal 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
|
||||
39
app/storage/mongo/experiments.py
Normal file
39
app/storage/mongo/experiments.py
Normal 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
|
||||
37
app/storage/mongo/staff.py
Normal file
37
app/storage/mongo/staff.py
Normal 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
|
||||
Reference in New Issue
Block a user