Skip to content

Commit

Permalink
Merge pull request #80 from whdalsrnt/master
Browse files Browse the repository at this point in the history
Fix Bugs
  • Loading branch information
whdalsrnt authored Jul 7, 2024
2 parents 20c90c4 + cb05478 commit 3cea5dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ def load(
) -> pd.DataFrame:
start, end = self._get_time_from_granularity(granularity, start, end)

print("=====")
print(start)
print(end)
if self.timediff:
start, end = self._change_time(start, end)
start, end = self._change_time(granularity, start, end)
print(">>>>>>")
print(start)
print(end)

if self.source_type == "COST":
self._analyze_cost(granularity, start, end, vars)
Expand Down Expand Up @@ -206,7 +212,7 @@ def _change_datetime_format(self, results: list) -> list:
changed_results.append(result)
return changed_results

def _change_time(self, start: str, end: str) -> Tuple[str, str]:
def _change_time(self, granularity: str, start: str, end: str) -> Tuple[str, str]:
start_len = len(start)
end_len = len(end)
start_time = self._get_datetime_from_str(start)
Expand All @@ -226,6 +232,20 @@ def _change_time(self, start: str, end: str) -> Tuple[str, str]:
start_time = start_time + relativedelta(days=days)
end_time = end_time + relativedelta(days=days)

if granularity == "YEARLY":
start_time = start_time.replace(month=1, day=1)
end_time = end_time.replace(month=1, day=1)
if start_time + relativedelta(years=3) <= end_time:
start_time = end_time - relativedelta(years=2)
elif granularity == "MONTHLY":
start_time = start_time.replace(day=1)
end_time = end_time.replace(day=1)
if start_time + relativedelta(months=12) <= end_time:
start_time = end_time - relativedelta(months=11)
else:
if start_time + relativedelta(months=1) <= end_time:
start_time = end_time - relativedelta(months=1) + relativedelta(days=1)

return (
self._change_str_from_datetime(start_time, start_len),
self._change_str_from_datetime(end_time, end_len),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get_data_and_labels_info(self) -> Tuple[dict, dict]:

def load(
self,
granularity: GRANULARITY = "DAILY",
granularity: GRANULARITY = "MONTHLY",
start: str = None,
end: str = None,
vars: dict = None,
Expand Down Expand Up @@ -262,11 +262,23 @@ def evaluate_data_table(
df = self._get_data_table(origin_vo, granularity, start, end, vars)

for expression in expressions:
if "@" in expression:
raise ERROR_INVALID_PARAMETER(
key="options.EVAL.expressions",
reason="It should not have '@' symbol.",
)

try:
key, value = expression.split("=")
key, value_expression = expression.split("=", 1)
key = key.replace("`", "").strip()
value_expression = value_expression.strip()
expression = f"`{key}` = {value_expression}"

self.data_keys = list(set(self.data_keys) | {key})

df = df.eval(expression)
df.eval(expression, inplace=True)
new_key = df.columns[-1:][0]
df.rename(columns={new_key: key}, inplace=True)

except Exception as e:
_LOGGER.error(f"[evaluate_data_table] eval error: {e}")
Expand Down

0 comments on commit 3cea5dc

Please sign in to comment.