From 54648a7f15795e9e3516c2ee4e8d6ffa80bee62a Mon Sep 17 00:00:00 2001 From: Steve Nyemba Date: Thu, 21 Dec 2017 20:33:35 -0600 Subject: [PATCH] CO: queue/message processing --- src/api/index.py | 177 ++++++++++++++--------------------------- src/utils/transport.py | 2 +- 2 files changed, 61 insertions(+), 118 deletions(-) diff --git a/src/api/index.py b/src/api/index.py index 4998686..5cfee85 100755 --- a/src/api/index.py +++ b/src/api/index.py @@ -95,94 +95,25 @@ def register() : plan = body['plan'] handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=uid) r = handler.register(plan) - return json.dumps(r) - -def __register(): - """ - This function is intended to manage users i.e : register a user given an email - Once the email is provided a key will be generated for the user - @param uid user's email address - @param plan plan identifier (monitor-{light,basic,xxx} - - """ - global p - - body = request.get_json(silent=True) + session['key'] = r['key'] # - # Before we start we must insure the user actually exists + # We should try to run a thread that loads the data for the user so when she requests it it is ready + # @TODO: # - print body['uid'] - auid = str(uuid.uuid4()) - headers = {"uid":body['uid'],"pid":body['plan'],"auid":json.dumps([auid])} - couchdb = factory.instance(type=class_read,args=p) - r = couchdb.view('federation/uid_map',key=body['uid']) - - if not r : - body['plan'] = CONFIG['plan'] - auid = str(uuid.uuid4()) - - headers = {"uid":body['uid'],"pid":body['plan'],"auid":json.dumps([auid])} - q = couchdb.view('federation/uid_map',key=auid) - - # The uid has not yet been assigned - url="https://the-phi.com/store/init/monitor" - r = requests.post(url,headers=headers) - - # - # Now we should create a document with the generated key - # @TODO: Have an endpoint map uid/key - args = dict(p) - args['dbname'] = body['plan'] - #args['dbname'] = body['plan'] - args['uid'] = auid - - couchdb = factory.instance(type=class_write,args=args) - couchdb.write(label='emails',row=[auid,body['uid']]) - # couchdb.write(label='emails',row=[auid]) - plan = {body['plan']:json.loads(r.text)} - session['plan'] = [plan] - couchdb.write(label='plan',row=plan) - r = auid - - pass - - else: - # - # at this point we have the alternate identifier - #@TODO : Initiate a retry - # - + try: + session['plan'] = r + key = r['key'] args = json.loads(json.dumps(p)) - couchdb = factory.instance(type=class_read,args=args) - args['uid'] = r #-- returned uid - plan = couchdb.view('plans/active',key=r) - args['dbname'] = plan['id'] - # - # get the new table i.e user's auid - args['uid'] = body['uid'] - couchdb = factory.instance(type=class_read,args=args) - document = couchdb.read() - - r = list(set(document['emails']) - set(body['uid'])) - r = [item for item in r if len(item) == 36] - if len(r) > 0 : - r = r[len(r)-1] - session['plan'] = document['plan'] - else: - r = "" - pass - # - # make sure the plan is a valid one i.e monitor-*-* - # This should be stored in the session so we can proceed going forward - if r != "": - session['key'] = r - return r -def init(): - """ - This function is designed to initialize with fundamental data i.e : - Which plan is valid, and currently on going. - """ - pass + args['dbname'] = r['name'] + gReader = factory.instance(type=class_read,args=args) + session['logs'] = {} + for id in ['app_status','log_size','emails'] : + session['logs'][id] = gReader.view('summary/'+id.strip(),key=key) + except Exception as e: + print (e) + + return json.dumps(r) + @app.route('/1/get/nodes') def get_nodes(): """ @@ -228,54 +159,66 @@ def get_logs() : else: try: - gReader = factory.instance(type=class_read,args=p) - plan = gReader.view('plans/active',key=key) - + #gReader = factory.instance(type=class_read,args=p) + #plan = gReader.view('plans/active',key=key) + plan = session['plan'] if plan : dbname = plan['name'] args = str(json.dumps(p)) args = json.loads(args) - args['dbname'] = dbname - scope = ['app_status','app_resources','folder_info','app_status_details','app_resource_usage_details'] + args['dbname'] = dbname + args['uid'] = key + # + # Let us persis this information (as well as the key) + #session['plan'] = plan['name'] + session['store']= args + session['key'] = key + + scope = ['app_resources','folder_info','app_status_details','app_resource_usage_details'] #,'emails','log_size'] + gReader = factory.instance(type=class_read,args=args) for id in scope : - gReader = factory.instance(type=class_read,args=args) view = ('summary/'+id).strip() r[id] = gReader.view(view,key=key) - + if 'logs' in session : + for id in session['logs'] : + r[id] = session['logs'][id] # r[id] = r[node_id] - except Exception,e: print (e) return json.dumps(r) - -@app.route('/1/get/logs/') -def get_summary(id): - """ - This function returns the summary i.e an overall assessment of resource usage - It will pull information out of the user's data-store (database & document) specified in the configuration - @param id {app_resources|app_status|folder_size} - """ - r = [] +@app.route("/1/set/logs",methods=['PUT']) +def update_profile(): try: + emails = request.get_json(silent=True) + args = session['store'] + emails = [item['email'] for item in emails] - key = request.headers['key'] - default_plan = CONFIG['plan'] - handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=key) - if handler.is_registered() : - plan = handler.get_info() - - args= json.loads(json.dumps(p)) - args['dbname'] = plan['name'] - args['uid'] = key - - gReader = factory.instance(type=class_read,args=args) - r = gReader.read() - r = r[id][len(r[id])-1] - except Exception,e: + gWriter= factory.instance(type=class_write,args=args) + gWriter.write(label='emails',data=emails) + pass + except Exception as e : print (e) - return json.dumps(r) + return '',204 +@app.route("/1/message",methods=['POST']) +def send_to_app () : + """ + This function will send a message to an agent, that will execute the appropriate command + @TODO: Provide some kind of feedback + """ + body = request.get_json(silent=True) + key = session['key'] + args = {"host":CONFIG['api'],"qid":body["node"],"uid":key} + print key + qhandler= factory.instance(type='QueueWriter',args=args) + label=body["node"] + qhandler.write(label=label,row=body) + + return '',204 +@app.route("/1/message/folder",methods=["POST"]) +def send_to_folder(): + pass @app.route("/1/sys/usage/trend") def get_usage_trend(): """ diff --git a/src/utils/transport.py b/src/utils/transport.py index f56569e..eeee0d3 100755 --- a/src/utils/transport.py +++ b/src/utils/transport.py @@ -601,7 +601,7 @@ class CouchdbWriter(Couchdb,Writer): else : if label not in document : document[label] = {} - if isinstance(params['data'],object) : + if isinstance(params['data'],list) == False : document[label] = dict(document[label],**params['data']) else: