Skip to content

Instantly share code, notes, and snippets.

@Unitech
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save Unitech/8d61538a0311f1be19e9 to your computer and use it in GitHub Desktop.

Select an option

Save Unitech/8d61538a0311f1be19e9 to your computer and use it in GitHub Desktop.

Revisions

  1. Unitech revised this gist Sep 28, 2014. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions timeseries.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    var timeseries = require('timeseries')({
    'rethinkConnectionString':"rethinkdb://username:[email protected]:57012/databasename",
    'redisConnectionString':"redis://redistogo:[email protected]:10833/"
    rethinkdb : {
    port : 28015,
    host : 'localhost',
    auth_key : null
    },
    redis : {
    port : 6379,
    host : 'localhost'
    }
    });

    /**
    @@ -9,6 +16,7 @@ var timeseries = require('timeseries')({
    timeseries.put({
    uid : 'specific-key',
    table : 'websocket-monitoring',
    rdb_database : 'specific-database',
    fields : ['clients', 'events_1sec', 'events_1min'],
    data : [
    {
    @@ -55,9 +63,10 @@ timeseries.put({
    timeseries.get({
    uid : 'specific-key',
    table : 'websocket-monitoring',
    rdb_database : 'specific-database',
    fields : ['clients', 'events_1sec', 'events_1min'],
    from : new Date(Date.now() - 6*60*1000), //6 minutes ago?
    to : new Date(Date.now()) //hour ago
    from : moment().toDate(),
    to : moment().subtract(7, 'days').toDate(),
    }, function(err, data) {
    if(err){
    throw err;
  2. @vodolaz095 vodolaz095 revised this gist Sep 24, 2014. 2 changed files with 0 additions and 70 deletions.
    47 changes: 0 additions & 47 deletions put_method.js
    Original file line number Diff line number Diff line change
    @@ -1,47 +0,0 @@
    var timeseries = require('timeseries');

    var incoming_data = {
    client : 2,
    events_1sec : 10,
    events_1min : 65
    };

    /**
    * The timeseries module must be state-less
    */
    timeseries.put({
    uid : 'specific-key',
    db_name : 'database-name',
    connection : connection_to_rethinkdb_database,
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min'],
    data : incoming_data
    });

    /**
    * 1- The data is first aggregated every minutes (in memory or Redis, memory should be enough)
    *
    * {
    * 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 mean calculation is made and we append the uid and the minute:
    *
    * {
    * clients : 25,
    * events_1sec : 40,
    * events_1min : 380,
    * at : 'Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)',
    * uid : 'specific-key'
    * }
    *
    * 3 - The data is then flushed to database
    *
    * r.db(opts.db_name)
    * .table(opts.table)
    * .insert(data_object)
    * .run(opts.connection, function(err, cursor) { }
    *
    */
    23 changes: 0 additions & 23 deletions retrieve_method.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    var timeseries = require('timeseries');

    timeseries.get({
    uid : 'specific-key',
    db_name : 'database-name',
    connection : connection_to_rethinkdb_database,
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min']
    }, function(err, data) {
    return console.log(data);
    });

    /**
    * The outputted data should look like this:
    *
    * {
    * clients : [0, 0, 14, 15, 43, 30],
    * events_1sec : [0, 0, 30, 50, 89, 90],
    * events_1min : [0, 0, 130, 400, 599, 300]
    * time : ['Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)', 'Wed Sep 24 2014 17:30:00 GMT+0200 (CEST)', '...every min...']
    * }
    *
    */
  3. @vodolaz095 vodolaz095 revised this gist Sep 24, 2014. 1 changed file with 79 additions and 0 deletions.
    79 changes: 79 additions & 0 deletions timeseries.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    var timeseries = require('timeseries')({
    'rethinkConnectionString':"rethinkdb://username:[email protected]:57012/databasename",
    'redisConnectionString':"redis://redistogo:[email protected]:10833/"
    });

    /**
    * The timeseries module must be state-less
    */
    timeseries.put({
    uid : 'specific-key',
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min'],
    data : [
    {
    clients : 2,
    events_1sec : 10,
    events_1min : 65
    },
    {
    clients : 2,
    events_1sec : 10,
    events_1min : 65
    }
    ]
    }, function(error, elementsStored){});

    /**
    * 1- The data is first aggregated every minutes (in memory or Redis, memory should be enough)
    *
    * {
    * 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 mean calculation is made and we append the uid and the minute:
    *
    * {
    * clients : 25,
    * events_1sec : 40,
    * events_1min : 380,
    * at : 'Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)',
    * uid : 'specific-key'
    * }
    *
    * 3 - The data is then flushed to database
    *
    * r.db(opts.db_name)
    * .table(opts.table)
    * .insert(data_object)
    * .run(opts.connection, function(err, cursor) { }
    *
    */

    timeseries.get({
    uid : 'specific-key',
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min'],
    from : new Date(Date.now() - 6*60*1000), //6 minutes ago?
    to : new Date(Date.now()) //hour ago
    }, function(err, data) {
    if(err){
    throw err;
    } else {
    console.log(data);
    }
    });

    /**
    * The outputted data should look like this:
    *
    * {
    * clients : [0, 0, 14, 15, 43, 30], //you mean 1 batch for every minute?
    * events_1sec : [0, 0, 30, 50, 89, 90],
    * events_1min : [0, 0, 130, 400, 599, 300]
    * time : ['Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)', 'Wed Sep 24 2014 17:30:00 GMT+0200 (CEST)', '...every min...']
    * }
    *
    */
  4. Unitech revised this gist Sep 24, 2014. 2 changed files with 6 additions and 5 deletions.
    2 changes: 1 addition & 1 deletion put_method.js
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,6 @@ timeseries.put({
    * r.db(opts.db_name)
    * .table(opts.table)
    * .insert(data_object)
    * .run(opts.connection, function(err, cursor) { /*...*/ }
    * .run(opts.connection, function(err, cursor) { }
    *
    */
    9 changes: 5 additions & 4 deletions retrieve_method.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,11 @@
    var timeseries = require('timeseries');

    timeseries.get({
    uid : 'specific-key',
    db : connection_to_database,
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min']
    uid : 'specific-key',
    db_name : 'database-name',
    connection : connection_to_rethinkdb_database,
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min']
    }, function(err, data) {
    return console.log(data);
    });
  5. Unitech revised this gist Sep 24, 2014. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Put method → put_method.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ var incoming_data = {
    };

    /**
    * The put method must be state-less
    * The timeseries module must be state-less
    */
    timeseries.put({
    uid : 'specific-key',
    File renamed without changes.
  6. Unitech created this gist Sep 24, 2014.
    47 changes: 47 additions & 0 deletions Put method
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    var timeseries = require('timeseries');

    var incoming_data = {
    client : 2,
    events_1sec : 10,
    events_1min : 65
    };

    /**
    * The put method must be state-less
    */
    timeseries.put({
    uid : 'specific-key',
    db_name : 'database-name',
    connection : connection_to_rethinkdb_database,
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min'],
    data : incoming_data
    });

    /**
    * 1- The data is first aggregated every minutes (in memory or Redis, memory should be enough)
    *
    * {
    * 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 mean calculation is made and we append the uid and the minute:
    *
    * {
    * clients : 25,
    * events_1sec : 40,
    * events_1min : 380,
    * at : 'Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)',
    * uid : 'specific-key'
    * }
    *
    * 3 - The data is then flushed to database
    *
    * r.db(opts.db_name)
    * .table(opts.table)
    * .insert(data_object)
    * .run(opts.connection, function(err, cursor) { /*...*/ }
    *
    */
    22 changes: 22 additions & 0 deletions Retrieve
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    var timeseries = require('timeseries');

    timeseries.get({
    uid : 'specific-key',
    db : connection_to_database,
    table : 'websocket-monitoring',
    fields : ['clients', 'events_1sec', 'events_1min']
    }, function(err, data) {
    return console.log(data);
    });

    /**
    * The outputted data should look like this:
    *
    * {
    * clients : [0, 0, 14, 15, 43, 30],
    * events_1sec : [0, 0, 30, 50, 89, 90],
    * events_1min : [0, 0, 130, 400, 599, 300]
    * time : ['Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)', 'Wed Sep 24 2014 17:30:00 GMT+0200 (CEST)', '...every min...']
    * }
    *
    */