|
1 | | -from pandas import DataFrame |
| 1 | +from io import StringIO |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import requests |
2 | 5 |
|
3 | 6 | from data_retrieval_app.data_deposit.data_depositor_base import DataDepositorBase |
| 7 | +from data_retrieval_app.logs.logger import data_deposit_logger as logger |
4 | 8 |
|
5 | 9 |
|
6 | 10 | class ItemBaseTypeDataDepositor(DataDepositorBase): |
7 | 11 | def __init__(self) -> None: |
8 | 12 | super().__init__(data_type="item_base_type") |
9 | 13 |
|
| 14 | + self.update_url = str(self.data_url) |
10 | 15 | self.data_url += "?on_duplicate_pkey_do_nothing=true" |
| 16 | + self.current_basetypes_df = self._get_current_base_types() |
| 17 | + |
| 18 | + def _get_current_base_types(self) -> pd.DataFrame: |
| 19 | + logger.info("Retrieving previously deposited data.") |
| 20 | + |
| 21 | + response = requests.get(self.data_url, headers=self.pom_auth_headers) |
| 22 | + |
| 23 | + df = pd.DataFrame() |
| 24 | + # Check if the request was successful |
| 25 | + if response.status_code == 200: |
| 26 | + # Load the JSON data into a pandas DataFrame |
| 27 | + json_io = StringIO(response.content.decode("utf-8")) |
| 28 | + df = pd.read_json(json_io, dtype=str) |
| 29 | + |
| 30 | + if df.empty: |
| 31 | + logger.info("Found no previously deposited data.") |
| 32 | + return pd.DataFrame( |
| 33 | + columns=[ |
| 34 | + "itemBaseTypeId", |
| 35 | + "baseType", |
| 36 | + "category", |
| 37 | + "subCategory", |
| 38 | + "relatedUniques", |
| 39 | + ] |
| 40 | + ) |
| 41 | + else: |
| 42 | + logger.info("Successfully retrieved previously deposited data.") |
| 43 | + return df |
| 44 | + |
| 45 | + def _update_duplicates(self, duplicate_df: pd.DataFrame): |
| 46 | + """ |
| 47 | + Note that this method does not remove uniques which are not present in the file. |
| 48 | + """ |
| 49 | + changed_rows = duplicate_df[ |
| 50 | + (duplicate_df["relatedUniques"] != duplicate_df["relatedUniques_y"]) |
| 51 | + & (~duplicate_df["relatedUniques"].isna()) |
| 52 | + ] |
| 53 | + |
| 54 | + if not changed_rows.empty: |
| 55 | + logger.info( |
| 56 | + "Found changes in related uniques for some item base types. Updating these changes." |
| 57 | + ) |
| 58 | + changed_rows["new_related_uniques"] = changed_rows.apply( |
| 59 | + lambda row: ( |
| 60 | + "|".join( |
| 61 | + set( |
| 62 | + row["relatedUniques"].split("|") |
| 63 | + + row["relatedUniques_y"].split("|") |
| 64 | + ) |
| 65 | + ) |
| 66 | + if row["relatedUniques_y"] |
| 67 | + else row["relatedUniques"] |
| 68 | + ), |
| 69 | + axis=1, |
| 70 | + ) |
| 71 | + headers = { |
| 72 | + "accept": "application/json", |
| 73 | + "Content-Type": "application/json", |
| 74 | + } |
| 75 | + headers.update(self.pom_auth_headers) |
| 76 | + |
| 77 | + for _, row in changed_rows.iterrows(): |
| 78 | + item_base_type_id = row["itemBaseTypeId"] |
| 79 | + data = { |
| 80 | + "baseType": row["baseType"], |
| 81 | + "category": row["category"], |
| 82 | + "subCategory": row["subCategory"], |
| 83 | + "relatedUniques": row["new_related_uniques"], |
| 84 | + } |
| 85 | + |
| 86 | + try: |
| 87 | + response = requests.put( |
| 88 | + self.update_url + str(item_base_type_id), |
| 89 | + json=data, |
| 90 | + headers=headers, |
| 91 | + # add HTTP Basic Auth |
| 92 | + ) |
| 93 | + response.raise_for_status() |
| 94 | + except Exception as e: |
| 95 | + logger.error( |
| 96 | + f"The following error occurred while making request during _update_duplicates modifiers: {e}" |
| 97 | + ) |
| 98 | + raise e |
| 99 | + |
| 100 | + def _process_data(self, df: pd.DataFrame) -> pd.DataFrame: |
| 101 | + merged_df = pd.merge( |
| 102 | + df, |
| 103 | + self.current_basetypes_df, |
| 104 | + how="left", |
| 105 | + on="baseType", |
| 106 | + suffixes=("", "_y"), |
| 107 | + ) |
| 108 | + non_duplicate_mask = merged_df["itemBaseTypeId"].isna() |
| 109 | + non_duplicate_df = merged_df[non_duplicate_mask] |
| 110 | + non_duplicate_df = non_duplicate_df.drop( |
| 111 | + columns=[ |
| 112 | + column for column in non_duplicate_df.columns if column.endswith("_y") |
| 113 | + ] |
| 114 | + ) |
| 115 | + |
| 116 | + self._update_duplicates(merged_df[~non_duplicate_mask]) |
11 | 117 |
|
12 | | - def _process_data(self, df: DataFrame) -> DataFrame: |
13 | | - return df |
| 118 | + return non_duplicate_df |
0 commit comments