Newer
Older
#!/usr/bin/env python3
from flask import Flask
from flask import request
from flask import jsonify
from pymongo import MongoClient
from bson.json_util import dumps
import json
client = MongoClient('localhost:27017')
db = client.run_list
latest_db = client.latest
@app.route("/add_coin_run", methods = ['POST'])
def add_contact():
try:
data = json.loads(request.data)
run_number = data['run_number']
if run_number:
status = db["coin"].insert_one(
{str(run_number): data}
)
return dumps({'message' : 'SUCCESS'})
except Exception:
return dumps({'error' : str(e)})
@app.route("/get_all_latest", methods = ['GET'])
def get_all_latest():
try:
runs = latest_db['coin'].find()
res = json.loads(dumps(runs))
runs = latest_db['hms'].find()
res2 = json.loads(dumps(runs))
runs = latest_db['shms'].find()
res3 = json.loads(dumps(runs))
res4 = [res, res2, res3]
return jsonify(res4)
except Exception:
return dumps({'error' : str(e)})
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
@app.route("/get_all_runs", methods = ['GET'])
def get_all_contact():
try:
runs = db['coin'].find()
print("derp")
return jsonify(json.loads(dumps(runs)))
except Exception:
return dumps({'error' : str(e)})
@app.route("/get_run/<run_number>", methods = ['GET'])
def get_run(run_number):
try:
runs = db['coin'].find({"run_number": str(run_number)})
print("derp {}".format(run_number))
#print(dumps(runs))
return jsonify(json.loads(dumps(runs)))
except Exception:
return dumps({'error' : str(e)})
@app.route("/get_run_info/<run_number>", methods = ['GET'])
def get_run_info(run_number):
try:
runs = db['coin'].find({"run_number": str(run_number)},{ "_id": 0, "run_info": 1})
print("derp {}".format(run_number))
#print(dumps(runs))
return jsonify(json.loads(dumps(runs)))
except Exception:
return dumps({'error' : str(e)})
@app.route("/run_info/<run_number>/<value>", methods = ['GET'])
def get_run_infovalue(run_number,value):
try:
runs = db['coin'].find({"run_number": str(run_number)},{ "_id": 0, "run_info": 1})
print("derp {}".format(run_number))
#print(dumps(runs))
return jsonify(json.loads(dumps(runs))[0]['run_info'][str(value)])
except Exception:
return dumps({'error' : str(e)})
@app.route("/run/latest/<daq>/<value>", methods = ['GET'])
def get_latest_run(daq,value):
try:
res = latest_db[daq].find({},{ "_id": 0, str(value): 1})
#print("derp {}".format(run_number))
#print(dumps(res))
json_res = json.loads(dumps(res))
print(json_res)
return jsonify(json_res[0][str(value)])
except Exception:
return dumps({'error' : str(e)})
data = json.loads(request.data)
print(data)
run_number = data['run_number']
print(run_number)
if not daq in ["coin","hms","shms"]:
return dumps({'error' : "daq value {} is not available".format(daq) })
if run_number:
status = latest_db[daq].update_one({}, {"$set": data})
print(status)
else :
return dumps({'error' : "missing run number" })
return dumps({'message' : 'SUCCESS'})
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
@app.route("/run/ref/<daq>/<value>", methods = ['GET'])
def get_ref_run(daq,value):
try:
res = ref_db[daq].find({},{ "_id": 0, str(value): 1})
#print("derp {}".format(run_number))
#print(dumps(res))
json_res = json.loads(dumps(res))
print(json_res)
return jsonify(json_res[0][str(value)])
except Exception:
return dumps({'error' : str(e)})
@app.route("/run/ref/<daq>", methods = ['POST'])
def update_ref_info(daq):
try:
data = json.loads(request.data)
print(data)
run_number = data['run_number']
print(run_number)
if not daq in ["coin","hms","shms"]:
return dumps({'error' : "daq value {} is not available".format(daq) })
if run_number:
status = ref_db[daq].update_one({}, {"$set": data})
print(status)
else :
return dumps({'error' : "missing run number" })
return dumps({'message' : 'SUCCESS'})
except Exception:
return dumps({'error' : "derp" })
if __name__ == "__main__":
app.run(host="cdaql1.jlab.org",port=5000)