Skip to content

Commit

Permalink
Add monthly_usage/main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Oct 8, 2024
1 parent f6d698d commit a6dce32
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions usage_function/monthly_usage/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Run the monthly usage function manually."""

import datetime
import logging
import time

from azure.core.exceptions import HttpResponseError

import utils.settings
from utils.usage import get_all_usage, retrieve_usage, send_usage


def main() -> None:
"""Collect usage information and send it to the API."""
config = utils.settings.get_settings()
# print(item.date.isoformat())
logger = logging.getLogger(__name__)
# Try up to 5 times to get usage and send to the API
for attempt in range(1):
logger.warning("Attempt %d", attempt + 1)

try:
usage_query = get_all_usage(
# change these as needed
datetime.datetime(2024, 8, 30),
datetime.datetime(2024, 9, 1), # 29, 23, 59, 59, 999999),
# todo last day of July?
billing_account_id=config.BILLING_ACCOUNT_ID,
# mgmt_group="ea"
)
usage_items = retrieve_usage(usage_query)

today = datetime.date.today()
for usage_item in usage_items:
usage_item.monthly_upload = today

# logger.warning("Sending usage for %s", dates)
try:
send_usage(config.API_URL, usage_items, monthly_usage_upload=False)
except Exception as e:
raise e

logger.warning("Monthly usage function finished.")
return

except HttpResponseError as e:
if attempt == 1 - 1:
logger.error("Could not retrieve usage data.")
raise RuntimeError("Could not retrieve usage data.")

logger.error("Request to azure failed. Trying again in 60 seconds")
logger.error(e)
time.sleep(60)


if __name__ == "__main__":
main()

0 comments on commit a6dce32

Please sign in to comment.