forked from ourresearch/jump-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsortium_calculate.py
80 lines (57 loc) · 2.71 KB
/
consortium_calculate.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
# coding: utf-8
import random
import datetime
from time import time
from time import sleep
import argparse
from app import get_db_cursor
from consortium import Consortium
from emailer import create_email, send
from util import elapsed
def consortium_calculate():
# command = "truncate jump_scenario_computed_update_queue"
# print command
# with get_db_cursor() as cursor:
# cursor.execute(command)
while True:
command = "select * from jump_scenario_computed_update_queue where completed is null order by random()"
# print command
with get_db_cursor() as cursor:
cursor.execute(command)
rows = cursor.fetchall()
for row in rows:
start_time = time()
print("in consortium_calculate, starting recompute_journal_dicts for scenario_id {}".format(
row["scenario_id"]))
my_consortium = Consortium(row["scenario_id"])
my_consortium.recompute_journal_dicts()
print("in consortium_calculate, done recompute_journal_dicts for scenario_id {} took {}s".format(
row["scenario_id"], elapsed(start_time)))
print("updating jump_scenario_computed_update_queue with completed")
command = "update jump_scenario_computed_update_queue set completed=sysdate where scenario_id='{}' and completed is null".format(
row["scenario_id"])
# print command
with get_db_cursor() as cursor:
cursor.execute(command)
if row["email"]:
print("SENDING EMAIL")
done_email = create_email(row["email"], 'Unsub update complete', 'update_done', {
'data': {
'consortium_name': row.get("consortium_name", ""),
'package_name': row.get("package_name", ""),
'start_time': row.get("created", ""),
'end_time': datetime.datetime.utcnow().isoformat(),
'institution_id': row.get("institution_id", ""),
'package_id': row.get("package_id", ""),
'scenario_id': row["scenario_id"]
}})
send(done_email, for_real=True)
print("SENT EMAIL DONE")
print("DONE UPDATING", row["scenario_id"])
sleep( 2 * random.random())
# python consortium_calculate.py
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run stuff :)")
parsed_args = parser.parse_args()
parsed_vars = vars(parsed_args)
consortium_calculate()