Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 19 additions & 20 deletions sds_data_manager/lambda_code/IAlirtCode/ialirt_catalog_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ def lambda_handler(event, context) -> dict:
'Content-Type': 'application/json'
},
'body': '{
"2025-02-06":
{"file_names":
["20250206_station1_01.txt", "20250206_station1_02.txt"],
"file_names":
["2025-02-06_station1_01.txt", "2025-02-06_station1_02.txt"],
"dates_modified":
["2025-02-12 18:37", "2025-02-12 18:37"]}
}'
Expand Down Expand Up @@ -96,29 +95,29 @@ def lambda_handler(event, context) -> dict:
config=botocore.client.Config(signature_version="s3v4"),
)

response_body = {}
response_body = {"file_names": [], "dates_modified": []}
while day < end_date:
day_path = f"pointing_schedules/{station}/{day.strftime('%Y%m%d')}/"
day_path = f"pointing_schedules/{station}/{day.strftime('%Y-%m-%d')}"
Comment thread
laspsandoval marked this conversation as resolved.
files = s3_client.list_objects_v2(Bucket=bucket, Prefix=day_path)
# Return all existing files.
if "Contents" not in files.keys():
return {
"statusCode": 404,
"headers": {"Content-Type": "application/json"},
"body": "There are not files associated with the provided date {day}. "
"Please supply a valid time range that is covered by existing "
"files.",
}
file_names = []
dates_modified = []
logger.info(f"No files found for {day.strftime('%Y-%m-%d')}")
day += timedelta(days=1)
continue
for file in files["Contents"]:
file_names.append(file["Key"].rsplit("/", 1)[1])
dates_modified.append(file["LastModified"].strftime("%Y-%m-%d %H:%M"))
response_body[f"{day}"] = {
"file_names": file_names,
"dates_modified": dates_modified,
}
response_body["file_names"].append(file["Key"].rsplit("/", 1)[1])
response_body["dates_modified"].append(
file["LastModified"].strftime("%Y-%m-%d %H:%M")
)
day += timedelta(days=1)

if not response_body["file_names"]:
return {
"statusCode": 404,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"message": "No files found for the requested range."}),
}

return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def generate_and_upload_schedule(bucket: str, region: str, station: str, day: st
Ex: "2025-08-11".
"""
file_name = f"{day}_{station}.txt"
s3_path = f"pointing_schedules/{station}/{day}/{file_name}"
s3_path = f"pointing_schedules/{station}/{file_name}"

output = generate_text_files(station, day)

Expand All @@ -48,10 +48,7 @@ def generate_and_upload_schedule(bucket: str, region: str, station: str, day: st
Key=s3_path,
Body="".join(output),
)
logger.info(
f"Pointing schedule '{day}_{station}.txt' uploaded to "
f"s3://{bucket}/{s3_path}"
)
logger.info(f"Pointing schedule uploaded to s3://{bucket}/{s3_path}")
except Exception as e:
logger.error(f"Error uploading file: {e}")

Expand Down
10 changes: 5 additions & 5 deletions tests/lambda_endpoints/test_ialirt_catalog_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_no_files_ialirt_catalog_api(s3_client):

def test_success_ialirt_catalog_api(s3_client):
"""Test a successful call to the catalog endpoint."""
test_file1 = "pointing_schedules/station1/20250206/20250206_station1_01.txt"
test_file2 = "pointing_schedules/station1/20250206/20250206_station1_02.txt"
test_file1 = "pointing_schedules/station1/2025-02-06_station1_01.txt"
test_file2 = "pointing_schedules/station1/2025-02-06_station1_02.txt"
s3_client.put_object(
Bucket="test-data-bucket",
Key=test_file1,
Expand All @@ -77,7 +77,7 @@ def test_success_ialirt_catalog_api(s3_client):
}
response = ialirt_catalog_api.lambda_handler(event=event, context=None)
assert response["statusCode"] == 200
assert json.loads(response["body"])["2025-02-06 00:00:00"]["file_names"] == [
"20250206_station1_01.txt",
"20250206_station1_02.txt",
assert json.loads(response["body"])["file_names"] == [
"2025-02-06_station1_01.txt",
"2025-02-06_station1_02.txt",
]