Compare commits
17 Commits
3c851c8e8f
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d48e86c5b | |||
|
|
3dd862b287 | ||
| 9207037ebe | |||
|
|
786f44a29c | ||
| aa309890cd | |||
|
|
64ef3ff776 | ||
| 73569e0839 | |||
| fdb4cc98e8 | |||
| 903a77102c | |||
| 71b1f1d4a3 | |||
| 03be5ff8e7 | |||
| 0583fae9cc | |||
| d8607b8efc | |||
| 342264d7c7 | |||
| 8b871775e6 | |||
| 81c70b61d4 | |||
| 90de13452f |
@@ -6,6 +6,7 @@ services:
|
||||
image: mathwave/sprint-repo:configurator
|
||||
networks:
|
||||
- configurator-development
|
||||
- monitoring
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
@@ -20,3 +21,5 @@ services:
|
||||
networks:
|
||||
configurator-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
||||
@@ -6,6 +6,7 @@ services:
|
||||
image: mathwave/sprint-repo:configurator
|
||||
networks:
|
||||
- configurator
|
||||
- monitoring
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
@@ -20,3 +21,5 @@ services:
|
||||
networks:
|
||||
configurator:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
||||
0
app/middlewares/__init__.py
Normal file
0
app/middlewares/__init__.py
Normal file
14
app/middlewares/metrics.py
Normal file
14
app/middlewares/metrics.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import datetime
|
||||
import zoneinfo
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from app.utils.monitoring import monitoring
|
||||
|
||||
class MetricsMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
response: Response = await call_next(request)
|
||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
monitoring.send_metric(start, end, request.url.path, response.status_code, request.method)
|
||||
return response
|
||||
@@ -13,7 +13,7 @@ class RequestPostBody(pydantic.BaseModel):
|
||||
|
||||
class RequestPutBody(pydantic.BaseModel):
|
||||
id: str
|
||||
value: dict
|
||||
value: dict|list
|
||||
|
||||
|
||||
class RequestDeleteBody(pydantic.BaseModel):
|
||||
@@ -23,7 +23,7 @@ class RequestDeleteBody(pydantic.BaseModel):
|
||||
class Config(pydantic.BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
value: dict
|
||||
value: dict|list
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import bson
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
@@ -37,14 +38,14 @@ async def post(body: RequestPostBody):
|
||||
|
||||
@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(id=body.id, enabled=body.enabled, condition=body.condition)
|
||||
changed = await experiments.update(id=bson.ObjectId(body.id), 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: RequestDeleteBody):
|
||||
changed = await experiments.delete(id=body.id)
|
||||
changed = await experiments.delete(id=bson.ObjectId(body.id))
|
||||
if not changed:
|
||||
raise fastapi.HTTPException(404)
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class PlatformStaff(pydantic.BaseModel):
|
||||
|
||||
|
||||
class ResponseBody(pydantic.BaseModel):
|
||||
configs: dict[str, dict]
|
||||
configs: dict[str, dict|list]
|
||||
experiments: dict[str, ExperimentData]
|
||||
platform_staff: PlatformStaff
|
||||
|
||||
@@ -28,7 +28,7 @@ class ResponseBody(pydantic.BaseModel):
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/fetch')
|
||||
@router.get('/api/v1/fetch')
|
||||
async def execute(stage: str, project: str):
|
||||
confs, exps, staffs = await asyncio.gather(
|
||||
configs.get(project=project, stage=stage),
|
||||
|
||||
@@ -23,6 +23,7 @@ def create_indexes():
|
||||
database.get_collection('experiments').create_index([
|
||||
('stage', 1),
|
||||
('project', 1),
|
||||
('name', 1),
|
||||
], unique=True)
|
||||
database.get_collection('staff').create_index([
|
||||
('platform_id', 1),
|
||||
|
||||
@@ -12,7 +12,7 @@ class Config(pydantic.BaseModel):
|
||||
name: str
|
||||
project: str
|
||||
stage: str
|
||||
value: dict
|
||||
value: dict|list
|
||||
_id: bson.ObjectId|None = None
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ async def create(config: Config) -> str:
|
||||
return result.inserted_id
|
||||
|
||||
|
||||
async def update_data(id: bson.ObjectId, value: dict) -> bool:
|
||||
async def update_data(id: bson.ObjectId, value: dict|list) -> bool:
|
||||
result = await collection.update_one({'_id': id}, {'$set': {'value': value}})
|
||||
return result.modified_count != 0
|
||||
|
||||
@@ -35,4 +35,5 @@ 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))
|
||||
result[-1]._id = item['_id']
|
||||
return result
|
||||
|
||||
@@ -36,4 +36,5 @@ 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))
|
||||
result[-1]._id = item['_id']
|
||||
return result
|
||||
|
||||
24
app/utils/monitoring.py
Normal file
24
app/utils/monitoring.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import requests
|
||||
|
||||
|
||||
class Monitroing:
|
||||
def __init__(self):
|
||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
||||
|
||||
def send_metric(self, start: datetime.datetime, end: datetime.datetime, endpoint: str, status_code: int, method: str):
|
||||
def send():
|
||||
requests.post(f'http://monitoring:1237/api/v1/metrics/endpoint', json={
|
||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||
'service': 'configurator',
|
||||
'endpoint': endpoint,
|
||||
'status_code': status_code,
|
||||
'response_time': (end - start).microseconds // 1000,
|
||||
'method': method,
|
||||
})
|
||||
|
||||
self.executor.submit(send)
|
||||
|
||||
|
||||
monitoring = Monitroing()
|
||||
2
main.py
2
main.py
@@ -1,6 +1,7 @@
|
||||
import fastapi
|
||||
import uvicorn
|
||||
|
||||
from app.middlewares.metrics import MetricsMiddleware
|
||||
from app.routers import experiments
|
||||
from app.routers import configs
|
||||
from app.routers import staff
|
||||
@@ -10,6 +11,7 @@ from app.storage import mongo
|
||||
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
app.add_middleware(MetricsMiddleware)
|
||||
|
||||
app.include_router(experiments.router)
|
||||
app.include_router(configs.router)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
annotated-types==0.7.0
|
||||
anyio==4.6.2.post1
|
||||
APScheduler==3.10.4
|
||||
certifi==2025.6.15
|
||||
charset-normalizer==3.4.2
|
||||
click==8.1.7
|
||||
dnspython==2.7.0
|
||||
fastapi==0.115.4
|
||||
@@ -12,9 +14,11 @@ pydantic_core==2.23.4
|
||||
pymongo==4.9.2
|
||||
pytz==2024.2
|
||||
redis==5.2.0
|
||||
requests==2.32.4
|
||||
six==1.16.0
|
||||
sniffio==1.3.1
|
||||
starlette==0.41.2
|
||||
typing_extensions==4.12.2
|
||||
tzlocal==5.2
|
||||
urllib3==2.4.0
|
||||
uvicorn==0.32.0
|
||||
|
||||
Reference in New Issue
Block a user