Commit b9a72588 authored by Cristian Aguirre's avatar Cristian Aguirre

Update match-and-exclude-records-actions_v1.py

parent eb0ad02e
......@@ -98,12 +98,13 @@ class MatchAndExcludeRecordsAction(ActionInterface):
self.app.logger.info(f"Insumos cargados - {self.identifier}")
if self.create_tmp_table:
import random
from app.main.engine.util.Timezone import Timezone
from sqlalchemy import text
# Creamos las tablas temporales del pivot y contraparte con sufijo N° aleatorio de 6 dígitos
random_number = random.randint(100000, 999999)
pivot_query = f"CREATE TABLE ENGINE_{pivot_table}_{random_number} AS SELECT * FROM {pivot_table}"
ctp_query = f"CREATE TABLE ENGINE_{ctp_table}_{random_number} AS SELECT * FROM {ctp_table}"
timezone = Timezone(self.app)
current_time = timezone.datetime_by_tzone().strftime("%Y%m%d%H%M")
pivot_query = f"CREATE TABLE ENGINE_{pivot_table}_{current_time} AS SELECT * FROM {pivot_table}"
ctp_query = f"CREATE TABLE ENGINE_{ctp_table}_{current_time} AS SELECT * FROM {ctp_table}"
source_obj.create_engine()
engine = source_obj.engine
with engine.connect() as conn:
......@@ -237,7 +238,8 @@ class MatchAndExcludeRecordsAction(ActionInterface):
total_cols = group_cols + [amount_col, id_col, "DIFF"]
self.app.logger.info(f"Aplicando algoritmo de exclusión. Máximo de {str(max_combinations)} combinaciones - {self.identifier}")
resultado = df3.groupby(group_cols)[total_cols].apply(lambda x: custom_func(x, amount_col, id_col, max_combinations))
if resultado.empty:
return pd.DataFrame([])
resultado = resultado.reset_index()
if len(resultado.columns) == 1:
resultado = pd.DataFrame([], columns=group_cols + ["LISTA_DIFF"])
......@@ -256,14 +258,16 @@ class MatchAndExcludeRecordsAction(ActionInterface):
meged2 = meged2.compute()
if meged2.empty:
pass
self.app.logger.info(f"El dataset resultante esta vacío luego de la ejecución del script")
elif self.exclude_pivot:
meged2['INTER_PIVOT_ID'] = meged2.apply(lambda row: self.array_except(row[self.pivot_params["id-column"]], row['LISTA_DIFF']), axis=1)
meged2 = meged2[meged2["INTER_PIVOT_ID"].apply(lambda x: True if isinstance(x, List) and len(x) > 0 else False)]
meged2 = meged2.rename(columns={self.ctp_params["id-column"]: "INTER_CTP_ID"})
if meged2['INTER_CTP_ID'].dtype == 'int64':
meged2['INTER_CTP_ID'] = meged2['INTER_CTP_ID'].apply(lambda x: [x]).astype('object')
else:
meged2['INTER_CTP_ID'] = meged2.apply(lambda row: self.array_except(row[self.ctp_params["id-column"]], row['LISTA_DIFF']), axis=1)
meged2 = meged2[meged2['INTER_CTP_ID'].apply(lambda x: True if isinstance(x, List) and len(x) > 0 else False)]
meged2 = meged2.rename(columns={self.pivot_params["id-column"]: "INTER_PIVOT_ID"})
if meged2['INTER_PIVOT_ID'].dtype == 'int64':
meged2['INTER_PIVOT_ID'] = meged2['INTER_PIVOT_ID'].apply(lambda x: [x]).astype('object')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment