-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcelery_tasks.py
More file actions
148 lines (120 loc) · 4.72 KB
/
celery_tasks.py
File metadata and controls
148 lines (120 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
import os
from io import BytesIO
from celery import Celery
import taxcalc
import btax
from btax.front_end_util import runner_json_tables
from collections import defaultdict
CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL',
'redis://localhost:6379')
CELERY_RESULT_BACKEND = os.environ.get('CELERY_RESULT_BACKEND',
'redis://localhost:6379')
celery_app = Celery('tasks2', broker=CELERY_BROKER_URL,
backend=CELERY_RESULT_BACKEND)
celery_app.conf.update(
task_serializer='json',
accept_content=['msgpack', 'json'],
)
def dropq_task(year_n, user_mods, start_year, use_puf_not_cps=True,
use_full_sample=True):
print(
'keywords to dropq',
dict(
year_n_n=year_n,
start_year=int(start_year),
use_puf_not_cps=use_puf_not_cps,
use_full_sample=use_full_sample,
user_mods=user_mods
)
)
raw_data = taxcalc.tbi.run_nth_year_taxcalc_model(
year_n=year_n,
start_year=int(start_year),
use_puf_not_cps=use_puf_not_cps,
use_full_sample=use_full_sample,
user_mods=user_mods
)
return raw_data
def postprocess(ans, postprocess_func):
all_to_process = defaultdict(list)
for year_data in ans:
for key, value in year_data.items():
all_to_process[key] += value
results = postprocess_func(all_to_process)
# Add taxcalc version to results
vinfo = taxcalc._version.get_versions()
results['taxcalc_version'] = vinfo['version']
# TODO: Make this the distributed app version, not the TC version
results['dropq_version'] = vinfo['version']
return json.dumps(results)
@celery_app.task(name='api.celery_tasks.dropq_task_async')
def dropq_task_async(year_n, user_mods, start_year, use_puf_not_cps=True):
return dropq_task(year_n, user_mods, start_year,
use_puf_not_cps=use_puf_not_cps, use_full_sample=True)
@celery_app.task(name='api.celery_tasks.dropq_task_small_async')
def dropq_task_small_async(year_n, user_mods, start_year,
use_puf_not_cps=True):
return dropq_task(year_n, user_mods, start_year,
use_puf_not_cps=use_puf_not_cps, use_full_sample=False)
@celery_app.task(name='api.celery_tasks.taxbrain_postprocess')
def taxbrain_postprocess(ans):
return postprocess(ans, taxcalc.tbi.postprocess)
@celery_app.task(name='api.celery_tasks.taxbrain_elast_async')
def taxbrain_elast_async(year_n, start_year,
use_puf_not_cps,
user_mods,
gdp_elasticity,
use_full_sample=True,
return_dict=True):
gdp_elast_i = taxcalc.tbi.run_nth_year_gdp_elast_model(
year_n=year_n,
start_year=start_year,
use_puf_not_cps=use_puf_not_cps,
use_full_sample=use_full_sample,
user_mods=user_mods,
gdp_elasticity=gdp_elasticity,
return_dict=True
)
print(gdp_elast_i)
return gdp_elast_i
@celery_app.task(name='api.celery_tasks.taxbrain_elast_postprocess')
def taxbrain_elast_postprocess(ans):
return postprocess(ans, taxcalc.tbi.postprocess_elast)
@celery_app.task(name='api.celery_tasks.btax_async')
def btax_async(user_mods, start_year):
print("user mods: ", user_mods)
user_mods['start_year'] = start_year
print("submitting btax data: ", user_mods)
results = {}
tables = json.loads(runner_json_tables(**user_mods))
if tables.get("json_table"):
results.update(tables["json_table"])
if tables.get("dataframes"):
dataframes = json.loads(tables["dataframes"])
for x, y in list(dataframes.items()):
dataframes[x] = json.loads(y)
results["dataframes"] = dataframes
else:
results.update(tables)
vinfo = taxcalc._version.get_versions()
results['taxcalc_version'] = vinfo['version']
results['dropq_version'] = vinfo['version']
binfo = btax._version.get_versions()
results['btax_version'] = binfo['version']
json_res = json.dumps(results)
return json_res
@celery_app.task(name='api.celery_tasks.file_upload_test')
def file_upload_test(data, compression):
import pandas as pd
df = pd.read_csv(BytesIO(data), compression='gzip')
desc = df.describe()
formatted = {'outputs': [], 'aggr_outputs': []}
formatted['aggr_outputs'].append({
'tags': {'default': 'default'},
'title': 'desc',
'downloadable': [{'filename': 'desc' + '.csv',
'text': desc.to_csv()}],
'renderable': desc.to_html()
})
return json.dumps(formatted)