Esempio n. 1
0
 def _get_bandwith_info(account, bucket_names):
     bucket_ids = [
         bucket
         for bucket in (bucket_names if isinstance(bucket_names, list
                                                   ) else [bucket_names])
     ]
     bandwith_cost = AWSDetailedLineitem.get_s3_bandwith_info_and_cost_per_name(
         account.get_aws_user_id(), bucket_ids)
     return bandwith_cost
Esempio n. 2
0
def aws_accounts_m_stats_s3bucketsizepername(accounts):
    """---
    get:
        tags:
            - aws
        produces:
            - application/csv
        description: &desc Stats about cost and usage of bandwith and storag on s3 buckets, organised by name
        summary: *desc
        responses:
            200:
                description: Stats about cost and usage of bandwith and storag on s3 buckets, organised by name
            403:
                description: Not logged in
            404:
                description: AWS account not registered
    """
    def _create_bandwith_breakdown(transfer_types_list, csv_row,
                                   bucket_bandwith_stat):
        for elem in transfer_types_list:
            _current_transfer_type = _check_if_in_list(
                bucket_bandwith_stat['transfer_stats'], elem, 'type')
            if _current_transfer_type is not None:
                csv_row[elem] = _current_transfer_type[
                    'data'] * 1024 * 1024 * 1024  # The is by default given in GB
        return csv_row

    def _create_csv_rows(bucket_list, account, bandwith_cost, csv_row_all):
        for bucket in bucket_list['buckets']:
            csv_row = {
                'account_id':
                account.get_aws_user_id(),
                'used_space':
                bucket['used_space'],
                'name':
                bucket['name'],
                'storage_cost':
                _check_if_in_list(bucket['prices'], bucket['provider'],
                                  'provider')['cost']
            }
            bucket_bandwith_stat = _check_if_in_list(bandwith_cost,
                                                     bucket['name'],
                                                     'bucket_name')
            if bucket_bandwith_stat is not None:
                csv_row = _create_bandwith_breakdown(transfer_types_list,
                                                     csv_row,
                                                     bucket_bandwith_stat)
            csv_row['bandwith_cost'] = bucket_bandwith_stat[
                'cost'] if bucket_bandwith_stat is not None else 0
            csv_row['total_cost'] = csv_row['storage_cost'] + csv_row[
                'bandwith_cost']
            csv_row_all.append(csv_row)
        return csv_row_all

    assert len(accounts) > 0
    csv_header = [
        'account_id', 'name', 'used_space', 'storage_cost', 'bandwith_cost',
        'total_cost'
    ]
    csv_row_all = []
    for account in accounts:
        bucket_list = AWSStat.latest_s3_space_usage(account)
        bucket_ids = [
            bucket['name'] for bucket in (
                bucket_list['buckets'] if bucket_list is not None else [])
        ]
        bandwith_cost = AWSDetailedLineitem.get_s3_bandwith_info_and_cost_per_name(
            account.get_aws_user_id(), bucket_ids)
        transfer_types_list = _build_list_used_transfer_types(bandwith_cost)
        csv_header = _append_to_header_list(csv_header, transfer_types_list)
        csv_row_all = _create_csv_rows(bucket_list, account, bandwith_cost,
                                       csv_row_all)

    if len(csv_row_all) > 0 and csv_row_all[0] is None:
        csv_row_all = []
    if 'csv' in request.args:
        return Response(generate_csv_clean(csv_row_all, csv_header))
    return jsonify(accounts=csv_row_all)