From 2be7ff0aae45beaaed32e980fd58a9d9f3f4939f Mon Sep 17 00:00:00 2001
From: adeveloper-wq <dajesch@gmail.com>
Date: Tue, 28 May 2024 14:44:49 +0200
Subject: [PATCH] Fix path to history directory

---
 manager/fetch-traffic-data.py |  6 +++---
 manager/predict-traffic.py    | 14 +++++++-------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/manager/fetch-traffic-data.py b/manager/fetch-traffic-data.py
index 33e46d1..4f5f2be 100644
--- a/manager/fetch-traffic-data.py
+++ b/manager/fetch-traffic-data.py
@@ -13,8 +13,8 @@ def main():
     api_link = f"https://api.hamburg.de/datasets/v1/verkehrslage/collections/verkehrslage/items?&f=json&limit=100&offset=0"
 
     # Create folder for data if it doesn't exist
-    if not os.path.exists('history'):
-        os.makedirs('history')
+    if not os.path.exists('/app/history'):
+        os.makedirs('/app/history')
 
     # Fetch traffic data from API
     traffic_data = []
@@ -82,7 +82,7 @@ def main():
     timestamp = int(time.time())
 
     # save data to json file
-    with open(f"history/{date}-{hour}.json", "w") as outfile:
+    with open(f"/app/history/{date}-{hour}.json", "w") as outfile:
         write = {"trafficflow": sum_score, "timestamp": timestamp, "paths": len(paths)}
         json.dump(write, outfile, indent=4)
 
diff --git a/manager/predict-traffic.py b/manager/predict-traffic.py
index 4355bef..278ca33 100644
--- a/manager/predict-traffic.py
+++ b/manager/predict-traffic.py
@@ -24,7 +24,7 @@ def main(prediction_path):
     global date_now, weekday_now
 
     # Get files in the history folder
-    files = os.listdir("history")
+    files = os.listdir("/app/history")
     files = [file for file in files if file.endswith(".json")]
     key = lambda x: int(time.mktime(time.strptime(x, "%d.%m.%Y-%H:%M.json")))
     files.sort(key=key, reverse=True)
@@ -66,7 +66,7 @@ def main(prediction_path):
         prediction.update({"quality_" + str(hour_to_check): None})
 
     # Get the current score by reading the first file (which is the newest one, because the list is sorted)
-    with open(f"history/{files[0]}", "r") as file:
+    with open(f"/app/history/{files[0]}", "r") as file:
         data = json.load(file)
         score_now = data["trafficflow"]
         prediction.update({"now": score_now})
@@ -90,7 +90,7 @@ def prune_old_files(files):
 
         # Delete files old files
         if timestamp < time_oldest:
-            os.remove(f"history/{filename}")
+            os.remove(f"/app/history/{filename}")
             removed.append(filename)
 
     for filename in removed:
@@ -114,7 +114,7 @@ def use_same_day(check_hour, files):
 
         # If we have data for the given hour and same day of the week
         if data_hour == check_hour and data_day == weekday_now and data_date != date_now:
-            with open(f"history/{filename}", "r") as file:
+            with open(f"/app/history/{filename}", "r") as file:
                 data = json.load(file)
                 score = data["trafficflow"]
                 scores.append(score)
@@ -142,7 +142,7 @@ def use_weekday_or_weekend(check_hour, files):
         if weekday_now <= 5:
             # workweek
             if data_hour == check_hour and data_day <= 5:
-                with open(f"history/{filename}",
+                with open(f"/app/history/{filename}",
                           "r") as file:
                     data = json.load(file)
                     score = data["trafficflow"]
@@ -151,7 +151,7 @@ def use_weekday_or_weekend(check_hour, files):
         else:
             # weekend
             if data_hour == check_hour and data_day > 5:
-                with open(f"history/{filename}",
+                with open(f"/app/history/{filename}",
                           "r") as file:
                     data = json.load(file)
                     score = data["trafficflow"]
@@ -176,7 +176,7 @@ def use_same_hour(check_hour, files):
 
         # If there is not enough data, get the average score for the given hour
         if data_hour == check_hour:
-            with open(f"history/{filename}", "r") as file:
+            with open(f"/app/history/{filename}", "r") as file:
                 data = json.load(file)
                 score = data["trafficflow"]
                 scores.append(score)