Skip to content

Commit

Permalink
Updates based on discussion in PR #6
Browse files Browse the repository at this point in the history
* Add comments to `cli.main` for readability
* Reorder `cli.main` for readability
* Update logging in SSMClient methods
  • Loading branch information
ehanson8 committed Nov 26, 2024
1 parent 35e0fc3 commit b25a434
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
88 changes: 44 additions & 44 deletions asati/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ def main(*, verbose: bool) -> None:
logger.info(configure_sentry())
logger.info("Running process")

# Get last accession ID from SSM parameter
ssm_client = SSMClient()
last_accession_parameter = os.environ["LAST_ACCESSION_PARAMETER"]
last_accession_uri_id = ssm_client.get_parameter(last_accession_parameter)
logger.info(
f"SSM parameter '{last_accession_parameter}' retrieved: '{last_accession_uri_id}'"
)

# Retrieve list of all accessions from ArchivesSpace
asnake_client = ASnakeClient(
baseurl=os.environ["ARCHIVESSPACE_URL"],
username=os.environ["ARCHIVESSPACE_USER"],
Expand All @@ -40,51 +39,52 @@ def main(*, verbose: bool) -> None:
"/repositories/2/accessions?all_ids=true"
).json()

if max(accession_uri_ids) <= int(last_accession_uri_id):
# Determine new accessions to add to Airtable, exit early if none
accessions_to_add = sorted(
[
accession_uri_id
for accession_uri_id in accession_uri_ids
if accession_uri_id > int(last_accession_uri_id)
]
)
if not accessions_to_add:
logger.info("No new accessions to add to Airtable.")
else:
airtable_api = pyairtable.Api(os.environ["AIRTABLE_TOKEN"])
airtable_table = airtable_api.table(
os.environ["AIRTABLE_BASE_ID"], os.environ["AIRTABLE_TABLE_NAME"]
)
return
logger.info(f"Adding the following accessions to Airtable: {accessions_to_add}")

# Establish connection to Airtable
airtable_api = pyairtable.Api(os.environ["AIRTABLE_TOKEN"])
airtable_table = airtable_api.table(
os.environ["AIRTABLE_BASE_ID"], os.environ["AIRTABLE_TABLE_NAME"]
)
logger.info(
f"Airtable client configured for base '{airtable_table.base}'"
f" and table '{airtable_table.name}'"
)

# Add accessions to Airtable
for accession_uri_id in accessions_to_add:
accession_uri = f"/repositories/2/accessions/{accession_uri_id}"
accession_record = asnake_client.get(accession_uri).json()
logger.debug(f"Retrieved record: {accession_uri}")
accession_data = {
"Accession Title": accession_record["title"],
"Accession Number": parse_accession_number(accession_record),
"Current Status": "Unassigned",
**parse_extent_data(accession_record),
}
logger.debug(f"Data extracted from ArchivesSpace: {accession_data}")

# Post new rows to Airtable
response = airtable_table.create(accession_data)
logger.info(
f"Airtable client configured for base '{airtable_table.base}'"
f" and table '{airtable_table.name}'"
)
accessions_to_add = sorted(
[
accession_uri_id
for accession_uri_id in accession_uri_ids
if accession_uri_id > int(last_accession_uri_id)
]
"Airtable row created for Accession Number: "
f"'{response["fields"]["Accession Number"]}'"
)
logger.info(f"Adding the following accessions to Airtable: {accessions_to_add}")
for accession_uri_id in accessions_to_add:
accession_uri = f"/repositories/2/accessions/{accession_uri_id}"
accession_record = asnake_client.get(accession_uri).json()
logger.debug(f"Retrieved record: {accession_uri}")
accession_data = {
"Accession Title": accession_record["title"],
"Accession Number": parse_accession_number(accession_record),
"Current Status": "Unassigned",
**parse_extent_data(accession_record),
}
logger.debug(f"Data extracted from ArchivesSpace: {accession_data}")

response = airtable_table.create(accession_data)
logger.info(
"Airtable row created for Accession Number: "
f"'{response["fields"]["Accession Number"]}'"
)

logger.info(f"Last accession ID processed: '{accession_uri_id}'")
updated_last_accession_uri_id = ssm_client.update_parameter(
last_accession_parameter, str(accession_uri_id)
)
logger.info(
f"Updated SSM parameter '{last_accession_parameter}' "
f"to '{updated_last_accession_uri_id}'"
)
# Update SSM parameter
logger.debug(f"Last accession ID processed: '{accession_uri_id}'")
ssm_client.update_parameter(last_accession_parameter, str(accession_uri_id))

elapsed_time = perf_counter() - start_time
logger.info(
Expand Down
6 changes: 4 additions & 2 deletions asati/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self) -> None:
def get_parameter(self, parameter_name: str) -> str:
parameter = self.client.get_parameter(Name=parameter_name)
parameter_value = parameter["Parameter"]["Value"]
logger.info(f"SSM parameter retrieved: {parameter_value}")
logger.info(f"SSM parameter '{parameter_name}' retrieved: '{parameter_value}'")
return parameter_value

def update_parameter(self, parameter_name: str, parameter_value: str) -> str:
Expand All @@ -63,7 +63,9 @@ def update_parameter(self, parameter_name: str, parameter_value: str) -> str:
updated_parameter = self.client.get_parameter(Name=parameter_name)
updated_parameter_value = updated_parameter["Parameter"]["Value"]
if parameter_value == updated_parameter_value:
logger.info(f"SSM parameter updated: '{updated_parameter_value}'")
logger.info(
f"Updated SSM parameter '{parameter_name}' to '{parameter_value}'"
)
else:
message = "SSM parameter update failed: '{parameter_value}' was not set"
raise RuntimeError(message)
Expand Down
1 change: 0 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ def test_cli_no_accessions_success(mocked_asnake, caplog, runner):
assert "Logger 'root' configured with level=INFO" in caplog.text
assert "Running process" in caplog.text
assert "No new accessions to add to Airtable." in caplog.text
assert "Total time to complete process" in caplog.text
assert "Airtable row created for Accession Number:" not in caplog.text
assert "Updated SSM parameter" not in caplog.text

0 comments on commit b25a434

Please sign in to comment.