From 9bb3a746b2f7c4a026178529f2289ff15556ccf0 Mon Sep 17 00:00:00 2001 From: steve Date: Wed, 11 Jan 2017 14:51:26 -0600 Subject: [PATCH] ml implementation --- src/api/index.py | 22 +++++++++++++++++++++- src/utils/ml.py | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/api/index.py b/src/api/index.py index 7be673c..15fa619 100644 --- a/src/api/index.py +++ b/src/api/index.py @@ -52,7 +52,6 @@ f.close() # # from threading import Thread, RLock -p = {'uri':'http://dev.the-phi.com:5984','dbname':'monitor','uid':'logs','filename':'logs.JSON'} p = CONFIG['store']['args'] class_read = CONFIG['store']['class']['read'] class_write= CONFIG['store']['class']['write'] @@ -132,6 +131,27 @@ def requirements(): def dashboard(): context = PARAMS['context'] return render_template('dashboard.html',context=context) + +""" + This function is designed to trigger learning for anomaly detection + @TODO: forward this to a socket i.e non-blocking socket +""" +@app.route('/learn') +def learn(): + app = request.args.get('app') + id = request.args.get('id') + p = CONFIG['store']['args'] + class_read = CONFIG['store']['class']['read'] + + p['qid'] =[id] #HANDLERS['processes']['config'].keys() + gReader = factory.instance(type=class_read,args=p) + + r = gReader.read() + r = r[id] + r = ML.Filter('label',app,r) + label = ML.Extract(['status'],r) + + if __name__== '__main__': mthread.start() diff --git a/src/utils/ml.py b/src/utils/ml.py index 43dbce2..15eeea7 100644 --- a/src/utils/ml.py +++ b/src/utils/ml.py @@ -78,10 +78,12 @@ class AnomalyDetection: tp = 0 # true positive fp = 0 # false positive fn = 0 # false negative + tn = 0 # true negative for i in range(0,N): tp += 1 if test[i][1]==labels[i] and test[i][1] == 1 fp += 1 if test[i][1] != labels[i] and test[i][1] == 1 fn += 1 if test[i][1] != labels[i] and test[i][1] == 0 + tn += 1 if test[i][1] == labels[i] and test[i][1] == 0 precision = tp / (tp + fp) recall = tp / (tp + fn) fscore = (2 * precision * recall)/ (precision + recall)