Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph feature #24

Merged
merged 6 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
from flask_pymongo import PyMongo
from tabulate import tabulate
from forms import HistoryForm, RegistrationForm, LoginForm, CalorieForm, UserProfileForm, EnrollForm, WorkoutForm
from service import history as history_service
import openai
import os
from utilities import *
import utilities as u
import time

# Set the OpenAI API key
Expand Down Expand Up @@ -266,7 +265,7 @@ def workout():
if form.validate_on_submit():
email = session.get('email')
burnout = form.burnout.data

print(burnout)
mongo.db.workout.insert_one({
'date': form.date.data.strftime('%Y-%m-%d'), # Get the selected date from the form
'email': email,
Expand Down Expand Up @@ -296,45 +295,69 @@ def history():

# Find out the last 7 day's calories burnt by the user
calorie_day_map = {}
entries = get_entries_for_email(mongo.db, email, (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'), datetime.today().strftime('%Y-%m-%d'))
for entry in entries:
entries_cal, entries_workout = u.get_entries_for_email(mongo.db, email, (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'), datetime.today().strftime('%Y-%m-%d'))
# print(entries)
for entry in entries_cal:
if entry['_id'] == 'Other':
continue
net_calories = int(entry['calories'])
curr_date = entry['date']
if(curr_date not in calorie_day_map):
calorie_day_map[curr_date] = net_calories
else:
calorie_day_map[curr_date] += net_calories

for entry in entries_workout:
if entry['_id'] == 'Other':
continue
net_calories = int(entry['calories'])-2000
net_calories = -int(entry['burnout'])
curr_date = entry['date']
if(curr_date not in calorie_day_map):
calorie_day_map[curr_date] = net_calories
else:
calorie_day_map[curr_date] += net_calories
calorie_day_map = dict(sorted(calorie_day_map.items()))

labels = list(calorie_day_map.keys())
values = list(calorie_day_map.values())
for i in range(len(values)):
values[i] = str(values[i])


# The first day when the user registered or started using the app
user_start_date = mongo.db.user.find({'email': email})[0]['start_date']
user_target_date = mongo.db.user.find({'email': email})[0]['target_date']
target_weight = mongo.db.user.find({'email': email})[0]['target_weight']
current_weight = mongo.db.user.find({'email': email})[0]['weight']
print(current_weight, target_weight, type(user_start_date), datetime.today().strftime('%Y-%m-%d'))

# Find out the actual calories which user needed to burn/gain to achieve goal from the start day
target_calories_to_burn = history_service.total_calories_to_burn(
target_calories_to_burn = u.total_calories_to_burn(
target_weight=int(target_weight), current_weight=int(current_weight))
print(f'########## {target_calories_to_burn}')

# Find out how many calories user has gained or burnt uptill now
calories_till_today = mongo.db.calories.aggregate(
history_service.get_calories_burnt_till_now_pipeline(
email, user_start_date))
query = {
'email': email,
'date': {'$gte': user_start_date, '$lte': datetime.today().strftime('%Y-%m-%d')}
}

entries_till_today_cal = mongo.db.calories.find(query)
entries_till_today_workout = mongo.db.workout.find(query)
current_calories = 0
for calorie in calories_till_today:
current_calories += calorie['SUM']
# current_calories = [x for x in calories_till_today][0]['SUM'] if len(list(calories_till_today)) != 0 else 0
for entry in entries_till_today_cal:
if entry['_id'] == 'Other':
continue
net_calories = int(entry['calories'])
current_calories += net_calories

for entry in entries_till_today_workout:
if entry['_id'] == 'Other':
continue
net_calories = -int(entry['burnout'])
current_calories += net_calories

# Find out no of calories user has to burn/gain in future per day
calories_to_burn = history_service.calories_to_burn(
calories_to_burn = u.calories_to_burn(
target_calories_to_burn,
current_calories,
target_date=datetime.strptime(user_target_date, '%Y-%m-%d'),
Expand Down Expand Up @@ -450,8 +473,8 @@ def bmi_calci():
if request.method == 'POST' and 'weight' in request.form:
weight = float(request.form.get('weight'))
height = float(request.form.get('height'))
bmi = calc_bmi(weight, height)
bmi_category = get_bmi_category(bmi)
bmi = u.calc_bmi(weight, height)
bmi_category = u.get_bmi_category(bmi)

return render_template("bmi_cal.html", bmi=bmi, bmi_category=bmi_category)

Expand All @@ -466,7 +489,7 @@ def chatbot():
def get_bot_response():
userText = request.args.get('msg')
return str(
get_response(chat_history, name, chatgpt_output, userText,
u.get_response(chat_history, name, chatgpt_output, userText,
history_file, impersonated_role, explicit_input))


Expand Down
16 changes: 8 additions & 8 deletions tests/test_history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
23/11 14:31:50 User: How are you?
23/11 14:31:50 Assistant: Mocked response

24/11 12:24:35 User: How are you?
24/11 12:24:35 Assistant: Mocked response
24/11 19:38:05 User: How are you?
24/11 19:38:05 Assistant: Mocked response

24/11 12:28:09 User: How are you?
24/11 12:28:09 Assistant: Mocked response
24/11 19:38:43 User: How are you?
24/11 19:38:43 Assistant: Mocked response

24/11 12:51:51 User: How are you?
24/11 12:51:51 Assistant: Mocked response
24/11 19:39:11 User: How are you?
24/11 19:39:11 Assistant: Mocked response

24/11 15:29:49 User: How are you?
24/11 15:29:49 Assistant: Mocked response
24/11 19:39:51 User: How are you?
24/11 19:39:51 Assistant: Mocked response
2 changes: 1 addition & 1 deletion tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_get_entries_for_email(self):
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 12, 31)

result = get_entries_for_email(mock_db, email, start_date, end_date)
result, [] = get_entries_for_email(mock_db, email, start_date, end_date)

self.assertEqual(result, entries_data)

Expand Down
42 changes: 29 additions & 13 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ def get_response(chat_history, name, chatgpt_output, userText, history_file,
return chat(chat_history, name, chatgpt_output, userText, history_file,
impersonated_role, explicit_input)

def get_entries_for_email(db, email, start_date, end_date):

# Query to find entries for a given email within the date range
query = {
'email': email,
'date': {'$gte': start_date, '$lte': end_date}
}

# Fetch entries from MongoDB
entries = db.calories.find(query)

return list(entries)

def calc_bmi(weight, height):
return round((weight / ((height / 100)**2)), 2)
Expand All @@ -112,4 +100,32 @@ def get_bmi_category(bmi):
elif bmi < 29.9:
return 'Overweight'
else:
return 'Obese'
return 'Obese'

def get_entries_for_email(db, email, start_date, end_date):

# Query to find entries for a given email within the date range
query = {
'email': email,
'date': {'$gte': start_date, '$lte': end_date}
}

# Fetch entries from MongoDB
entries_cal = db.calories.find(query)
entries_workout = db.workout.find(query)

return list(entries_cal), list(entries_workout)

def total_calories_to_burn(target_weight: int, current_weight: int):
return int((target_weight - current_weight) * 7700)


def calories_to_burn(target_calories: int, current_calories: int,
target_date: datetime, start_date: datetime):
actual_current_calories = current_calories - (
(datetime.today() - start_date).days * 2000)

new_target = target_calories - actual_current_calories

days_remaining = (target_date - datetime.today()).days
return int(new_target / days_remaining)
Loading