CO: queue/message processing

community
Steve Nyemba 7 years ago
parent 26be112e31
commit 54648a7f15

@ -95,94 +95,25 @@ def register() :
plan = body['plan'] plan = body['plan']
handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=uid) handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=uid)
r = handler.register(plan) r = handler.register(plan)
return json.dumps(r) session['key'] = r['key']
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)
# #
# 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'] try:
auid = str(uuid.uuid4()) session['plan'] = r
headers = {"uid":body['uid'],"pid":body['plan'],"auid":json.dumps([auid])} key = r['key']
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
#
args = json.loads(json.dumps(p)) args = json.loads(json.dumps(p))
couchdb = factory.instance(type=class_read,args=args) args['dbname'] = r['name']
args['uid'] = r #-- returned uid gReader = factory.instance(type=class_read,args=args)
plan = couchdb.view('plans/active',key=r) session['logs'] = {}
args['dbname'] = plan['id'] for id in ['app_status','log_size','emails'] :
# session['logs'][id] = gReader.view('summary/'+id.strip(),key=key)
# get the new table i.e user's auid except Exception as e:
args['uid'] = body['uid'] print (e)
couchdb = factory.instance(type=class_read,args=args)
document = couchdb.read() return json.dumps(r)
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
@app.route('/1/get/nodes') @app.route('/1/get/nodes')
def get_nodes(): def get_nodes():
""" """
@ -228,54 +159,66 @@ def get_logs() :
else: else:
try: try:
gReader = factory.instance(type=class_read,args=p) #gReader = factory.instance(type=class_read,args=p)
plan = gReader.view('plans/active',key=key) #plan = gReader.view('plans/active',key=key)
plan = session['plan']
if plan : if plan :
dbname = plan['name'] dbname = plan['name']
args = str(json.dumps(p)) args = str(json.dumps(p))
args = json.loads(args) args = json.loads(args)
args['dbname'] = dbname args['dbname'] = dbname
scope = ['app_status','app_resources','folder_info','app_status_details','app_resource_usage_details'] 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 : for id in scope :
gReader = factory.instance(type=class_read,args=args)
view = ('summary/'+id).strip() view = ('summary/'+id).strip()
r[id] = gReader.view(view,key=key) 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] # r[id] = r[node_id]
except Exception,e: except Exception,e:
print (e) print (e)
return json.dumps(r) return json.dumps(r)
@app.route("/1/set/logs",methods=['PUT'])
@app.route('/1/get/logs/<id>') def update_profile():
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 = []
try: try:
emails = request.get_json(silent=True)
args = session['store']
emails = [item['email'] for item in emails]
key = request.headers['key'] gWriter= factory.instance(type=class_write,args=args)
default_plan = CONFIG['plan'] gWriter.write(label='emails',data=emails)
handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=key) pass
if handler.is_registered() : except Exception as e :
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:
print (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") @app.route("/1/sys/usage/trend")
def get_usage_trend(): def get_usage_trend():
""" """

@ -601,7 +601,7 @@ class CouchdbWriter(Couchdb,Writer):
else : else :
if label not in document : if label not in document :
document[label] = {} document[label] = {}
if isinstance(params['data'],object) : if isinstance(params['data'],list) == False :
document[label] = dict(document[label],**params['data']) document[label] = dict(document[label],**params['data'])
else: else:

Loading…
Cancel
Save