[DEV-12162] Agency loader#8
Conversation
+ Creating the agency references + Creating the initial script containing most of the work for cgac and frec loading
+ Also add subtier data
…-12162-agency-loader
+ black + flake + removing prints
| cgac_data["display_name"] = cgac_data.apply( | ||
| lambda row: ( | ||
| f"{row["agency_name"]} ({row["agency_abbreviation"]})" | ||
| if row["agency_abbreviation"] | ||
| else f"{row["agency_name"]} (nan)" | ||
| ), | ||
| axis=1, | ||
| ) |
There was a problem hiding this comment.
This could be done column-wise instead of row-wise to increase performance:
| cgac_data["display_name"] = cgac_data.apply( | |
| lambda row: ( | |
| f"{row["agency_name"]} ({row["agency_abbreviation"]})" | |
| if row["agency_abbreviation"] | |
| else f"{row["agency_name"]} (nan)" | |
| ), | |
| axis=1, | |
| ) | |
| cgac_data["display_name"] = ( | |
| cgac_data["agency_name"] | |
| + " (" | |
| + cgac_data["agency_abbreviation"].replace("", "nan").fillna("nan") | |
| + ")" | |
| ) |
| frec_data["display_name"] = frec_data.apply( | ||
| lambda row: ( | ||
| f"{row["agency_name"]} ({row["agency_abbreviation"]})" | ||
| if row["agency_abbreviation"] | ||
| else f"{row["agency_name"]} (nan)" | ||
| ), | ||
| axis=1, | ||
| ) |
There was a problem hiding this comment.
same thing here re: column-wise vs row-wise operations
| frec_data = clean_data( | ||
| raw_data, | ||
| frec_mapping, | ||
| {"frec": {"keep_null": False}, "cgac_code": {"pad_to_length": 3}, "frec_code": {"pad_to_length": 4}}, | ||
| ) | ||
|
|
||
| # de-dupe | ||
| frec_data = frec_data[frec_data.frec_cgac == "TRUE"] | ||
| frec_data.drop(["frec_cgac"], axis=1, inplace=True) | ||
| frec_data.drop_duplicates(subset=["frec_code"], inplace=True) |
There was a problem hiding this comment.
instead of mutating the data in place, you could chain these:
| frec_data = clean_data( | |
| raw_data, | |
| frec_mapping, | |
| {"frec": {"keep_null": False}, "cgac_code": {"pad_to_length": 3}, "frec_code": {"pad_to_length": 4}}, | |
| ) | |
| # de-dupe | |
| frec_data = frec_data[frec_data.frec_cgac == "TRUE"] | |
| frec_data.drop(["frec_cgac"], axis=1, inplace=True) | |
| frec_data.drop_duplicates(subset=["frec_code"], inplace=True) | |
| frec_data = ( | |
| clean_data( | |
| raw_data, | |
| frec_mapping, | |
| {"frec": {"keep_null": False}, "cgac_code": {"pad_to_length": 3}, "frec_code": {"pad_to_length": 4}}, | |
| ) | |
| .loc[lambda df: df.frec_cgac == "TRUE"] | |
| .drop(columns=["frec_cgac"]) | |
| .drop_duplicates(subset=["frec_code"]) | |
| ) |
| raw_data.loc[condition, "PRIORITY"] = 1 | ||
| raw_data.loc[~condition, "PRIORITY"] = 2 | ||
| raw_data["PRIORITY"] = raw_data["PRIORITY"].astype(int) | ||
| raw_data.replace({"TRUE": True, "FALSE": False}, inplace=True) |
There was a problem hiding this comment.
Same approach here re: method chaining.
| raw_data.loc[condition, "PRIORITY"] = 1 | |
| raw_data.loc[~condition, "PRIORITY"] = 2 | |
| raw_data["PRIORITY"] = raw_data["PRIORITY"].astype(int) | |
| raw_data.replace({"TRUE": True, "FALSE": False}, inplace=True) | |
| raw_data = ( | |
| raw_data | |
| .assign(PRIORITY=lambda df: np.where(df["TOPTIER_FLAG"] == "TRUE", 1, 2).astype(int)) | |
| .replace({"TRUE": True, "FALSE": False}) | |
| ) |
| DESCRIPTION = "CGAC Data after processing" | ||
| CSV_NAME = "cgac.csv" | ||
| PK = "cgac_id" | ||
| UNIQUE_CONSTRAINTS = [] |
There was a problem hiding this comment.
I know UNIQUE_CONSTRAINTS hasn't been setup as an extra validation yet. The intent is for any other columns (or group of columns) needing a unique check. I'm gonna make another PR to 1) fix DEFCGroup's PK but also 2) update the type mapping of UNIQUE_CONSTRAINTS to be List[(str,) | str]; so it can be ['col x', ('col y', 'col z')] to support single column uniqueness but also multiple columns as well.
So in this case (as well as FRECGold and SubtierAgencyGold), since the PKs are x_id, the other unique col would be ["cgac_code"] (or ["frec_code"], ["subtier_code"]).
Again, not functional yet so I'm fine with leaving it alone as it may change in the future when we do implement it. Just letting you know where I'm thinking for now.
| cgac_data["display_name"] = ( | ||
| cgac_data["agency_name"] + " (" + cgac_data["agency_abbreviation"].replace("", "nan").fillna("nan") + ")" | ||
| ) | ||
| logger.info("Overwriting new CGAC data to Broker") |
There was a problem hiding this comment.
| logger.info("Overwriting new CGAC data to Broker") | |
| logger.info("Overwriting new CGAC data to CGACGold") |
| metrics_json = load_cgac(raw_data, start_time, force_reload, metrics_json) | ||
| metrics_json = load_frec(raw_data, start_time, force_reload, metrics_json) | ||
| metrics_json = load_subtier(raw_data, start_time, force_reload, metrics_json) |
There was a problem hiding this comment.
Instead of passing in start_time, could you pull it from metrics_json? I know it gets messy with the strings vs datetimes but when saving the metrics file json.dump(metrics, metrics_file, default=str) including that default=str stringifys the dates automatically which is nice.
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description="Process the bronze defc data into the gold defc table.") |
There was a problem hiding this comment.
I know this was likely based on defc_gold.py which doesnt include this (making another PR fix for that). Could you add a
from brus_backend_common.logging import configure_logging
...
if __name__ == "__main__":
configure_logging()
here? Running it locally I could see none of the logs.
I'm going to also start looking into a shared Loader class or shared methods that standardizes all our scripts so that we don't need to remember little details like this.
| """Loads the FREC data into the gold table | ||
|
|
||
| Args: | ||
| raw_data: the raw agency codes bronze table\ |
There was a problem hiding this comment.
| raw_data: the raw agency codes bronze table\ | |
| raw_data: the raw agency codes bronze table |
Description:
Creating a loader to load in all agency data (CGAC, FREC, Subtier).
Technical Details:
Created 3 new gold tables and a new bronze table to house agency data. Updated the existing table models from what was in Broker to simplify without the availability of the FKs. FREC and Subtier tables now use the related codes directly instead of using IDs of higher tier tables.
Requirements for PR Merge:
Explain N/A in above checklist: