скрипт загрузки данных в бд проверен в докере, успешно работает

This commit is contained in:
Inohara 2024-10-17 13:51:33 +04:00
parent 7028b67078
commit ef18fc2f4b
9 changed files with 35 additions and 44 deletions

11
.env
View File

@ -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

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.venv
.idea
__pycache__

View File

@ -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"]

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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)}")

Binary file not shown.

View File

@ -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")