Skip to content

Instantly share code, notes, and snippets.

@Unitech
Created February 16, 2015 01:50
Show Gist options
  • Select an option

  • Save Unitech/9dca6b8e747f85a4991a to your computer and use it in GitHub Desktop.

Select an option

Save Unitech/9dca6b8e747f85a4991a to your computer and use it in GitHub Desktop.
var timeseries = require('timeseries')({
elasticsearch_hosts : [{
port : 9200,
host : 'localhost'
}],
redis : {
port : 6379,
host : 'localhost'
}
});
/**
* Here is the data we receive every second
*
* data = {
* metrics : {
* clients : 2,
* events_1sec : 10,
* events_10sec : 100
* },
* process: {
* name : "auto-throw",
* pm_id: 11,
* server: "ultraio",
* rev: "1703a71b2d2a449f8f18ab0501e73a86f24eab2a"
* }
* };
*
*/
/**
* 1- The data is first aggregated every minutes (in Redis)
*
* {
* clients : [0, 0, 14, 15, 43, 30],
* events_1sec : [0, 0, 30, 50, 89, 90],
* events_1min : [0, 0, 130, 400, 599, 300]
* }
*
* 2- When the current minute is reached a simple average calculation is made and we append the extra_data and the minute:
*
* {
* clients : 25,
* events_1sec : 40,
* events_1min : 380,
* at : 'Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)',
* process : {
* name : "auto-throw",
* pm_id : 11,
* server : "ultraio",
* rev : "1703a71b2d2a449f8f18ab0501e73a86f24eab2a"
* }
* }
*
* 3 - The data is then flushed to database
*
*/
timeseries.put({
//uid : 'app_name:id', // Should we keep it or we can instead use the extra_data field to generate this UID?
index : 'my:index:metrics',
extra_data : { // This data must be attached to the entry when save into Elasticsearch
process: {
name : "app-name",
pm_id : 11,
server: "server-name",
rev : "1703a71b2d2a449f8f18ab0501e73a86f24eab2a"
}
},
// fields : ['clients', 'events_1sec'], // fields must be automatically retrieved depending on the data we save
data : {
clients : 2,
events_1sec : 10,
events_1min : 65
}
}, function(error, elementsStored){});
/**
* The outputted data should look like this (ES automatically do this):
*
* {
* clients : [[12312233212, 1], [12312331232, 10], [12312233212, 14]], //mean value for grouped by minute
* events_1sec : [[12312233212, 120], [12312331232, 120], [12312233212, 145], //mean value for grouped by minute
* events_1min : [[12312233212, 1900], [12312331232, 600], [12312233212, 1490], //mean value for every minute
* }
*
* fields: if it's null or empty, it should first get the most recent entry and extract the fields
* Then it does an aggs query on all these fields
* filter: for each key/value we add on it, it should just add a filter parameter
* EG: query.body.query.filtered.filter.and.push({
* term: {
* 'process.name' : opts.app_name
* }
* });
*
*/
timeseries.get({
//uid : 'app_name:id',
index : 'my:index:metrics',
fields : ['clients', 'events_1sec'], // If this field is empty or null it should retrieve fields dynamically
from : 'now-14d',
to : 'now'
interval : 'hour',
filter : { // These key/value append a filter
'process.name' : 'app-name'
}
}, function(err, data) {
if(err){
throw err;
} else {
console.log(data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment