Skip to content

Commit

Permalink
dev: add db using by ORM draft
Browse files Browse the repository at this point in the history
  • Loading branch information
kudep committed Jan 16, 2024
1 parent d0441c5 commit ceb0ef3
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 12 deletions.
4 changes: 4 additions & 0 deletions df_designer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import uvicorn

from df_designer.settings import app
from sqlalchemy import create_engine
from df_designer.db_connection import Base

cli = typer.Typer()

Expand All @@ -25,6 +27,8 @@ def run_app(
):
"""Run the application."""
app.dir_logs = dir_logs
engine = create_engine(f"sqlite:///{app.database_file}")
Base.metadata.create_all(engine)
config = uvicorn.Config(
app=app.conf_app,
host=ip_address,
Expand Down
21 changes: 21 additions & 0 deletions df_designer/db_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

from df_designer.settings import app


class Base(DeclarativeBase):
pass


class Logs(Base):
__tablename__ = "logs"

id: Mapped[int] = mapped_column(primary_key=True)
datetime: Mapped[str]
path: Mapped[str]
status: Mapped[str]


async_engine = create_async_engine(f"sqlite+aiosqlite:///{app.database_file}")
async_session = async_sessionmaker(async_engine, expire_on_commit=False)
52 changes: 41 additions & 11 deletions df_designer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
import os
from datetime import datetime
from pathlib import Path
import time

import aiofiles
import dff
from fastapi import Request, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from sqlalchemy import insert, select, update
from websockets import ConnectionClosedOK

from df_designer.db_connection import async_session, Logs
from df_designer.logic import get_data, save_data
from df_designer.settings import app

Expand Down Expand Up @@ -145,21 +148,26 @@ async def build_post() -> dict[str, str]:
@app.get("/log")
async def logs():
"""get logs"""
if Path(app.dir_logs).exists():
files = os.listdir(app.dir_logs)
else:
files = []
return {"status": "ok", "files": files}
async with async_session() as session:
stmt = select(Logs)
result = await session.execute(stmt)
logs_list = result.scalars().all()

return {"status": "ok", "logs": logs_list}


@app.get("/log/{log_file}")
async def log_file(log_file: str):
@app.get("/log/{log_id}")
async def log_file(log_id: str):
"""get log file"""
log = Path(app.dir_logs, log_file)
if log.exists():
async with aiofiles.open(log, "r", encoding="utf-8") as file:
async with async_session() as session:
stmt = select(Logs).where(Logs.id == log_id)
result = await session.execute(stmt)
log = result.scalar()
log_file = Path(log.path)
if log_file.exists():
async with aiofiles.open(log_file, "r", encoding="utf-8") as file:
data = await file.read()
return {"status": "ok", "data": data}
return {"status": "ok", "meta": log, "data": data}
else:
return JSONResponse(
status_code=404, content={"status": "error", "data": "File is not found."}
Expand All @@ -177,8 +185,22 @@ async def websocket(websocket: WebSocket):
)
file_log_name = datetime.now().strftime("%Y_%m_%d_%H_%M_%s") + ".txt"
file_for_log = Path(app.dir_logs, file_log_name)

async with async_session() as session:
stmt = (
insert(Logs)
.values(
datetime=time.time(),
path=str(Path(file_for_log).absolute()),
status="start",
)
.returning()
)
id_record = await session.execute(stmt)
await session.commit()
if not Path(app.dir_logs).exists():
Path(app.dir_logs).mkdir()

async with aiofiles.open(file_for_log, "a") as file:
while True:
line = await proc.stdout.readline()
Expand All @@ -190,6 +212,14 @@ async def websocket(websocket: WebSocket):
await websocket.send_text(data)
except ConnectionClosedOK:
proc.terminate()
async with async_session() as session:
stmt = (
update(Logs)
.values(status="stop")
.where(Logs.id == id_record.inserted_primary_key[0])
)
await session.execute(stmt)
await session.commit()
break
else:
break
1 change: 1 addition & 0 deletions df_designer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Application(FastAPI):
conf_log_level = "info"
conf_reload = True
dir_logs = "logs"
database_file = "database.sqlite"


app = Application()
Loading

0 comments on commit ceb0ef3

Please sign in to comment.