-
Notifications
You must be signed in to change notification settings - Fork 888
backend #43
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
base: main
Are you sure you want to change the base?
backend #43
Changes from all commits
994316a
dfc4527
c5e6e75
83dce4e
ca483db
cac3366
f0ef794
4c31da6
bb27e96
4d93ea5
5b51888
4ee3a40
63a900c
3161700
c566a90
9a4ee21
146ca12
ed119f5
6a54f82
d1a8cfc
7f2660d
189a438
c49d814
38359fa
d840787
fc71f49
ecabb0c
f652371
a1ee154
6501d70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pandas as pd | ||
from datetime import datetime, timedelta | ||
|
||
SAPLING_COST = 50 | ||
FERTILIZER_COST_PER_KG = 0.5 | ||
CYCLE_MONTHS = 12 | ||
|
||
# Load your data once | ||
DATA = pd.read_csv('C:/Users/Niranjan S/Useless Projects/Data Models/banana_growth_kerala_updated.csv') | ||
|
||
def simulate_growth(start_date_str): | ||
current_date = datetime.today() | ||
planting_date = datetime.strptime(start_date_str, "%Y-%m-%d") | ||
|
||
current_trees = 1 | ||
total_profit = 0 | ||
history = [] | ||
|
||
date = planting_date | ||
while date <= current_date: | ||
year = date.year | ||
month = date.month | ||
|
||
row = DATA[(DATA["Year"] == year) & (DATA["Month"] == month)] | ||
if row.empty: | ||
break | ||
|
||
row = row.iloc[0] | ||
yield_per_tree = row["Yield_per_Tree_kg"] | ||
market_price = row["Market_Price_per_kg"] | ||
fertilizer_kg = row["Fertilizer_Used_kg"] | ||
suckers_per_tree = row["Suckers_per_Tree"] | ||
|
||
income = current_trees * yield_per_tree * market_price | ||
fertilizer_cost = fertilizer_kg * current_trees * FERTILIZER_COST_PER_KG | ||
profit = income - fertilizer_cost | ||
|
||
suckers_generated = int(current_trees * suckers_per_tree) | ||
saplings_from_profit = int(profit // SAPLING_COST) | ||
|
||
new_trees = suckers_generated + saplings_from_profit | ||
total_profit += profit | ||
|
||
history.append({ | ||
"Cycle": date.strftime("%Y-%m"), | ||
"Trees": current_trees, | ||
"Income": round(income), | ||
"Profit": round(profit), | ||
"Total_Profit": round(total_profit), | ||
"New_Trees": new_trees | ||
}) | ||
|
||
current_trees = new_trees | ||
date += timedelta(days=30 * CYCLE_MONTHS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use accurate date arithmetic. Same issue as simulation2.py - inaccurate month calculation. - date += timedelta(days=30 * CYCLE_MONTHS)
+ # Use proper month arithmetic
+ from dateutil.relativedelta import relativedelta
+ date += relativedelta(months=1)
🤖 Prompt for AI Agents
|
||
|
||
return { | ||
"profit": round(total_profit), | ||
"history": history | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||||||||||||||||||||||||||
from flask import Flask, request, jsonify | ||||||||||||||||||||||||||||||
from flask_cors import CORS | ||||||||||||||||||||||||||||||
from Simulation import simulate_growth | ||||||||||||||||||||||||||||||
import traceback # Add this | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
app = Flask(__name__) | ||||||||||||||||||||||||||||||
CORS(app) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@app.route("/") | ||||||||||||||||||||||||||||||
def home(): | ||||||||||||||||||||||||||||||
return "✅ Banana Simulation API is running. Use /simulate?date=YYYY-MM-DD" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@app.route("/simulate", methods=["GET"]) | ||||||||||||||||||||||||||||||
def simulate_route(): | ||||||||||||||||||||||||||||||
start_date = request.args.get('date') | ||||||||||||||||||||||||||||||
if not start_date: | ||||||||||||||||||||||||||||||
return jsonify({"error": "Missing date parameter"}), 400 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add the same input validation as in app2.py. Consider adding date format validation for consistency with app2.py. def simulate():
start_date = request.args.get('date')
if not start_date:
return jsonify({"error": "Missing date parameter"}), 400
+
+ try:
+ from datetime import datetime
+ datetime.strptime(start_date, "%Y-%m-%d")
+ except ValueError:
+ return jsonify({"error": "Invalid date format. Use YYYY-MM-DD"}), 400 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||
result = simulate_growth(start_date) | ||||||||||||||||||||||||||||||
return jsonify(result) | ||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||
traceback.print_exc() # 🔍 This shows the actual error in the console | ||||||||||||||||||||||||||||||
return jsonify({"error": str(e)}), 500 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||||||||||
app.run(debug=True, host="0.0.0.0", port=5000) | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||||||||||||||||||||||||||||
from flask import Flask, request, jsonify | ||||||||||||||||||||||||||||||||
from flask_cors import CORS | ||||||||||||||||||||||||||||||||
from simulation2 import simulate_growth | ||||||||||||||||||||||||||||||||
import traceback | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
app = Flask(__name__) | ||||||||||||||||||||||||||||||||
CORS(app) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@app.route("/") | ||||||||||||||||||||||||||||||||
def home(): | ||||||||||||||||||||||||||||||||
return "✅ Banana & Child Simulation API is running. Use /simulate?date=YYYY-MM-DD" | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@app.route("/simulate", methods=["GET"]) | ||||||||||||||||||||||||||||||||
def simulate_route(): | ||||||||||||||||||||||||||||||||
start_date = request.args.get('date') | ||||||||||||||||||||||||||||||||
if not start_date: | ||||||||||||||||||||||||||||||||
return jsonify({"error": "Missing date parameter"}), 400 | ||||||||||||||||||||||||||||||||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add input validation for date parameter. Currently only checks if the date parameter exists. Consider validating the date format to provide better error messages. def simulate_route():
start_date = request.args.get('date')
if not start_date:
return jsonify({"error": "Missing date parameter"}), 400
+
+ # Validate date format
+ try:
+ datetime.strptime(start_date, "%Y-%m-%d")
+ except ValueError:
+ return jsonify({"error": "Invalid date format. Use YYYY-MM-DD"}), 400 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||
result = simulate_growth(start_date) | ||||||||||||||||||||||||||||||||
return jsonify(result) | ||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||
traceback.print_exc() | ||||||||||||||||||||||||||||||||
return jsonify({"error": str(e)}), 500 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||||||||||||
app.run(debug=True, host="0.0.0.0", port=5000) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[LocalizedFileNames] | ||
Screenshot 2025-08-02 164959.png=@Screenshot 2025-08-02 164959,0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace hardcoded file path with configurable path.
Same issue as in simulation2.py - hardcoded absolute paths make the code non-portable.
📝 Committable suggestion
🤖 Prompt for AI Agents