Skip to content

Commit ee1a19f

Browse files
committed
#817 Can now add related uniques to base types
1 parent 2d9492a commit ee1a19f

2 files changed

Lines changed: 109 additions & 4 deletions

File tree

src/backend_data_retrieval/data_retrieval_app/data_deposit/item_base_type/item_base_type_data/Jewels.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ baseType,category,subCategory,relatedUniques
44
"Cobalt Jewel","any jewel","base jewel","Grand Spectrum|Forbidden Flesh|The Balance of Terror"
55
"Viridian Jewel","any jewel","base jewel","Impossible Escape|Grand Spectrum"
66
"Prismatic Jewel","any jewel","base jewel","Watcher's Eye|Sublime Vision|The Light of Meaning|Bound By Destiny"
7-
"Timeless Jewel","any jewel","base jewel","Glorious Vanity|Lethal Pride|Brutal Restraint|Militant Faith|Elegant Hubris"
7+
"Timeless Jewel","any jewel","base jewel","Glorious Vanity|Lethal Pride|Brutal Restraint|Militant Faith|Elegant Hubris|Heroic Tragedy"
88
"Large Cluster Jewel","any jewel","cluster jewel","Voices"
99
"Medium Cluster Jewel","any jewel","cluster jewel",
1010
"Small Cluster Jewel","any jewel","cluster jewel",
Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,118 @@
1-
from pandas import DataFrame
1+
from io import StringIO
2+
3+
import pandas as pd
4+
import requests
25

36
from data_retrieval_app.data_deposit.data_depositor_base import DataDepositorBase
7+
from data_retrieval_app.logs.logger import data_deposit_logger as logger
48

59

610
class ItemBaseTypeDataDepositor(DataDepositorBase):
711
def __init__(self) -> None:
812
super().__init__(data_type="item_base_type")
913

14+
self.update_url = str(self.data_url)
1015
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])
11117

12-
def _process_data(self, df: DataFrame) -> DataFrame:
13-
return df
118+
return non_duplicate_df

0 commit comments

Comments
 (0)