PIbd-42_SSPR/insert_to_db.py

75 lines
2.8 KiB
Python

from pathlib import Path
import yaml
from settings import settings
from clickhouse_tools import ClickHouseClient
import utils
from postgres_tools import PostgresClient
# Загрузка конфигурации из файла config.yaml
with open('config.yaml', 'r') as config_file:
config = yaml.safe_load(config_file)
MAIN_PATH = config['paths']['main']
main_path = Path(MAIN_PATH)
def add_data_to_db(experiment_parameters, load_parameters, recycling_parameters):
geometry_path = (f"{experiment_parameters['outer_blades_count']}_{experiment_parameters['outer_blades_length']}_"
f"{experiment_parameters['outer_blades_angle']}_{experiment_parameters['middle_blades_count']}")
experiments_path = main_path / geometry_path / 'experiments'
load_path = experiments_path / str(experiment_parameters['load'])
load_parameters_path = load_path / 'parameters'
recycling_path = load_path / str(experiment_parameters['recycling'])
load_parameters_file = load_parameters_path / f"load_{experiment_parameters['load']}_parameters.yaml"
plot_csv = recycling_path / 'plot.csv'
table_csv = recycling_path / 'data_table.csv'
file_id = utils.calculate_hash(experiment_parameters)
clickhouse_client = ClickHouseClient("localhost", 8123, settings.DATABASE, settings.CLICKHOUSE_USER, settings.CLICKHOUSE_PASSWORD)
# Инициализация базы данных
db = PostgresClient(
dbname=settings.DATABASE,
user=settings.POSTGRES_USER,
password=settings.POSTGRES_PASSWORD,
host="localhost",
port="5432"
)
try:
if load_parameters_file.exists():
with open(load_parameters_file, 'r') as fuel_dict_file:
fuel_parameters = yaml.safe_load(fuel_dict_file)
load_parameters['primary_air_consumption'] = fuel_parameters['primary_air_consumption']
load_parameters['secondary_air_consumption'] = fuel_parameters['secondary_air_consumption']
load_parameters['gas_inlet_consumption'] = fuel_parameters['gas_inlet_consumption']
# Вставка данных в load_parameters и получение id
load_id = db.insert_load_parameters(load_parameters)
# Вставка данных в recycling_parameters и получение id
recycling_id = db.insert_recycling_parameters(recycling_parameters, load_id)
# Вставка данных в experiment_parameters
db.insert_experiment_parameters(experiment_parameters, load_id, recycling_id, file_id)
# Сохранение изменений
db.connection.commit()
db.save_csv_to_postgres(plot_csv, file_id)
clickhouse_client.save_csv_to_clickhouse(table_csv, file_id)
print('Загружено: ', experiment_parameters)
finally:
db.close()