Parent: [18557b] (diff)

Child: [0b9770] (diff)

Download this file

smartRuleSystem.js    250 lines (241 with data), 7.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
//smartRuleSystem is to automatically and intelligently create new rules
var mysql = require('../controllers/mysql');
var email = require('../controllers/emailer');
var logger = require('../config/logger.js');
var predict = require('predict');
//Handle the rules system and creates Automatically New Rules
module.exports.smartRulesHandler = function(){
var handler = "backend.controllers.mysql.getRulesList";
logger.info("Starting: ",handler);
//Get All Rules on Specific Rule
mysql.getRulesList(function(isOK, rules){
if(isOK){
logger.info("GetRulesList Success");
rules.forEach(function(eachRule, ruleIndex){
var handler = "backend.controllers.mysql.retrieveStatisticsList";
logger.info("Starting: ",handler);
//Get Statistics on Specific Rule
mysql.retrieveStatisticsList(eachRule.rulesID, function(isOK, rows, total){
if(isOK){
if(total.totalNotifications > 5){
switch (eachRule.conditionValue){
case ">":
case ">=":
if(eachRule.description.search("System Create Automatically New rule") > -1){
if(total.averageValue < parseFloat(eachRule.threshold / 100)*parseFloat(eachRule.controlValue)){
updateRule(eachRule, 0);
}else if(total.averageValue < parseFloat(eachRule.controlValue)){
checkLastFalseNotifications(eachRule, function(isOK, newValue){
if(isOK) {
updateRule(eachRule, newValue);
}
})
}else{
deleteRule(eachRule.rulesID);
}
}else{
checkIfExists(eachRule, function(OK, callback){
if(!OK) {
if(total.averageValue < parseFloat(eachRule.threshold / 100)*parseFloat(eachRule.controlValue)){
createNewRule(eachRule, 0);
}else if(total.averageValue < parseFloat(eachRule.controlValue)){
checkLastFalseNotifications(eachRule, function(isOK, newValue){
if(isOK) {
createNewRule(eachRule, newValue);
}
})
}
}
})
}
break;
case "<":
case "<=":
if(eachRule.description.search("System Create Automatically New rule") > -1){
if(total.averageValue > parseFloat(eachRule.threshold / 100)*parseFloat(eachRule.controlValue)){
updateRule(eachRule, 0);
}else if(total.averageValue > parseFloat(eachRule.controlValue)){
checkLastFalseNotifications(eachRule, function(isOK, newValue){
if(isOK) {
updateRule(eachRule, newValue);
}
})
}else{
deleteRule(eachRule.rulesID);
}
}else{
checkIfExists(eachRule, function(OK, callback){
if(!OK) {
if(total.averageValue > parseFloat(eachRule.threshold / 100)*parseFloat(eachRule.controlValue)){
createNewRule(eachRule, 0);
}else if(total.averageValue > parseFloat(eachRule.controlValue)){
checkLastFalseNotifications(eachRule, function(isOK, newValue){
if(isOK) {
createNewRule(eachRule, newValue);
}
})
}
}
})
}
break;
default:
}
}
}
else{
logger.error("retrieveStatisticsList Failed: ", rows);
}
})
})
}else{
logger.error("GetRulesList Failed: ", rules);
}
})
}
//Function for Update Data on Specific Rule
/*
input: rule (Specific Rule), newConditionValue
*/
function updateRule (rule, newConditionValue){
var handler = "backend.controllers.mysql.updateRule";
if(newConditionValue == 0){
var data = {
description: "System Create Automatically New rule: " + rule.description,
parameter: rule.parameter,
conditionValue: rule.conditionValue,
controlValue: parseFloat(rule.threshold / 100)*parseFloat(rule.controlValue),
threshold: rule.threshold,
notificationType: rule.notificationType,
token: rule.token
};
}else if(newConditionValue > 0){
var data = {
description: "System Create Automatically New rule: " + rule.description,
parameter: rule.parameter,
conditionValue: rule.conditionValue,
controlValue: newConditionValue,
threshold: rule.threshold,
notificationType: rule.notificationType,
token: rule.token
};
}
mysql.updateRule(data, function(isOK, callback){
if(!isOK) {
logger.error(handler,"Fail");
}else{
logger.info(handler,"Success");
}
});
}
//Function for Delete Specific Rule
/*
input: rulesID (Specific Rule)
*/
function deleteRule (rulesID){
var handler = "backend.controllers.mysql.deleteRule";
mysql.deleteRule(rulesID, function(isOK, callback){
if(!isOK) {
logger.error(handler,"Fail");
}else{
logger.info(handler,"Success");
}
})
}
//Function for Create New Rule (Automatically New Rule by System)
/*
input: rule (Specific Rule), newConditionValue
*/
function createNewRule (rule, newConditionValue){
if(newConditionValue == 0){
var data = {
description: "System Create Automatically New rule: " + rule.description,
parameter: rule.parameter,
conditionValue: rule.conditionValue,
controlValue: parseFloat(rule.threshold / 100)*parseFloat(rule.controlValue),
threshold: rule.threshold,
notificationType: rule.notificationType,
token: rule.token
};
}else if(newConditionValue > 0){
var data = {
description: "System Create Automatically New rule: " + rule.description,
parameter: rule.parameter,
conditionValue: rule.conditionValue,
controlValue: newConditionValue,
threshold: rule.threshold,
notificationType: rule.notificationType,
token: rule.token
};
}
var handler = "backend.controllers.mysql.insertNotificationRules";
logger.info("Starting: ",handler);
mysql.insertNotificationRules(data, function(isOK, callback){
if(!isOK) {
logger.error("Create Automatically New Rule: Fail");
}
else{
logger.info("Create Automatically New Rule: Sucess");
var handler = "backend.controllers.mysql.getEmailTovAppByToken";
logger.info("Starting: ",handler);
mysql.getEmailTovAppByToken(rule.token, function(isOK, emailTo){
if(!isOK) {
logger.error("Error Get Email by Token");
}
else{
logger.info("Success Get Email by Token");
email.sendEmailNewRule(emailTo[0].developerID, "System Create Automatically New rule: " + rule.description);
}
})
}
})
}
//Function for Check if that Specific Automatically Rule Created by System Exists
/*
input: rule (Specific Rule)
output: true/false
*/
function checkIfExists (rule, cb){
mysql.checkIfRuleAutomaticExists(rule, function(isOK, callback){
if(!isOK) {
logger.info("There is no Rule Created Automatically");
cb(false);
}
else{
logger.info("Rule already Created");
cb(true);
}
})
}
//Function for get Last 5 False Notifications
/*
input: rule (Specific Rule)
output: true/false
*/
function checkLastFalseNotifications (rule, cb){
var prediction = predict.movingAverage(5);
var aux = 0;
mysql.retrieveStatisticsList(rule.rulesID, function(isOK, rows){
if(!isOK) {
cb(false);
}
else{
if(rows.length > 0){
rows.slice(-5).forEach(function(eachRow, index){
if(eachRow.result == "false"){
prediction.pushValues([parseFloat(eachRow.subjectValue)]);
aux++;
}
})
if(aux == 5){
cb(true, prediction.predictNextValue().toFixed(2));
}else{
cb(false);
}
}else{
cb(false);
}
}
})
}