Commit 824c5222 authored by Alan Szlosek's avatar Alan Szlosek

update timeseries percentile-alert example to match new API

parent ab32b102
Pipeline #13278830 passed with stage
in 52 seconds
/*
1. Our sample metric is called "sample"
2. Every second, calculate the average of the metric values over the past 10 seconds
1. We create a timeseries called "sample"
2. Every 500ms we add a timer value to it from the times array on line 64
3. Every second, calculate the average of the values over the past 10 seconds
3. Alert when the average is over 50
4. Populate the metric with random integers every 500ms
*/
var Harbinger = require('../../index');
var log = require('../../lib/log')('examples/timeseries/percentile-alert', true);
......@@ -15,26 +16,28 @@ function getRandomInteger(min, max) {
var redisClient = redis.createClient(6379, 'localhost');
// Instantiate a timeseries dataset called "sample" that preserves 60 seconds worth of data
var metric = new Harbinger.Timeseries(redisClient, 'percentile-alert', 60);
// Instantiate a timeseries dataset called "percentile-alert" that preserves 60 seconds worth of data
var timer = new Harbinger.Timeseries.Timer(redisClient, 'percentile-alert', 60);
var averageInterval;
var countInterval;
var done = function() {
clearInterval(averageInterval);
clearInterval(countInterval);
redisClient.quit();
};
// Calculate the average of the metric every second, and print the average if it reaches our threshold
// Calculate the average of the timer values every second, and print the average if it reaches our threshold
var calculationInterval = setInterval(
function() {
var done = function() {
clearInterval(averageInterval);
clearInterval(countInterval);
redisClient.quit();
};
// Exclude the current second to avoid partial calculations
var startSeconds = moment().subtract(11, 'seconds').unix();
var endSeconds = startSeconds + 10;
metric.getTimeStatistics(
// getTimeStatistics calculates average and other statistics for a series of timer values
timer.getStatistics(
startSeconds,
endSeconds,
function(err, stats, secondsSpanned) {
......@@ -64,11 +67,11 @@ var times = [
10, 100, 110, 90, 200, 201, 300, 150, 205, 225, 500
];
// Fill the metric with values every 500ms
// Add a timer value every 500ms
var addInterval = setInterval(
function() {
var time = times.shift();
metric.time(moment().unix(), time);
timer.add(moment().unix(), time);
},
500
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment