diff --git a/atst/domain/csp/reports.py b/atst/domain/csp/reports.py index 3f9ccbf8..0e29c5ff 100644 --- a/atst/domain/csp/reports.py +++ b/atst/domain/csp/reports.py @@ -1,6 +1,7 @@ from collections import defaultdict import json from decimal import Decimal +import pendulum def load_fixture_data(): @@ -136,3 +137,26 @@ class MockReportingProvider: CLIN_spend_dict[clin]["invoiced"] += Decimal(spend) return CLIN_spend_dict return {} + + +def prepare_azure_reporting_data(rows: list): + """ + Returns a dict representing invoiced and estimated funds for a portfolio given + a list of rows from CostManagementQueryCSPResult.properties.rows + { + invoiced: Decimal, + estimated: Decimal + } + """ + + estimated = [] + while rows: + if pendulum.parse(rows[-1][1]) >= pendulum.now(tz="utc").start_of("month"): + estimated.append(rows.pop()) + else: + break + + return dict( + invoiced=Decimal(sum([row[0] for row in rows])), + estimated=Decimal(sum([row[0] for row in estimated])), + ) diff --git a/tests/domain/cloud/reports/test_reports.py b/tests/domain/cloud/reports/test_reports.py index 1e74e9b9..4c85a9bc 100644 --- a/tests/domain/cloud/reports/test_reports.py +++ b/tests/domain/cloud/reports/test_reports.py @@ -1,5 +1,7 @@ -from atst.domain.csp.reports import MockReportingProvider +from atst.domain.csp.reports import MockReportingProvider, prepare_azure_reporting_data from tests.factories import PortfolioFactory +from decimal import Decimal +import pendulum def test_get_environment_monthly_totals(): @@ -56,3 +58,46 @@ def test_get_application_monthly_totals(): assert totals["last_month"] == 700 assert totals["total"] == 2500 assert [env["name"] for env in totals["environments"]] == ["A", "Z"] + + +class TestPrepareAzureData: + start_of_month = pendulum.today(tz="utc").start_of("month").replace(tzinfo=None) + next_month = start_of_month.add(months=1).to_atom_string() + this_month = start_of_month.to_atom_string() + last_month = start_of_month.subtract(months=1).to_atom_string() + two_months_ago = last_month = start_of_month.subtract(months=2).to_atom_string() + + def test_estimated_and_invoiced(self): + rows = [ + [150.0, self.two_months_ago, "", "USD"], + [100.0, self.last_month, "e0500a4qhw", "USD"], + [50.0, self.this_month, "", "USD"], + [50.0, self.next_month, "", "USD"], + ] + output = prepare_azure_reporting_data(rows) + + assert output.get("invoiced") == Decimal(250.0) + assert output.get("estimated") == Decimal(100.0) + + def test_just_estimated(self): + rows = [ + [100.0, self.this_month, "", "USD"], + ] + output = prepare_azure_reporting_data(rows) + + assert output.get("invoiced") == Decimal(0.0) + assert output.get("estimated") == Decimal(100.0) + + def test_just_invoiced(self): + rows = [ + [100.0, self.last_month, "", "USD"], + ] + output = prepare_azure_reporting_data(rows) + + assert output.get("invoiced") == Decimal(100.0) + assert output.get("estimated") == Decimal(0.0) + + def test_no_rows(self): + output = prepare_azure_reporting_data([]) + assert output.get("invoiced") == Decimal(0.0) + assert output.get("estimated") == Decimal(0.0)