diff --git a/.env b/.env index 7990399..0abe858 100644 --- a/.env +++ b/.env @@ -1,11 +1,10 @@ -DB_USER=postgres -DB_PASSWORD=password -DB_HOST=localhost -DB_PORT=5432 -; DB_PORT=20111 -DB_NAME=test DATABASE=SuperService POSTGRES_USER=UserSuperService POSTGRES_PASSWORD=NotWarningWord1 CLICKHOUSE_USER=UserMyHouse CLICKHOUSE_PASSWORD=NotWarningWord2 +DB_USER=postgres +DB_PASSWORD=password +DB_HOST=localhost +DB_PORT=5432 +DB_NAME=test \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74a9069 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv +.idea +__pycache__ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2161134..df684a7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,10 @@ FROM python:3.9-slim WORKDIR /app COPY requirements.txt requirements.txt +RUN pip install --upgrade pip RUN pip install -r requirements.txt + COPY . . CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] diff --git a/db/config.py b/db/config.py deleted file mode 100644 index 2612e8a..0000000 --- a/db/config.py +++ /dev/null @@ -1,29 +0,0 @@ -from pydantic_settings import BaseSettings, SettingsConfigDict - - -class Settings(BaseSettings): - DB_USER: str - DB_PASSWORD: str - DB_HOST: str - DB_PORT: int - DB_NAME: str - DATABASE: str - POSTGRES_USER: str - POSTGRES_PASSWORD: str - CLICKHOUSE_USER: str - CLICKHOUSE_PASSWORD: str - - @property - def db_url_asyncpg(self): - # 'postgresql+asyncpg://username:password@localhost:5432/database_name' - return f'postgresql+asyncpg://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}' - - @property - def db_url_asyncpg_docker(self): - # 'postgresql+asyncpg://username:password@localhost:5432/database_name' - return f'postgresql+asyncpg://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@db:{self.DB_PORT}/{self.DATABASE}' - - model_config = SettingsConfigDict(env_file='../.env') - - -settings = Settings() \ No newline at end of file diff --git a/db/csv_to_db.py b/db/csv_to_db.py index dde3e95..f613f77 100644 --- a/db/csv_to_db.py +++ b/db/csv_to_db.py @@ -1,7 +1,7 @@ import pandas as pd from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker from sqlalchemy import insert -from db.config import settings +from settings import settings from db.models.base import Base from db.models.ch_experimentdb_experiment_data_model import ChExperimentDBExperimentData from db.models.experiment_data_model import ExperimentData @@ -72,11 +72,11 @@ async def test(file: str, model_class): async def csv_to_db(): await drop_all_tables() await create_all_tables() - await load_data_to_db('./files/ch_experimentdb_experiment_data.csv', ChExperimentDBExperimentData) - await test('./files/experiment_data.csv', ExperimentData) - await load_data_to_db('./files/load_parameters.csv', LoadParameters) - await load_data_to_db('./files/recycling_parameters.csv', RecyclingParameters) - await load_data_to_db('./files/experiment_parameters.csv', ExperimentParameters) + await load_data_to_db('./db/files/ch_experimentdb_experiment_data.csv', ChExperimentDBExperimentData) + await test('./db/files/experiment_data.csv', ExperimentData) + await load_data_to_db('./db/files/load_parameters.csv', LoadParameters) + await load_data_to_db('./db/files/recycling_parameters.csv', RecyclingParameters) + await load_data_to_db('./db/files/experiment_parameters.csv', ExperimentParameters) diff --git a/db/db_connection.py b/db/db_connection.py index 6cd6d8f..f1db0f0 100644 --- a/db/db_connection.py +++ b/db/db_connection.py @@ -1,5 +1,5 @@ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker -from config import settings +from settings import settings engine = create_async_engine(url=settings.db_url_asyncpg, echo=True) diff --git a/main.py b/main.py index f8f9062..ac84965 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import asyncio from fastapi import FastAPI, HTTPException from pydantic import BaseModel from new_experiment_planner import run_experiment # Импортируем функцию из твоего скрипта @@ -32,10 +33,10 @@ def run_experiment_api(params: ExperimentParameters): raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") -@app.post('/init_db_data') +@app.get('/init_db_data') def init_db_data(): try: - csv_to_db() + asyncio.run(csv_to_db()) return {"status": "success", "message": "Experiment started successfully."} except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") diff --git a/requirements.txt b/requirements.txt index 104c6d9..bf79f07 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/settings.py b/settings.py index 9022623..85f38b6 100644 --- a/settings.py +++ b/settings.py @@ -1,12 +1,27 @@ from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): + DB_USER: str + DB_PASSWORD: str + DB_HOST: str + DB_PORT: int + DB_NAME: str DATABASE: str POSTGRES_USER: str POSTGRES_PASSWORD: str CLICKHOUSE_USER: str CLICKHOUSE_PASSWORD: str + @property + def db_url_asyncpg(self): + # 'postgresql+asyncpg://username:password@localhost:5432/database_name' + return f'postgresql+asyncpg://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}' + + @property + def db_url_asyncpg_docker(self): + # 'postgresql+asyncpg://username:password@localhost:5432/database_name' + return f'postgresql+asyncpg://{self.POSTGRES_USER}:{self.POSTGRES_PASSWORD}@db:{self.DB_PORT}/{self.DATABASE}' + model_config = SettingsConfigDict(env_file=".env")