Switch to side-by-side view

--- a/backend/notificationManager.js
+++ b/backend/notificationManager.js
@@ -21,10 +21,12 @@
 	var AllData= [{}];
 	var flag = false;
 
+	//Define Day for Cronjob
 	endoftwoweeks.setDate(endoftwoweeks.getDate() + 14);
 	endofweek.setDate(endofweek.getDate() + 7);
 	endofthreedays.setDate(endofthreedays.getDate() + 3);
 
+	//Get Rules by Token
 	mysql.getRulesListByToken(token, function(isOK, ruleslist){
 		if(!isOK){
 			callback(false);
@@ -32,6 +34,12 @@
 			for(j = 0; j < body.length; j++){
 				var parameter = body[j].subject;
 				var valueNotification = parseFloat(body[j].subjectValue);
+				var data = {
+					emailTo: emailTo,
+					subject: parameter,
+					subjectValue: valueNotification,
+					token: token
+				};
 				for(i = 0; i < ruleslist.length; i++){
 					var valueRules = parseFloat(ruleslist[i].controlValue);
 					if(ruleslist[i].conditionValue == ">"){
@@ -39,783 +47,593 @@
 							if(ruleslist[i].notificationType == 1){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 2){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, endoftwoweeks.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 3){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, endofweek.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 4){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, endofthreedays.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 5){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, today.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}
+						}else if(valueNotification > valueRules || (parameter != ruleslist[i].parameter)){
+							strSubject = parameter + " = " + valueNotification;
+							AllData[j] = strSubject + " : Parameter is not valid";
+							if(checkOut(j, body.length, flag) == 1){
+								callback(true, AllData);
+							}else if(checkOut(j, body.length, flag) == 2){
+								callback(false, AllData);
+							}
+						}else{
+							strSubject = parameter + " = " + valueNotification;
+							AllData[j] = strSubject + " : Value within the limits of the Rules";
+							mysql.insertStatistics(false, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+								if(!isOK) {
+									logger.error(handler + " InsertStatistics Failed");
+								}else {
+									logger.info(handler + " InsertStatistics Success");
+									if(checkOut(j, body.length, flag) == 1){
 										callback(true, AllData);
-									}
-									else{
+									}else if(checkOut(j, body.length, flag) == 2){
 										callback(false, AllData);
 									}
 								}
-							}else if(ruleslist[i].notificationType == 2){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, endoftwoweeks.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}else if(ruleslist[i].notificationType == 3){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, endofweek.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}else if(ruleslist[i].notificationType == 4){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, endofthreedays.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}else if(ruleslist[i].notificationType == 5){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, today.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalN = ruleslist[i].totalNotifications + 1;
-								var totalApplyRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalN;
-								var percentage = (totalApplyRules * 100 ) /  totalN;
-								mysql.updateValues(totalN, totalApplyRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}
-						}else if(valueNotification > valueRules || (parameter != ruleslist[i].parameter)){
-							strSubject = parameter + " = " + valueNotification;
-							AllData[j] = strSubject + " : Parameter is not valid";
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
-								}
-								else{
-									callback(false, AllData);
-								}
-							}
-						}
-						else{
-							strSubject = parameter + " = " + valueNotification;
-							AllData[j] = strSubject + " : Value within the limits of the Rules";
-							var totalN = ruleslist[i].totalNotifications + 1;
-							var totalApplyRules = ruleslist[i].totalNotificationsApplyByRules;
-							var totalValue = valueNotification + ruleslist[i].totalValue;
-							var averageValue = (totalValue / totalN).toFixed(2);
-							var percentage = ((totalApplyRules * 100 ) /  totalN).toFixed(2);
-							mysql.updateValues(totalN, totalApplyRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
-								}
-								else{
-									callback(false, AllData);
-								}
-							}
+							});
 						}
 					} else if(ruleslist[i].conditionValue == ">="){
 						if((valueNotification >= valueRules) && (parameter == ruleslist[i].parameter)){
 							if(ruleslist[i].notificationType == 1){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 2){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, endoftwoweeks.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 3){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, endofweek.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 4){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, endofthreedays.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}else if(ruleslist[i].notificationType == 5){
+								strSubject = parameter + " = " + valueNotification;
+								cronjob(emailTo, strSubject, today.getDate());
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
+							}
+						}else if(valueNotification >= valueRules || (parameter != ruleslist[i].parameter)){
+							strSubject = parameter + " = " + valueNotification;
+							AllData[j] = strSubject + " : Parameter is not valid";
+							console.log("1 - checkOut(j, body.length, flag): " + checkOut(j, body.length, flag));
+							if(checkOut(j, body.length, flag) == 1){
+								callback(true, AllData);
+							}else if(checkOut(j, body.length, flag) == 2){
+								callback(false, AllData);
+							}
+						}else{
+							strSubject = parameter + " = " + valueNotification;
+							AllData[j] = strSubject + " : Value within the limits of the Rules";
+							console.log("2 - checkOut(j, body.length, flag): " + checkOut(j, body.length, flag));
+							mysql.insertStatistics(false, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+								if(!isOK) {
+									logger.error(handler + " InsertStatistics Failed");
+								}else {
+									logger.info(handler + " InsertStatistics Success");
+									if(checkOut(j, body.length, flag) == 1){
 										callback(true, AllData);
-									}
-									else{
+									}else if(checkOut(j, body.length, flag) == 2){
 										callback(false, AllData);
 									}
 								}
-							}else if(ruleslist[i].notificationType == 2){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, endoftwoweeks.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}else if(ruleslist[i].notificationType == 3){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, endofweek.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}else if(ruleslist[i].notificationType == 4){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, endofthreedays.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}else if(ruleslist[i].notificationType == 5){
-								strSubject = parameter + " = " + valueNotification;
-								cronjob(emailTo, strSubject, today.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
-							}
-						}else if(valueNotification >= valueRules || (parameter != ruleslist[i].parameter)){
-							strSubject = parameter + " = " + valueNotification;
-							AllData[j] = strSubject + " : Parameter is not valid";
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
-								}
-								else{
-									callback(false, AllData);
-								}
-							}
-						}
-						else{
-							strSubject = parameter + " = " + valueNotification;
-							AllData[j] = strSubject + " : Value within the limits of the Rules";
-							var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-							var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules;
-							var totalValue = valueNotification + ruleslist[i].totalValue;
-							var averageValue = totalValue / totalNotifications;
-							var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-							mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
-								}
-								else{
-									callback(false, AllData);
-								}
-							}
+							});
 						}
 					} else if(ruleslist[i].conditionValue == "<"){
 						if((valueNotification < valueRules) && (parameter == ruleslist[i].parameter)){
 							if(ruleslist[i].notificationType == 1){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 2){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, endoftwoweeks.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 3){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, endofweek.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 4){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, endofthreedays.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 5){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, today.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}
 						}else if(valueNotification < valueRules || (parameter != ruleslist[i].parameter)){
 							strSubject = parameter + " = " + valueNotification;
 							AllData[j] = strSubject + " : Parameter is not valid";
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
-								}
-								else{
-									callback(false, AllData);
-								}
+							if(checkOut(j, body.length, flag) == 1){
+								callback(true, AllData);
+							}else if(checkOut(j, body.length, flag) == 2){
+								callback(false, AllData);
 							}
 						}
 						else{
 							strSubject = parameter + " = " + valueNotification;
 							AllData[j] = strSubject + " : Value within the limits of the Rules";
-							var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-							var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules;
-							var totalValue = valueNotification + ruleslist[i].totalValue;
-							var averageValue = totalValue / totalNotifications;
-							var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-							mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
+							mysql.insertStatistics(false, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+								if(!isOK) {
+									logger.error(handler + " InsertStatistics Failed");
+								}else {
+									logger.info(handler + " InsertStatistics Success");
+									if(checkOut(j, body.length, flag) == 1){
+										callback(true, AllData);
+									}else if(checkOut(j, body.length, flag) == 2){
+										callback(false, AllData);
+									}
 								}
-								else{
-									callback(false, AllData);
-								}
-							}
+							});
 						}
 					}	else{
 						if((valueNotification <= valueRules) && (parameter == ruleslist[i].parameter)){
 							if(ruleslist[i].notificationType == 1){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 2){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, endoftwoweeks.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 3){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, endofweek.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 4){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, endofthreedays.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}else if(ruleslist[i].notificationType == 5){
 								strSubject = parameter + " = " + valueNotification;
 								cronjob(emailTo, strSubject, today.getDate());
-								var data = {
-									emailTo: emailTo,
-									subject: parameter,
-									subjectValue: valueNotification,
-									token: token
-								};
-								flag = true;
-								AllData[j] = strSubject + " : Success";
-								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
-								var totalValue = valueNotification + ruleslist[i].totalValue;
-								var averageValue = totalValue / totalNotifications;
-								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-								mysql.insertNotification(data, function(isOK, data){
-									if(!isOK) {
-										logger.error(handler + " InsertNotification",data);
-									}else {
-										logger.info(handler + " InsertNotification",data);
-									}
-								});
-
-								if(j == body.length - 1){
-									if(flag == true){
-										callback(true, AllData);
-									}
-									else{
-										callback(false, AllData);
-									}
-								}
+								AllData[j] = strSubject + " : Success";
+								mysql.insertNotification(data, function(isOK, data){
+									if(!isOK) {
+										logger.error(handler + " InsertNotification",data);
+									}else {
+										logger.info(handler + " InsertNotification",data);
+										flag = true;
+										mysql.insertStatistics(true, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+											if(!isOK) {
+												logger.error(handler + " InsertStatistics Failed");
+											}else {
+												logger.info(handler + " InsertStatistics Success");
+												if(checkOut(j, body.length, flag) == 1){
+													callback(true, AllData);
+												}else if(checkOut(j, body.length, flag) == 2){
+													callback(false, AllData);
+												}
+											}
+										});
+									}
+								});
 							}
 						}else if(valueNotification <= valueRules || (parameter != ruleslist[i].parameter)){
 							strSubject = parameter + " = " + valueNotification;
 							AllData[j] = strSubject + " : Parameter is not valid";
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
-								}
-								else{
-									callback(false, AllData);
-								}
+							if(checkOut(j, body.length, flag) == 1){
+								callback(true, AllData);
+							}else if(checkOut(j, body.length, flag) == 2){
+								callback(false, AllData);
 							}
 						}
 						else{
 							strSubject = parameter + " = " + valueNotification;
 							AllData[j] = strSubject + " : Value within the limits of the Rules";
-							var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
-							var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules;
-							var totalValue = valueNotification + ruleslist[i].totalValue;
-							var averageValue = totalValue / totalNotifications;
-							var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
-							mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
-							if(j == body.length - 1){
-								if(flag == true){
-									callback(true, AllData);
+							mysql.insertStatistics(false, body[j].subjectValue, body[j].subject, ruleslist[i].rulesID, function(isOK){
+								if(!isOK) {
+									logger.error(handler + " InsertStatistics Failed");
+								}else {
+									logger.info(handler + " InsertStatistics Success");
+									if(checkOut(j, body.length, flag) == 1){
+										callback(true, AllData);
+									}else if(checkOut(j, body.length, flag) == 2){
+										callback(false, AllData);
+									}
 								}
-								else{
-									callback(false, AllData);
-								}
-							}
+							});
 						}
 					}
 				}
@@ -828,6 +646,20 @@
 function cronjob(emailTo, subject, day){
 	var crontab = require('node-crontab');
 	var jobId = crontab.scheduleJob("* * * "+day+" * *", function(){
-		email.sendEmail(emailTo, subject);
+		for(var i = 0; i < emailTo.length; i++){
+			email.sendEmail(emailTo[i], subject);
+		}
 	}, null, null, false);
+}
+
+function checkOut(j, length, flag){
+	if(j == length - 1){
+		if(flag == true){
+			return 1;
+		}else{
+			return 2;
+		}
+	}else{
+		return 0;
+	}
 }