Skip to content

Commit

Permalink
Move usage to v2 model
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Jan 3, 2025
1 parent 40a4d93 commit 0adf0a0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion usage_function/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ WORKDIR /home/site/wwwroot

RUN ~/.local/share/pypoetry/venv/bin/poetry config virtualenvs.create false

COPY pyproject.toml poetry.lock host.json ./
COPY function_app.py pyproject.toml poetry.lock host.json ./

RUN ~/.local/share/pypoetry/venv/bin/poetry install --only main
17 changes: 17 additions & 0 deletions usage_function/function_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import azure.functions as func

app = func.FunctionApp()


@app.function_name(name="usage")
@app.timer_trigger(schedule="0 0 * * * *", arg_name="my_timer", run_on_startup=False)
def usage_func(my_timer: func.TimerRequest) -> None:
usage.main(my_timer)


@app.function_name(name="monthly_usage")
@app.timer_trigger(
schedule="0 10 */2 7,8 * *", arg_name="my_timer", run_on_startup=False
)
def monthly_usage_func(my_timer: func.TimerRequest) -> None:
montly_usage.main(my_timer)
4 changes: 0 additions & 4 deletions usage_function/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,5 @@
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
},
"functions": [
"usage",
"monthly_usage"
],
"functionTimeout": "04:00:00"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

MAX_ATTEMPTS = 5

app = func.FunctionApp()


def get_dates() -> Union[None, Tuple[date], Tuple[date, date]]:
"""Get up to two dates to process.
Expand Down Expand Up @@ -47,10 +45,6 @@ def get_dates() -> Union[None, Tuple[date], Tuple[date, date]]:
return day1, day2


@app.function_name(name="monthly_usage")
@app.timer_trigger(
schedule="0 10 */2 7,8 * *", arg_name="my_timer", run_on_startup=False
)
def main(my_timer: func.TimerRequest) -> None:
"""Collect usage information for the previous month."""
# If incorrect settings have been given,
Expand Down
66 changes: 66 additions & 0 deletions usage_function/usage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""An Azure Function App to collect usage information."""

import logging
import time
from datetime import datetime, timedelta

import azure.functions as func
from azure.core.exceptions import HttpResponseError

import utils.settings
from utils.logutils import add_log_handler_once
from utils.usage import date_range, get_all_usage, retrieve_and_send_usage


def main(mytimer: func.TimerRequest) -> None:
"""Collect usage information and send it to the API."""
# If incorrect settings have been given,
# better to find out sooner rather than later.
config = utils.settings.get_settings()

logging.basicConfig(
level=config.LOG_LEVEL,
format="%(asctime)s %(message)s",
datefmt="%d/%m/%Y %I:%M:%S %p",
)

add_log_handler_once(__name__)
logger = logging.getLogger(__name__)

logger.warning("Usage function starting.")

if mytimer.past_due:
logger.info("The timer is past due.")

now = datetime.now()
start_datetime = (
now
- timedelta(days=config.USAGE_HISTORY_DAYS - 1)
- timedelta(days=config.USAGE_HISTORY_DAYS_OFFSET)
)
end_datetime = now - timedelta(days=config.USAGE_HISTORY_DAYS_OFFSET)

logger.warning(
"Requesting all data between %s and %s in reverse order",
start_datetime,
end_datetime,
)

for usage_date in reversed(list(date_range(start_datetime, end_datetime))):
# Try up to 5 times to get usage and send to the API
for _ in range(5):
logger.warning("Requesting all usage data for %s", usage_date)
usage = get_all_usage(
usage_date,
usage_date,
billing_account_id=config.BILLING_ACCOUNT_ID,
mgmt_group=config.MGMT_GROUP,
)

try:
retrieve_and_send_usage(config.API_URL, usage)
break
except HttpResponseError as e:
logger.error("Request to azure failed. Trying again in 60 seconds")
logger.error(e)
time.sleep(60)
Empty file removed usage_function/usage/sample.dat
Empty file.

0 comments on commit 0adf0a0

Please sign in to comment.