Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
9
Indexable
import pandas as pd
import mysql.connector
import json
from base64 import b64decode as base_b64decode
from base64 import b64encode as base_b64encode
import logging
import datetime
import os

releaseId = '275.2'
releaseType = 'testing'
replicationTarget = 'TESTING'
catalogId = ''
opId = 'HOB'
buId = 'DEFAULT'
replicationJobId = 'REP_990_234'
json_file_path = "/app/scripts/PPM_Release_Management/Product_Catalog_ETL/config/ppm_reply.json"
sql_log_file = f"/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.sql"
logging.basicConfig(filename=f'/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.log',
                    level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
query_count = 0



def write_sql(query_info):
    with open(sql_log_file, "w") as log_file:
        log_file.write(query_info)
        log_file.write('\n')


try:
    with open(json_file_path) as json_file:
        json_data = json.load(json_file)
    # For PPM_PC
    encrypt = json_data.get('PPM_PC', {}).get('ENCRYPT')
    host = json_data.get('PPM_PC', {}).get('DB_HOST')
    port = json_data.get('PPM_PC', {}).get('DB_PORT')
    user = json_data.get('PPM_PC', {}).get('DB_USER')
    schema = json_data.get(replicationTarget, {}).get('DB_SCHEMA')
    logging.info("DATABASE - HOST FOR PPM_PC - " + host)
    logging.info("DATABASE - PORT FOR PPM_PC - " + port)
    logging.info("DATABASE - USER - PPM_PC" + user)

    if encrypt == 'Y':
        password = base_b64decode(json_data.get('PPM_PC', {}).get('DB_PASSWORD')).decode('utf-8')
    else:
        password = json_data.get('PPM_PC', {}).get('DB_PASSWORD')

    cnx = mysql.connector.connect(user=user, password=password, host=host, port=port)
    cursor = cnx.cursor()
    logging.info(f"connected to database server PPM_PC: {host}:{port}")

    primary_query = f"SELECT * FROM {schema}.etl_ppm_replication_master"
    cursor.execute(primary_query)
    logging.info(f"executed primary query - {primary_query}")
    query_info = f"-- ++++++++++++++++++++|PRIMARY_QUERY\n"
    query_info += f"-- #Query:\n{primary_query};\n"
    write_sql(query_info)

    rows = cursor.fetchall()
    columns = [desc[0] for desc in cursor.description]
    df = pd.DataFrame(rows, columns=columns)

    # replicationTarget
    encrypt_tar = json_data.get(replicationTarget, {}).get('ENCRYPT')
    host_tar = json_data.get(replicationTarget, {}).get('DB_HOST')
    port_tar = json_data.get(replicationTarget, {}).get('DB_PORT')
    user_tar = json_data.get(replicationTarget, {}).get('DB_USER')
    logging.info("DATABASE - HOST FOR REPLICATION_TARGET - " + host_tar)
    logging.info("DATABASE - PORT FOR REPLICATION_TARGET - " + port_tar)
    logging.info("DATABASE - USER - REPLICATION_TARGET -" + user_tar)