Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalization 2 #29

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/normalize/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,19 @@ def parse_files(s3, s3_fs, source_prefix, destination_prefix, start_date, state_
str(cur_processing.day),
)
LOGGER.info(f"Processing date: {date_partition}")
max_timestamp = cur_processing.timestamp()
max_epoch_timestamp = cur_processing.timestamp()

trip_updates_pa, vehicles_pa, alerts_pa = None, None, None
for page in paginator.paginate(Bucket=source_bucket, Prefix=date_partition, PaginationConfig={'PageSize': 10}):
trip_updates_pa = None
vehicles_pa = None
alerts_pa = None

for page in paginator.paginate(Bucket=source_bucket, Prefix=date_partition, PaginationConfig={'PageSize': 60}):
for obj in page.get("Contents", []):
key = obj["Key"]
if key.endswith(".binpb"):
# Check if this file is newer than the last processed file
file_write_time = float(key.split("/")[-1].removesuffix(".binpb"))
if file_write_time > cur_processing.timestamp():
file_write_epoch_time = float(key.split("/")[-1].removesuffix(".binpb"))
if file_write_epoch_time > cur_processing.timestamp():
LOGGER.info(f"Processing file: {key}")
# Download the file
response = s3.get_object(Bucket=source_bucket, Key=key)
Expand Down Expand Up @@ -150,32 +152,44 @@ def parse_files(s3, s3_fs, source_prefix, destination_prefix, start_date, state_
else cur_alerts_pa
)

max_timestamp = max(max_timestamp, file_write_time)
max_epoch_timestamp = max(max_epoch_timestamp, file_write_epoch_time)

if trip_updates_pa:
s3_uri = f"{destination_prefix}/trip-updates"
LOGGER.info(f"Writing {trip_updates_pa.num_rows} entries to {s3_uri}")
time_range = pa.compute.min_max(trip_updates_pa['time'])
LOGGER.info(
f"Writing {trip_updates_pa.num_rows} entries to {s3_uri}. "
f"Min timestamp {time_range['min']}, max timestamp {time_range['max']}"
)
write_data(s3_fs, trip_updates_pa, s3_uri)
if vehicles_pa:
s3_uri = f"{destination_prefix}/vehicles"
LOGGER.info(f"Writing {vehicles_pa.num_rows} entries to {s3_uri}")
time_range = pa.compute.min_max(vehicles_pa['time'])
LOGGER.info(
f"Writing {vehicles_pa.num_rows} entries to {s3_uri}. "
f"Min timestamp {time_range['min']}, max timestamp {time_range['max']}"
)
write_data(s3_fs, vehicles_pa, s3_uri)
if alerts_pa:
s3_uri = f"{destination_prefix}/alerts"
LOGGER.info(f"Writing {alerts_pa.num_rows} entries to {s3_uri}")
time_range = pa.compute.min_max(alerts_pa['time'])
LOGGER.info(
f"Writing {alerts_pa.num_rows} entries to {s3_uri}. "
f"Min timestamp {time_range['min']}, max timestamp {time_range['max']}"
)
write_data(s3_fs, alerts_pa, s3_uri)

# Update the last processed timestamp
if max_timestamp == last_processed:
if max_epoch_timestamp == last_processed:
LOGGER.warning(
f"No data found in partition: {date_partition} "
f"- is this expected?"
)
LOGGER.info(
f"Updating last processed timestamp to "
f"maximum file timestamp: {dt.datetime.utcfromtimestamp(max_timestamp).isoformat()}"
f"maximum file timestamp: {dt.datetime.utcfromtimestamp(max_epoch_timestamp).isoformat()}"
)
update_last_processed_timestamp(s3, state_bucket, state_key, max_timestamp)
update_last_processed_timestamp(s3, state_bucket, state_key, max_epoch_timestamp)
cur_processing = (cur_processing + dt.timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)


Expand Down
Loading