From faf8e3b519aea83299bb2cc531ba1256b30c30b7 Mon Sep 17 00:00:00 2001 From: dandds Date: Wed, 19 Sep 2018 14:12:06 -0400 Subject: [PATCH 1/5] mock total workspace data for predetermined names --- atst/domain/reports.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/atst/domain/reports.py b/atst/domain/reports.py index 586dda10..cecd07ca 100644 --- a/atst/domain/reports.py +++ b/atst/domain/reports.py @@ -141,18 +141,45 @@ CUMULATIVE_BUDGET_BELUGA = { "09/2018": {"spend": 14500, "cumulative": 19338}, } +REPORT_FIXTURE_MAP = { + "aardvark": { + "cumulative": CUMULATIVE_BUDGET_AARDVARK, + "monthly": MONTHLY_SPEND_AARDVARK, + "budget": 500_000, + }, + "beluga": { + "cumulative": CUMULATIVE_BUDGET_BELUGA, + "monthly": MONTHLY_SPEND_BELUGA, + "budget": 70_000, + } +} + +def _sum_monthly_spend(data): + return sum( + [ + spend + for project in data.values() + for env in project.values() + for spend in env.values() + ] + ) class Reports: @classmethod def workspace_totals(cls, workspace): - if workspace.request and workspace.request.task_order: + if workspace.name in REPORT_FIXTURE_MAP.keys(): + budget = REPORT_FIXTURE_MAP[workspace.name]["budget"] + spent = _sum_monthly_spend(REPORT_FIXTURE_MAP[workspace.name]["monthly"]) + elif workspace.request and workspace.request.task_order: ws_to = workspace.request.task_order budget = ws_to.budget + spent = 0 else: budget = 0 + spent = 0 # spent will be derived from CSP data - return {"budget": budget, "spent": 0} + return {"budget": budget, "spent": spent} @classmethod def monthly_totals(cls, alternate): From 4e75927b52b55af7c17384387f24589b5e5b43b0 Mon Sep 17 00:00:00 2001 From: dandds Date: Thu, 20 Sep 2018 09:28:50 -0400 Subject: [PATCH 2/5] mock report data for predetermined workspace names --- atst/domain/reports.py | 81 ++++++++++++++++++++++++--------------- atst/routes/workspaces.py | 5 +-- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/atst/domain/reports.py b/atst/domain/reports.py index cecd07ca..951027c3 100644 --- a/atst/domain/reports.py +++ b/atst/domain/reports.py @@ -151,9 +151,10 @@ REPORT_FIXTURE_MAP = { "cumulative": CUMULATIVE_BUDGET_BELUGA, "monthly": MONTHLY_SPEND_BELUGA, "budget": 70_000, - } + }, } + def _sum_monthly_spend(data): return sum( [ @@ -164,46 +165,63 @@ def _sum_monthly_spend(data): ] ) + +def _derive_project_totals(data): + project_totals = {} + for project, environments in data.items(): + project_spend = [ + (month, spend) + for env in environments.values() + for month, spend in env.items() + ] + project_totals[project] = { + month: sum([spend[1] for spend in spends]) + for month, spends in groupby(sorted(project_spend), lambda x: x[0]) + } + + return project_totals + + +def _derive_workspace_totals(project_totals): + monthly_spend = [ + (month, spend) + for project in project_totals.values() + for month, spend in project.items() + ] + workspace_totals = {} + for month, spends in groupby(sorted(monthly_spend), lambda m: m[0]): + workspace_totals[month] = sum([spend[1] for spend in spends]) + + return workspace_totals + + class Reports: @classmethod def workspace_totals(cls, workspace): - if workspace.name in REPORT_FIXTURE_MAP.keys(): + if workspace.name in REPORT_FIXTURE_MAP: budget = REPORT_FIXTURE_MAP[workspace.name]["budget"] spent = _sum_monthly_spend(REPORT_FIXTURE_MAP[workspace.name]["monthly"]) elif workspace.request and workspace.request.task_order: ws_to = workspace.request.task_order budget = ws_to.budget + # spent will be derived from CSP data spent = 0 else: budget = 0 spent = 0 - # spent will be derived from CSP data return {"budget": budget, "spent": spent} @classmethod - def monthly_totals(cls, alternate): - data = MONTHLY_SPEND_BELUGA if alternate else MONTHLY_SPEND_AARDVARK - project_totals = {} - for project, environments in data.items(): - project_spend = [ - (month, spend) - for env in environments.values() - for month, spend in env.items() - ] - project_totals[project] = { - month: sum([spend[1] for spend in spends]) - for month, spends in groupby(sorted(project_spend), lambda x: x[0]) - } - - monthly_spend = [ - (month, spend) - for project in project_totals.values() - for month, spend in project.items() - ] - workspace_totals = {} - for month, spends in groupby(sorted(monthly_spend), lambda m: m[0]): - workspace_totals[month] = sum([spend[1] for spend in spends]) + def monthly_totals(cls, workspace): + if workspace.name in REPORT_FIXTURE_MAP: + data = REPORT_FIXTURE_MAP[workspace.name]["monthly"] + project_totals = _derive_project_totals(data) + workspace_totals = _derive_workspace_totals(project_totals) + else: + data = {} + project_totals = {} + workspace_totals = {} return { "environments": data, @@ -212,9 +230,10 @@ class Reports: } @classmethod - def cumulative_budget(cls, alternate): - return { - "months": CUMULATIVE_BUDGET_BELUGA - if alternate - else CUMULATIVE_BUDGET_AARDVARK - } + def cumulative_budget(cls, workspace): + if workspace.name in REPORT_FIXTURE_MAP: + months = REPORT_FIXTURE_MAP[workspace.name]["cumulative"] + else: + months = {} + + return {"months": months} diff --git a/atst/routes/workspaces.py b/atst/routes/workspaces.py index 365f2836..9879d90c 100644 --- a/atst/routes/workspaces.py +++ b/atst/routes/workspaces.py @@ -98,7 +98,6 @@ def workspace_reports(workspace_id): "view workspace reports", ) - alternate_reports = http_request.args.get("alternate") today = date.today() month = http_request.args.get("month", today.month) year = http_request.args.get("year", today.year) @@ -113,9 +112,9 @@ def workspace_reports(workspace_id): return render_template( "workspaces/reports/index.html", - cumulative_budget=Reports.cumulative_budget(alternate_reports), + cumulative_budget=Reports.cumulative_budget(workspace), workspace_totals=Reports.workspace_totals(workspace), - monthly_totals=Reports.monthly_totals(alternate_reports), + monthly_totals=Reports.monthly_totals(workspace), current_month=current_month, prev_month=prev_month, two_months_ago=two_months_ago, From b2984bc86bdb87aba238d6ea44d39a0338615416 Mon Sep 17 00:00:00 2001 From: dandds Date: Thu, 20 Sep 2018 10:11:56 -0400 Subject: [PATCH 3/5] changes sample workspace name case --- atst/domain/reports.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/atst/domain/reports.py b/atst/domain/reports.py index 951027c3..22d4f753 100644 --- a/atst/domain/reports.py +++ b/atst/domain/reports.py @@ -142,12 +142,12 @@ CUMULATIVE_BUDGET_BELUGA = { } REPORT_FIXTURE_MAP = { - "aardvark": { + "Aardvark": { "cumulative": CUMULATIVE_BUDGET_AARDVARK, "monthly": MONTHLY_SPEND_AARDVARK, "budget": 500_000, }, - "beluga": { + "Beluga": { "cumulative": CUMULATIVE_BUDGET_BELUGA, "monthly": MONTHLY_SPEND_BELUGA, "budget": 70_000, From 9f7284070ed0a01adb72fae063ee36f2ba40120d Mon Sep 17 00:00:00 2001 From: dandds Date: Thu, 20 Sep 2018 10:38:29 -0400 Subject: [PATCH 4/5] month selector for total spend per month section in reports --- atst/filters.py | 6 ++++++ templates/workspaces/reports/index.html | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/atst/filters.py b/atst/filters.py index 0ee5f1cd..217b9e2f 100644 --- a/atst/filters.py +++ b/atst/filters.py @@ -1,4 +1,5 @@ import re +import datetime from flask import current_app as app from werkzeug.datastructures import FileStorage @@ -72,6 +73,10 @@ def formattedDate(value, formatter="%m/%d/%Y"): return "-" +def dateFromString(value, formatter="%m/%Y"): + return datetime.datetime.strptime(value, formatter) + + def register_filters(app): app.jinja_env.filters["iconSvg"] = iconSvg app.jinja_env.filters["dollars"] = dollars @@ -82,3 +87,4 @@ def register_filters(app): app.jinja_env.filters["findFilter"] = findFilter app.jinja_env.filters["renderList"] = renderList app.jinja_env.filters["formattedDate"] = formattedDate + app.jinja_env.filters["dateFromString"] = dateFromString diff --git a/templates/workspaces/reports/index.html b/templates/workspaces/reports/index.html index 172d5e07..563a7838 100644 --- a/templates/workspaces/reports/index.html +++ b/templates/workspaces/reports/index.html @@ -301,8 +301,21 @@

Total spend per month

- + {% for m in cumulative_budget["months"] %} + {% set month = m | dateFromString %} + + {% endfor %}
From 2a67fb619794bbc17bdbacfa360a2a7435c3ff8d Mon Sep 17 00:00:00 2001 From: dandds Date: Thu, 20 Sep 2018 11:42:03 -0400 Subject: [PATCH 5/5] add task order data to seed workspace --- script/seed.py | 7 +++++-- tests/factories.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/script/seed.py b/script/seed.py index cebe5528..ee113afd 100644 --- a/script/seed.py +++ b/script/seed.py @@ -12,7 +12,7 @@ from atst.domain.requests import Requests from atst.domain.workspaces import Workspaces from atst.domain.projects import Projects from atst.domain.exceptions import AlreadyExistsError -from tests.factories import RequestFactory +from tests.factories import RequestFactory, TaskOrderFactory from atst.routes.dev import _DEV_USERS as DEV_USERS WORKSPACE_USERS = [ @@ -63,7 +63,10 @@ def seed_db(): Requests.submit(request) requests.append(request) - workspace = Workspaces.create(requests[0], name="{}'s workspace".format(user.first_name)) + request = requests[0] + request.task_order = TaskOrderFactory.build() + + workspace = Workspaces.create(request, name="{}'s workspace".format(user.first_name)) for workspace_user in WORKSPACE_USERS: Workspaces.create_member(user, workspace, workspace_user) diff --git a/tests/factories.py b/tests/factories.py index dd342b09..b462701c 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -170,7 +170,7 @@ class TaskOrderFactory(Base): source = Source.MANUAL funding_type = FundingType.PROC funding_type_other = None - number = "toABC123" + number = factory.Faker("md5") expiration_date = factory.LazyFunction( lambda: datetime.date( datetime.date.today().year + random.randrange(1, 15), 1, 1