Skip to content

Commit

Permalink
Merge pull request #875 from hdoupe/back-notavail
Browse files Browse the repository at this point in the history
Merged #875
  • Loading branch information
hdoupe authored Mar 29, 2018
2 parents f68f846 + 63b17bb commit 1d98b54
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 70 deletions.
2 changes: 2 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Release 1.5.0 on 2018-03-2?
- [#865](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/865) - Extend tests and fix warning/error handling regression - Hank Doupe
- [#868](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/868) - Remove special processing for AMT parameters - Hank Doupe
- [#870](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/870) - Update RELEASES.md and increase compute time to 100 seconds - Hank Doupe
- [#874](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/874) - Add special HTML pages for when a model result is not found or is incompatible - Sean Wang
- [#875](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/875) - Back-end for not_avail.html page - Hank Doupe

Release 1.4.4 on 2018-03-08
----------------------------
Expand Down
12 changes: 6 additions & 6 deletions templates/btax/not_avail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
<div class="container">

<h3>
We are unable to display the results for model run #XXX. We apologize for the inconvenience and are working to
provide a satisfactory solution for displaying and/or recovering results created using older versions of
We are unable to display the results for this simulation. We apologize for the inconvenience and are working to
provide a satisfactory solution for displaying or recovering results created using older versions of
PolicyBrain. Here is one option for re-creating your results:
</h2>

<h3>
Go to the <a href=http://www.ospc.org/ccc/edit/{{ unique_url.pk }}/?start_year={{fill_in_start_year}}>input page</a> and
Go to the <a href = {{ edit_href }}>input page</a> and
re-submit your parameters using the current version of PolicyBrain. Note that for older runs
some parameters may have been deprecated. We recommend
<a href="https://github.com/open-source-economics/B-Tax/tree/{{btax_version}}"> B-tax's</a> documentation for alternative parameter choices.
<a href="https://github.com/open-source-economics/B-Tax/tree/{{btax_version}}"> B-Tax's</a> documentation for alternative parameter choices.
</h3>

<h3>
The B-tax package may have more options for re-creating results with the exact version of
the package used in this run. For more details, please contact the OSPC team.
The B-Tax package may have more options for re-creating results with the exact version of
B-Tax used in this simulation. For more details, please <a href="https://www.ospc.org/hello/"> contact the OSPC team. </a>
</h3>

</div>
Expand Down
8 changes: 4 additions & 4 deletions templates/taxbrain/not_avail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
<div class="container">

<h3>
We are unable to display the results for model run #XXX. We apologize for the inconvenience and are working to
provide a satisfactory solution for displaying and/or recovering results created using older versions of
We are unable to display the results for this simulation. We apologize for the inconvenience and are working to
provide a satisfactory solution for displaying or recovering results created using older versions of
PolicyBrain. Here is one option for re-creating your results:
</h2>

<h3>
Go to the <a href=http://www.ospc.org/taxbrain/edit/{{ unique_url.pk }}/?start_year={{fill_in_start_year}}>input page</a> and
Go to the <a href="{{edit_href}}">input page</a> and
re-submit your parameters using the current version of PolicyBrain. Note that for older runs
some parameters may have been deprecated. We recommend
<a href="https://github.com/open-source-economics/Tax-Calculator/tree/{{taxcalc_version}}"> Tax-Calculator's</a> documentation for alternative parameter choices.
</h3>

<h3>
The Tax-Calculator package may have more options for re-creating results with the exact version of
the package used in this run. For more details, please contact the OSPC team.
Tax-Caclulator used in this simulation. For more details, please <a href="https://www.ospc.org/hello/"> contact the OSPC team. </a>
</h3>

</div>
Expand Down
29 changes: 29 additions & 0 deletions webapp/apps/btax/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mock

from ..models import BTaxSaveInputs, BTaxOutputUrl
from ..forms import BTaxExemptionForm
from ...taxbrain.models import WorkerNodesCounter
from ..compute import (DropqComputeBtax, MockComputeBtax,
MockFailedComputeBtax, NodeDownComputeBtax)
Expand Down Expand Up @@ -161,3 +162,31 @@ def test_btax_edit_ccc_switches_show_correctly(self):
bsi = BTaxSaveInputs.objects.get(pk=out.model_pk)
assert bsi.btax_depr_5yr == u'btax_depr_5yr_ads_Switch'
assert bsi.btax_depr_5yr_ads_Switch == 'True'

def test_get_not_avail_page_renders(self):
"""
Make sure not_avail.html page is rendered if exception is thrown
while parsing results
"""
start_year = 2018
#Monkey patch to mock out running of compute jobs
import sys
from webapp.apps.btax import views as webapp_views
webapp_views.dropq_compute = MockComputeBtax()
fields = {'first_year': str(start_year),
'btax_depr_5yr': 'btax_depr_5yr_ads_Switch'}
form = BTaxExemptionForm(str(start_year), fields)
model = form.save()
model.tax_result = "unrenderable"
model.save()
unique_url = BTaxOutputUrl()
unique_url.unique_inputs = model
unique_url.save()

pk = unique_url.pk
url = '/ccc/{}/'.format(pk)
response = self.client.get(url)
assert any([t.name == 'btax/not_avail.html'
for t in response.templates])
edit_exp = '/ccc/edit/{}/?start_year={}'.format(pk, start_year)
assert response.context['edit_href'] == edit_exp
42 changes: 28 additions & 14 deletions webapp/apps/btax/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pdfkit
import json
import pytz

import traceback
#Mock some module for imports because we can't fit them on Heroku slugs
from mock import Mock
import sys
Expand Down Expand Up @@ -356,19 +356,33 @@ def output_detail(request, pk):

model = url.unique_inputs
if model.tax_result:
exp_num_minutes = 0.25
JsonResponse({'eta': exp_num_minutes, 'wait_interval': 15000}, status=202)
tax_result = url.unique_inputs.tax_result
tables = json.loads(tax_result)[0]
first_year = url.unique_inputs.first_year
created_on = url.unique_inputs.creation_date
tables["tooltips"] = {
"metr": METR_TOOLTIP,
"mettr": METTR_TOOLTIP,
"coc": COC_TOOLTIP,
"dprc": DPRC_TOOLTIP,
}
bubble_js, bubble_div, cdn_js, cdn_css = bubble_plot_tabs(tables['dataframes'])
# try to render table; if failure render not available page
try:
exp_num_minutes = 0.25
JsonResponse({'eta': exp_num_minutes, 'wait_interval': 15000}, status=202)
tax_result = url.unique_inputs.tax_result
tables = json.loads(tax_result)[0]
first_year = url.unique_inputs.first_year
created_on = url.unique_inputs.creation_date
tables["tooltips"] = {
"metr": METR_TOOLTIP,
"mettr": METTR_TOOLTIP,
"coc": COC_TOOLTIP,
"dprc": DPRC_TOOLTIP,
}
bubble_js, bubble_div, cdn_js, cdn_css = bubble_plot_tabs(tables['dataframes'])
except Exception as e:
print('Exception rendering pk', pk, e)
traceback.print_exc()
edit_href = '/ccc/edit/{}/?start_year={}'.format(
pk,
model.first_year
)
print('edit_href', edit_href, pk, model.first_year)
not_avail_context = dict(edit_href=edit_href,
is_btax=True,
**context_vers_disp)
return render(request, 'btax/not_avail.html', not_avail_context)

inputs = url.unique_inputs
is_registered = True if request.user.is_authenticated() else False
Expand Down
41 changes: 40 additions & 1 deletion webapp/apps/dynamic/tests/test_behavioral.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

from .utils import do_dynamic_sim, START_YEAR
from ...test_assets.utils import (check_posted_params, do_micro_sim,
get_post_data, get_file_post_data)
get_post_data, get_file_post_data,
get_taxbrain_model)
from ..models import DynamicBehaviorOutputUrl
from ..forms import DynamicBehavioralInputsModelForm

@pytest.mark.usefixtures("r1")
class DynamicBehavioralViewsTests(TestCase):
Expand Down Expand Up @@ -125,3 +128,39 @@ def test_behavioral_reform_from_file(self):
user_mods = json.loads(post["user_mods"])
assert post["first_budget_year"] == int(START_YEAR)
assert user_mods["behavior"][str(START_YEAR)]["_BE_sub"][0] == 0.25

def test_get_not_avail_page_renders(self):
"""
Make sure not_avail.html page is rendered if exception is thrown
while parsing results
"""
start_year = 2018
fields = get_post_data(start_year, _ID_BenefitSurtax_Switches=False)
fields['BE_sub'] = ['0.25']
fields["first_year"] = start_year
unique_url = get_taxbrain_model(fields,
first_year=start_year,
taxcalc_vers="0.14.2",
webapp_vers="1.3.0",
Form=DynamicBehavioralInputsModelForm,
UrlModel=DynamicBehaviorOutputUrl)

model = unique_url.unique_inputs
model.raw_input_fields = None
model.input_fields = None
model.deprecated_fields = None
model.tax_result = "unrenderable"
model.save()
unique_url.unique_inputs = model
unique_url.save()

pk = unique_url.pk
url = '/dynamic/behavior_results/{}/'.format(pk)
response = self.client.get(url)
assert any([t.name == 'taxbrain/not_avail.html'
for t in response.templates])
edit_exp = '/dynamic/behavioral/edit/{}/?start_year={}'.format(
pk,
start_year
)
assert response.context['edit_href'] == edit_exp
101 changes: 57 additions & 44 deletions webapp/apps/dynamic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#Mock some module for imports because we can't fit them on Heroku slugs
from mock import Mock
import sys
import traceback
MOCK_MODULES = []

sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
Expand Down Expand Up @@ -766,50 +767,62 @@ def behavior_results(request, pk):

model = url.unique_inputs
if model.tax_result:

output = model.get_tax_result()
first_year = model.first_year
created_on = model.creation_date
if 'fiscal_tots' in output:
# Use new key/value pairs for old data
output['aggr_d'] = output['fiscal_tots']
output['aggr_1'] = output['fiscal_tots']
output['aggr_2'] = output['fiscal_tots']
del output['fiscal_tots']

tables = taxcalc_results_to_tables(output, first_year)
tables["tooltips"] = {
'distribution': DISTRIBUTION_TOOLTIP,
'difference': DIFFERENCE_TOOLTIP,
'payroll': PAYROLL_TOOLTIP,
'income': INCOME_TOOLTIP,
'base': BASE_TOOLTIP,
'reform': REFORM_TOOLTIP,
'bins': INCOME_BINS_TOOLTIP,
'deciles': INCOME_DECILES_TOOLTIP
}
is_registered = True if request.user.is_authenticated() else False
hostname = os.environ.get('BASE_IRI', 'http://www.ospc.org')
microsim_url = hostname + "/taxbrain/" + str(model.micro_sim.pk)

# TODO: Fix the java script mapping problem. There exists somewhere in
# the taxbrain javascript code a mapping to the old table names. As
# soon as this is changed to accept the new table names, this code NEEDS
# to be removed.
tables['fiscal_change'] = add_summary_column(tables.pop('aggr_d'))
tables['fiscal_currentlaw'] = add_summary_column(tables.pop('aggr_1'))
tables['fiscal_reform'] = add_summary_column(tables.pop('aggr_2'))
tables['mY_dec'] = tables.pop('dist2_xdec')
tables['mX_dec'] = tables.pop('dist1_xdec')
tables['df_dec'] = tables.pop('diff_itax_xdec')
tables['pdf_dec'] = tables.pop('diff_ptax_xdec')
tables['cdf_dec'] = tables.pop('diff_comb_xdec')
tables['mY_bin'] = tables.pop('dist2_xbin')
tables['mX_bin'] = tables.pop('dist1_xbin')
tables['df_bin'] = tables.pop('diff_itax_xbin')
tables['pdf_bin'] = tables.pop('diff_ptax_xbin')
tables['cdf_bin'] = tables.pop('diff_comb_xbin')
json_table = json.dumps(tables)
# try to render table; if failure render not available page
try:
output = model.get_tax_result()
first_year = model.first_year
created_on = model.creation_date
if 'fiscal_tots' in output:
# Use new key/value pairs for old data
output['aggr_d'] = output['fiscal_tots']
output['aggr_1'] = output['fiscal_tots']
output['aggr_2'] = output['fiscal_tots']
del output['fiscal_tots']

tables = taxcalc_results_to_tables(output, first_year)
tables["tooltips"] = {
'distribution': DISTRIBUTION_TOOLTIP,
'difference': DIFFERENCE_TOOLTIP,
'payroll': PAYROLL_TOOLTIP,
'income': INCOME_TOOLTIP,
'base': BASE_TOOLTIP,
'reform': REFORM_TOOLTIP,
'bins': INCOME_BINS_TOOLTIP,
'deciles': INCOME_DECILES_TOOLTIP
}
is_registered = True if request.user.is_authenticated() else False
hostname = os.environ.get('BASE_IRI', 'http://www.ospc.org')
microsim_url = hostname + "/taxbrain/" + str(model.micro_sim.pk)

# TODO: Fix the java script mapping problem. There exists somewhere in
# the taxbrain javascript code a mapping to the old table names. As
# soon as this is changed to accept the new table names, this code NEEDS
# to be removed.
tables['fiscal_change'] = add_summary_column(tables.pop('aggr_d'))
tables['fiscal_currentlaw'] = add_summary_column(tables.pop('aggr_1'))
tables['fiscal_reform'] = add_summary_column(tables.pop('aggr_2'))
tables['mY_dec'] = tables.pop('dist2_xdec')
tables['mX_dec'] = tables.pop('dist1_xdec')
tables['df_dec'] = tables.pop('diff_itax_xdec')
tables['pdf_dec'] = tables.pop('diff_ptax_xdec')
tables['cdf_dec'] = tables.pop('diff_comb_xdec')
tables['mY_bin'] = tables.pop('dist2_xbin')
tables['mX_bin'] = tables.pop('dist1_xbin')
tables['df_bin'] = tables.pop('diff_itax_xbin')
tables['pdf_bin'] = tables.pop('diff_ptax_xbin')
tables['cdf_bin'] = tables.pop('diff_comb_xbin')
json_table = json.dumps(tables)

except Exception as e:
print('Exception rendering pk', pk, e)
traceback.print_exc()
edit_href = '/dynamic/behavioral/edit/{}/?start_year={}'.format(
pk,
model.start_year
)
not_avail_context = dict(edit_href=edit_href,
**context_vers_disp)
return render(request, 'taxbrain/not_avail.html', not_avail_context)

context = {
'locals':locals(),
Expand Down
30 changes: 30 additions & 0 deletions webapp/apps/taxbrain/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,33 @@ def test_taxbrain_old_data_gives_deprecation_errors(self):
for param in ["ALD_Alimony_hc", "PT_exclusion_rt",
"PT_exclusion_wage_limit"]:
assert msg.format(param) in str(response.context["form"].errors)

def test_get_not_avail_page_renders(self):
"""
Make sure not_avail.html page is rendered if exception is thrown
while parsing results
"""

start_year = 2018
fields = get_post_data(start_year)
fields["first_year"] = start_year
unique_url = get_taxbrain_model(fields,
first_year=start_year,
taxcalc_vers="0.14.2",
webapp_vers="1.3.0")
model = unique_url.unique_inputs
model.raw_input_fields = None
model.input_fields = None
model.deprecated_fields = None
model.tax_result = "unrenderable"
model.save()
unique_url.unique_inputs = model
unique_url.save()

pk = unique_url.pk
url = '/taxbrain/{}/'.format(pk)
response = CLIENT.get(url)
assert any([t.name == 'taxbrain/not_avail.html'
for t in response.templates])
edit_exp = '/taxbrain/edit/{}/?start_year={}'.format(pk, start_year)
assert response.context['edit_href'] == edit_exp
16 changes: 15 additions & 1 deletion webapp/apps/taxbrain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import tempfile
import re
import traceback


#Mock some module for imports because we can't fit them on Heroku slugs
Expand Down Expand Up @@ -758,7 +759,20 @@ def output_detail(request, pk):
context_vers_disp = {'taxcalc_version': taxcalc_vers_disp,
'webapp_version': webapp_vers_disp}
if model.tax_result:
context = get_result_context(model, request, url)
# try to render table; if failure render not available page
try:
context = get_result_context(model, request, url)
except Exception as e:
print('Exception rendering pk', pk, e)
traceback.print_exc()
edit_href = '/taxbrain/edit/{}/?start_year={}'.format(
pk,
model.start_year
)
not_avail_context = dict(edit_href=edit_href,
**context_vers_disp)
return render(request, 'taxbrain/not_avail.html', not_avail_context)

context.update(context_vers_disp)
context["raw_reform_text"] = model.json_text.raw_reform_text if model.json_text else ""
context["raw_assumption_text"] = model.json_text.raw_assumption_text if model.json_text else ""
Expand Down

0 comments on commit 1d98b54

Please sign in to comment.