Skip to content

Commit

Permalink
return cached values
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate-Wessel committed Dec 13, 2024
1 parent e64957f commit 819264c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
21 changes: 21 additions & 0 deletions backend/app/get_travel_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 (
Expand Down
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

0 comments on commit 819264c

Please sign in to comment.