Update app.py
Browse files
app.py
CHANGED
|
@@ -446,69 +446,79 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 446 |
"""
|
| 447 |
reg_df = pd.read_sql(registry_query, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 448 |
|
| 449 |
-
target_table_name =
|
| 450 |
if not reg_df.empty and pd.notna(reg_df.iloc[0]['Target_Table']):
|
| 451 |
val = str(reg_df.iloc[0]['Target_Table']).strip()
|
| 452 |
if val and val.lower() not in ["none", "nan"]:
|
| 453 |
target_table_name = val
|
| 454 |
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
q_rv = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 460 |
-
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 461 |
-
except Exception as sql_e:
|
| 462 |
-
conn_map.close()
|
| 463 |
-
return pd.DataFrame({"Error": [f"λ§€ν '{target_table_name}'μ μ¬λ λ° μ€ν¨νμ΅λλ€. ν
μ΄λΈ μ΄λ¦μ νμΈνμΈμ: {str(sql_e)}"]})
|
| 464 |
-
conn_map.close()
|
| 465 |
-
|
| 466 |
-
cols_fw_lower = {c.lower(): c for c in df_fw.columns}
|
| 467 |
-
|
| 468 |
-
if 'base_type' in cols_fw_lower and 'comp_type' in cols_fw_lower:
|
| 469 |
-
b_col = cols_fw_lower['base_type']
|
| 470 |
-
c_col = cols_fw_lower['comp_type']
|
| 471 |
-
|
| 472 |
-
df_fw[b_col] = df_fw[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 473 |
-
df_fw[c_col] = df_fw[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 474 |
-
df_fw = df_fw[(df_fw[b_col] == type_b.strip().upper()) & (df_fw[c_col] == type_c.strip().upper())]
|
| 475 |
-
|
| 476 |
-
if not df_rv.empty:
|
| 477 |
-
df_rv[b_col] = df_rv[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 478 |
-
df_rv[c_col] = df_rv[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 479 |
-
df_rv = df_rv[(df_rv[c_col] == type_b.strip().upper()) & (df_rv[b_col] == type_c.strip().upper())]
|
| 480 |
-
|
| 481 |
-
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 482 |
-
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 483 |
-
|
| 484 |
-
if b_sec not in df_fw.columns or c_sec not in df_fw.columns:
|
| 485 |
-
return pd.DataFrame({"Error": [f"'{target_table_name}' μ '{b_sec}' λλ '{c_sec}' μ΄μ΄ μμ΅λλ€. λμλ¬Έμλ₯Ό νμΈνμΈμ."]})
|
| 486 |
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
else:
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
|
|
|
|
|
|
| 512 |
|
| 513 |
df_base = df_base.rename(columns=internal_rename_b)
|
| 514 |
df_comp = df_comp.rename(columns=internal_rename_c)
|
|
|
|
| 446 |
"""
|
| 447 |
reg_df = pd.read_sql(registry_query, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 448 |
|
| 449 |
+
target_table_name = None
|
| 450 |
if not reg_df.empty and pd.notna(reg_df.iloc[0]['Target_Table']):
|
| 451 |
val = str(reg_df.iloc[0]['Target_Table']).strip()
|
| 452 |
if val and val.lower() not in ["none", "nan"]:
|
| 453 |
target_table_name = val
|
| 454 |
|
| 455 |
+
# π‘ [ν΅μ¬] μ¬μ©μλ μλλ¦¬μ€ μλ²½ μ μ©
|
| 456 |
+
is_same_std = (base_std.strip().upper() == comp_std.strip().upper() and type_b.strip().upper() == type_c.strip().upper())
|
| 457 |
+
df_mapping = pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
+
if target_table_name:
|
| 460 |
+
try:
|
| 461 |
+
q_fw = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?"
|
| 462 |
+
df_fw = pd.read_sql(q_fw, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 463 |
+
|
| 464 |
+
q_rv = f"SELECT * FROM [{target_table_name}] WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 465 |
+
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 466 |
+
|
| 467 |
+
cols_fw_lower = {c.lower(): c for c in df_fw.columns}
|
| 468 |
+
|
| 469 |
+
if 'base_type' in cols_fw_lower and 'comp_type' in cols_fw_lower:
|
| 470 |
+
b_col = cols_fw_lower['base_type']
|
| 471 |
+
c_col = cols_fw_lower['comp_type']
|
| 472 |
+
|
| 473 |
+
df_fw[b_col] = df_fw[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 474 |
+
df_fw[c_col] = df_fw[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 475 |
+
df_fw = df_fw[(df_fw[b_col] == type_b.strip().upper()) & (df_fw[c_col] == type_c.strip().upper())]
|
| 476 |
+
|
| 477 |
+
if not df_rv.empty:
|
| 478 |
+
df_rv[b_col] = df_rv[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 479 |
+
df_rv[c_col] = df_rv[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 480 |
+
df_rv = df_rv[(df_rv[c_col] == type_b.strip().upper()) & (df_rv[b_col] == type_c.strip().upper())]
|
| 481 |
+
|
| 482 |
+
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 483 |
+
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 484 |
+
|
| 485 |
+
if b_sec not in df_fw.columns or c_sec not in df_fw.columns:
|
| 486 |
+
conn_map.close()
|
| 487 |
+
return pd.DataFrame({"Error": [f"'{target_table_name}' μ '{b_sec}' λλ '{c_sec}' μ΄μ΄ μμ΅λλ€. λμλ¬Έμλ₯Ό νμΈνμΈμ."]})
|
| 488 |
+
|
| 489 |
+
df_fw = df_fw[[b_sec, c_sec]].rename(columns={b_sec: 'Base_section', c_sec: 'Comp_section'})
|
| 490 |
+
if not df_rv.empty:
|
| 491 |
+
df_rv = df_rv[[b_sec, c_sec]].rename(columns={b_sec: 'Comp_section', c_sec: 'Base_section'})
|
| 492 |
+
else:
|
| 493 |
+
df_rv = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
| 494 |
+
|
| 495 |
+
df_mapping = pd.concat([df_fw, df_rv], ignore_index=True)
|
| 496 |
+
|
| 497 |
+
if not df_mapping.empty:
|
| 498 |
+
df_mapping['Base_section'] = df_mapping['Base_section'].astype(str).str.replace('\n', ',').str.split(',')
|
| 499 |
+
df_mapping['Comp_section'] = df_mapping['Comp_section'].astype(str).str.replace('\n', ',').str.split(',')
|
| 500 |
+
df_mapping = df_mapping.explode('Base_section').explode('Comp_section')
|
| 501 |
+
|
| 502 |
+
df_mapping['Base_section'] = df_mapping['Base_section'].apply(clean_key_val)
|
| 503 |
+
df_mapping['Comp_section'] = df_mapping['Comp_section'].apply(clean_key_val)
|
| 504 |
+
|
| 505 |
+
df_mapping = df_mapping[(df_mapping['Base_section'] != "") & (df_mapping['Comp_section'] != "")]
|
| 506 |
+
bridge = df_mapping.dropna().drop_duplicates()
|
| 507 |
+
else:
|
| 508 |
+
bridge = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
| 509 |
+
|
| 510 |
+
except Exception as sql_e:
|
| 511 |
+
conn_map.close()
|
| 512 |
+
return pd.DataFrame({"Error": [f"λ§€ν ν
μ΄λΈ '{target_table_name}'μ μ¬λ λ° μ€ν¨νμ΅λλ€. ν
μ΄λΈ μ΄λ¦μ νμΈνμΈμ: {str(sql_e)}"]})
|
| 513 |
else:
|
| 514 |
+
if is_same_std:
|
| 515 |
+
all_keys = list(set(df_base['merge_key']).union(set(df_comp['merge_key'])))
|
| 516 |
+
bridge = pd.DataFrame({'Base_section': all_keys, 'Comp_section': all_keys})
|
| 517 |
+
else:
|
| 518 |
+
conn_map.close()
|
| 519 |
+
return pd.DataFrame({"Error": ["λ§€ν ν
μ΄λΈμ΄ μ‘΄μ¬νμ§ μμ΅λλ€."]})
|
| 520 |
+
|
| 521 |
+
conn_map.close()
|
| 522 |
|
| 523 |
df_base = df_base.rename(columns=internal_rename_b)
|
| 524 |
df_comp = df_comp.rename(columns=internal_rename_c)
|