Skip to content
Snippets Groups Projects
app.py 4.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • Cdaq User's avatar
    Cdaq User committed
    #!/usr/bin/env python3
    from flask import Flask
    from flask import request
    from flask import jsonify
    
    from flask_cors import CORS
    
    Cdaq User's avatar
    Cdaq User committed
    from pymongo import MongoClient
    from bson.json_util import dumps
    import json
    
    client = MongoClient('localhost:27017')
    db = client.run_list
    latest_db = client.latest
    
    ref_db = client.reference
    
    Cdaq User's avatar
    Cdaq User committed
    
    app = Flask(__name__)
    
    CORS(app)
    
    Cdaq User's avatar
    Cdaq User committed
    
    @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)})
    
    
    Cdaq User's avatar
    Cdaq User committed
    @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)})
    
    
    Whitney Armstrong's avatar
    Whitney Armstrong committed
    @app.route("/run/latest/<daq>", methods = ['POST'])
    
    def update_run_latest(daq):
    
    Whitney Armstrong's avatar
    Whitney Armstrong committed
        try:
    
            print(request.data)
    
    Whitney Armstrong's avatar
    Whitney Armstrong committed
            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'})
    
        except Exception as ex:
            print(ex)
    
    Whitney Armstrong's avatar
    Whitney Armstrong committed
            return dumps({'error' : "derp" })
    
    
    Cdaq User's avatar
    Cdaq User committed
    
    
    @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" })
    
    
    
    Cdaq User's avatar
    Cdaq User committed
    
    if __name__ == "__main__":
        app.run(host="cdaql1.jlab.org",port=5000)