From 63cb05249f576bf641b38de3260f827cd213708b Mon Sep 17 00:00:00 2001 From: graham-dds Date: Thu, 6 Feb 2020 14:45:05 -0500 Subject: [PATCH] Add Reports domain method to get portfolio spend --- atst/domain/reports.py | 24 ++++++++++++++++++++++++ tests/domain/test_reports.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/atst/domain/reports.py b/atst/domain/reports.py index 99b229e3..30996290 100644 --- a/atst/domain/reports.py +++ b/atst/domain/reports.py @@ -1,5 +1,11 @@ from flask import current_app from itertools import groupby +from atst.domain.csp.cloud.models import ( + ReportingCSPPayload, + CostManagementQueryCSPResult, +) +from atst.domain.csp.reports import prepare_azure_reporting_data +import pendulum class Reports: @@ -42,3 +48,21 @@ class Reports: } ) return output + + @classmethod + def get_portfolio_spending(cls, portfolio): + # TODO: Extend this function to make from_date and to_date configurable + from_date = pendulum.now().subtract(years=1).add(days=1).format("YYYY-MM-DD") + to_date = pendulum.now().format("YYYY-MM-DD") + rows = [] + + if portfolio.csp_data: + payload = ReportingCSPPayload( + from_date=from_date, to_date=to_date, **portfolio.csp_data + ) + response: CostManagementQueryCSPResult = current_app.csp.cloud.get_reporting_data( + payload + ) + rows = response.properties.rows + + return prepare_azure_reporting_data(rows) diff --git a/tests/domain/test_reports.py b/tests/domain/test_reports.py index 33ac926e..cdd5de5e 100644 --- a/tests/domain/test_reports.py +++ b/tests/domain/test_reports.py @@ -1,8 +1,31 @@ -# TODO: Implement when we get real reporting data -def test_expired_task_orders(): - pass +import pytest + +from atst.domain.reports import Reports +from tests.factories import PortfolioFactory +from decimal import Decimal -# TODO: Implement when we get real reporting data -def test_obligated_funds_by_JEDI_clin(): - pass +@pytest.fixture(scope="function") +def portfolio(): + portfolio = PortfolioFactory.create() + return portfolio + + +class TestGetPortfolioSpending: + csp_data = { + "tenant_id": "", + "billing_profile_properties": { + "invoice_sections": [{"invoice_section_id": "",}] + }, + } + + def test_with_csp_data(self, portfolio): + portfolio.csp_data = self.csp_data + data = Reports.get_portfolio_spending(portfolio) + assert data["invoiced"] == Decimal(1551.0) + assert data["estimated"] == Decimal(500.0) + + def test_without_csp_data(self, portfolio): + data = Reports.get_portfolio_spending(portfolio) + assert data["invoiced"] == Decimal(0) + assert data["estimated"] == Decimal(0)