Skip to content

Commit

Permalink
Add Timeline Views
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffshek committed Sep 4, 2020
1 parent 5377ac3 commit 06cebac
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ repos:
hooks:
- id: black
args: [--line-length=88, --safe]
language_version: python3
4 changes: 2 additions & 2 deletions open/core/betterself/utilities/demo_user_factory_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def create_demo_fixtures_for_user(user):
# do a week ahead of time, that way you don't really have to deal with constantly rerunning
# this script for now on deployments
utc_now = get_utc_now()
relative_end_date_of_fixtures_creation = utc_now + get_time_relative_units_forward(
days=7
relative_end_date_of_fixtures_creation = get_time_relative_units_forward(
utc_now, days=7
)

dates_to_create = []
Expand Down
87 changes: 87 additions & 0 deletions open/core/betterself/utilities/history_overview_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
mean_time,
)

import dateutil.parser

PRODUCTIVITY_LOG_VALUE_FIELDS = [
"uuid",
"date",
Expand All @@ -48,6 +50,13 @@
"mistakes",
]

SUPPLEMENT_LOG_TYPE = "Supplement"
PRODUCTIVITY_LOG_TYPE = "Productivity"
SLEEP_LOG_TYPE = "Sleep"
WELL_BEING_LOG_TYPE = "Well Being"
ACTIVITY_LOG_TYPE = "Activity"
FOOD_LOG_TYPE = "Food"


def get_overview_supplements_data(user, start_period, end_period):
response = {
Expand All @@ -61,6 +70,7 @@ def get_overview_supplements_data(user, start_period, end_period):
# TODO - not sure how much i need this now that it's handled by ag-grid
"summary": [],
"total_quantity": 0,
"log_type": SUPPLEMENT_LOG_TYPE,
}

supplement_logs = (
Expand Down Expand Up @@ -119,6 +129,7 @@ def get_overview_productivity_data(
"start_period": start_period.date().isoformat(),
"end_period": end_period.date().isoformat(),
"logs": [],
"log_type": PRODUCTIVITY_LOG_TYPE,
}

# get more data, that way we can calculate a running historical average
Expand Down Expand Up @@ -188,6 +199,7 @@ def get_overview_sleep_data(user, start_period, end_period):
"end_time_mean": None,
"total_duration_minutes": None,
"total_duration_hours": None,
"log_type": SLEEP_LOG_TYPE,
}

sleep_logs = SleepLog.objects.filter(
Expand Down Expand Up @@ -232,6 +244,7 @@ def get_overview_well_being_data(user, start_period, end_period):
"start_period": start_period.date().isoformat(),
"end_period": end_period.date().isoformat(),
"logs": [],
"log_type": WELL_BEING_LOG_TYPE,
}

logs = WellBeingLog.objects.filter(
Expand All @@ -249,6 +262,7 @@ def get_overview_activity_data(user, start_period, end_period):
"start_period": start_period.date().isoformat(),
"end_period": end_period.date().isoformat(),
"logs": [],
"log_type": ACTIVITY_LOG_TYPE,
}

logs = (
Expand All @@ -270,6 +284,7 @@ def get_overview_food_data(user, start_period, end_period):
"start_period": start_period.date().isoformat(),
"end_period": end_period.date().isoformat(),
"logs": [],
"log_type": FOOD_LOG_TYPE,
}

logs = (
Expand All @@ -282,3 +297,75 @@ def get_overview_food_data(user, start_period, end_period):

response["logs"] = FoodLogReadSerializer(logs, many=True).data
return response


def build_timeline_item(time, log_type, summary):
result = {"time": time, "log_type": log_type, "summary": summary}
return result


def get_timeline_data(
sleep_data,
supplements_data,
productivity_data,
well_being_data,
activities_data,
foods_data,
):
response = []

if sleep_data["logs"]:
for sleep_log in sleep_data["logs"]:
sleep_log_type = sleep_data["log_type"]

sleep_start_item = {
"time": sleep_log["start_time"],
"log_type": sleep_log_type,
"summary": "Went To Sleep",
}
sleep_end_time = {
"time": sleep_log["end_time"],
"log_type": sleep_log_type,
"summary": "Woke Up!",
}

response.extend([sleep_start_item, sleep_end_time])

logs_with_time_attributes = [
supplements_data,
well_being_data,
activities_data,
foods_data,
]
for data in logs_with_time_attributes:
logs = data["logs"]
if not logs:
continue

log_type = data["log_type"]

for log in logs:
summary = ""
if log_type == SUPPLEMENT_LOG_TYPE:
summary = f"Took {log['quantity']} of {log['supplement']['name']}"
elif log_type == WELL_BEING_LOG_TYPE:
if log["mental_value"]:
summary += f"Mental Score: {log['mental_value']}\n"
if log["physical_value"]:
summary += f"Physical Score: {log['physical_value']}\n"
if log["notes"]:
summary += f"Notes: {log['notes']}"
elif log_type == ACTIVITY_LOG_TYPE:
summary = f"Did {log['activity']['name']}."
elif log_type == FOOD_LOG_TYPE:
summary = f"Ate {log['food']['name']}."

timeline_item = {
"time": log["time"],
"log_type": log_type,
"summary": summary,
}
response.append(timeline_item)

response.sort(key=lambda x: dateutil.parser.parse(x["time"]))
return response
12 changes: 11 additions & 1 deletion open/core/betterself/views/daily_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get_overview_well_being_data,
get_overview_activity_data,
get_overview_food_data,
get_timeline_data,
)


Expand Down Expand Up @@ -52,6 +53,14 @@ def get(self, request, date):
foods_data = get_overview_food_data(
user=user, start_period=start_period, end_period=end_period
)
timeline_data = get_timeline_data(
sleep_data=sleep_data,
supplements_data=supplements_data,
productivity_data=productivity_data,
well_being_data=well_being_data,
activities_data=activities_data,
foods_data=foods_data,
)

response = {
# change it back to a date, so it doesn't look super confusing on api response ...
Expand All @@ -61,7 +70,8 @@ def get(self, request, date):
"productivity": productivity_data,
"sleep": sleep_data,
"supplements": supplements_data,
"well_being_data": well_being_data,
"well_being": well_being_data,
"timeline": timeline_data,
}

return Response(response)

0 comments on commit 06cebac

Please sign in to comment.