feat: troppa roba davvero
This commit is contained in:
@@ -2,10 +2,11 @@ from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from core.mongodb import get_database
|
||||
from models.auth import AuthUserDocument
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
from app.core.mongodb import get_database
|
||||
from app.models.auth import AuthUserDocument
|
||||
|
||||
USERS_COLLECTION_NAME = "auth_users"
|
||||
REVOKED_COLLECTION_NAME = "revoked_tokens"
|
||||
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from core.mongodb import get_database
|
||||
from models.example import AddonExampleCreateInput, AddonExampleDocument
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
COLLECTION_NAME = "addon_examples"
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
async def collection() -> AsyncIOMotorCollection[dict[str, Any]]:
|
||||
database = await get_database()
|
||||
return database[COLLECTION_NAME]
|
||||
|
||||
|
||||
def deserialize(raw: Mapping[str, Any] | None) -> AddonExampleDocument | None:
|
||||
if raw is None:
|
||||
return None
|
||||
document = dict(raw)
|
||||
document.pop("_id", None)
|
||||
return AddonExampleDocument.model_validate(document)
|
||||
|
||||
|
||||
async def ensure_example_indexes() -> None:
|
||||
examples = await collection()
|
||||
await examples.create_index("id", unique=True, name="uq_addon_example_id")
|
||||
|
||||
|
||||
async def list_custom_examples() -> list[AddonExampleDocument]:
|
||||
examples = await collection()
|
||||
cursor = examples.find({}).sort("name", 1)
|
||||
|
||||
result: list[AddonExampleDocument] = []
|
||||
async for raw in cursor:
|
||||
example = deserialize(raw)
|
||||
if example is not None:
|
||||
result.append(example)
|
||||
return result
|
||||
|
||||
|
||||
async def create_custom_example(
|
||||
payload: AddonExampleCreateInput,
|
||||
) -> AddonExampleDocument:
|
||||
now = utcnow()
|
||||
example = AddonExampleDocument(
|
||||
id=payload.id,
|
||||
name=payload.name,
|
||||
description=payload.description,
|
||||
category=payload.category,
|
||||
code=payload.code,
|
||||
source_url=payload.source_url,
|
||||
built_in=False,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
examples = await collection()
|
||||
await examples.insert_one(example.model_dump(exclude_none=True))
|
||||
return example
|
||||
|
||||
|
||||
async def delete_custom_example(example_id: str) -> bool:
|
||||
examples = await collection()
|
||||
result = await examples.delete_one({"id": example_id})
|
||||
return result.deleted_count > 0
|
||||
@@ -0,0 +1,80 @@
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.mongodb import get_database
|
||||
from app.models.health import HealthSnapshot
|
||||
|
||||
COLLECTION_NAME = "health_history"
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
async def collection() -> AsyncIOMotorCollection[dict[str, Any]]:
|
||||
database = await get_database()
|
||||
return database[COLLECTION_NAME]
|
||||
|
||||
|
||||
async def ensure_health_indexes() -> None:
|
||||
samples = await collection()
|
||||
await samples.create_index(
|
||||
[("proxy_port", 1), ("timestamp", -1)],
|
||||
name="ix_health_proxy_port_timestamp",
|
||||
)
|
||||
await samples.create_index(
|
||||
"expires_at",
|
||||
expireAfterSeconds=0,
|
||||
name="ttl_health_history",
|
||||
)
|
||||
|
||||
|
||||
def deserialize_health_sample(raw: dict[str, Any] | None) -> HealthSnapshot | None:
|
||||
if raw is None:
|
||||
return None
|
||||
document = dict(raw)
|
||||
document.pop("_id", None)
|
||||
document.pop("expires_at", None)
|
||||
return HealthSnapshot.model_validate(document)
|
||||
|
||||
|
||||
async def record_health_sample(snapshot: HealthSnapshot) -> HealthSnapshot:
|
||||
samples = await collection()
|
||||
document = snapshot.model_dump(mode="python")
|
||||
document["expires_at"] = document["timestamp"] + timedelta(
|
||||
seconds=settings.HEALTH_HISTORY_TTL_SECONDS,
|
||||
)
|
||||
await samples.insert_one(document)
|
||||
return snapshot
|
||||
|
||||
|
||||
async def get_latest_health_sample(proxy_port: int) -> HealthSnapshot | None:
|
||||
samples = await collection()
|
||||
raw = await samples.find_one(
|
||||
{"proxy_port": proxy_port},
|
||||
sort=[("timestamp", -1)],
|
||||
)
|
||||
return deserialize_health_sample(raw)
|
||||
|
||||
|
||||
async def list_health_history(
|
||||
proxy_port: int,
|
||||
limit: int = 40,
|
||||
) -> list[HealthSnapshot]:
|
||||
samples = await collection()
|
||||
cursor = samples.find({"proxy_port": proxy_port}).sort("timestamp", -1).limit(limit)
|
||||
history: list[HealthSnapshot] = []
|
||||
async for raw in cursor:
|
||||
snapshot = deserialize_health_sample(raw)
|
||||
if snapshot is not None:
|
||||
history.append(snapshot)
|
||||
history.reverse()
|
||||
return history
|
||||
|
||||
|
||||
async def delete_health_history(proxy_port: int) -> None:
|
||||
samples = await collection()
|
||||
await samples.delete_many({"proxy_port": proxy_port})
|
||||
@@ -2,11 +2,12 @@ from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from core.mongodb import get_database
|
||||
from models.proxy import ProxyCreateInput, ProxyDocument, ProxyUpdateInput
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
from pymongo import ReturnDocument
|
||||
|
||||
from app.core.mongodb import get_database
|
||||
from app.models.proxy import ProxyCreateInput, ProxyDocument, ProxyUpdateInput
|
||||
|
||||
COLLECTION_NAME = "proxies"
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
from app.core.mongodb import get_database
|
||||
from app.models.template import ProxyTemplateCreateInput, ProxyTemplateDocument
|
||||
|
||||
COLLECTION_NAME = "addon_templates"
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
async def collection() -> AsyncIOMotorCollection[dict[str, Any]]:
|
||||
database = await get_database()
|
||||
return database[COLLECTION_NAME]
|
||||
|
||||
|
||||
def deserialize(raw: Mapping[str, Any] | None) -> ProxyTemplateDocument | None:
|
||||
if raw is None:
|
||||
return None
|
||||
document = dict(raw)
|
||||
document.pop("_id", None)
|
||||
return ProxyTemplateDocument.model_validate(document)
|
||||
|
||||
|
||||
async def ensure_template_indexes() -> None:
|
||||
templates = await collection()
|
||||
await templates.create_index("id", unique=True, name="uq_addon_template_id")
|
||||
|
||||
|
||||
async def list_custom_templates() -> list[ProxyTemplateDocument]:
|
||||
templates = await collection()
|
||||
cursor = templates.find({}).sort("name", 1)
|
||||
|
||||
result: list[ProxyTemplateDocument] = []
|
||||
async for raw in cursor:
|
||||
template = deserialize(raw)
|
||||
if template is not None:
|
||||
result.append(template)
|
||||
return result
|
||||
|
||||
|
||||
async def create_custom_template(
|
||||
payload: ProxyTemplateCreateInput,
|
||||
) -> ProxyTemplateDocument:
|
||||
now = utcnow()
|
||||
template = ProxyTemplateDocument(
|
||||
id=payload.id,
|
||||
name=payload.name,
|
||||
description=payload.description,
|
||||
category=payload.category,
|
||||
protocol=payload.protocol,
|
||||
family=payload.family,
|
||||
tags=payload.tags,
|
||||
action=payload.action,
|
||||
summary=payload.summary,
|
||||
behavior=payload.behavior,
|
||||
code=payload.code,
|
||||
source_url=payload.source_url,
|
||||
built_in=False,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
templates = await collection()
|
||||
await templates.insert_one(template.model_dump(exclude_none=True))
|
||||
return template
|
||||
|
||||
|
||||
async def delete_custom_template(template_id: str) -> bool:
|
||||
templates = await collection()
|
||||
result = await templates.delete_one({"id": template_id})
|
||||
return result.deleted_count > 0
|
||||
Reference in New Issue
Block a user