From 6fc11be24997c0ed8c61239c41bb88b086651ae2 Mon Sep 17 00:00:00 2001 From: Nate Wessel Date: Tue, 17 Dec 2024 14:56:21 -0500 Subject: [PATCH] Cache travel time results (#171) * table for storing cached times temporarily in personal schema for ease of testing * store the whole results object trading space for speed in setting this up, for now * return cached values * set up for caching * store cache * try all table interactions just in case something ever breaks, I don't want to be dependent on caching --- backend/app/get_travel_time.py | 45 +++++++++++++++++++++++++--- backend/app/routes.py | 7 +---- backend/tables/travel_time_cache.sql | 8 +++++ 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 backend/tables/travel_time_cache.sql diff --git a/backend/app/get_travel_time.py b/backend/app/get_travel_time.py index 47c2072..67e249f 100644 --- a/backend/app/get_travel_time.py +++ b/backend/app/get_travel_time.py @@ -8,6 +8,8 @@ import math import pandas import random +import json +from app.getGitHash import getGitHash # the way we currently do it def mean_daily_mean(obs): @@ -20,9 +22,44 @@ def mean_daily_mean(obs): # average the days together return numpy.mean(daily_means) +def checkCache(uri): + 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: + try: + cursor.execute(query, {'uri': uri, 'hash': getGitHash()}) + for (record,) in cursor: # will skip if no records + return record # there could only be one + except: + pass + +def cacheAndReturn(obj,uri): + query = f''' + INSERT INTO nwessel.cached_travel_times (uri_string, commit_hash, results) + VALUES (%(uri)s, %(hash)s, %(results)s) + ''' + connection = getConnection() + with connection: + with connection.cursor() as cursor: + try: + cursor.execute(query, {'uri': uri, 'hash': getGitHash(), 'results': json.dumps(obj)}) + finally: + return obj + 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) + if cachedValue: + return cachedValue + holiday_clause = '' if not include_holidays: holiday_clause = '''AND NOT EXISTS ( @@ -112,7 +149,7 @@ def get_travel_time(start_node, end_node, start_time, end_time, start_date, end_ if len(sample) < 1: # no travel times or related info to return here - return { + return cacheAndReturn({ 'results': { 'travel_time': None, 'observations': [], @@ -124,7 +161,7 @@ def get_travel_time(start_node, end_node, start_time, end_time, start_date, end_ 'corridor': {'links': links, 'map_version': map_version}, 'query_params': query_params } - } + }, cacheURI) tt_seconds = mean_daily_mean(sample) @@ -143,7 +180,7 @@ def get_travel_time(start_node, end_node, start_time, end_time, start_date, end_ } } - return { + return cacheAndReturn({ 'results': { 'travel_time': timeFormats(tt_seconds,1), 'confidence': { @@ -156,4 +193,4 @@ def get_travel_time(start_node, end_node, start_time, end_time, start_date, end_ 'corridor': {'links': links, 'map_version': map_version}, 'query_params': query_params } - } + },cacheURI) 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. diff --git a/backend/tables/travel_time_cache.sql b/backend/tables/travel_time_cache.sql new file mode 100644 index 0000000..a030f0e --- /dev/null +++ b/backend/tables/travel_time_cache.sql @@ -0,0 +1,8 @@ +CREATE TABLE nwessel.cached_travel_times ( + uri_string text CHECK(uri_string ~ '^\/\d+\/\d+\/\d{1,2}\/\d{1,2}\/\d{4}-\d{2}-\d{2}\/\d{4}-\d{2}-\d{2}\/(true|false)\/[1-7]{1,7}$'), + commit_hash text, + results jsonb NOT NULL, + PRIMARY KEY (uri_string, commit_hash) +); + +GRANT SELECT, INSERT ON nwessel.cached_travel_times TO tt_request_bot;