Aardvark beluga #160300247
This commit is contained in:
dandds 2018-09-20 13:03:31 -04:00 committed by GitHub
commit 81f56dd881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 39 deletions

View File

@ -141,22 +141,32 @@ CUMULATIVE_BUDGET_BELUGA = {
"09/2018": {"spend": 14500, "cumulative": 19338}, "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,
},
}
class Reports:
@classmethod
def workspace_totals(cls, workspace):
if workspace.request and workspace.request.task_order:
ws_to = workspace.request.task_order
budget = ws_to.budget
else:
budget = 0
# spent will be derived from CSP data def _sum_monthly_spend(data):
return {"budget": budget, "spent": 0} return sum(
[
spend
for project in data.values()
for env in project.values()
for spend in env.values()
]
)
@classmethod
def monthly_totals(cls, alternate): def _derive_project_totals(data):
data = MONTHLY_SPEND_BELUGA if alternate else MONTHLY_SPEND_AARDVARK
project_totals = {} project_totals = {}
for project, environments in data.items(): for project, environments in data.items():
project_spend = [ project_spend = [
@ -169,6 +179,10 @@ class Reports:
for month, spends in groupby(sorted(project_spend), lambda x: x[0]) for month, spends in groupby(sorted(project_spend), lambda x: x[0])
} }
return project_totals
def _derive_workspace_totals(project_totals):
monthly_spend = [ monthly_spend = [
(month, spend) (month, spend)
for project in project_totals.values() for project in project_totals.values()
@ -178,6 +192,37 @@ class Reports:
for month, spends in groupby(sorted(monthly_spend), lambda m: m[0]): for month, spends in groupby(sorted(monthly_spend), lambda m: m[0]):
workspace_totals[month] = sum([spend[1] for spend in spends]) 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:
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
return {"budget": budget, "spent": spent}
@classmethod
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 { return {
"environments": data, "environments": data,
"projects": project_totals, "projects": project_totals,
@ -185,9 +230,10 @@ class Reports:
} }
@classmethod @classmethod
def cumulative_budget(cls, alternate): def cumulative_budget(cls, workspace):
return { if workspace.name in REPORT_FIXTURE_MAP:
"months": CUMULATIVE_BUDGET_BELUGA months = REPORT_FIXTURE_MAP[workspace.name]["cumulative"]
if alternate else:
else CUMULATIVE_BUDGET_AARDVARK months = {}
}
return {"months": months}

View File

@ -1,4 +1,5 @@
import re import re
import datetime
from flask import current_app as app from flask import current_app as app
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
@ -72,6 +73,10 @@ def formattedDate(value, formatter="%m/%d/%Y"):
return "-" return "-"
def dateFromString(value, formatter="%m/%Y"):
return datetime.datetime.strptime(value, formatter)
def register_filters(app): def register_filters(app):
app.jinja_env.filters["iconSvg"] = iconSvg app.jinja_env.filters["iconSvg"] = iconSvg
app.jinja_env.filters["dollars"] = dollars app.jinja_env.filters["dollars"] = dollars
@ -82,3 +87,4 @@ def register_filters(app):
app.jinja_env.filters["findFilter"] = findFilter app.jinja_env.filters["findFilter"] = findFilter
app.jinja_env.filters["renderList"] = renderList app.jinja_env.filters["renderList"] = renderList
app.jinja_env.filters["formattedDate"] = formattedDate app.jinja_env.filters["formattedDate"] = formattedDate
app.jinja_env.filters["dateFromString"] = dateFromString

View File

@ -98,7 +98,6 @@ def workspace_reports(workspace_id):
"view workspace reports", "view workspace reports",
) )
alternate_reports = http_request.args.get("alternate")
today = date.today() today = date.today()
month = http_request.args.get("month", today.month) month = http_request.args.get("month", today.month)
year = http_request.args.get("year", today.year) year = http_request.args.get("year", today.year)
@ -113,9 +112,9 @@ def workspace_reports(workspace_id):
return render_template( return render_template(
"workspaces/reports/index.html", "workspaces/reports/index.html",
cumulative_budget=Reports.cumulative_budget(alternate_reports), cumulative_budget=Reports.cumulative_budget(workspace),
workspace_totals=Reports.workspace_totals(workspace), workspace_totals=Reports.workspace_totals(workspace),
monthly_totals=Reports.monthly_totals(alternate_reports), monthly_totals=Reports.monthly_totals(workspace),
current_month=current_month, current_month=current_month,
prev_month=prev_month, prev_month=prev_month,
two_months_ago=two_months_ago, two_months_ago=two_months_ago,

View File

@ -12,7 +12,7 @@ from atst.domain.requests import Requests
from atst.domain.workspaces import Workspaces from atst.domain.workspaces import Workspaces
from atst.domain.projects import Projects from atst.domain.projects import Projects
from atst.domain.exceptions import AlreadyExistsError 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 from atst.routes.dev import _DEV_USERS as DEV_USERS
WORKSPACE_USERS = [ WORKSPACE_USERS = [
@ -63,7 +63,10 @@ def seed_db():
Requests.submit(request) Requests.submit(request)
requests.append(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: for workspace_user in WORKSPACE_USERS:
Workspaces.create_member(user, workspace, workspace_user) Workspaces.create_member(user, workspace, workspace_user)

View File

@ -301,8 +301,21 @@
<div class='spend-table__header'> <div class='spend-table__header'>
<h2 class='spend-table__title'>Total spend per month </h2> <h2 class='spend-table__title'>Total spend per month </h2>
<select name='month' id='month' class='spend-table__month-select'> <select name='month' id='month' onchange='location = this.value' class='spend-table__month-select'>
<option value='03/2019'>{{ current_month.strftime('%B %Y') }}</option> {% for m in cumulative_budget["months"] %}
{% set month = m | dateFromString %}
<option
{% if month.month == current_month.month and month.year == current_month.year %}
selected='selected'
{% endif %}
value='{{ url_for("workspaces.workspace_reports",
workspace_id=workspace.id,
month=month.month,
year=month.year) }}'
>
{{ month.strftime('%B %Y') }}
</option>
{% endfor %}
</select> </select>
</div> </div>

View File

@ -170,7 +170,7 @@ class TaskOrderFactory(Base):
source = Source.MANUAL source = Source.MANUAL
funding_type = FundingType.PROC funding_type = FundingType.PROC
funding_type_other = None funding_type_other = None
number = "toABC123" number = factory.Faker("md5")
expiration_date = factory.LazyFunction( expiration_date = factory.LazyFunction(
lambda: datetime.date( lambda: datetime.date(
datetime.date.today().year + random.randrange(1, 15), 1, 1 datetime.date.today().year + random.randrange(1, 15), 1, 1