-
Notifications
You must be signed in to change notification settings - Fork 6
/
render_template.py
145 lines (118 loc) · 5.32 KB
/
render_template.py
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
import argparse
import collections
import json
import pprint
from flask import Flask, render_template
app = Flask(__name__, template_folder=".")
DATA_JSON_PATH = "data.json"
TEMPLATE_PATH = "main_page_template.html"
DAILY_TEMPLATE_PATH = "daily_page_template.html"
CATEGORY_MAP = {
"INCONCLUSIVE": "inconclusive",
"NEG": "negative",
"POS": "positive",
"INVALID": "inconclusive"
}
def compute_template_args():
with open(DATA_JSON_PATH, "rt") as f:
data = json.load(f)
counts_by_day = collections.defaultdict(lambda: collections.defaultdict(int))
for entry in data['results']:
day = entry['day']
category = entry['result']
try:
category = CATEGORY_MAP[category]
except KeyError as e:
print("WARNING: unknown cateory: " + str(category) + " in entry: " + str(entry))
continue
counts_by_day[day][category] += entry['count']
if entry['locale'] == "MA":
counts_by_day[day]['samplesFromMA' ] += entry['count']
elif entry['locale'] == "Non-MA":
counts_by_day[day]['samplesFromOutOfState'] += entry['count']
# note the pool count also only includes POSITIVES, NEGATIVES, INCONCLUSIVES, AND INVALIDS
if 'pooled_samples' in entry:
# counts total number of pooled samples
counts_by_day[day]['total_pooled_samples'] += entry['pooled_samples']
if category == 'positive':
counts_by_day[day]['total_pooled_positives'] += entry['pooled_samples']
if 'pool_size' in entry:
# counts total number of individuals tested via pooling (swab count by tube)
counts_by_day[day]['total_pooled_individuals'] += entry['pool_size']
entries_by_day = []
total_completed = 0
total_positive = 0
total_inconclusive = 0
total_from_MA = 0
total_from_out_of_state = 0
total_pooled_samples = 0
total_pooled_individuals = 0
total_pooled_positives = 0
for day, counters in counts_by_day.items():
if day == '2020-03-23' or day == '2020-03-24': # or day == datetime.datetime.now().strftime("%Y-%m-%d"):
continue
dateTokens = day.split("-")
shortDate = str(int(dateTokens[1])) + "/" +str(int(dateTokens[2]))
total_completed += counters['positive'] + counters['negative'] + counters['inconclusive']
total_positive += counters['positive']
total_inconclusive += counters['inconclusive']
total_from_MA += counters['samplesFromMA']
total_from_out_of_state += counters['samplesFromOutOfState']
total_pooled_samples += counters['total_pooled_samples']
total_pooled_individuals += counters['total_pooled_individuals']
total_pooled_positives += counters['total_pooled_positives']
entries_by_day.append({
'day': day,
'positive': counters['positive'],
'negative': counters['negative'],
'inconclusive': counters['inconclusive'],
'shortDate': shortDate,
'samplesPooled': counters['total_pooled_samples'],
'individualsPooled': counters['total_pooled_individuals'],
'poolPositives': counters['total_pooled_positives']
#'samplesFromMA': counters['samplesFromMA'],
#'samplesFromOutOfState': counters['samplesFromOutOfState'],
})
entries_by_day.sort(key=lambda x: x['day'])
total_positive_percent = int(round((100.0 * total_positive) / total_completed))
total_inconclusive_percent = "%0.1f" % round((100.0 * total_inconclusive) / total_completed, 1)
total_from_MA_percent = int(round((100.0 * total_from_MA) / total_completed))
total_from_out_of_state_percent = int(round((100.0 * total_from_out_of_state) / total_completed))
result = {
'DATA': entries_by_day,
'TOTAL_COMPLETED': f"{total_completed:,}",
'TOTAL_POSITIVE': f"{total_positive:,}",
'TOTAL_POSITIVE_PERCENT': total_positive_percent,
'TOTAL_INCONCLUSIVE': f"{total_inconclusive:,}",
'TOTAL_INCONCLUSIVE_PERCENT': total_inconclusive_percent,
'TOTAL_FROM_MA': f"{total_from_MA:,}",
'TOTAL_FROM_MA_PERCENT': total_from_MA_percent,
'TOTAL_FROM_OUT_OF_STATE': f"{total_from_out_of_state:,}",
'TOTAL_FROM_OUT_OF_STATE_PERCENT': total_from_out_of_state_percent,
'TOTAL_POOLED': total_pooled_samples,
'TOTAL_INDIVIDUALS_POOLED': total_pooled_individuals,
'TOTAL_POOLED_POSITIVES': total_pooled_positives
}
#with open("daily_counts.json", "wt") as f:
# json.dump(entries_by_day, f)
pprint.pprint(result)
return result
@app.route("/")
def index():
template_args = compute_template_args()
return render_template(TEMPLATE_PATH, **template_args)
def main():
p = argparse.ArgumentParser()
p.add_argument("--dev", action="store_true", help="Dev mode. Starts a server on localhost for viewing the template.")
args = p.parse_args()
if args.dev:
app.run(debug=True)
return
template_args = compute_template_args()
with app.app_context():
with open("index.html", "wt") as f:
f.write(render_template(TEMPLATE_PATH, **template_args))
with open("daily/index.html", "wt") as f:
f.write(render_template(DAILY_TEMPLATE_PATH, **template_args))
if __name__ == "__main__":
main()