Skip to content

Commit

Permalink
fix: time handling for delta
Browse files Browse the repository at this point in the history
  • Loading branch information
roleyfoley committed Feb 23, 2021
1 parent b0a0ec9 commit 698c77b
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions s3-age-metric/src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def lambda_handler(event, context):
cloudwatch_metric_namespace = os.environ.get('CLOUDWATCH_METRIC_NAMESPACE', 'S3ObjectAge' )

now_time = datetime.now(timezone.utc)
oldest_item = now_time
oldest_item_age = now_time
oldest_item_key = None
continuation_token = None

Expand All @@ -44,18 +44,17 @@ def lambda_handler(event, context):

if 'Contents' in s3_objects:
s3_contents = sorted(s3_objects['Contents'], key= lambda i: i['LastModified'] )
oldest_item_in_set = s3_contents[0]['LastModified']
if oldest_item_in_set < oldest_item:
oldest_item = oldest_item_in_set
oldest_item_age_in_set = s3_contents[0]['LastModified']
if oldest_item_age_in_set < oldest_item_age:
oldest_item_age = oldest_item_age_in_set
oldest_item_key = s3_contents[0]['Key']

if s3_objects['IsTruncated']:
continuation_token = s3_objects['NextContinuationToken']
else:
break

oldest_item_age = (now_time - oldest_item).seconds

oldest_item_age = now_time - oldest_item_age
try:
cloudwatch_client.put_metric_data(
Namespace=cloudwatch_metric_namespace,
Expand All @@ -69,7 +68,7 @@ def lambda_handler(event, context):
}
],
'Timestamp' : now_time,
'Value' : oldest_item_age,
'Value' : oldest_item_age.total_seconds(),
'Unit' : 'Seconds',
}
]
Expand All @@ -78,10 +77,10 @@ def lambda_handler(event, context):
logger.fatal(str(e))
raise e

logger.info(f'Oldest Item: s3://{bucket_name}/{oldest_item_key} - Age: {oldest_item_age} seconds')
logger.info(f'Oldest Item: s3://{bucket_name}/{oldest_item_key} - Age: {oldest_item_age.days} days')
return {
'bucket_name': bucket_name,
'bucket_prefix': bucket_prefix,
'oldest_item_age': oldest_item_age,
'oldest_item_age': oldest_item_age.total_seconds(),
'oldest_item_key': oldest_item_key
}

0 comments on commit 698c77b

Please sign in to comment.