{
  "output": "Successfully created and wrote to new file: /Users/bob/git/dxw/aws-stuff/audits/bw.py. Here is the updated code:\nimport boto3\nfrom botocore.exceptions import ClientError\nfrom aws_utils import (\n    get_account_names,\n    get_previous_month_range,\n    get_boto_session,\n    export_to_sheets,\n)\n\ndef fetch_bandwidth(ce_client, start_date, end_date, all_accounts):\n    \"\"\"\n    Fetches bandwidth usage grouped by account.\n    Returns a dict: { account_id: total_usage_in_gb }\n    \"\"\"\n    account_totals = {acc_id: 0.0 for acc_id in all_accounts.keys()}\n    next_token = None\n\n    try:\n        while True:\n            params = {\n                \"TimePeriod\": {\"Start\": start_date, \"End\": end_date},\n                \"Granularity\": \"MONTHLY\",\n                \"Metrics\": [\"UsageQuantity\"],\n                \"GroupBy\": [\n                    {\"Type\": \"DIMENSION\", \"Key\": \"LINKED_ACCOUNT\"},\n                    {\"Type\": \"DIMENSION\", \"Key\": \"USAGE_TYPE\"},\n                ],\n            }\n            if next_token:\n                params[\"NextPageToken\"] = next_token\n\n            response = ce_client.get_cost_and_usage(**params)\n\n            for result in response.get(\"ResultsByTime\", []):\n                for group in result.get(\"Groups\", []):\n                    acc_id = group[\"Keys\"][0]\n                    usage_type = group[\"Keys\"][1]\n                    usage_amount = float(group[\"Metrics\"][\"UsageQuantity\"][\"Amount\"])\n                    unit = group[\"Metrics\"][\"UsageQuantity\"][\"Unit\"]\n\n                    if \"DataTransfer\" in usage_type:\n                        if unit == \"Bytes\":\n                            usage_amount /= 1024**3\n                        elif unit in [\"MB\", \"Megabytes\"]:\n                            usage_amount /= 1024\n\n                        if acc_id in account_totals:\n                            account_totals[acc_id] += usage_amount\n                        else:\n                            account_totals[acc_id] = usage_amount\n\n            next_token = response.get(\"NextPageToken\")\n            if not next_token:\n                break\n    except ClientError as e:\n        print(f\"Error fetching bandwidth: {e}\")\n\n    return account_totals\n\ndef run():\n    threshold_gb = 100.0\n    session = get_boto_session()\n    ce_client = session.client(\"ce\")\n    all_accounts = get_account_names()\n    \n    if not all_accounts:\n        print(\"No accounts found.\")\n        return\n\n    start_date, end_date = get_previous_month_range()\n    \n    account_totals = fetch_bandwidth(ce_client, start_date, end_date, all_accounts)\n\n    print(f\"\\n--- Full Bandwidth Report ({start_date} to {end_date}) ---\")\n    print(f\"{'Account Name':<25} | {'Total GB':<12} | {'Overage (>100GB)'}\")\n    print(\"-\" * 75)\n\n    sorted_accounts = sorted(\n        account_totals.items(),\n        key=lambda x: (x[1], all_accounts.get(x[0], x[0])),\n        reverse=True,\n    )\n\n    headers = [\"Account Name\", \"Account ID\", \"Total GB\", \"Overage (>100GB)\"]\n    rows = []\n\n    for acc_id, total_usage in sorted_accounts:\n        acc_name = all_accounts.get(acc_id, f\"Deleted/Unknown ({acc_id})\")\n        overage = max(0, total_usage - threshold_gb)\n        status_icon = \"⚠️\" if overage > 0 else \"✅\"\n        print(\n            f\"{acc_name[:25]:<25} | {total_usage:>10.2f} GB | {overage:>8.2f} GB {status_icon}\"\n        )\n\n        rows.append([acc_name, acc_id, round(total_usage, 2), round(overage, 2)])\n\n    # Export to Google Sheets\n    export_to_sheets(\"AWS_Bandwidth_Usage\", headers, rows)\n\nif __name__ == \"__main__\":\n    run()\n"
}