-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_answers.py
74 lines (63 loc) · 2.44 KB
/
get_answers.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
import json
import requests
import datamodel as dm
import configparser
import re
import csv
from datetime import datetime, timedelta, date
qrowddb_tasks = "http://streambase1.disi.unitn.it:8095/gettasksbyuser"
qrowddb_answer = "http://streambase1.disi.unitn.it:8095/gettaskanswer"
# For trip detected question
def get_task_answer(user_id, task_id):
headers = {
'cache-control' : 'no-cache',
'email': '[email protected]',
'password': '202c653a505f6815357bfa5d067ca5af0a742a839d764582e686d826554b52ad',
'userid': user_id,
'instanceid': task_id
}
print(headers)
r = requests.get(qrowddb_answer, headers=headers)
return r.json()
def get_trip_answers(date,user_id):
qs = dm.Question.select().join(dm.Trip).where((dm.Question.citizen_id == user_id) & (dm.Trip.start_timestamp <= date) & (dm.Question.answer_json.is_null()))
ans = [(q.task_id,get_task_answer(user_id,q.task_id)) for q in qs]
return ans
def get_failsafe_answers(date,user_id):
qs = dm.QuestionFailsafe.select().where((dm.QuestionFailsafe.citizen_id == user_id) & (dm.QuestionFailsafe.date <= date) & (dm.QuestionFailsafe.answer_json.is_null()))
ans = [(q.task_id,get_task_answer(user_id,q.task_id)) for q in qs]
return ans
def update_answer(answer,qtype='Trip'):
ans = answer[1]
if ans['status'] == 'error_message':
print(ans['status'])
return None
if qtype == 'Trip':
q = dm.Question.get(dm.Question.task_id == answer[0])
elif qtype == 'Failsafe':
q = dm.QuestionFailsafe.get(dm.QuestionFailsafe.task_id == answer[0])
q.answer_json = ans
q.save()
return q
# The flow of the current answer means that all edits are in the json file
def update_answers(answers,qtype='Trip'):
updates = {'answered':0,'pending':0}
for a in answers:
q = update_answer(a,qtype)
if q is None:
updates['pending'] += 1
else:
updates['answered'] += 1
return updates
def main():
config = configparser.ConfigParser()
config.read('test/test-config.ini')
dm.db.init('test/'+config['db']['dbPath'])
citizen = config['test']['test_citizen_id']
instance = config['ilog']['expcode']
#ans = get_trip_answers(datetime(2019,5,21),citizen)
ans = get_failsafe_answers(datetime(2019,8,21),citizen)
update = update_answers(ans,qtype='Failsafe')
print(update)
if __name__=="__main__":
main()