Refactor code structure for improved readability and maintainability

This commit is contained in:
Artem Kashaev
2026-05-28 11:20:10 +05:00
parent d5a889ed6d
commit e48b1fc0e9
21 changed files with 171 additions and 1068 deletions
+11
View File
@@ -0,0 +1,11 @@
.venv/
.ruff_cache/
.pytest_cache/
.ty/
**/.venv/
**/.ruff_cache/
**/.pytest_cache/
**/.ty/
__pycache__/
**/__pycache__/
*.pyc
+1
View File
@@ -4,3 +4,4 @@ __pycache__/
**/__pycache__/
*.pyc
.pytest_cache/
.ty/
+7 -4
View File
@@ -2,10 +2,13 @@ FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev
COPY app ./app
COPY alembic.ini ./alembic.ini
COPY alembic ./alembic
COPY bff/pyproject.toml ./bff/pyproject.toml
COPY logic/pyproject.toml ./logic/pyproject.toml
RUN uv sync --frozen --no-dev --all-packages
COPY bff/app ./bff/app
COPY bff/alembic.ini ./bff/alembic.ini
COPY bff/alembic ./bff/alembic
WORKDIR /app/bff
EXPOSE 8000
CMD ["/app/.venv/bin/uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
+1 -2
View File
@@ -1,10 +1,9 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from alembic import context
from app.core import settings
from app.models import Base
from sqlalchemy import engine_from_config, pool
config = context.config
config.set_main_option("sqlalchemy.url", settings.database_url)
@@ -8,9 +8,8 @@ Create Date: 2026-05-28 10:00:00.000000
from collections.abc import Sequence
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "0001_initial"
down_revision: str | None = None
+9 -2
View File
@@ -7,20 +7,27 @@ from sqlalchemy.orm import Session, sessionmaker
from app.core import settings
engine = create_engine(settings.database_url, pool_pre_ping=True)
engine = create_engine(
settings.database_url,
pool_pre_ping=True,
connect_args={"connect_timeout": 3},
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def create_schema(metadata: MetaData, attempts: int = 30, delay_seconds: int = 2) -> None:
last_error: OperationalError | None = None
for _ in range(attempts):
for attempt in range(1, attempts + 1):
try:
print(f"Connecting to database, attempt {attempt}/{attempts}", flush=True)
with engine.begin() as connection:
connection.execute(text("SELECT 1"))
metadata.create_all(bind=connection)
print("Database schema is ready", flush=True)
return
except OperationalError as exc:
last_error = exc
print(f"Database is not ready: {exc}", flush=True)
sleep(delay_seconds)
if last_error:
raise last_error
+2 -13
View File
@@ -16,16 +16,5 @@ dependencies = [
"sqlalchemy>=2.0.41",
]
[dependency-groups]
dev = [
"pytest>=8.3.5",
"ruff>=0.11.11",
"ty>=0.0.1a6",
]
[tool.ruff]
line-length = 100
target-version = "py314"
[tool.ruff.lint]
select = ["E", "F", "I", "UP", "B"]
[tool.uv]
package = false
+1
View File
@@ -4,3 +4,4 @@ __pycache__/
**/__pycache__/
*.pyc
.pytest_cache/
.ty/
+7 -4
View File
@@ -2,10 +2,13 @@ FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev
COPY app ./app
COPY alembic.ini ./alembic.ini
COPY alembic ./alembic
COPY bff/pyproject.toml ./bff/pyproject.toml
COPY logic/pyproject.toml ./logic/pyproject.toml
RUN uv sync --frozen --no-dev --all-packages
COPY logic/app ./logic/app
COPY logic/alembic.ini ./logic/alembic.ini
COPY logic/alembic ./logic/alembic
WORKDIR /app/logic
EXPOSE 8000
CMD ["/app/.venv/bin/uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
+1 -2
View File
@@ -1,10 +1,9 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from alembic import context
from app.core import settings
from app.models import Base
from sqlalchemy import engine_from_config, pool
config = context.config
config.set_main_option("sqlalchemy.url", settings.database_url)
@@ -8,9 +8,8 @@ Create Date: 2026-05-28 10:00:00.000000
from collections.abc import Sequence
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "0001_initial"
down_revision: str | None = None
+9 -2
View File
@@ -7,20 +7,27 @@ from sqlalchemy.orm import Session, sessionmaker
from app.core import settings
engine = create_engine(settings.database_url, pool_pre_ping=True)
engine = create_engine(
settings.database_url,
pool_pre_ping=True,
connect_args={"connect_timeout": 3},
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def create_schema(metadata: MetaData, attempts: int = 30, delay_seconds: int = 2) -> None:
last_error: OperationalError | None = None
for _ in range(attempts):
for attempt in range(1, attempts + 1):
try:
print(f"Connecting to database, attempt {attempt}/{attempts}", flush=True)
with engine.begin() as connection:
connection.execute(text("SELECT 1"))
metadata.create_all(bind=connection)
print("Database schema is ready", flush=True)
return
except OperationalError as exc:
last_error = exc
print(f"Database is not ready: {exc}", flush=True)
sleep(delay_seconds)
if last_error:
raise last_error
+2 -13
View File
@@ -10,16 +10,5 @@ dependencies = [
"sqlalchemy>=2.0.41",
]
[dependency-groups]
dev = [
"pytest>=8.3.5",
"ruff>=0.11.11",
"ty>=0.0.1a6",
]
[tool.ruff]
line-length = 100
target-version = "py314"
[tool.ruff.lint]
select = ["E", "F", "I", "UP", "B"]
[tool.uv]
package = false
-1001
View File
File diff suppressed because it is too large Load Diff
+25
View File
@@ -0,0 +1,25 @@
[project]
name = "train-watcher-services"
version = "0.1.0"
requires-python = ">=3.14"
dependencies = []
[tool.uv]
package = false
[tool.uv.workspace]
members = ["bff", "logic"]
[dependency-groups]
dev = [
"pytest>=8.3.5",
"ruff>=0.11.11",
"ty>=0.0.1a6",
]
[tool.ruff]
line-length = 100
target-version = "py314"
[tool.ruff.lint]
select = ["E", "F", "I", "UP", "B"]
+43 -8
View File
@@ -2,6 +2,13 @@ version = 1
revision = 3
requires-python = ">=3.14"
[manifest]
members = [
"train-watcher-bff",
"train-watcher-logic",
"train-watcher-services",
]
[[package]]
name = "alembic"
version = "1.18.4"
@@ -949,7 +956,7 @@ wheels = [
[[package]]
name = "train-watcher-bff"
version = "0.1.0"
source = { virtual = "." }
source = { virtual = "bff" }
dependencies = [
{ name = "alembic" },
{ name = "boto3" },
@@ -964,13 +971,6 @@ dependencies = [
{ name = "sqlalchemy" },
]
[package.dev-dependencies]
dev = [
{ name = "pytest" },
{ name = "ruff" },
{ name = "ty" },
]
[package.metadata]
requires-dist = [
{ name = "alembic", specifier = ">=1.16.0" },
@@ -986,6 +986,41 @@ requires-dist = [
{ name = "sqlalchemy", specifier = ">=2.0.41" },
]
[[package]]
name = "train-watcher-logic"
version = "0.1.0"
source = { virtual = "logic" }
dependencies = [
{ name = "alembic" },
{ name = "fastapi", extra = ["standard"] },
{ name = "psycopg", extra = ["binary"] },
{ name = "pydantic-settings" },
{ name = "sqlalchemy" },
]
[package.metadata]
requires-dist = [
{ name = "alembic", specifier = ">=1.16.0" },
{ name = "fastapi", extras = ["standard"], specifier = ">=0.115.12" },
{ name = "psycopg", extras = ["binary"], specifier = ">=3.2.9" },
{ name = "pydantic-settings", specifier = ">=2.9.1" },
{ name = "sqlalchemy", specifier = ">=2.0.41" },
]
[[package]]
name = "train-watcher-services"
version = "0.1.0"
source = { virtual = "." }
[package.dev-dependencies]
dev = [
{ name = "pytest" },
{ name = "ruff" },
{ name = "ty" },
]
[package.metadata]
[package.metadata.requires-dev]
dev = [
{ name = "pytest", specifier = ">=8.3.5" },