Skip to content

Instantly share code, notes, and snippets.

@chicagobuss
Last active July 25, 2023 17:49
Show Gist options
  • Save chicagobuss/f4b7f83077d6af83a795e3c864d37a93 to your computer and use it in GitHub Desktop.
Save chicagobuss/f4b7f83077d6af83a795e3c864d37a93 to your computer and use it in GitHub Desktop.
boto3 dynamodb query with filter
#!/usr/bin/env python
import boto3
import json
from boto3.dynamodb.conditions import Key, Attr
from datetime import datetime
DIMENSION = 'logicalCpuCount'
TABLE_NAME = 'ms-usage-metering'
notes = '''
$ aws --profile dev-m dynamodb list-tables
{
"TableNames": [
"ms-observed-events",
"ms-observed-instance-status-summary",
"ms-observed-instances",
"ms-usage-metering",
"ms-usage-metering-export-jobs"
]
}
'''
def get_usage(table, run_ts):
now = datetime.fromtimestamp(run_ts)
# TODO: Consider datetime edge cases
day_str = now.strftime("%Y%m%d")
#hour_start_ts = run_ts - run_ts % 3600
#hour_end_ts = hour_start_ts + 3600
fivemin_start_ts = run_ts - run_ts % 300
fivemin_end_ts = fivemin_start_ts + 300
print("getting five-minute usage aggregation with run_ts %s for day %s between %s and %s" % \
(run_ts, day_str, fivemin_start_ts, fivemin_end_ts))
# try the table resource
response = table.query(
IndexName="ms-usage-metering-timestamp-index",
KeyConditionExpression = Key(
'billing-period').eq(day_str) & Key( \
'timestamp').between(hour_start_ts,hour_end_ts),
FilterExpression="attribute_exists(#payer_id)",
ExpressionAttributeNames={"#payer_id": "instance-owner-payer-id"}
)
# make the results dict
usage_by_payer_id = {}
for item in response["Items"]:
#print(item)
if item['dimension'] == DIMENSION:
payer_id = item['instance-owner-payer-id']
if payer_id in usage_by_payer_id:
usage_by_payer_id[payer_id] += int(item['value'])
else:
usage_by_payer_id[payer_id] = int(item['value'])
else:
print("observed event %s doesn't have dimension = %s, ignoring" % (item('event-id'), DIMENSION))
return usage_by_payer_id
if __name__ == '__main__':
# set default profile to dev-monitoring-ro
boto3.setup_default_session(profile_name='dev-monitoring-ro')
# ddb Table resource
try:
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table(TABLE_NAME)
except Exception as e:
print("Failed to get ddb table %s: %s" % (TABLE_NAME, e))
run_ts = 1690263159
total_usage = get_usage(table, run_ts)
# print results
print('##################')
print('Total usage by payer_id within this 5-minute interval:')
print(json.dumps(total_usage, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment