Skip to content

Commit

Permalink
option to include the deployment image counts
Browse files Browse the repository at this point in the history
  • Loading branch information
KatrionaGoldmann committed Nov 6, 2024
1 parent e79c8c5 commit cd83384
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions print_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ def get_deployments(username, password):
print(f"Error: {err}")
sys.exit(1)

def print_deployments(include_inactive=False, subset_countries=None):
def count_files(s3_client, bucket_name, prefix):
"""
Count number of files for a given prefix.
"""
paginator = s3_client.get_paginator("list_objects_v2")
operation_parameters = {"Bucket": bucket_name, "Prefix": prefix}
page_iterator = paginator.paginate(**operation_parameters)
count=0
for page in page_iterator:
if not os.path.basename(page.get("Contents", [])[0]["Key"]).startswith("$"):
count += page.get("KeyCount", 0)
file_i = page.get("Contents", [])[0]["Key"]

return count

def print_deployments(include_inactive=False, subset_countries=None, print_image_count=True):
"""
Provide the deployments available through the object store.
"""
Expand All @@ -47,11 +62,19 @@ def print_deployments(include_inactive=False, subset_countries=None):
password = aws_credentials["UKCEH_password"]
all_deployments = get_deployments(username, password)

session = boto3.Session(
aws_access_key_id=aws_credentials["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=aws_credentials["AWS_SECRET_ACCESS_KEY"],
region_name=aws_credentials["AWS_REGION"],
)
s3_client = session.client("s3", endpoint_url=aws_credentials["AWS_URL_ENDPOINT"])


if not include_inactive:
act_string = 'active '
act_string = ' active '
all_deployments = [x for x in all_deployments if x['status'] == 'active']
else:
act_string = ''
act_string = ' '

# Loop through each country to print deployment information
all_countries = list(set([dep['country'].title() for dep in all_deployments]))
Expand All @@ -68,12 +91,22 @@ def print_deployments(include_inactive=False, subset_countries=None):
country_code = list(set([x['country_code'] for x in country_depl]))[0]
print("\n\033[1m" + country + " (" + country_code + ") has " + str(len(country_depl)) + act_string + "deployments:\033[0m")
all_deps = list(set([x['deployment_id'] for x in country_depl]))



for dep in sorted(all_deps):
dep_info = [x for x in country_depl if x['deployment_id'] == dep][0]
print(f"\033[1m - Deployment ID: {dep_info['deployment_id']}, Name: {dep_info['location_name']}, Deployment Key: '{dep_info['location_name']} - {dep_info['camera_id']}'\033[0m")
print(f" Location ID: {dep_info['location_id']}, Latitute: {dep_info['lat']}, Longitute: {dep_info['lon']}, Camera ID: {dep_info['camera_id']}, System ID: {dep_info['system_id']}, Status: {dep_info['status']}")

# get the number of images for this deployment
prefix = f"{dep_info['deployment_id']}/snapshot_images"
bucket_name = dep_info["country_code"].lower()

if print_image_count:
count = count_files(s3_client, bucket_name, prefix)
print(f" - This deployment has \033[1m{count}\033[0m images.\n")


if __name__ == "__main__":
with open("./credentials.json", encoding="utf-8") as config_file:
Expand All @@ -89,7 +122,9 @@ def print_deployments(include_inactive=False, subset_countries=None):
)
parser.add_argument("--include_inactive", action=argparse.BooleanOptionalAction,
default=False, help="Flag to include inactive deployments.")
parser.add_argument("--print_image_count", action=argparse.BooleanOptionalAction,
default=False, help="Flag to print the number of images per deployment.")
parser.add_argument("--subset_countries", nargs='+', help="Optional list to subset for specific countries (e.g. --subset_countries 'Panama' 'Thailand').", default=None)
args = parser.parse_args()

print_deployments(args.include_inactive, args.subset_countries)
print_deployments(args.include_inactive, args.subset_countries, args.print_image_count)

0 comments on commit cd83384

Please sign in to comment.