Skip to content

Commit

Permalink
Cache travel time results (#171)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nate-Wessel authored Dec 17, 2024
1 parent 25296c8 commit 6fc11be
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
45 changes: 41 additions & 4 deletions backend/app/get_travel_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 (
Expand Down Expand Up @@ -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': [],
Expand All @@ -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)

Expand All @@ -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': {
Expand All @@ -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)
7 changes: 1 addition & 6 deletions backend/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<start_node>/<end_node>/<start_time>/<end_time>/<start_date>/<end_date>/<include_holidays>/<dow_str>',
methods=['GET']
)
@app.route('/aggregate-travel-times/<start_node>/<end_node>/<start_time>/<end_time>/<start_date>/<end_date>/<include_holidays>/<dow_str>')
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.
Expand Down
8 changes: 8 additions & 0 deletions backend/tables/travel_time_cache.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 6fc11be

Please sign in to comment.