Skip to content

nikolovskimilos/redistimeseries-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tests npm version

redistimeseries-js

JavaScript client for RedisTimeSeries

npm install redistimeseries-js

Example

const RedisTimeSeries = require('redistimeseries-js');


// check base redis client for options
// https://github.com/NodeRedis/node_redis
const options = {
  host: 'localhost',
  port: 6379
}

const rtsClient = new RedisTimeSeries(options);
const key = 'temperature';

const updateTemperature = async () => {
  await rtsClient.add(key, Date.now(), Math.floor(Math.random()*30)).send();
}

const start = async () => {
  await rtsClient.connect();
  await rtsClient.create(key).retention(60000).send();
  setInterval(updateTemperature, 1000);
}

start();

Filtering

Filters are represented as array of conditions

const { Filter } = require('redistimeseries-js');

const myFilter = [
  Filter.equal('area_id', 32),
  Filter.notEqual('sensor_id', 1),
  Filter.exists('sub_area_id'),
  Filter.notExists('outdoor'),
  Filter.in('secitons', [2, 3, 4]),
  Filter.notIn('secitons', [5, 6])
];

Aggregation

Possible aggregation values

const { Aggregation } = require('redistimeseries-js');

// Aggregation.AVG
// Aggregation.SUM
// Aggregation.MIN
// Aggregation.MAX
// Aggregation.RANGE
// Aggregation.COUNT
// Aggregation.FIRST
// Aggregation.LAST
// Aggregation.STDP
// Aggregation.STDS
// Aggregation.VARP
// Aggregation.VARS

Duplicate Policy

Possible duplicate policy values

const { DuplicatePolicy } = require('redistimeseries-js');

// DuplicatePolicy.BLOCK
// DuplicatePolicy.FIRST
// DuplicatePolicy.LAST
// DuplicatePolicy.MIN
// DuplicatePolicy.MAX
// DuplicatePolicy.SUM

Methods

Examples for each method are shown below, notice that optional parameters are always represented as object which is the last argument in the methods.

create

// TS.CREATE temperature:2:32 RETENTION 60000 LABELS sensor_id 2 area_id 32 UNCOMPRESSED

const RedisTimeSeries = require('redistimeseries-js');
const { DuplicatePolicy } = RedisTimeSeries;

client
  .create('temperature:2:32')
  .retention(60000)
  .labels({ sensor_id: 2, area_id: 32 })
  .duplicatePolicy(DuplicatePolicy.LAST)
  .uncompressed()
  .send();

alter

// TS.ALTER temperature:2:32 LABELS sensor_id 2 area_id 32 sub_area_id 15

client
  .alter('temperature:2:32')
  .labels({ sensor_id: 2, area_id: 32, sub_area_id: 15 })
  .send();

add

// TS.ADD temperature:2:32 1548149180000 26 LABELS sensor_id 2 area_id 32

const RedisTimeSeries = require('redistimeseries-js');
const { DuplicatePolicy } = RedisTimeSeries;

client
  .add('temperature:2:32', 1548149180000, 26)
  .onDuplicate(DuplicatePolicy.SUM)
  .labels({ sensor_id: 2, area_id: 32 })
  .send();
// TS.ADD temperature:3:11 1548149183000 27 RETENTION 3600

client
  .add('temperature:2:32', 1548149180000, 26)
  .retention(3600)
  .send();

madd

// TS.MADD temperature:2:32 1548149180000 26 cpu:2:32 1548149183000 54

client
  .madd([
    { key: 'temperature:2:32', timestamp: 1548149180000, value: 26 },
    { key: 'cpu:2:32', timestamp: 1548149183000, value: 54 }
  ]);
  .send();

incrBy

// TS.INCRBY temperature:2:32 3 RETENTION 30000

client
  .incrBy('temperature:2:32', 3)
  .retention(30000)
  .send();

decrBy

// TS.DECRBY temperature:2:32 5 RETENTION 30000 UNCOMPRESSED

client
  .decrBy('temperature:2:32', 5)
  .retention(30000)
  .uncompressed()
  .send();

createRule

// TS.CREATERULE temperature:2:32 temperature:avg AGGREGATION avg 60000

const RedisTimeSeries = require('redistimeseries-js');
const { Aggregation } = RedisTimeSeries;

// ...

client
  .createRule('temperature:2:32', 'temperature:avg')
  .aggregation(Aggregation.AVG, 60000)
  .send();

deleteRule

// TS.DELETE temperature:2:32 temperature:avg

client.deleteRule('temperature:2:32', 'temperature:avg').send();

range

// TS.RANGE temperature:3:32 1548149180000 1548149210000 AGGREGATION avg 5000
// TS.REVRANGE temperature:3:32 1548149180000 1548149210000 AGGREGATION avg 5000

const RedisTimeSeries = require('redistimeseries-js');
const { Aggregation } = RedisTimeSeries;

// ...

client
  .range('temperature:2:32', 1548149180000, 1548149210000)
  .aggregation(Aggregation.AVG, 5000)
  .send();

mrange

// TS.MRANGE 1548149180000 1548149210000 AGGREGATION avg 5000 FILTER area_id=32 sensor_id!=1
// TS.MREVRANGE 1548149180000 1548149210000 AGGREGATION avg 5000 FILTER area_id=32 sensor_id!=1

const RedisTimeSeries = require('redistimeseries-js');
const { Aggregation, Filter } = RedisTimeSeries;

// ...

client
  .mrange('temperature:2:32', 1548149180000, 1548149210000)
  .filter([
    Filter.equal('area_id', 32),
    Filter.notEqual('sensor_id', 1)
  ])
  .aggregation(Aggregation.AVG, 5000)
  .send();
// TS.MRANGE 1548149180000 1548149210000 AGGREGATION avg 5000 WITHLABELS FILTER area_id=32 sensor_id!=1
// TS.MREVRANGE 1548149180000 1548149210000 AGGREGATION avg 5000 WITHLABELS FILTER area_id=32 sensor_id!=1

const RedisTimeSeries = require('redistimeseries-js');
const { Aggregation, Filter } = RedisTimeSeries;

// ...

client
  .mrevrange('temperature:2:32', 1548149180000, 1548149210000)
  .aggregation(Aggregation.AVG, 5000)
  .withLabels()
  .filter([
    Filter.equal('area_id', 32),
    Filter.notEqual('sensor_id', 1)
  ])
  .send();

get

// TS.GET temperature:2:32

client
  .get('temperature:2:32')
  .send();

mget

// TS.MGET FILTER area_id=32

const RedisTimeSeries = require('redistimeseries-js');
const { Filter } = RedisTimeSeries;

// ...

client
  .mget()
  .filter([ Filter.equal('area_id', 32) ])
  .send();

info

// TS.INFO temperature:2:32

client
  .info('temperature:2:32')
  .send();

queryIndex

// TS.QUERYINDEX sensor_id=2

client
  .queryIndex([ Filter.equal('sensor_id', 2) ])
  .send();