Exemple #1
0
def get_data_sources(tenant_id, case_id, column_name, master=False):
    """Helper to get all the required table data for the businesss rules to apply
    """
    get_datasources_query = "SELECT * from `data_sources`"
    db_config['tenant_id'] = tenant_id
    business_rules_db = DB('business_rules', **db_config)
    data_sources = business_rules_db.execute(get_datasources_query)

    # sources
    sources = json.loads(list(data_sources[column_name])[0])

    data = {}
    for database, tables in sources.items():
        db = DB(database, **db_config)
        for table in tables:
            if master:
                query = f"SELECT * from `{table}`"
                try:
                    df = db.execute(query)
                except:
                    df = db.execute_(query)

                data[table] = df.to_dict(orient='records')
            else:
                query = f"SELECT * from `{table}` WHERE case_id = %s"
                params = [case_id]
                df = db.execute(query, params=params)
                if not df.empty:
                    data[table] = df.to_dict(orient='records')[0]
                else:
                    data[table] = {}

    case_id_based_sources = json.loads(list(data_sources['case_id_based'])[0])

    return data
def get_data(tenant_id,
             database,
             table,
             case_id,
             case_id_based=True,
             view='records'):
    """give the data from database
    Args:
        
    Returns:
        result (dict)
        result is a dict having keys flag, data.Depending on the flag the data changes.
        if flag is True: data contains the key value and value is the data.
        if flag is False: data contains the error_msg say why its failed.
    Example:
            x = get_data('invesco.acelive.ai','extraction','ocr','INV4D15EFC')"""
    result = {}
    db = DB(database, tenant_id=tenant_id, **db_config)
    logging.info(f"case_id based: {case_id_based}")
    try:
        if case_id_based:
            query = f"SELECT * from `{table}` WHERE `case_id` = '{case_id}'"
            try:
                df = db.execute(query)
            except:
                df = db.execute_(query)
            table_data = df.to_dict(orient=view)
            result['flag'] = True
            result['data'] = {"value": table_data}

        else:
            query = f"SELECT * from `{table}`"
            df = db.execute(query)
            if not df.empty:
                table_data = df.to_dict(orient=view)
            else:
                table_data = {}
            result['flag'] = True
            result['data'] = {"value": table_data}
    except Exception as e:
        logging.error(f"Failed in getting tables data from database")
        logging.error(e)
        result['flag'] = 'False'
        result['data'] = {
            'reason': 'Failed in getting tables data from database',
            'error_msg': str(e)
        }
    return result