Skip to content

Commit

Permalink
refactor fastapi code into modules
Browse files Browse the repository at this point in the history
- Extracted `database.models` and `api.router` to `engine` and `ltm`.
- Add experiments migrations
  • Loading branch information
rasca committed Nov 5, 2024
1 parent b2a23bf commit 0d8f326
Show file tree
Hide file tree
Showing 16 changed files with 778 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"Flou",
"websockets"
],
"cSpell.enabledLanguageIds": [
"markdown",
],
"python.testing.pytestArgs": [
],
"python.testing.cwd": "${workspaceFolder}/tests",
Expand Down
5 changes: 4 additions & 1 deletion flou/flou/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

from flou.conf import settings
from flou.database import get_db, get_session
from flou.database.models import Error
from flou.engine import get_engine
from flou.api.dependencies import get_redis
from flou.engine.models import Error
from flou.registry import registry

router = APIRouter()
from flou.engine.router import router as engine_router

router.include_router(engine_router)


@router.get("/example")
Expand Down
8 changes: 4 additions & 4 deletions flou/flou/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import sys
import traceback

import json
import jsonpatch
from redis import Redis
from sqlalchemy import update, func, ARRAY, String, cast, text, literal_column
from sqlalchemy import update, func, cast, text, literal_column
from sqlalchemy.dialects.postgresql import insert, JSONB

from . import get_session
from .models import Error, LTM
from .utils import json_dumps
from flou.conf import settings
from flou.engine.models import Error
from flou.ltm.models import LTM


redis = Redis(host=settings.redis.host, port=settings.redis.port, db=settings.redis.db)
Expand Down Expand Up @@ -185,7 +185,7 @@ def to_brackets(path):
}
update_query = text(
f"""
UPDATE ltms SET
UPDATE ltm_ltms SET
snapshots = snapshots || :snapshot,
{', '.join([f"{key} = CAST('{value}' AS JSONB) " for i, (key, value) in enumerate(sql_updates.items())])}
WHERE id=:ltm_id
Expand Down
28 changes: 28 additions & 0 deletions flou/flou/engine/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import uuid

from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.sql.functions import now
from sqlalchemy.types import String

from flou.database.models import Base
from flou.database.utils import JSONType


class Error(Base):
__tablename__ = "engine_error"

id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
ltm_id: Mapped[int] = mapped_column(ForeignKey("ltm_ltms.id"))
reason: Mapped[str] = mapped_column(String(30), nullable=False)
item: Mapped[dict] = mapped_column(JSONType(), nullable=False)
retries: Mapped[list] = mapped_column(JSONType(), default=list, nullable=False)
retrying: Mapped[bool] = mapped_column(default=True, nullable=False)
success: Mapped[bool] = mapped_column(default=False, nullable=False)

def __repr__(self):
return f"Error(ltm_id={self.ltm_id!r})"

def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}

14 changes: 14 additions & 0 deletions flou/flou/engine/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi import APIRouter


from flou.conf import settings

router = APIRouter()


@router.get("/example")
def read_example():
return {
"message": "Hello, World!",
"engine": settings.old_database.engine,
}
48 changes: 48 additions & 0 deletions flou/flou/engine/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from datetime import datetime
from typing import List

from pydantic import BaseModel, Field


class LTM(BaseModel):
id: int
name: str
fqn: str
snapshots_count: int
created_at: datetime
updated_at: datetime


class LTMCreation(BaseModel):
fqn: str = Field(..., description="Fully qualified name of the LTM class")
payload: dict = Field({}, description="Initial payload as a json object")
playground: bool = Field(
False, description="If true, the LTM will be created in the playground"
)


class Transition(BaseModel):
transition: str = Field(..., description="The label of the transition to perform")
namespace: str = Field(..., description="The namespace of the transition")
params: list[dict] | None = Field(
None, description="If a parameterized transition, it's parameters"
)
payload: dict | None = Field(
None, description="Optional payload for the transition"
)
wait_until_transition: str | None = Field(
None,
description="Await return until this transition `namespace:label` executes",
)


class SnapshotIndex(BaseModel):
index: int = Field(..., description="The index of the desired snapshot")


class RollbackIndex(BaseModel):
index: int = Field(..., description="The index of the desired rollback")


class ErrorList(BaseModel):
ids: List[str] = Field(..., description="The errors UUIDs to retry")
1 change: 1 addition & 0 deletions flou/flou/ltm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .ltm import LTM
Loading

0 comments on commit 0d8f326

Please sign in to comment.