Untitled
unknown
plain_text
2 years ago
4.9 kB
8
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 re
releaseId = '275.2'
releaseType = ''
replicationTarget = 'TESTING'
catalogId = ''
opId = 'HOB'
buId = 'DEFAULT'
replicationJobId = ''
json_file_path = "/app/scripts/PPM_Release_Management/Product_Catalog_ETL/config/ppm_reply.json"
try:
with open(json_file_path) as json_file:
json_data = json.load(json_file)
encrypt = json_data.get(replicationTarget, {}).get('ENCRYPT')
host = json_data.get(replicationTarget, {}).get('DB_HOST')
port = json_data.get(replicationTarget, {}).get('DB_PORT')
user = json_data.get(replicationTarget, {}).get('DB_USER')
if encrypt == 'Y':
password = base_b64decode(json_data.get(releaseType, {}).get('DB_PASSWORD')).decode('utf-8')
else:
password = json_data.get(releaseType, {}).get('DB_PASSWORD')
# Establish connection for primary query
primary_cnx = mysql.connector.connect(user=user, password=password, host=host, port=port, database='PPM_PC')
primary_cursor = primary_cnx.cursor()
primary_query = "SELECT * FROM tibtcare_ppm_st2.etl_ppm_replication_master"
primary_cursor.execute(primary_query)
rows = primary_cursor.fetchall()
columns = [desc[0] for desc in primary_cursor.description]
df = pd.DataFrame(rows, columns=columns)
filtered_df = df[df['eprm_catalog'].isin(['PC']) & (df['eprm_enabled_flg'] == 'Y') & df['eprm_table_type'].isin(['MASTER', 'AUDIT'])]
# Establish connection for secondary query
secondary_host = json_data.get(replicationTarget, {}).get('DB_HOST')
secondary_port = json_data.get(replicationTarget, {}).get('DB_PORT')
secondary_user = json_data.get(replicationTarget, {}).get('DB_USER')
if encrypt == 'Y':
secondary_password = base_b64decode(json_data.get(replicationTarget, {}).get('DB_PASSWORD')).decode('utf-8')
else:
secondary_password = json_data.get(replicationTarget, {}).get('DB_PASSWORD')
secondary_cnx = mysql.connector.connect(user=secondary_user, password=secondary_password, host=secondary_host, port=secondary_port, database=replicationTarget)
secondary_cursor = secondary_cnx.cursor()
for _, row in filtered_df.iterrows():
eprm_table_name = row['eprm_table_name']
eprm_join_cols_entity = row['eprm_join_cols_entity']
eprm_join_cols_reim = row['eprm_join_cols_reim']
eprm_table_alias = row['eprm_table_alias']
eprm_table_type = row['eprm_table_type']
clause_removed_reim_join = eprm_join_cols_reim.replace(" AND", "").replace("=", "")
remove_string = [eprm_table_alias + "." + v for v in eprm_join_cols_entity.split(",")]
if eprm_table_alias + ".version" not in remove_string and eprm_table_alias + ".version" in eprm_join_cols_reim:
remove_string = remove_string + [eprm_table_alias + ".version"]
eprm_join_cols_entity = eprm_join_cols_entity + ", version"
reim_select_cols = clause_removed_reim_join
for v in remove_string:
reim_select_cols = reim_select_cols.replace(v, "").replace("reim.", "")
reim_select_cols = reim_select_cols.replace(" ", ",")
if eprm_table_type == "AUDIT" and eprm_table_alias + ".version" in remove_string and "reim.version" not in eprm_join_cols_reim:
reim_select_cols = reim_select_cols + ",version"
split_parts = reim_select_cols.split(',')
replaced_parts = []
for part in split_parts:
dot_index = part.find('.')
if dot_index != -1:
replaced_part = part[dot_index + 1:]
replaced_parts.append(replaced_part)
else:
replaced_parts.append(part)
replaced_string = ','.join(replaced_parts)
secondary_query = f"SELECT COUNT(*) FROM {eprm_table_name} WHERE ("+eprm_join_cols_entity+") IN (SELECT "+replaced_string+" FROM tibtcare_ppm_st2.release_entity_inst_map WHERE release_id='"+releaseId+"' AND op_id='"+opId+"' AND bu_id='"+buId+"')"
try:
secondary_cursor.execute(secondary_query)
result = secondary_cursor.fetchone()
print(f"Count for {eprm_table_name}: {result[0]}")
except mysql.connector.Error as err:
print(f"Error occurred while executing the query: {err}")
primary_cursor.close()
primary_cnx.close()
secondary_cursor.close()
secondary_cnx.close()
except FileNotFoundError:
print(f"File {json_file_path} not found.")
except json.JSONDecodeError:
print(f"Error occurred while parsing JSON file.")
except mysql.connector.Error as err:
print(f"Error occurred while connecting to the MySQL database: {err}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Editor is loading...