diff --git a/site-modules/profile/files/azure_billing_report/compute_stats.py b/site-modules/profile/files/azure_billing_report/compute_data.py rename from site-modules/profile/files/azure_billing_report/compute_stats.py rename to site-modules/profile/files/azure_billing_report/compute_data.py --- a/site-modules/profile/files/azure_billing_report/compute_stats.py +++ b/site-modules/profile/files/azure_billing_report/compute_data.py @@ -5,6 +5,7 @@ from jinja2 import Environment, FileSystemLoader import click +import json import matplotlib.pyplot as plt import pandas @@ -95,6 +96,14 @@ generate_data_files(cost_per_service_per_day[['Date','ServiceName', 'ServiceResource', 'Cost']], output_dir + "/cost_per_service_per_day") + ## + # Load balance information + ## + balances = None + + with open(f"{output_dir}/balances.json", "r") as balancesFile: + balances = json.load(balancesFile) + ## # index.html page generation ## @@ -106,7 +115,7 @@ template_file_loader = FileSystemLoader(searchpath='./') env = Environment(loader=template_file_loader) template = env.get_template('index.html.tmpl') - index = template.render(generated_date=generated_date) + index = template.render(generated_date=generated_date, balances=balances) with open(index_file_name, 'w') as f: f.write(index) diff --git a/site-modules/profile/files/azure_billing_report/get_csv.py b/site-modules/profile/files/azure_billing_report/get_data.py rename from site-modules/profile/files/azure_billing_report/get_csv.py rename to site-modules/profile/files/azure_billing_report/get_data.py --- a/site-modules/profile/files/azure_billing_report/get_csv.py +++ b/site-modules/profile/files/azure_billing_report/get_data.py @@ -8,6 +8,7 @@ from selenium.webdriver.support import expected_conditions as EC from datetime import datetime, timedelta +import json import os import time @@ -15,14 +16,15 @@ START_DATE_FORMAT = "%Y-%m-01" END_DATE_FORMAT = "%Y-%m-%d" BASE_SPONSORSHIP_URL = "https://www.microsoftazuresponsorships.com" -EXPECTED_FILE = "AzureUsage.csv" +EXPECTED_CSV_FILE = "AzureUsage.csv" +BALANCE_FILE = "balances.json" def wait_for_download(): MAX_COUNT = 10 print("Waiting for download", end="") count = 0 - while not os.path.exists(EXPECTED_FILE) and count < MAX_COUNT: + while not os.path.exists(EXPECTED_CSV_FILE) and count < MAX_COUNT: time.sleep(2) print(".", end="") count += 1 @@ -58,7 +60,7 @@ driver = webdriver.Chrome(options=options) driver.set_page_load_timeout(30) - + print("Going to the portal login page...") driver.get(f"{BASE_SPONSORSHIP_URL}/Account/Login") wait = WebDriverWait(driver, 30) @@ -107,6 +109,28 @@ wait_for_download() - print(f"Usage csv file downloaded and available in the {EXPECTED_FILE} file") + print(f"Usage csv file downloaded and available in the {EXPECTED_CSV_FILE} file") + + print("Downloading balances information") + BALANCE_URL = f"{BASE_SPONSORSHIP_URL}/Balance" + print(f"Balance url: {BALANCE_URL}") + + driver.get(BALANCE_URL) + + wait.until(EC.visibility_of_element_located((By.ID, "balance-container"))) + if DEBUG: + driver.save_screenshot("balances.png") + + balances = { + "usedCredits": driver.find_element(by=By.CSS_SELECTOR, value="span.used-balance.value").text, + "remainingCredits": driver.find_element(by=By.CSS_SELECTOR, value="span.remaining-balance.value").text, + "remainingDays": driver.find_element(by=By.CSS_SELECTOR, value="span.remainingDays").text + } + + print("Writing balance information in {BALANCE_FILE}") + with open(BALANCE_FILE, "w") as balanceFile: + json.dump(balances, balanceFile) driver.close() + + print("Done") diff --git a/site-modules/profile/files/azure_billing_report/index.html.tmpl b/site-modules/profile/files/azure_billing_report/index.html.tmpl --- a/site-modules/profile/files/azure_billing_report/index.html.tmpl +++ b/site-modules/profile/files/azure_billing_report/index.html.tmpl @@ -8,6 +8,17 @@

Azure reporting until the {{ generated_date.strftime('%Y-%m-%d') }}

+ +

Balances

+

+ All time consumption: {{ "{:,.2f}".format(balances.usedCredits | float).replace(",", " ") }} $ +

+

+ Remaining credits : {{ "{:,.2f}".format(balances.remainingCredits | float).replace(",", " ") }} $ +

+

+ {{ balances.remainingDays }} +

Cost per day

@@ -23,10 +34,10 @@

Cost per service per day

Raw data: html / markdown

- +

Cost per service per month

Raw data: html / markdown

- +

generation date: {{ generated_date }}

diff --git a/site-modules/profile/files/azure_billing_report/refresh_azure_report.sh b/site-modules/profile/files/azure_billing_report/refresh_azure_report.sh --- a/site-modules/profile/files/azure_billing_report/refresh_azure_report.sh +++ b/site-modules/profile/files/azure_billing_report/refresh_azure_report.sh @@ -13,7 +13,7 @@ if [ ! -e "${CSV_FILE}" ]; then echo "Getting new statistics from azure portal..." - ${DATA_DIRECTORY}/.venv/bin/python3 ${INSTALL_DIRECTORY}/get_csv.py + ${DATA_DIRECTORY}/.venv/bin/python3 ${INSTALL_DIRECTORY}/get_data.py else echo "${CSV_FILE} already exists, reusing it..." fi @@ -21,6 +21,6 @@ echo "Generating report..." pushd ${INSTALL_DIRECTORY} -${DATA_DIRECTORY}/.venv/bin/python3 ${INSTALL_DIRECTORY}/compute_stats.py ${DATA_DIRECTORY} +${DATA_DIRECTORY}/.venv/bin/python3 ${INSTALL_DIRECTORY}/compute_data.py ${DATA_DIRECTORY} echo "Report refreshed."