Skip to content

Commit

Permalink
allow function to default to NOW
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate-Wessel committed Jan 10, 2025
1 parent 9ef6f96 commit 217f82d
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions backend/app/selectMapVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@
# Selects Here map version to use based on dates in the provided query.
# however the query also has nodes which may or may not be version-specific

def selectMapVersion(start_date, end_date):
query = """
WITH coverage AS (
SELECT
street_version,
valid_range * daterange(%(start_date)s, %(end_date)s,'[)') AS overlap
FROM here.street_valid_range
)
rangeQuery = """
WITH coverage AS (
SELECT
street_version,
UPPER(overlap) - LOWER(overlap) AS days_covered
FROM coverage
WHERE UPPER(overlap) - LOWER(overlap) IS NOT NULL
ORDER BY days_covered DESC NULLS LAST
"""
valid_range * daterange(%(start_date)s, %(end_date)s,'[)') AS overlap
FROM here.street_valid_range
)
SELECT street_version
FROM coverage
WHERE UPPER(overlap) - LOWER(overlap) IS NOT NULL
ORDER BY UPPER(overlap) - LOWER(overlap) DESC NULLS LAST;
"""

nowQuery = """
SELECT street_version
FROM here.street_valid_range
WHERE valid_range @> NOW()::date;
"""

def selectMapVersion(start_date=None, end_date=None):
connection = getConnection()
with connection:
with connection.cursor() as cursor:
cursor.execute(query,{'start_date':start_date,'end_date':end_date})
(map_version, coverage) = cursor.fetchone()
# a daterange has been provided
cursor.execute(
rangeQuery if start_date and end_date else nowQuery,
{'start_date':start_date,'end_date':end_date}
)
(map_version) = cursor.fetchone()
connection.close()
return map_version
return map_version

0 comments on commit 217f82d

Please sign in to comment.