Skip to content

Commit

Permalink
Revamped the graph-feature code for proper functioning
Browse files Browse the repository at this point in the history
  • Loading branch information
rishi2019194 committed Nov 25, 2023
1 parent 796f308 commit aed0b9a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 87 deletions.
13 changes: 5 additions & 8 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 @@ -296,7 +295,7 @@ def history():

# Find out the last 7 day's calories burnt by the user
calorie_day_map = {}
entries_cal, entries_workout = get_entries_for_email(mongo.db, email, (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'), datetime.today().strftime('%Y-%m-%d'))
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':
Expand All @@ -318,13 +317,11 @@ def history():
else:
calorie_day_map[curr_date] += net_calories
calorie_day_map = dict(sorted(calorie_day_map.items()))
print(calorie_day_map)

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

# The first day when the user registered or started using the app
user_start_date = mongo.db.user.find({'email': email})[0]['start_date']
Expand All @@ -334,7 +331,7 @@ def history():
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}')

Expand All @@ -360,7 +357,7 @@ def history():
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 @@ -492,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
123 changes: 55 additions & 68 deletions service/history.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,57 @@
from datetime import datetime, timedelta
# from datetime import datetime, timedelta


# def get_calories_per_day_pipeline(days: int):
# start_date = (datetime.today() - timedelta(days=days))
# end_date = datetime.today()
# bucket_boundaries = [(start_date + timedelta(days=i)).strftime('%Y-%m-%d')
# for i in range(days + 1)]
# print(bucket_boundaries)
# date_range_filter = {
# '$match': {
# 'date': {
# '$gte': start_date.strftime('%Y-%m-%d'),
# '$lte': end_date.strftime('%Y-%m-%d')
# },
# }
# }
# print(date_range_filter)
# total_calories_each_day = {
# '$bucket': {
# 'groupBy': '$date',
# 'boundaries': bucket_boundaries,
# 'default': 'Other',
# 'output': {
# 'date': {
# '$max': '$date'
# },
# 'total_calories': {
# '$sum': '$calories'
# }
# }
# }
# }
# print(total_calories_each_day)
# return [date_range_filter, total_calories_each_day]


# def get_calories_burnt_till_now_pipeline(email: str, start_date: str):
# end_date = datetime.today().strftime('%Y-%m-%d')
# return [{
# '$match': {
# 'date': {
# '$gte': start_date,
# '$lte': end_date
# },
# 'email': email
# }
# }, {
# '$group': {
# '_id': 'sum of calories',
# 'SUM': {
# '$sum': '$calories'
# }
# }
# }]


def get_calories_per_day_pipeline(days: int):
start_date = (datetime.today() - timedelta(days=days))
end_date = datetime.today()
bucket_boundaries = [(start_date + timedelta(days=i)).strftime('%Y-%m-%d')
for i in range(days + 1)]
print(bucket_boundaries)
date_range_filter = {
'$match': {
'date': {
'$gte': start_date.strftime('%Y-%m-%d'),
'$lte': end_date.strftime('%Y-%m-%d')
},
}
}
print(date_range_filter)
total_calories_each_day = {
'$bucket': {
'groupBy': '$date',
'boundaries': bucket_boundaries,
'default': 'Other',
'output': {
'date': {
'$max': '$date'
},
'total_calories': {
'$sum': '$calories'
}
}
}
}
print(total_calories_each_day)
return [date_range_filter, total_calories_each_day]


def get_calories_burnt_till_now_pipeline(email: str, start_date: str):
end_date = datetime.today().strftime('%Y-%m-%d')
return [{
'$match': {
'date': {
'$gte': start_date,
'$lte': end_date
},
'email': email
}
}, {
'$group': {
'_id': 'sum of calories',
'SUM': {
'$sum': '$calories'
}
}
}]


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)
34 changes: 23 additions & 11 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ 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 calc_bmi(weight, height):
return round((weight / ((height / 100)**2)), 2)


def get_bmi_category(bmi):
if bmi < 18.5:
return 'Underweight'
elif bmi < 24.9:
return 'Normal Weight'
elif bmi < 29.9:
return 'Overweight'
else:
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
Expand All @@ -104,16 +119,13 @@ def get_entries_for_email(db, email, start_date, end_date):
def total_calories_to_burn(target_weight: int, current_weight: int):
return int((target_weight - current_weight) * 7700)

def calc_bmi(weight, height):
return round((weight / ((height / 100)**2)), 2)

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)

def get_bmi_category(bmi):
if bmi < 18.5:
return 'Underweight'
elif bmi < 24.9:
return 'Normal Weight'
elif bmi < 29.9:
return 'Overweight'
else:
return 'Obese'
new_target = target_calories - actual_current_calories

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

0 comments on commit aed0b9a

Please sign in to comment.