diff --git a/etl/month_end_automation_wave_2_no_15.py b/etl/month_end_automation_wave_2_no_15.py new file mode 100644 index 0000000..c4e3613 --- /dev/null +++ b/etl/month_end_automation_wave_2_no_15.py @@ -0,0 +1,169 @@ +# Wave 2's month end automation +from tqdm import tqdm +from monday import MondayClient +from etl.osmosis_complaince_address_to_files import get_all_items, extract_asset_ids +from pprint import pprint +import pandas as pd +import json + +monday_key = "eyJhbGciOiJIUzI1NiJ9.eyJ0aWQiOjQ5ODc2ODQxOCwiYWFpIjoxMSwidWlkIjozNjE3ODAzNCwiaWFkIjoiMjAyNS0wNC0xMVQxMToyMzoxNy40NjdaIiwicGVyIjoibWU6d3JpdGUiLCJhY3RpZCI6MTM5OTc4MjMsInJnbiI6InVzZTEifQ.-2Lit4s46ZF6AXuMW9t0TxIaFLkHqD4Yo-PyM9i2XZY" +monday = MondayClient(monday_key) +# WCHG SHDF 2.1 Mansard +board_ids = ["5636990610"] + +for board in tqdm(board_ids): + board_data = monday.boards.fetch_boards_by_id(board) + columns = board_data["data"]["boards"][0]["columns"] + col_id_map = {col["title"].lower(): col["id"] for col in columns} + reversed_col_id_map = {v: k for k, v in col_id_map.items()} + + + items = get_all_items(board, monday) + + all_records = [] + for row in tqdm(items): + data = {} + data.update({"address": row['name']}) + data.update({"client": row['group']['title']}) + for col in row.get("column_values", []): + if col.get("id") in reversed_col_id_map: + if col.get("type") == "file": + value = col.get("value") + no_of_files = 0 + + if value: + value = json.loads(col["value"]) + no_of_files = len(value.get('files', [])) + data.update({reversed_col_id_map[col.get("id")]: no_of_files}) + elif "no show" in reversed_col_id_map[col.get("id")]: + def extract_number_from_text(text): + number_str = '' + + for char in text: + if char.isnumeric(): + number_str += char + elif number_str: + break # stop once a number sequence ends + + return int(number_str) if number_str else None + text = col.get("text") + if text is None: + data.update({ + reversed_col_id_map[col.get("id")]: col.get("text") + }) + else: + data.update({ + reversed_col_id_map[col.get("id")]: extract_number_from_text(text) + }) + else: + data.update({ + reversed_col_id_map[col.get("id")]: col.get("text") + }) + all_records.append(data) + +# Convert to DataFrame +df = pd.DataFrame(all_records) + +filtered_dfs = [] + +def get_df(df, column_name, success_critera, job_name): + _ = df[ + df[column_name].str.lower().isin(success_critera) + ].copy() + _["job_type"] = job_name + return _ + +# RA +ra = get_df(df, "ra status", ["completed rdsap 9.9", "completed rdsap 10"], "RA") +filtered_dfs.append(ra) + + +# PRE- ATT +att = get_df(df, "pre att", ["completed"], "ATT") +filtered_dfs.append(att) + +# V1 Coordination +v1 = get_df(df, "coordination status (ioe mtp)".lower(), [ + "rc complete", +], "V1 Coordination") +filtered_dfs.append(v1) + +# V2 Coordination +# v2 = get_df(df, "mtp v2 status", ["rc v2 complete"], "V2 Coordination") +# filtered_dfs.append(v2) + +# # V3 Coordination +# v3 = get_df(df, "v3 rc status", ["uploaded"], "V3 Coordination") +# filtered_dfs.append(v3) + +# v3 = get_df(df, "v3 invoice status", ["to be invoice"], "V3 Coordination") +# filtered_dfs.append(v3) + +# Design stage 1 +design1 = get_df(df, "design invoice status", ["to invoice"], "Design") +filtered_dfs.append(design1) + +# Design revision +# design2 = get_df(df, "design revision invoice", [ +# "Rev. A to invoice".lower(), +# "Rev. B to invoice".lower(), +# "Rev. C to invoice".lower(), +# "Rev. D to invoice".lower(), +# ], "Design Revision") +# filtered_dfs.append(design2) + +# Lodgement Phase 1 +lodg1 = get_df(df, "tm phase 1 invoice satus (lodgment)".lower(), ["to invoice"], "Lodgement Phase 1") +filtered_dfs.append(lodg1) + + +# Full Lodgement Phase +full_lodgement = get_df(df, "lodgement invoice status".lower(), ["to invoice"], "Full Lodgement") +filtered_dfs.append(full_lodgement) + +# POST EPC +post_epc = get_df(df, "post-epc status", ["uploaded", "completed"], "POST EPC") +filtered_dfs.append(post_epc) + + +# # POST EPR +# post_epr = df[ +# df["post-epc status"].str.lower().isin(["post epr completed"]) +# ].copy() +# post_epr["job_type"] = "POST ATT" +# filtered_dfs.append(post_epr) + +# Post ATT +post_att = get_df(df, "post-att status", ["uploaded", "completed"], "POST ATT") +filtered_dfs.append(post_att) + +# Retrofit Evaluation +retro = get_df(df, "retrofit evaluation", ["completed", "uploaded"], "Retrofit Evaluation") +filtered_dfs.append(retro) + +# RA NO Show +ra_ns = df[ + df["ra no show evidence"].fillna(-9999) != df["ra no show invoice"].fillna(-9999) +].copy() +ra_ns["job_type"] = "RA NO SHOW" +filtered_dfs.append(ra_ns) + + +# ATT NO Show +att_ns = df[ + df["att no show evidence"].fillna(-9999) != df["att no show invoice"].fillna(-9999) +].copy() +att_ns["job_type"] = "ATT NO SHOW" +filtered_dfs.append(att_ns) + + +# Post visit no show +epc_ns = df[ + df["epc no show evidence"].fillna(-9999) != df["epc no show invoice"].fillna(-9999) +].copy() +epc_ns["job_type"] = "EPC NO SHOW" +filtered_dfs.append(epc_ns) + +final_df = pd.concat(filtered_dfs).reset_index(drop=True) + +final_df[['address', 'client', 'job_type']] \ No newline at end of file