diff --git a/requirements.txt b/requirements.txt index bf7be19..363ce68 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ aniso8601==1.2.0 -certifi==2017.11.5 chardet==3.0.4 click==6.6 couchdbkit==0.6.5 @@ -23,7 +22,6 @@ MarkupSafe==0.23 ngram==3.3.0 numpy==1.11.3 pika==0.10.0 -pkg-resources==0.0.0 pycparser==2.18 pyOpenSSL==17.3.0 python-dateutil==2.6.0 diff --git a/src/api/index.py b/src/api/index.py index 5cfee85..6b71a6d 100755 --- a/src/api/index.py +++ b/src/api/index.py @@ -1,16 +1,16 @@ """ - This is a RESTful interface implemented using Flask micro framework. - The API is driven by configuration that is organized in terms of the monitoring classes - - The API is both restful and websocket/socketio enabled. - - We designed the classes to be reusable (and powered by labels): - 'monitoring-type': - 'class':'' - 'config':' - @TODO: - - In order to make this Saas we need to have the configuration be session driven - - Add socketio, so that each section of the dashboard updates independently + This is a RESTful interface implemented using Flask micro framework. + The API is driven by configuration that is organized in terms of the monitoring classes + + The API is both restful and websocket/socketio enabled. + + We designed the classes to be reusable (and powered by labels): + 'monitoring-type': + 'class':'' + 'config':' + @TODO: + - In order to make this Saas we need to have the configuration be session driven + - Add socketio, so that each section of the dashboard updates independently """ from flask import Flask, session, request, redirect, Response @@ -56,619 +56,620 @@ factory = DataSourceFactory() @app.route('/') def home(): - """ - This is the initial function (entry point), This function will load a dashboard - @param key customer's key - """ - context = PARAMS['context'] - if 'title' in PARAMS : - title = PARAMS['title'] - else: - title = 'Dashboard' - apps = [] - try: - # - # The user must pass a key in heref - key = request.header['key'] - args= dict(p) - args['uid'] = key - - gReader = factory.instance(type=class_read,args=p) - apps = gReader.view('summary/nodes',key=p['uid']) - - except Exception, e: - print (e) - return render_template('dashboard.html',context=context,title=title,app_names=apps) - -@app.route('/register',methods=['POST']) -def register() : - """ - This function registers a user and provides - """ - global p - body = request.get_json(silent=True) - uid = body['uid'] - default_plan = CONFIG['plan'] - if 'plan' not in body : - plan = CONFIG['plan'] - else: - plan = body['plan'] - handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=uid) - r = handler.register(plan) - session['key'] = r['key'] - # - # We should try to run a thread that loads the data for the user so when she requests it it is ready - # @TODO: - # - try: - session['plan'] = r - key = r['key'] - args = json.loads(json.dumps(p)) - 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) + """ + This is the initial function (entry point), This function will load a dashboard + @param key customer's key + """ + context = PARAMS['context'] + if 'title' in PARAMS : + title = PARAMS['title'] + else: + title = 'Dashboard' + apps = [] + try: + # + # The user must pass a key in href + key = request.header['key'] + args= dict(p) + args['uid'] = key + + gReader = factory.instance(type=class_read,args=p) + apps = gReader.view('summary/nodes',key=p['uid']) + + except Exception as e: + print (e) + return render_template('dashboard.html',context=context,title=title,app_names=apps) + + +@app.route('/register', methods=['POST']) +def register(): + """ + This function registers a user and provides + """ + global p + body = request.get_json(silent=True) + uid = body['uid'] + default_plan = CONFIG['plan'] + if 'plan' not in body : + plan = CONFIG['plan'] + else: + plan = body['plan'] + handler = Register(write=class_write,read=class_read,store=p,default=default_plan,uid=uid) + r = handler.register(plan) + session['key'] = r['key'] + # + # We should try to run a thread that loads the data for the user so when she requests it it is ready + # @TODO: + # + try: + session['plan'] = r + key = r['key'] + args = json.loads(json.dumps(p)) + 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(): - """ - This function returns the labels of applications for every node registered - @param None - """ - r = [] - try: - app_key = request.headers['key'] - gReader = factory.instance(type=class_read,args=p) - r = gReader.view('summary/nodes',key=app_key) - session['default.node'] = r[0] - except Exception,e: - print (e) - return json.dumps(r) - + """ + This function returns the labels of applications for every node registered + @param None + """ + r = [] + try: + app_key = request.headers['key'] + gReader = factory.instance(type=class_read, args=p) + r = gReader.view('summary/nodes', key=app_key) + session['default.node'] = r[0] + except Exception as e: + print (e) + return json.dumps(r) + @app.route('/1/get/apps') def get_apps(): - """ - This function returns the applications for a given node - @param node identifier e.g: apps@zulu.org - """ - r = [] - try: - node_id = request.args.get('node') - gReader = factory.instance(type=class_read,args=p) - r = gReader.view('summary/app_names',key=p['uid']) - r = r[node_id] - session['default.app'] = r[0] - except Exception,e: - print (e) - return json.dumps(r) + """ + This function returns the applications for a given node + @param node identifier e.g: apps@zulu.org + """ + r = [] + try: + node_id = request.args.get('node') + gReader = factory.instance(type=class_read,args=p) + r = gReader.view('summary/app_names',key=p['uid']) + r = r[node_id] + session['default.app'] = r[0] + except Exception as e: + print (e) + return json.dumps(r) @app.route('/1/get/logs') def get_logs() : - r = {} - #session['key'] = 'c259e8b1-e2fb-40df-bf03-f521f8ee352d' - key = session['key'] if 'key' in session else None - if key is None and 'key' in request.headers: - key = request.headers['key'] - if key is None : - return json.dumps(r) - else: - try: - - #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 - 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 : - 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) + r = {} + #session['key'] = 'c259e8b1-e2fb-40df-bf03-f521f8ee352d' + key = session['key'] if 'key' in session else None + if key is None and 'key' in request.headers: + key = request.headers['key'] + if key is None : + return json.dumps(r) + else: + try: + + #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 + 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 : + 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 as e: + print (e) + return json.dumps(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] - - gWriter= factory.instance(type=class_write,args=args) - gWriter.write(label='emails',data=emails) - pass - except Exception as e : - print (e) - return '',204 + try: + emails = request.get_json(silent=True) + args = session['store'] + emails = [item['email'] for item in emails] + + gWriter= factory.instance(type=class_write,args=args) + gWriter.write(label='emails',data=emails) + pass + except Exception as e : + print (e) + 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 + """ + 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 + pass @app.route("/1/sys/usage/trend") def get_usage_trend(): - """ + """ - This function returns cpu/memory usage for the entire system being monitored. It will return the 24 most recent observations in the logs - @param None - @return {memory_usage:[],cpu_usage:[],app_count:value,memory_available:[]} - """ - r = {} - try: - gReader = factory.instance(type=class_read,args=p) - r = gReader.view('summary/resource_usage_trend',key=p['uid']) - except Exception,e: - print (e) - return json.dumps(r) + This function returns cpu/memory usage for the entire system being monitored. It will return the 24 most recent observations in the logs + @param None + @return {memory_usage:[],cpu_usage:[],app_count:value,memory_available:[]} + """ + r = {} + try: + gReader = factory.instance(type=class_read,args=p) + r = gReader.view('summary/resource_usage_trend',key=p['uid']) + except Exception as e: + print (e) + return json.dumps(r) @app.route("/1/app/usage/trend") def get_usage_detail(): - """ - This function returns detailed information about usage per application monitored. It will return the 24 most recent observations in the logs - - @param node node identifier e.g: apps@zulu.io - @return {node_x:{app_1:{memory_usage:[],cpu_usage:[]}},...} - """ - r = {} - try: - if 'id' not in request.args and 'node' not in request.args : - id = session['default.node'] - else: - id = request.args['id'] if 'id' in request.args else request.args.get('node') - if 'app' not in request.args : - if 'default.app' in session : - app_id = session['default.app'] - else: - app_id = None - else: - app_id = request.args.get('app') - # - # removing trailing white spaces - - gReader = factory.instance(type=class_read,args=p) - r = gReader.view('summary/app_resource_usage_details',key=p['uid']) - id = id.strip() - if app_id is not None : - app_id = app_id.strip() - r = r[id][app_id] - else : - r = r[id] - - except Exception,e: - print ' *** ',(e) - return json.dumps(r) + """ + This function returns detailed information about usage per application monitored. It will return the 24 most recent observations in the logs + + @param node node identifier e.g: apps@zulu.io + @return {node_x:{app_1:{memory_usage:[],cpu_usage:[]}},...} + """ + r = {} + try: + if 'id' not in request.args and 'node' not in request.args : + id = session['default.node'] + else: + id = request.args['id'] if 'id' in request.args else request.args.get('node') + if 'app' not in request.args : + if 'default.app' in session : + app_id = session['default.app'] + else: + app_id = None + else: + app_id = request.args.get('app') + # + # removing trailing white spaces + + gReader = factory.instance(type=class_read,args=p) + r = gReader.view('summary/app_resource_usage_details',key=p['uid']) + id = id.strip() + if app_id is not None : + app_id = app_id.strip() + r = r[id][app_id] + else : + r = r[id] + + except Exception as e: + print ' *** ',(e) + return json.dumps(r) @app.route('/1/app/status') def app_status() : - """ - This function aggregates the number of crashes/running/idle instances found in the past 24 log entries - for a particular application - @param nid node identifier e.g: app@zulu.io - @param app application identifier e.g: kate, firefox, chrome ... specified in the configuraiton - """ - r = [] - try: - nid = request.args.get('node') # Node identifier - aid = request.args.get('app') # application identifier - - gReader = factory.instance(type=class_read,args=p) - r = gReader.view('summary/app_status_details',key=p['uid']) - # - #@TODO: Once the back-end enables the nodes in which the application is running, uncomment the line below - # - print[nid,aid] - r = r[nid][aid] - - except Exception,e: - print e - - return json.dumps(r) + """ + This function aggregates the number of crashes/running/idle instances found in the past 24 log entries + for a particular application + @param nid node identifier e.g: app@zulu.io + @param app application identifier e.g: kate, firefox, chrome ... specified in the configuraiton + """ + r = [] + try: + nid = request.args.get('node') # Node identifier + aid = request.args.get('app') # application identifier + + gReader = factory.instance(type=class_read,args=p) + r = gReader.view('summary/app_status_details',key=p['uid']) + # + #@TODO: Once the back-end enables the nodes in which the application is running, uncomment the line below + # + print[nid,aid] + r = r[nid][aid] + + except Exception as e: + print e + + return json.dumps(r) #@app.route('/get/') #def procs(id): - #try: - #gReader = factory.instance(type=class_read,args=p) - #data = gReader.read() - #ahandler = AnalyzeAnomaly() - #learn = {} - #if 'learn' in data : - #for row in data['learn'] : - #label = row['label'] - #learn[label] = row - #r = {} - #for label in data : - #if label not in ['learn','folders'] : - #index = len(data[label]) - 1 - #row = data[label][index] - #r[label] = row - ## - ## Let us determine if this is a normal operation or not - ## We will update the status of the information ... - ## - - #for row in r[label] : - #index = r[label].index(row) - #if row['label'] in learn: - #id = row['label'] - #px = ahandler.predict([row],learn[id]) - #if px : - - ## row['anomaly'] = px[1]==1 - #print "" - #print label,' *** ',index - #row = dict(row,**px) - #r[label][index] =row - ## - ## @TODO: - ## Compile a report here that will be sent to the mailing list - ## - - #except Exception, e: - #print e - #r = [] - - #return json.dumps(r) + #try: + #gReader = factory.instance(type=class_read,args=p) + #data = gReader.read() + #ahandler = AnalyzeAnomaly() + #learn = {} + #if 'learn' in data : + #for row in data['learn'] : + #label = row['label'] + #learn[label] = row + #r = {} + #for label in data : + #if label not in ['learn','folders'] : + #index = len(data[label]) - 1 + #row = data[label][index] + #r[label] = row + ## + ## Let us determine if this is a normal operation or not + ## We will update the status of the information ... + ## + + #for row in r[label] : + #index = r[label].index(row) + #if row['label'] in learn: + #id = row['label'] + #px = ahandler.predict([row],learn[id]) + #if px : + + ## row['anomaly'] = px[1]==1 + #print "" + #print label,' *** ',index + #row = dict(row,**px) + #r[label][index] =row + ## + ## @TODO: + ## Compile a report here that will be sent to the mailing list + ## + + #except Exception, e: + #print e + #r = [] + + #return json.dumps(r) @app.route('/init/collector',methods=['POST']) def init_collector(): - """ - This function will provide context for the calling code to be able to submit data - @header key user's key - @header id node identifier - - @return {"plan":...,"store":...} - """ - r = {} - if 'key' in request.headers and 'id' in request.headers : - try: - key = request.headers['key'] - node= request.headers['id'] - config = json.loads(json.dumps(CONFIG)) - handler = Register(write=class_write,read=class_read,store=config['store']['args'],default=CONFIG['plan'],uid=key) - plan = handler.get_active_plan() - if plan : - store = json.loads(json.dumps(CONFIG['store'])) - store['args']['uid'] = key - store['args']['dbname'] = plan['name'] - - r = {"plan":plan,"store":store} - pass - except Exception as e: - print e - else: - """ - Invalid parameters were sent - """ - pass - return json.dumps(r) + """ + This function will provide context for the calling code to be able to submit data + @header key user's key + @header id node identifier + + @return {"plan":...,"store":...} + """ + r = {} + if 'key' in request.headers and 'id' in request.headers : + try: + key = request.headers['key'] + node= request.headers['id'] + config = json.loads(json.dumps(CONFIG)) + handler = Register(write=class_write,read=class_read,store=config['store']['args'],default=CONFIG['plan'],uid=key) + plan = handler.get_active_plan() + if plan : + store = json.loads(json.dumps(CONFIG['store'])) + store['args']['uid'] = key + store['args']['dbname'] = plan['name'] + + r = {"plan":plan,"store":store} + pass + except Exception as e: + print e + else: + """ + Invalid parameters were sent + """ + pass + return json.dumps(r) def InitCollector(): - """ - This endpoint is intended to initialize the collection agent - @pre registration of the client should be done against the store API - """ - r = [] - manager={} - - factory = DataSourceFactory() - try: - - key = request.headers['key'] - node= request.headers['id'] - #scope=request.headers['scope'] if 'scope' in request.headers else {} - #scope= json.loads(scope) - body= request.get_json(silent=True) - # - # @TODO : Validate the account & plan, insure preconditions are met/satisfied - # - m = {'apps':'monitor.DetailProcess','folders':'monitor.FileWatch'} - a = {'apps':'actor.Apps','mailer':'actor.Mailer','folders':'actor.Folders'} - lagents = [] - lactors = [] - for id in m : - if id in body : - agent = eval(m[id]+"()") - - args = body[id] if id in body else None - if args is not None : - agent.init(args) - - lagents.append(agent) - if id in a : - #-- This variable should not be confused with defined namespace - # The manager will properly initialize the objects - # - _actor = eval(a[id]+"()") - lactors.append(_actor) - - # - # @TODO: The database name should be provided by the active plan - # The database name will be overriden by the user's current plan - # - #global class_write,class_read,CONFIG - print 'plan' in session,CONFIG['store']['args'] - if 'plan' not in session : - config = json.loads(json.dumps(CONFIG)) - plan = config['plan'] - handler = Register(write=class_write,read=class_read,store=config['store']['args'],default=plan,uid=key) - plan = handler.get_active_plan() - session['plan'] = plan - else: - plan = session['plan'] - - _config = json.loads(json.dumps(CONFIG)) - #_config['store']['args']['dbname'] = plan['name'] #'monitor-logs' - _config['store']['args']['uid'] = key - _config['store']['args']['dbname'] = plan['name'] - manager = Manager() - manager.init(actors = lactors,agents=lagents,config=_config,key=key,node=node) ; - r = [pickle.dumps(manager)] - except Exception as e: - print ' init (error) ',str(e) - return json.dumps(r) - + """ + This endpoint is intended to initialize the collection agent + @pre registration of the client should be done against the store API + """ + r = [] + manager={} + + factory = DataSourceFactory() + try: + + key = request.headers['key'] + node= request.headers['id'] + #scope=request.headers['scope'] if 'scope' in request.headers else {} + #scope= json.loads(scope) + body= request.get_json(silent=True) + # + # @TODO : Validate the account & plan, insure preconditions are met/satisfied + # + m = {'apps':'monitor.DetailProcess','folders':'monitor.FileWatch'} + a = {'apps':'actor.Apps','mailer':'actor.Mailer','folders':'actor.Folders'} + lagents = [] + lactors = [] + for id in m : + if id in body : + agent = eval(m[id]+"()") + + args = body[id] if id in body else None + if args is not None : + agent.init(args) + + lagents.append(agent) + if id in a : + #-- This variable should not be confused with defined namespace + # The manager will properly initialize the objects + # + _actor = eval(a[id]+"()") + lactors.append(_actor) + + # + # @TODO: The database name should be provided by the active plan + # The database name will be overriden by the user's current plan + # + #global class_write,class_read,CONFIG + print 'plan' in session,CONFIG['store']['args'] + if 'plan' not in session : + config = json.loads(json.dumps(CONFIG)) + plan = config['plan'] + handler = Register(write=class_write,read=class_read,store=config['store']['args'],default=plan,uid=key) + plan = handler.get_active_plan() + session['plan'] = plan + else: + plan = session['plan'] + + _config = json.loads(json.dumps(CONFIG)) + #_config['store']['args']['dbname'] = plan['name'] #'monitor-logs' + _config['store']['args']['uid'] = key + _config['store']['args']['dbname'] = plan['name'] + manager = Manager() + manager.init(actors = lactors,agents=lagents,config=_config,key=key,node=node) ; + r = [pickle.dumps(manager)] + except Exception as e: + print ' init (error) ',str(e) + return json.dumps(r) + """ - This function/endpoint will assess n-virtual environments and return the results - @TODO: Should this be stored for future mining (I don't think so but could be wrong) + This function/endpoint will assess n-virtual environments and return the results + @TODO: Should this be stored for future mining (I don't think so but could be wrong) """ @app.route('/sandbox') def sandbox(): - global CONFIG + global CONFIG - if 'sandbox' in CONFIG: #CONFIG['monitor']: - #handler = HANDLERS['sandbox']['class'] - #conf = HANDLERS['sandbox']['config'] - r = [] - # p = Factory.instance('sandbox',CONFIG) - handler = monitor.Sandbox() - conf = CONFIG['sandbox'] + if 'sandbox' in CONFIG: #CONFIG['monitor']: + #handler = HANDLERS['sandbox']['class'] + #conf = HANDLERS['sandbox']['config'] + r = [] + # p = Factory.instance('sandbox',CONFIG) + handler = monitor.Sandbox() + conf = CONFIG['sandbox'] - for id in conf: - try: - handler.init(conf[id]) - r.append (dict(handler.composite(),**{"label":id})) - except Exception,e: - pass - else: + for id in conf: + try: + handler.init(conf[id]) + r.append (dict(handler.composite(),**{"label":id})) + except Exception as e: + pass + else: - r = [] + r = [] - return json.dumps(r) + return json.dumps(r) #@app.route('/trends') #def trends (): - #id = request.args.get('id') - #app = request.args.get('app').strip() - #p = CONFIG['store']['args'] - #class_read = CONFIG['store']['class']['read'] - - - #gReader = factory.instance(type=class_read,args=p) - #r = gReader.read() - #if id in r: - #r = r[id] #--matrix - #series = [] - - #for row in r: - - #series += [item for item in row if str(item['label'])== app] - #if len(series) > 12 : - #beg = len(series) - 8 - #series = series[beg:] - #return json.dumps(series) - #else: - #return "[]" + #id = request.args.get('id') + #app = request.args.get('app').strip() + #p = CONFIG['store']['args'] + #class_read = CONFIG['store']['class']['read'] + + + #gReader = factory.instance(type=class_read,args=p) + #r = gReader.read() + #if id in r: + #r = r[id] #--matrix + #series = [] + + #for row in r: + + #series += [item for item in row if str(item['label'])== app] + #if len(series) > 12 : + #beg = len(series) - 8 + #series = series[beg:] + #return json.dumps(series) + #else: + #return "[]" #@app.route('/download',methods=['POST']) #def requirements(): - #stream = request.form['missing'] - #print stream - #stream = "\n".join(json.loads(stream)) - #headers = {"content-disposition":"attachment; filename=requirements.txt"} - #return Response(stream,mimetype='text/plain',headers=headers) + #stream = request.form['missing'] + #print stream + #stream = "\n".join(json.loads(stream)) + #headers = {"content-disposition":"attachment; filename=requirements.txt"} + #return Response(stream,mimetype='text/plain',headers=headers) @app.route('/dashboard/') def dashboard(): - context = PARAMS['context'] - if 'title' in PARAMS : - title = PARAMS['title'] - else: - title = 'Dashboard' - apps = [] - try: - gReader = factory.instance(type=class_read,args=p) - apps = gReader.view('summary/app_names',key=p['uid']) - except Exception, e: - print (e) - return render_template('dashboard.html',context=context,title=title,app_names=apps) + context = PARAMS['context'] + if 'title' in PARAMS : + title = PARAMS['title'] + else: + title = 'Dashboard' + apps = [] + try: + gReader = factory.instance(type=class_read,args=p) + apps = gReader.view('summary/app_names',key=p['uid']) + except Exception, e: + print (e) + return render_template('dashboard.html',context=context,title=title,app_names=apps) @app.route('/upgrade') def upgrade(): - context = PARAMS['context'] - if 'title' in PARAMS : - title = PARAMS['title'] - else: - title = 'Upgrade' - return render_template('upgrade.html',context=context,title=title) + context = PARAMS['context'] + if 'title' in PARAMS : + title = PARAMS['title'] + else: + title = 'Upgrade' + return render_template('upgrade.html',context=context,title=title) @app.route('/user') def user(): - context = PARAMS['context'] - if 'title' in PARAMS : - title = PARAMS['title'] - else: - title = 'Upgrade' - return render_template('user.html',context=context,title=title) + context = PARAMS['context'] + if 'title' in PARAMS : + title = PARAMS['title'] + else: + title = 'Upgrade' + return render_template('user.html',context=context,title=title) #""" - #This function is designed to trigger learning for anomaly detection - #@TODO: forward this to a socket i.e non-blocking socket + #This function is designed to trigger learning for anomaly detection + #@TODO: forward this to a socket i.e non-blocking socket #""" #@app.route('/anomalies/get') #def learn(): - #global CONFIG - #p = CONFIG['store']['args'] - #class_read = CONFIG['store']['class']['read'] - #gReader = factory.instance(type=class_read,args=p) - #d = gReader.read() - - #if 'learn' in d : - #info = d['learn'] - - #del d['learn'] - #else : - #info = [] - #r = [] - #if 'id' in request.args: - #id = request.args['id'] - #d = d[id] - #params = {} - #for item in info: - - #label = item['label'] - #params[label] = item - - ##apps = list(set(ML.Extract(['label'],d))) - #r = [] - #if params : - ## - ## If we have parameters available - #p = AnomalyDetection() - #apps = params.keys() - #for name in apps : - #if name not in params: - #continue - #_info = params[name] - #try: - #xo = ML.Filter('label',name,d) - #except Exception,e: - #xo = [] - ##print name,e - #if len(xo) == 0: - #continue - #xo = [xo[ len(xo) -1]] - - #value = p.predict(xo,_info)[0] - - #if len(value): - #report = dict(_info,**{'predicton':value}) - #r.append(report) - - - - ##print app,value - ##if value is not None: - ## r.append(value) - - #return json.dumps(r) + #global CONFIG + #p = CONFIG['store']['args'] + #class_read = CONFIG['store']['class']['read'] + #gReader = factory.instance(type=class_read,args=p) + #d = gReader.read() + + #if 'learn' in d : + #info = d['learn'] + + #del d['learn'] + #else : + #info = [] + #r = [] + #if 'id' in request.args: + #id = request.args['id'] + #d = d[id] + #params = {} + #for item in info: + + #label = item['label'] + #params[label] = item + + ##apps = list(set(ML.Extract(['label'],d))) + #r = [] + #if params : + ## + ## If we have parameters available + #p = AnomalyDetection() + #apps = params.keys() + #for name in apps : + #if name not in params: + #continue + #_info = params[name] + #try: + #xo = ML.Filter('label',name,d) + #except Exception as e: + #xo = [] + ##print name,e + #if len(xo) == 0: + #continue + #xo = [xo[ len(xo) -1]] + + #value = p.predict(xo,_info)[0] + + #if len(value): + #report = dict(_info,**{'predicton':value}) + #r.append(report) + + + + ##print app,value + ##if value is not None: + ## r.append(value) + + #return json.dumps(r) """ - This function returns anomalies for a given context or group of processes - The information returned is around precision/recall and f-score and parameters + This function returns anomalies for a given context or group of processes + The information returned is around precision/recall and f-score and parameters """ #@app.route('/anomalies/status') #def anomalies_status(): - #global CONFIG - #p = CONFIG['store']['args'] - #class_read = CONFIG['store']['class']['read'] - #gReader = factory.instance(type=class_read,args=p) - #d = gReader.read() - #if 'learn' in d : - #info = d['learn'] - - #del d['learn'] - #else : - #info = [] - #print info - #r = [] - #if 'id' in request.args: - #id = request.args['id'] - #r = info - #return json.dumps(r) + #global CONFIG + #p = CONFIG['store']['args'] + #class_read = CONFIG['store']['class']['read'] + #gReader = factory.instance(type=class_read,args=p) + #d = gReader.read() + #if 'learn' in d : + #info = d['learn'] + + #del d['learn'] + #else : + #info = [] + #print info + #r = [] + #if 'id' in request.args: + #id = request.args['id'] + #r = info + #return json.dumps(r) #@app.route('/folders') #def get_folders(): - #global CONFIG - #p = CONFIG['store']['args'] - #class_read = CONFIG['store']['class']['read'] - #gReader = factory.instance(type=class_read,args=p) - #d = gReader.read() - #if 'folders' in d: - #d = d['folders'] - #hosts = set([row[0]['id'] for row in d]) - #m = {} - #for id in hosts: - #for row in d: - #if id == row[0]['id'] : - #m[id] = row - #d = m.values() - #for row in d: - #print row[0]['id'] - ## index = len(d) - 1 - ## d = d[index] - - - ## m = {} - ## for row in d : - - ## key = row.keys()[0] - - ## row = row[key] - ## if key not in m: - ## r.append(row) - ## m[key] = len(r) -1 - ## else: - ## index = m[key] - ## r[index] = row - ## d = r - #else: - #d = [] - - #return json.dumps(d) + #global CONFIG + #p = CONFIG['store']['args'] + #class_read = CONFIG['store']['class']['read'] + #gReader = factory.instance(type=class_read,args=p) + #d = gReader.read() + #if 'folders' in d: + #d = d['folders'] + #hosts = set([row[0]['id'] for row in d]) + #m = {} + #for id in hosts: + #for row in d: + #if id == row[0]['id'] : + #m[id] = row + #d = m.values() + #for row in d: + #print row[0]['id'] + ## index = len(d) - 1 + ## d = d[index] + + + ## m = {} + ## for row in d : + + ## key = row.keys()[0] + + ## row = row[key] + ## if key not in m: + ## r.append(row) + ## m[key] = len(r) -1 + ## else: + ## index = m[key] + ## r[index] = row + ## d = r + #else: + #d = [] + + #return json.dumps(d) if __name__== '__main__': # ThreadManager.start(CONFIG) - if 'port' not in SYS_ARGS.PARAMS : - SYS_ARGS.PARAMS['port'] = 8484 - PORT = int(SYS_ARGS.PARAMS['port']) - app.run(host='0.0.0.0' ,port=PORT,debug=True,threaded=True) - + if 'port' not in SYS_ARGS.PARAMS : + SYS_ARGS.PARAMS['port'] = 8484 + PORT = int(SYS_ARGS.PARAMS['port']) + app.run(host='0.0.0.0' ,port=PORT,debug=True,threaded=True) + diff --git a/src/api/static/js/dashboard.js b/src/api/static/js/dashboard.js index 2859b86..c8045b5 100755 --- a/src/api/static/js/dashboard.js +++ b/src/api/static/js/dashboard.js @@ -1,12 +1,5 @@ type = ['','info','success','warning','danger']; -function getNode(name){ - var nodeName = {} - nodeName.name = name - console.log('nodename...', nodeName.name) - dashboard.initChartist(nodeName) -} - dashboard = { initPickColor: function(){ $('.pick-class-label').click(function(){ @@ -22,38 +15,39 @@ dashboard = { }); }, - initChartist: function(nodeName){ + initChartist: function(){ + + function make_array() { + triple_array = []; + for (i=0;i<3;i++){ + myarray = Array.from({length: 24}, () => Math.floor(Math.random() * 100)) + triple_array.push(myarray) + } + return triple_array + } + +// monitoring apps chart var getData = $.get('/1/app/usage/trend'); getData.done(function(results) { - var data = JSON.parse(results) - if (typeof nodeName === 'undefined'){ - var node = data['apps@michaels-MBP']; - console.log("undefined nodename.........") - } else { - console.log('this is nodeName...', nodeName) - console.log('this is nodeName.name...', nodeName.name) - var node = data[nodeName.name] - console.log('data.name..', data[name]) + var app_usage_trend = JSON.parse(results); + if ($.isEmptyObject(app_usage_trend)){ + console.log('app_usage_trend is empty, setting testing values.'); + app_usage_trend = []; + random_array = make_array(); + app_usage_trend = random_array } - var app = node['chrome']; - var cpu = app['cpu']; - var memory_used = app['memory_used']; - console.log('data...',data) - console.log('node...',node) - console.log('app...',app) - console.log('cpu...',cpu) - console.log('memory_used...', memory_used) - -// monitoring apps chart var dataChart = { labels: ['24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6','5','4','3','2','1' ], - series: [cpu, memory_used, [0,0.1,0.3,0.2,0.4,0.6,0.2], ] // TODO: Check the order, the graph is by index not name. + series: app_usage_trend //TODO: Check the order, the graph is by index not name. }; - dataChartArray = dataChart.series - var maxlist = dataChartArray.map(dataChartArray => Math.max.apply(null, dataChartArray)); - maxNum = Math.max.apply(null, maxlist) + dataChartArray = dataChart.series; + var maxlist = dataChartArray.map(function (dataChartArray) { + return Math.max.apply(null, dataChartArray); + }); + + maxNum = Math.max.apply(null, maxlist); var optionsChart = { lineSmooth: false, @@ -62,13 +56,13 @@ dashboard = { showArea: true, height: "245px", axisX: { - showGrid: false, + showGrid: false }, lineSmooth: Chartist.Interpolation.simple({ divisor: 1 }), showLine: true, - showPoint: false, + showPoint: false }; var responsiveChart = [ @@ -83,10 +77,10 @@ dashboard = { Chartist.Line('#chartHours', dataChart, optionsChart, responsiveChart); -// cpu and memory -------------------------- +// cpu and memory usage-------------------------- var data = { labels: ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], - series: [cpu, memory_used] + series: make_array() }; @@ -111,16 +105,21 @@ dashboard = { Chartist.Line('#chartActivity', data, options, responsiveOptions); - var getStatus = $.get('1/get/summary/app_status'); +//app status pie chart, idle, running, crash + var getStatus = $.get('1/app/status'); getStatus.done(function(results) { - var data = JSON.parse(results) - let getStatusList = [] - getStatusList.push(data.idle) - getStatusList.push(data.crash) - getStatusList.push(data.running) + var pie_chart = JSON.parse(results); + if ($.isEmptyObject(pie_chart)){ + console.log('pie_chart is empty, setting testing values.'); + app_status = [1,2,3] + } + // var app_status = []; + // app_status.push(pie_chart.idle); + // app_status.push(pie_chart.crash); + // app_status.push(pie_chart.running); Chartist.Pie('#chartPreferences', { - labels: getStatusList, - series: getStatusList + labels: app_status, + series: app_status },{donut:true}); }) /// end getStatus }) @@ -135,7 +134,7 @@ dashboard = { scrollwheel: false, //we disable de scroll over the map, it is a really annoing when you scroll through page styles: [{"featureType":"water","stylers":[{"saturation":43},{"lightness":-11},{"hue":"#0088ff"}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"hue":"#ff0000"},{"saturation":-100},{"lightness":99}]},{"featureType":"road","elementType":"geometry.stroke","stylers":[{"color":"#808080"},{"lightness":54}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ece2d9"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#ccdca1"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#767676"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]},{"featureType":"landscape.natural","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#b8cb93"}]},{"featureType":"poi.park","stylers":[{"visibility":"on"}]},{"featureType":"poi.sports_complex","stylers":[{"visibility":"on"}]},{"featureType":"poi.medical","stylers":[{"visibility":"on"}]},{"featureType":"poi.business","stylers":[{"visibility":"simplified"}]}] - } + }; var map = new google.maps.Map(document.getElementById("map"), mapOptions); var marker = new google.maps.Marker({ @@ -162,9 +161,9 @@ dashboard = { align: align } }); - }, + } -} +}; /** @@ -178,29 +177,29 @@ g.summary = {} * Initializing the top section of the dashboard (apps and folders) */ g.summary.factory = function (url,pointer) { - var object = {} - object.url = url - var observer = null + var object = {}; + object.url = url; + var observer = null; var TIME_ELLAPSED = 2000 ; object.callback = function (r) { - r = JSON.parse(r.responseText) + r = JSON.parse(r.responseText); pointer(r) //observer.notify() - } + }; object.init = function (observer) { observer = observer - var httpclient = HttpClient.instance() + var httpclient = HttpClient.instance(); //httpclient.setAsync(false) - httpclient.get(this.url, this.callback) + httpclient.get(this.url, this.callback); setTimeout(function(){ observer.notify() },TIME_ELLAPSED) ; //observer.notify() - } + }; return object -} +}; diff --git a/src/api/templates/dashboard.html b/src/api/templates/dashboard.html index 93dc163..92d4b85 100644 --- a/src/api/templates/dashboard.html +++ b/src/api/templates/dashboard.html @@ -46,39 +46,40 @@ - - - +{# #} +{# #} +{# #}