Permalink
Browse files

Refactor alert treshold model code

  • Loading branch information...
1 parent 633fa6d commit dcca4cec3b6c85263c17516a2cf5e69f72542ca5 @fzaninotto committed Jan 20, 2013
Showing with 49 additions and 21 deletions.
  1. +4 −4 app/dashboard/views/_checkDetails.ejs
  2. +45 −17 models/check.js
@@ -32,17 +32,17 @@
<label class="control-label">Slow threshold</label>
<div class="controls">
<div class="input-append">
- <input type="text" name="check[maxTime]" value="<%= check.maxTime %>" class="span2" />
+ <input type="text" name="check[maxTime]" value="<%= check.maxTime %>" class="span1" />
<span class="add-on">ms</span>
</div>
</div>
</div>
<div class="control-group">
- <label class="control-label">Threshold Alert</label>
+ <label class="control-label">Alert Treshold</label>
<div class="controls">
<div class="input-append">
- <input type="text" name="check[nbErrors]" value="<%= check.nbErrors %>" class="span2" />
- <span class="add-on">ms</span>
+ <input type="text" name="check[nbErrors]" value="<%= check.nbErrors %>" class="span1" />
+ <span class="add-on">failed pings</span>
</div>
</div>
</div>
View
@@ -19,8 +19,8 @@ var Check = new Schema({
url : String,
interval : { type: Number, default: 60000 }, // interval between two pings
maxTime : { type: Number, default: 1500 }, // time under which a ping is considered responsive
- nbErrors : { type: Number, default: 1 }, // nb of errors from which to trigger a new CheckEvent
- statusCounter : { type: Number, default: 4 }, // count number of errors
+ alertTreshold : { type: Number, default: 1 }, // nb of errors from which to trigger a new CheckEvent
+ errorCount : { type: Number, default: 0 }, // count number of errors
tags : [String],
lastChanged : Date,
firstTested : Date,
@@ -75,31 +75,22 @@ Check.methods.togglePause = function() {
Check.methods.setLastTest = function(status, time, error) {
var now = time ? new Date(time) : new Date();
+ var mustNotifyEvent = this.mustNotifyEvent(status);
+
if (!this.firstTested) {
this.firstTested = now;
}
+
this.lastTested = now;
-
-
- if (!status && this.isUp != status) {
- this.statusCounter = 1;
- } else if(status && this.isUp != status && this.statusCounter >= this.nbErrors) {
- this.statusCounter = this.nbErrors;
- } else if(!status && this.statusCounter<(this.nbErrors+1)) {
- this.statusCounter++;
- }
-
- if (this.isUp != status) {
+ if (this.isUp != status) {
this.lastChanged = now;
this.isUp = status;
this.uptime = 0;
this.downtime = 0;
+ }
- }
-
- if(this.statusCounter == this.nbErrors) {
- this.statusCounter = this.nbErrors + 1;
+ if (mustNotifyEvent) {
var event = new CheckEvent({
timestamp: now,
check: this,
@@ -112,6 +103,7 @@ Check.methods.setLastTest = function(status, time, error) {
event.downtime = now.getTime() - this.lastChanged.getTime();
}
event.save();
+ this.markEventNotified();
}
var durationSinceLastChange = now.getTime() - this.lastChanged.getTime();
if (status) {
@@ -122,6 +114,42 @@ Check.methods.setLastTest = function(status, time, error) {
return this;
};
+Check.methods.mustNotifyEvent = function(status) {
+ if (!this.firstTested) {
+ return true;
+ }
+ if (!status) {
+ // check is down
+ if (this.isUp != status) {
+ // check goes down for the first time
+ this.errorCount = 1;
+ }
+ if (this.errorCount < this.alertTreshold) {
+ // repeated down pings - increase error count until reaching the down alert treshold
+ this.errorCount++;
+ return false;
+ }
+ if (this.errorCount === this.alertTreshold) {
+ // enough down pings to trigger notofication
+ return true;
+ }
+ // error count higher than treshold, that means the alert was already sent
+ return false;
+ }
+ // check is up
+ if (this.isUp != status && this.errorCount > this.alertTreshold) {
+ // check goes up after reaching the down alert treshold before
+ return true;
+ }
+ // check either goes up after less than alertTreshold down pings, or is already up for long
+ return false;
+}
+
+Check.methods.markEventNotified = function() {
+ // increase error count to disable notification if the next ping has the same status
+ this.errorCount = this.alertTreshold + 1;
+}
+
Check.methods.getQosPercentage = function() {
if (!this.qos) return false;
return (this.qos.ups / this.qos.count) * 100;

0 comments on commit dcca4ce

Please sign in to comment.