From aed0b9a6146cf982209559441c8010d0a94f51bd Mon Sep 17 00:00:00 2001 From: RISHI SINGHAL Date: Fri, 24 Nov 2023 19:23:18 -0500 Subject: [PATCH] Revamped the graph-feature code for proper functioning --- application.py | 13 ++--- service/history.py | 123 ++++++++++++++++++++------------------------- utilities.py | 34 +++++++++---- 3 files changed, 83 insertions(+), 87 deletions(-) diff --git a/application.py b/application.py index adca5590..2307abe2 100644 --- a/application.py +++ b/application.py @@ -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 @@ -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': @@ -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'] @@ -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}') @@ -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'), @@ -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)) diff --git a/service/history.py b/service/history.py index 1cf9b8aa..7f054e6d 100644 --- a/service/history.py +++ b/service/history.py @@ -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) diff --git a/utilities.py b/utilities.py index ae5ebe7b..a2f038ff 100644 --- a/utilities.py +++ b/utilities.py @@ -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 @@ -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' \ No newline at end of file + new_target = target_calories - actual_current_calories + + days_remaining = (target_date - datetime.today()).days + return int(new_target / days_remaining) \ No newline at end of file