Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Fix path to history directory
Browse files Browse the repository at this point in the history
  • Loading branch information
adeveloper-wq committed May 28, 2024
1 parent 16b31b1 commit 2be7ff0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions manager/fetch-traffic-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 7 additions & 7 deletions manager/predict-traffic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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"]
Expand All @@ -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"]
Expand All @@ -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)
Expand Down

0 comments on commit 2be7ff0

Please sign in to comment.