diff --git a/api-front/.vscode/settings.json b/api-front/.vscode/settings.json new file mode 100644 index 0000000..3e99ede --- /dev/null +++ b/api-front/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/api-front/__pycache__/main.cpython-311.pyc b/api-front/__pycache__/main.cpython-311.pyc new file mode 100644 index 0000000..b4823f6 Binary files /dev/null and b/api-front/__pycache__/main.cpython-311.pyc differ diff --git a/api-front/lasso_model.pkl b/api-front/lasso_model.pkl new file mode 100644 index 0000000..8d7f577 Binary files /dev/null and b/api-front/lasso_model.pkl differ diff --git a/api-front/main.py b/api-front/main.py new file mode 100644 index 0000000..71d1327 --- /dev/null +++ b/api-front/main.py @@ -0,0 +1,124 @@ +from fastapi import FastAPI +import pickle +from pydantic import BaseModel +import numpy as np +import joblib +from datetime import datetime, timedelta +from fastapi.middleware.cors import CORSMiddleware +import psycopg2 + +# model_lasso +lasso_model = joblib.load('lasso_model.pkl') +class PredictionRequest(BaseModel): + day: int + month: int + yesterday_price: float + forecast_days: int + + + +# api +app = FastAPI() + +origins = [ + "http://localhost", + "http://localhost:4200", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +def connexion(): + try: + conn = psycopg2.connect( + host="localhost", + port = "5432", + database="postgres", + user="postgres", + password="4242" + ) + except psycopg2.Error as e: + return {"error": f"Failed to connect to database: {str(e)}"} + return conn + + +@app.post("/predict") +def predict(data: PredictionRequest): + day = data.day + month = data.month + yesterday_price = data.yesterday_price + forecast_days = data.forecast_days + + # Connect to the PostgreSQL database + conn=connexion() + + # Create the table if it doesn't exist + with conn: + with conn.cursor() as cur: + cur.execute(""" + CREATE TABLE IF NOT EXISTS predictions ( + id SERIAL PRIMARY KEY, + date DATE NOT NULL, + prediction FLOAT NOT NULL + ) + """) + + predictions = [] + current_day = day + current_month = month + current_price = yesterday_price + + for _ in range(forecast_days): + # Make a prediction using the current inputs + prediction = lasso_model.predict([[current_day, current_month, current_price]]) + predictions.append({"date": datetime(year=2023, month=current_month, day=current_day).strftime("%d/%m/%Y"), "prediction": prediction[0]}) + + # Insert the prediction into the PostgreSQL database + with conn: + with conn.cursor() as cur: + try: + cur.execute(""" + INSERT INTO postgres (date, prediction) VALUES (%s, %s) + """, (datetime(year=2023, month=current_month, day=current_day), prediction[0])) + except psycopg2.Error as e: + return {"error": f"Failed to insert data into database: {str(e)}"} + + + # Update the inputs + current_date = datetime(year=2023, month=current_month, day=current_day) + next_date = current_date + timedelta(days=1) + current_day = next_date.day + current_month = next_date.month + current_price = prediction[0] + + # Close the database connection + conn.close() + +@app.get("/get_predict") +def get_predict(): + conn = connexion() + cur = conn.cursor() + + # Execute a SELECT query to retrieve all rows from the 'predictions' table + cur.execute("SELECT * FROM predictions") + + # Fetch all rows and print them + rows = cur.fetchall() + # cur.close() + # conn.close() + + return rows + +# # Close the cursor and database connection + +if __name__ == '__main__': + import uvicorn + uvicorn.run(app, host='localhost', port=5000) + # python -m uvicorn main:app --reload + + \ No newline at end of file diff --git a/api-front/query.py b/api-front/query.py new file mode 100644 index 0000000..b1691d9 --- /dev/null +++ b/api-front/query.py @@ -0,0 +1,25 @@ +import psycopg2 + +# Connect to the PostgreSQL database +conn = psycopg2.connect( + host="localhost", + port = "5432", + database="postgres", + user="postgres", + password="4242" +) + +# Create a cursor object to execute queries +cur = conn.cursor() + +# Execute a SELECT query to retrieve all rows from the 'predictions' table +cur.execute("SELECT * FROM postgres") + +# Fetch all rows and print them +rows = cur.fetchall() +for row in rows: + print(row) + +# Close the cursor and database connection +cur.close() +conn.close() \ No newline at end of file diff --git a/api_model/lasso_model.pkl b/api_model/lasso_model.pkl deleted file mode 100644 index e69de29..0000000 diff --git a/api_model/main.py b/api_model/main.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/line-chart/line-chart.component.ts b/src/app/line-chart/line-chart.component.ts index b64931f..da0942e 100644 --- a/src/app/line-chart/line-chart.component.ts +++ b/src/app/line-chart/line-chart.component.ts @@ -28,10 +28,10 @@ export class LineChartComponent { const input = { - "day": 15, + "day": 18, "month": 5, "yesterday_price": 27639, - "forecast_days": 10 + "forecast_days": 5 }