Eureka2 / services /comparison_engine.py
Bosch09's picture
Upload 15 files
19641b3 verified
Raw History Blame Contribute Delete
5.97 kB
# ============================================================
# services/comparison_engine.py
# ============================================================
import pandas as pd
import numpy as np
# ============================================================
# NORMALIZE VALUE
# ============================================================
def normalize_value(
value,
ignore_spaces=True,
match_case=False,
relaxed_decimals=True,
normalize_dates=True,
treat_blank_as_null=True
):
# ========================================================
# NULL HANDLING
# ========================================================
if pd.isna(value):
return None
value = str(value)
# ========================================================
# SPACES
# ========================================================
if ignore_spaces:
value = value.strip()
# ========================================================
# BLANK AS NULL
# ========================================================
if treat_blank_as_null and value == "":
return None
# ========================================================
# CASE
# ========================================================
if not match_case:
value = value.lower()
# ========================================================
# DECIMAL NORMALIZATION
# ========================================================
if relaxed_decimals:
try:
num = float(value)
value = round(num, 6)
except:
pass
# ========================================================
# DATE NORMALIZATION
# ========================================================
if normalize_dates:
try:
dt = pd.to_datetime(value)
value = dt.strftime("%Y-%m-%d %H:%M:%S")
except:
pass
return value
# ============================================================
# RUN COMPARISON
# ============================================================
def run_comparison(
source_df,
target_df,
key_columns,
ignore_spaces=True,
match_case=False,
relaxed_decimals=True,
normalize_dates=True,
treat_blank_as_null=True
):
src = source_df.copy()
tgt = target_df.copy()
# ========================================================
# REMOVE NULL KEYS
# ========================================================
src_clean = src.dropna(subset=key_columns)
tgt_clean = tgt.dropna(subset=key_columns)
# ========================================================
# MERGE
# ========================================================
merged = src_clean.merge(
tgt_clean,
how="outer",
on=key_columns,
suffixes=("_src", "_tgt"),
indicator=True
)
# ========================================================
# MISSING / EXTRA
# ========================================================
missing_df = merged[
merged["_merge"] == "left_only"
].drop(columns=["_merge"])
extra_df = merged[
merged["_merge"] == "right_only"
].drop(columns=["_merge"])
both_df = merged[
merged["_merge"] == "both"
].drop(columns=["_merge"])
# ========================================================
# COMMON COLUMNS
# ========================================================
common_cols = [
c for c in src_clean.columns
if (
c in tgt_clean.columns
and c not in key_columns
)
]
# ========================================================
# MISMATCH DETECTION
# ========================================================
mismatch_rows = []
for col in common_cols:
src_col = both_df[f"{col}_src"]
tgt_col = both_df[f"{col}_tgt"]
for idx in range(len(both_df)):
src_val = normalize_value(
src_col.iloc[idx],
ignore_spaces,
match_case,
relaxed_decimals,
normalize_dates,
treat_blank_as_null
)
tgt_val = normalize_value(
tgt_col.iloc[idx],
ignore_spaces,
match_case,
relaxed_decimals,
normalize_dates,
treat_blank_as_null
)
if src_val != tgt_val:
row = {}
for key in key_columns:
row[key] = both_df.iloc[idx][key]
row["Column"] = col
row["Source"] = src_col.iloc[idx]
row["Target"] = tgt_col.iloc[idx]
mismatch_rows.append(row)
mismatches_df = pd.DataFrame(mismatch_rows)
# ========================================================
# MATCH %
# ========================================================
total_records = len(src_clean)
total_issues = (
len(missing_df)
+ len(extra_df)
+ len(mismatches_df)
)
if total_records == 0:
match_percent = 0
else:
match_percent = round(
max(
0,
100 - ((total_issues / total_records) * 100)
),
2
)
return {
"missing": missing_df,
"extra": extra_df,
"mismatches": mismatches_df,
"summary": {
"missing": len(missing_df),
"extra": len(extra_df),
"mismatches": len(mismatches_df),
"match_percent": match_percent
}
}