def aws_accounts_m_stats_monthlycostbyregion(accounts, nb_months): """--- get: tags: - aws produces: - application/json description: &desc Get monthly costs summed by region summary: *desc responses: 200: description: List of AWS accounts schema: properties: months: type: array items: properties: month: type: string products: type: array items: properties: cost: type: number region: type: string 403: description: Not logged in 404: description: AWS account not registered """ assert len(accounts) > 0 now = datetime.utcnow() date_from = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) - relativedelta(months=nb_months - 1) date_to = now.replace(day=calendar.monthrange(now.year, now.month)[1], hour=23, minute=59, second=59, microsecond=999999) raw_data = AWSDetailedLineitem.get_cost_by_region(keys=[account.get_aws_user_id() for account in accounts], date_from=date_from, date_to=date_to)['intervals']['buckets'] res = [ { 'month': data['key_as_string'].split('T')[0], 'regions': [ { 'region': region['key'], 'cost': region['cost']['value'], } for region in data['regions']['buckets'] ], } for data in raw_data ] if 'csv' in request.args: return Response(generate_csv(res, 'regions', 'region'), mimetype='text/csv') return jsonify(months=res)
def aws_accounts_m_stats_monthlycostbyregionbytagbyaccount( accounts, nb_months): """--- get: tags: - aws produces: - application/json description: &desc Get monthly costs summed by region for each account summary: *desc responses: 200: description: List of AWS accounts schema: properties: months: type: array items: properties: month: type: string products: type: array items: properties: cost: type: number region: type: string 403: description: Not logged in 404: description: AWS account not registered """ assert len(accounts) > 0 now = datetime.utcnow() date_from = now.replace( day=1, hour=0, minute=0, second=0, microsecond=0) - relativedelta(months=nb_months - 1) date_to = now.replace(day=calendar.monthrange(now.year, now.month)[1], hour=23, minute=59, second=59, microsecond=999999) raw_data = AWSDetailedLineitem.get_cost_by_region( keys=[account.get_aws_user_id() for account in accounts], tagged=True, byaccount=True, date_from=date_from, date_to=date_to)['accounts']['buckets'] def tagged_cost(bucket, total): total_tag = 0.0 for tag in bucket: total_tag += tag['cost']['value'] yield (tag['key'], tag['cost']['value']) if total != total_tag: yield ('untagged', total - total_tag) res = [{ 'account_id': account['key'], 'account_name': [a.pretty for a in accounts if a.get_aws_user_id() == account['key']][0], 'months': [{ 'month': data['key_as_string'].split('T')[0], 'regions': [{ 'region': region['key'], 'tags': [{ 'name': tag[0], 'cost': tag[1], } for tag in tagged_cost(region['tags']['buckets'], region['cost']['value'])], } for region in data['regions']['buckets']], } for data in account['intervals']['buckets']] } for account in raw_data] if 'csv' in request.args: return Response(generate_csv(res, 'regions', 'region', account=True, tagged=True), mimetype='text/csv') return jsonify(accounts=res)