From 819264cb7be601ec4bbd8682bfb5c15b5a7adf85 Mon Sep 17 00:00:00 2001 From: Nate-Wessel Date: Fri, 13 Dec 2024 22:08:38 +0000 Subject: [PATCH] return cached values --- backend/app/get_travel_time.py | 21 +++++++++++++++++++++ backend/app/routes.py | 7 +------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/backend/app/get_travel_time.py b/backend/app/get_travel_time.py index 47c2072..824bac3 100644 --- a/backend/app/get_travel_time.py +++ b/backend/app/get_travel_time.py @@ -8,6 +8,7 @@ import math import pandas import random +from app.getGitHash import getGitHash # the way we currently do it def mean_daily_mean(obs): @@ -20,9 +21,29 @@ def mean_daily_mean(obs): # average the days together return numpy.mean(daily_means) +def checkCache(uri,hash): + query = f''' + SELECT results + FROM nwessel.cached_travel_times + WHERE uri_string = %(uri)s AND commit_hash = %(hash)s + ''' + connection = getConnection() + with connection: + with connection.cursor() as cursor: + cursor.execute(query, {'uri': uri, 'hash': hash}) + for (record,) in cursor: # will skip if no records + return record # there could only be one + def get_travel_time(start_node, end_node, start_time, end_time, start_date, end_date, include_holidays, dow_list): """Function for returning data from the aggregate-travel-times/ endpoint""" + # first check the cache + cacheURI = f'/{start_node}/{end_node}/{start_time}/{end_time}/{start_date}/{end_date}/{str(include_holidays).lower()}/{"".join(map(str,dow_list))}' + cachedValue = checkCache(cacheURI,getGitHash()) + + if cachedValue: + return cachedValue + holiday_clause = '' if not include_holidays: holiday_clause = '''AND NOT EXISTS ( diff --git a/backend/app/routes.py b/backend/app/routes.py index 2c9a4b4..346df02 100644 --- a/backend/app/routes.py +++ b/backend/app/routes.py @@ -116,13 +116,8 @@ def get_here_links_between_two_nodes(network, from_node_id, to_node_id): }) - - # test URL /aggregate-travel-times/30310940/30310942/9/12/2020-05-01/2020-06-01/true/2 -@app.route( - '/aggregate-travel-times////////', - methods=['GET'] -) +@app.route('/aggregate-travel-times////////') def aggregate_travel_times(start_node, end_node, start_time, end_time, start_date, end_date, include_holidays, dow_str): """ Return averaged travel times given the specified parameters.