Skip to content

Commit

Permalink
move iso_range_to_ts_range to datetime_utils.py
Browse files Browse the repository at this point in the history
This function is used by db_utils but might be better categorized in datetime_utils.
Previously called`get_ts_range`, its new name is `iso_range_to_ts_range`, since I thought that was more descriptive.

-- Also, I noticed `arrow` was imported in db_utils twice, so i fixed that while I was here
  • Loading branch information
JGreenlee committed Feb 5, 2024
1 parent 0897cf9 commit 719199a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
27 changes: 26 additions & 1 deletion utils/datetime_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import arrow

MAX_EPOCH_TIME = 2 ** 31 - 1


def iso_range_to_ts_range(start_date: str, end_date: str, tz: str):
"""
Returns a tuple of (start_ts, end_ts) as epoch timestamps, given start_date and end_date in
ISO format and the timezone mode in which the dates should be resolved to timestamps ('utc' or 'local')
"""
start_ts, end_ts = None, MAX_EPOCH_TIME
if start_date is not None:
if tz == 'utc':
start_ts = arrow.get(start_date).timestamp()
elif tz == 'local':
start_ts = arrow.get(start_date, tzinfo='local').timestamp()
if end_date is not None:
if tz == 'utc':
end_ts = arrow.get(end_date).replace(
hour=23, minute=59, second=59).timestamp()
elif tz == 'local':
end_ts = arrow.get(end_date, tzinfo='local').replace(
hour=23, minute=59, second=59).timestamp()
return (start_ts, end_ts)


def iso_to_date_only(*iso_strs: str):
"""
For each ISO date string in the input, returns only the date part in the format 'YYYY-MM-DD'
e.g. '2021-01-01T00:00:00.000Z' -> '2021-01-01'
"""
return [iso_str[:10] if iso_str else None for iso_str in iso_strs]

27 changes: 3 additions & 24 deletions utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import arrow
from uuid import UUID

import arrow

import pandas as pd
import pymongo

Expand All @@ -15,26 +13,7 @@

from utils import constants
from utils import permissions as perm_utils

MAX_EPOCH_TIME = 2 ** 31 - 1

def get_ts_range(start_date: str, end_date: str, tz: str):
"""
Returns a tuple of (start_ts, end_ts) as timestamps, given start_date and end_date in ISO format
and the timezone mode in which the dates should be resolved to timestamps ('utc' or 'local')
"""
start_ts, end_ts = None, MAX_EPOCH_TIME
if start_date is not None:
if tz == 'utc':
start_ts = arrow.get(start_date).timestamp()
elif tz == 'local':
start_ts = arrow.get(start_date, tzinfo='local').timestamp()
if end_date is not None:
if tz == 'utc':
end_ts = arrow.get(end_date).replace(hour=23, minute=59, second=59).timestamp()
elif tz == 'local':
end_ts = arrow.get(end_date, tzinfo='local').replace(hour=23, minute=59, second=59).timestamp()
return (start_ts, end_ts)
from utils.datetime_utils import iso_range_to_ts_range

def query_uuids(start_date: str, end_date: str, tz: str):
# As of now, time filtering does not apply to UUIDs; we just query all of them.
Expand Down Expand Up @@ -74,7 +53,7 @@ def query_uuids(start_date: str, end_date: str, tz: str):
return df

def query_confirmed_trips(start_date: str, end_date: str, tz: str):
(start_ts, end_ts) = get_ts_range(start_date, end_date, tz)
(start_ts, end_ts) = iso_range_to_ts_range(start_date, end_date, tz)
ts = esta.TimeSeries.get_aggregate_time_series()
# Note to self, allow end_ts to also be null in the timequery
# we can then remove the start_time, end_time logic
Expand Down Expand Up @@ -157,7 +136,7 @@ def query_demographics():

def query_trajectories(start_date: str, end_date: str, tz: str):

(start_ts, end_ts) = get_ts_range(start_date, end_date, tz)
(start_ts, end_ts) = iso_range_to_ts_range(start_date, end_date, tz)
ts = esta.TimeSeries.get_aggregate_time_series()
entries = ts.find_entries(
key_list=["analysis/recreated_location"],
Expand Down

0 comments on commit 719199a

Please sign in to comment.