2024-10-22 16:46:39 +04:00
|
|
|
import asyncio
|
2024-10-21 14:31:39 +04:00
|
|
|
|
2024-10-14 16:57:57 +04:00
|
|
|
import pandas as pd
|
2024-10-15 00:10:09 +04:00
|
|
|
from sqlalchemy import insert
|
2024-10-22 16:46:39 +04:00
|
|
|
from db.db_connection import async_session, engine
|
2024-10-15 00:10:09 +04:00
|
|
|
from db.models.base import Base
|
2024-10-15 16:34:09 +04:00
|
|
|
from db.models.ch_experimentdb_experiment_data_model import ChExperimentDBExperimentData
|
2024-10-15 00:10:09 +04:00
|
|
|
from db.models.experiment_data_model import ExperimentData
|
|
|
|
from db.models.experiment_parameters_model import ExperimentParameters
|
2024-10-15 16:34:09 +04:00
|
|
|
from db.models.load_parameters_model import LoadParameters
|
2024-10-15 00:10:09 +04:00
|
|
|
from db.models.recycling_parameters_model import RecyclingParameters
|
2024-10-14 16:57:57 +04:00
|
|
|
|
2024-10-15 00:10:09 +04:00
|
|
|
|
2024-10-15 16:34:09 +04:00
|
|
|
def add_ids_in_csv(file: str):
|
|
|
|
try:
|
|
|
|
df = pd.read_csv(file)
|
2024-10-21 14:31:39 +04:00
|
|
|
df.insert(0, 'id', pd.Series(range(1, len(df) + 1)))
|
2024-10-15 16:34:09 +04:00
|
|
|
df.to_csv(file, index=False)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'Exception!! {e}')
|
|
|
|
|
|
|
|
|
|
|
|
def print_headers_and_types(file: str):
|
|
|
|
df = pd.read_csv(file)
|
|
|
|
headers = df.columns.tolist()
|
|
|
|
print(headers)
|
|
|
|
|
|
|
|
for header in headers:
|
|
|
|
column_type = df[header].dtype
|
|
|
|
print(column_type)
|
|
|
|
|
2024-10-15 00:10:09 +04:00
|
|
|
|
|
|
|
async def create_all_tables():
|
2024-10-22 16:46:39 +04:00
|
|
|
print('create_all_tables')
|
2024-10-15 00:10:09 +04:00
|
|
|
async with engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
|
2024-10-15 16:34:09 +04:00
|
|
|
|
2024-10-15 00:10:09 +04:00
|
|
|
async def drop_all_tables():
|
2024-10-22 16:46:39 +04:00
|
|
|
print('drop_all_tables')
|
2024-10-15 00:10:09 +04:00
|
|
|
async with engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
|
|
|
|
|
|
|
|
async def load_data_to_db(file: str, model_class):
|
2024-10-22 16:46:39 +04:00
|
|
|
print('load_data_to_db')
|
2024-10-15 00:10:09 +04:00
|
|
|
async with async_session() as session:
|
|
|
|
df = pd.read_csv(file).dropna()
|
|
|
|
# Преобразование данных из DataFrame в формат, подходящий для SQLAlchemy
|
|
|
|
data_records = df.to_dict(orient='records')
|
|
|
|
|
|
|
|
# Пакетная вставка всех записей
|
|
|
|
stmt = insert(model_class).values(data_records)
|
|
|
|
await session.execute(stmt)
|
|
|
|
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
|
2024-10-21 14:31:39 +04:00
|
|
|
async def insert_batch(session, model_class, batch_data):
|
|
|
|
stmt = insert(model_class).values(batch_data)
|
|
|
|
await session.execute(stmt)
|
|
|
|
await session.commit()
|
|
|
|
|
2024-10-15 16:34:09 +04:00
|
|
|
async def test(file: str, model_class):
|
2024-10-22 16:46:39 +04:00
|
|
|
print('test')
|
|
|
|
df = pd.read_csv(file)
|
|
|
|
data_records = df.to_dict(orient='records')
|
|
|
|
|
|
|
|
# async with async_session() as session:
|
|
|
|
# batch_size = 3000
|
|
|
|
# tasks = []
|
|
|
|
#
|
|
|
|
# for i in range(0, len(data_records), batch_size):
|
|
|
|
# batch_data = data_records[i:i + batch_size]
|
|
|
|
# tasks.append(insert_batch(session, model_class, batch_data))
|
|
|
|
#
|
|
|
|
# await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
# две минуты добавляет
|
2024-10-15 16:34:09 +04:00
|
|
|
async with async_session() as session:
|
2024-10-21 14:31:39 +04:00
|
|
|
batch_size = 3000
|
2024-10-22 16:46:39 +04:00
|
|
|
print(batch_size)
|
2024-10-21 14:31:39 +04:00
|
|
|
for i in range(0, len(data_records), batch_size):
|
|
|
|
batch_data = data_records[i:i + batch_size]
|
2024-10-22 16:46:39 +04:00
|
|
|
stmt = insert(model_class).values(batch_data)
|
|
|
|
await session.execute(stmt)
|
|
|
|
await session.commit()
|
2024-10-15 16:34:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
async def csv_to_db():
|
|
|
|
await drop_all_tables()
|
|
|
|
await create_all_tables()
|
2024-10-17 13:51:33 +04:00
|
|
|
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)
|
2024-10-15 16:34:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# asyncio.run(csv_to_db())
|
2024-10-21 14:31:39 +04:00
|
|
|
|
|
|
|
|
|
|
|
|