Skip to content

Commit

Permalink
optimized get_best_day API by 10sec
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitsinghai77 committed Jan 17, 2024
1 parent 7e88e7f commit 04781bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 5 additions & 7 deletions app/api/crud/nemodeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,11 @@ def get_total_hours(self):

@check_empty_dataframe
def get_best_day(self):
self.df["weekday"] = self.df["created_at"].dt.strftime("%b %d")
self.df = self.df.groupby(by="weekday", as_index=False).sum()
self.df = self.df.rename({"weekday": "full_date"}, axis=1) # rename column
maximum_value_index = self.df["duration"].idxmax()
result = self.df.loc[maximum_value_index]
return result.to_dict()

self.df["full_date"] = self.df["created_at"].dt.strftime("%a, %b %d %Y").rename("weekday")
result = self.df.groupby(by="full_date", as_index=False)["duration"].sum().sort_values("duration", ascending=False).to_dict("records")[0]
result = {"best_day_full_date": result.pop("full_date"), "best_day_duration": result.pop("duration")}
return result

@check_empty_dataframe
def get_current_goal(self):
current_date = pd.to_datetime("today")
Expand Down
10 changes: 6 additions & 4 deletions tests/test_deta.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def test_get_best_day(google_id):
best_day = NemoDeta.analytics_get_best_day(google_id)
assert best_day is not None
assert len(best_day) > 1
assert best_day["full_date"]
assert best_day["best_day_full_date"]
assert best_day["best_day_duration"]
assert best_day["best_day_duration"] > 1


def test_get_current_goal(google_id):
Expand Down Expand Up @@ -195,6 +197,6 @@ def test_delete_all_user_analytics(google_id):
get_total_hrs = NemoDeta.get_analytics(google_id)
get_best_day = NemoDeta.analytics_get_best_day(google_id)
get_current_goal = NemoDeta.analytics_get_current_goal(google_id)
assert get_total_hrs is None
assert get_best_day is None
assert get_current_goal is None
assert get_total_hrs == []
assert get_best_day == []
assert get_current_goal == []

0 comments on commit 04781bd

Please sign in to comment.