Home

Awesome

Test Coverage Maintainability Build Status npm version GitHub

Redis-Time-Series

A Javascript client for RedisLab/RedisTimeSeries Module implemented in TypeScript and based on ioredis

Requirements

Install

npm i redis-time-series-ts

Usage

import { 
    Label, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory();
    const redisTimeSeries = factory.create();

    await redisTimeSeries.create("temperature", [new Label("sensor", 1)], 50000);

    const info = await redisTimeSeries.info("temperature");
    const label = info.labels.shift();

    if (label != null) {
        console.log(`label: ${label.getName()}=${label.getValue()}`); // label: sensor=1
    }

    console.log(`retention (ms): ${info.retentionTime}`); // retention (ms): 50000
    console.log(`last timestamp: ${info.lastTimestamp}`); // last timestamp: 0

    await redisTimeSeries.disconnect();
};

example();

If no param is provided to RedisTimeSeries constructor, it creates RedsiTimeSeries object with a default connection (port: 6379, host: "127.0.0.1" and database: 15). You can specify your connection params by providing an object of ConnectionOptions type (an IORedis RedisOptions type) which will overwrite those default connection params:

import { 
    ConnectionOptions, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const options: ConnectionOptions = {
    port: 6381,
    host: "127.0.0.1",
    db: 15
};

const factory = new RedisTimeSeriesFactory(options);
const redisTimeSeries = factory.create();

Take a look at the full list of connections params for IORedis.

Commands

After creating a RedisTimeSeries from RedisTimeSeries::create you can issue the following async commands. All of them return a Promise if the command was executed successfully, otherwise, an Error will be thrown.

.create

Creates a new time-series with an optional array of labels and optional retention. If the time-series key already exists, an Error will be thrown.

redisTimeSeries.create(
    key: string, 
    labels?: Label[], 
    retention?: number, 
    chunkSize?: number,
    duplicatePolicy?: string,
    uncompressed?: boolean
): Promise<boolean>

Label

It represents metadata labels of the time-series

Label(name: string, value: string | number)

Response

True if the time-series was created, otherwise false

Example

import { 
    Label, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory();
    const redisTimeSeries = factory.create();

    const created = await redisTimeSeries.create("temperature", [new Label("sensor", 1)], 50000);
    console.log(created); // true

    const info = await redisTimeSeries.info("temperature");
    const label = info.labels.shift();

    if (label != null) {
        console.log(`label: ${label.getName()}=${label.getValue()}`); // label: sensor=1
    }

    await redisTimeSeries.disconnect();
};

example();

More info: TS.CREATE

.alter

Updates the retention and labels of an existing time-series. Same params as create.

redisTimeSeries.alter(    
    key: string, 
    labels?: Label[], 
    retention?: number, 
    chunkSize?: number,
    duplicatePolicy?: string,
    uncompressed?: boolean
): Promise<boolean>

Response

True if the time-series was altered, otherwise false

Example

import { 
    Label, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();

    await redisTimeSeries.create("temperature", [new Label("sensor", 1)], 50000);

    const infoCreate = await redisTimeSeries.info("temperature");
    const labelCreate = infoCreate.labels.shift();

    if (labelCreate != null) {
        console.log(`label: ${labelCreate.getName()}=${labelCreate.getValue()}`); // label: sensor=1
    }

    console.log(`retention (ms): ${infoCreate.retentionTime}`); // retention (ms): 50000

    // labels are removed and retention is updated to 70000
    const altered = await redisTimeSeries.alter("temperature", [], 70000);
    console.log(altered); // true

    const infoAltered = await redisTimeSeries.info("temperature");
    const labelAltered = infoAltered.labels.shift();

    if (labelAltered != null) {
        // never executed since we removed labels
        console.log(`label: ${labelAltered.getName()}=${labelAltered.getValue()}`);
    }

    console.log(`retention (ms): ${infoAltered.retentionTime}`); // retention (ms): 70000

    await redisTimeSeries.delete("temperature");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.ALTER

.add

Appends, or first creates a time-series and then appends, a new value to the time-series.

redisTimeSeries.add(
    sample: Sample, 
    labels?: Label[], 
    retention?: number,
    chunkSize?: number,
    onDuplicate?: string,
    uncompressed?: boolean
): Promise<number>

If this command is used to add data to an existing time-series, retentionTime and labels are ignored.

Sample

A sample represents the new value to be added where:

Sample(key: string, value: number, timestamp?: number)

Response

The timestamp value of the sample added

Example

import { 
    Label, 
    Sample, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = Date.now();

    let added = await redisTimeSeries.add(
        new Sample("temperature", 100, date - 10000),
        [new Label("sensor", 1)],
        50000
    );
    console.log(added); // date - 10000

    let info = await redisTimeSeries.info("temperature");
    let label = info.labels.shift();

    if (label != null) {
        console.log(`label: ${label.getName()}=${label.getValue()}`); // label: sensor=1
    }

    console.log(`retention (ms): ${info.retentionTime}`); // retention (ms): 50000

    let sample = await redisTimeSeries.get("temperature");
    console.log(`${sample.getKey()}`); // temperature
    console.log(`${sample.getValue()}`); // 100
    console.log(`${sample.getTimestamp()}`); // date - 10000

    // a new value is added, labels and retention are ignored since we added them previously
    added = await redisTimeSeries.add(
        new Sample("temperature", 500, date - 5000),
        [new Label("sensor", 2)],
        70000
    );
    console.log(added); // date - 5000

    info = await redisTimeSeries.info("temperature");
    label = info.labels.shift();

    if (label != null) {
        console.log(`label: ${label.getName()}=${label.getValue()}`); // still  sensor=1
    }

    console.log(`retention (ms): ${info.retentionTime}`); // still retention (ms): 50000

    sample = await redisTimeSeries.get("temperature");
    console.log(`${sample.getKey()}`); // temperature
    console.log(`${sample.getValue()}`); // 500
    console.log(`${sample.getTimestamp()}`); // date - 5000

    await redisTimeSeries.delete("temperature");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.ADD

.multiAdd

Similar to Add but it appends a list of new values to a time-series, the key specified in each sample must exist.

redisTimeSeries.multiAdd(samples: Sample[]): Promise<(number | MultiAddResponseError)[]>

Response

It returns an array of integers for each value added which is the timestamp specified in the sample, following the order the samples were added. If an error happens when the sample is added, instead of an integer and object of type MultiAddResponseError will be returned:

type MultiAddResponseError = {
    stack: string;
    message: string;
};

Example

import { 
    Label, 
    Sample, 
    FilterBuilder, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = Date.now();
    const label = new Label("sensor", 1);
    const sample1 = new Sample("temperature1", 100, date - 10000);
    const sample2 = new Sample("temperature2", 200, date - 5000);

    await redisTimeSeries.create("temperature1", [label]);
    await redisTimeSeries.create("temperature2", [label]);

    const multiAdded = await redisTimeSeries.multiAdd([sample1, sample2]);
    console.log(multiAdded[0]); // date - 10000
    console.log(multiAdded[1]); // date - 5000

    const multiGet = await redisTimeSeries.multiGet(new FilterBuilder("sensor", 1));
    const temperature1 = multiGet[0];
    const temperature2 = multiGet[1];

    console.log(`${temperature1.data.getKey()}`); // temperature1
    console.log(`${temperature1.data.getValue()}`); // 100
    console.log(`${temperature1.data.getTimestamp()}`); // date - 10000

    console.log(`${temperature2.data.getKey()}`); // temperature2
    console.log(`${temperature2.data.getValue()}`); // 200
    console.log(`${temperature2.data.getTimestamp()}`); // date - 5000

    await redisTimeSeries.delete("temperature1", "temperature2");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.MADD

.incrementBy/.decrementBy

Increment or decrement the latest value in a time-series

redisTimeSeries.incrementBy(sample: Sample, labels?: Label[], retention?: number, uncompressed?: boolean): Promise<number>

redisTimeSeries.decrementBy(sample: Sample, labels?: Label[], retention?: number, uncompressed?: boolean): Promise<number>

You can use these command to add data to an non existing time-series, then labels and retention are ignored. By default, data are compressed in a time-series, you can revert this behavior by setting uncompressed to true

Response

The timestamp value of the sample incremented/decremented

Example

import { 
    Label, 
    Sample, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = Date.now();
    const label = new Label("sensor", 1);
    const sample1 = new Sample("temperature", 100, date - 10000);
    const sample2 = new Sample("temperature", 200, date - 5000);

    const increment = await redisTimeSeries.incrementBy(sample1, [label]);
    console.log(increment); // date - 10000

    let temperature = await redisTimeSeries.get("temperature");

    console.log(`${temperature.getKey()}`); // temperature
    console.log(`${temperature.getValue()}`); // 100
    console.log(`${temperature.getTimestamp()}`); // date - 10000

    const decrement = await redisTimeSeries.decrementBy(sample2, [label]);
    console.log(decrement); // date - 5000

    temperature = await redisTimeSeries.get("temperature");

    console.log(`${temperature.getKey()}`); // temperature
    console.log(`${temperature.getValue()}`); // -100
    console.log(`${temperature.getTimestamp()}`); // date - 5000

    await redisTimeSeries.delete("temperature");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.INCRBY / TS.DECRBY

.createRule/.deleteRule

it creates a compaction rule.

redisTimeSeries.createRule(sourceKey: string, destKey: string, aggregation: Aggregation): Promise<boolean>

Deletes a previous compaction rule.

redisTimeSeries.deleteRule(sourceKey: string, destKey: string): Promise<boolean>

Source and destination key must exist and be different

Aggregation

A aggregation represents a rule:

Aggregation(type: string, timeBucketInMs: number)

Response

True if the aggregation rule was created/deleted, otherwise false

Example

import { 
    Aggregation, 
    AggregationType, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();

    await redisTimeSeries.create("rule1");
    await redisTimeSeries.create("rule2");

    const aggregation = new Aggregation(AggregationType.AVG, 50000);
    const ruled = await redisTimeSeries.createRule("rule1", "rule2", aggregation);
    console.log(ruled); // true

    let info1 = await redisTimeSeries.info("rule1");
    console.log(info1.rules.rule2.getTimeBucketInMs()); // 50000
    console.log(info1.rules.rule2.getType()); // avg

    let info2 = await redisTimeSeries.info("rule2");
    console.log(info2.sourceKey); // rule1

    const deleted = await redisTimeSeries.deleteRule("rule1", "rule2");
    console.log(deleted); // true

    info1 = await redisTimeSeries.info("rule1");
    console.log(info1.rules); // {}}

    info2 = await redisTimeSeries.info("rule2");
    console.log(info2.sourceKey); // undefined

    await redisTimeSeries.delete("rule1", "rule2");
    await redisTimeSeries.disconnect();
};

example();

More info:

.range/.revRange

It queries a timestamp range.

redisTimeSeries.range(key: string, range: TimestampRange, count?: number, aggregation?: Aggregation): Promise<Array<Sample>>

TimestampRange

It represents a timestamp filter for the query:

TimestampRange(from?: number, to?: number)

Response

An array of samples which represent all the samples included in the query

Example

import { 
    TimestampRange, 
    AggregationType, 
    Aggregation, 
    Sample, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = new Date(2020, 1, 6, 11).getTime();

    await redisTimeSeries.create("range1");
    for (let i = 0; i < 10; i++) {
        await redisTimeSeries.add(new Sample("range1", 20 + i, date + i * 1000));
    }

    const aggregation = new Aggregation(AggregationType.AVG, 1000);
    const timestampRange = new TimestampRange(date, date + 10000);
    const samples = await redisTimeSeries.range("range1", timestampRange, undefined, aggregation);

    for (const sample of samples) {
        console.log(sample.getKey()); // range1
        console.log(sample.getValue()); // >=20 and < 30
        console.log(sample.getTimestamp()); // between 1580983200000 and 1580983209000 timestamp values
    }

    await redisTimeSeries.delete("range1");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.RANGE/TS.REVRANGE

.multiRange/multiRevRange

It queries a timestamp range across multiple time-series by using filters.

redisTimeSeries.multiRange(range: TimestampRange, filters: FilterBuilder, count?: number, aggregation?: Aggregation, withLabels?: boolean): Promise<Array<MultiRangeResponse>>

FilterBuilder

FilterBuilder(label: string, value: string | number)

The label and value in the constructor create a first filter where label=value. More filters can be created by calling the different methods in FilterBuilder:

Response

An array of MultiRangeResponse objects

interface MultiRangeResponse {
    key: string;
    labels: Label[];
    data: Sample[];
}

if withLabels is true, labels in MultiRangeResponse will be empty.

If some of the keys returned by the filter doesn't include any sample because, for instance, the chosen timestamp range doesn't match MultiRangeResponse.data will still include one sample in the array with value = 0 and timestamp = first timestamp found in the time-series which will be equel to 0 if the time-series has no data stored.

Example

import { 
    Label, 
    Sample, 
    Aggregation, 
    AggregationType, 
    TimestampRange, 
    FilterBuilder, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = new Date(2020, 1, 6, 11).getTime();
    const label1 = new Label("label", "1");
    const sensor1 = new Label("sensor", "1");
    const sensor2 = new Label("sensor", "2");

    await redisTimeSeries.create("multirange1", [label1, sensor1]);
    await redisTimeSeries.create("multirange2", [label1, sensor2]);

    await redisTimeSeries.create("range1");
    for (let i = 0; i < 10; i++) {
        await redisTimeSeries.add(new Sample("multirange1", 20 + i, date + i * 1000));
        await redisTimeSeries.add(new Sample("multirange2", 30 + i, date + i * 1000));
    }

    const aggregation = new Aggregation(AggregationType.MAX, 5000);
    const timestampRange = new TimestampRange(date, date + 10000);
    const filter = new FilterBuilder("label", 1).equal("sensor", 1);
    const multiRanges = await redisTimeSeries.multiRange(timestampRange, filter, undefined, aggregation, true);
    const multiRange = multiRanges.shift();
    
    console.log(multiRange.key); //multirange1
    
    const labels = multiRange.labels;
    console.log(labels.shift()); // Label { name: 'label', value: '1' }
    console.log(labels.shift()); // Label { name: 'sensor', value: '1' }

    
    const samples = multiRange.data;
    
    console.log(samples.shift().getValue()); // 24  
    console.log(samples.shift().getValue()); // 29;
    console.log(multiRanges.length); // 0

    await redisTimeSeries.delete("range1");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.MRANGE/TS.MREVRANGE

.get

Get the last sample from an existing time-series.

redisTimeSeries.get(key: string): Promise<Sample>

Response

The last sample in the time-series specified by key

Example

import { 
    Sample, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = new Date(2020, 1, 6, 11).getTime();

    await redisTimeSeries.add(new Sample("get", 20, date));

    const sample = await redisTimeSeries.get("get");

    console.log(sample.getKey()); // get
    console.log(sample.getValue()); // 20
    console.log(sample.getTimestamp()); // 1580983200000;

    await redisTimeSeries.delete("get");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.GET

.multiGet

Gets the last samples matching the specific filter.

redisTimeSeries.multiGet(filters: FilterBuilder): Promise<Array<MultiGetResponse>>

The filters param is a FilterBuilder which will generate an array of filters to be applied across multiple time-series

Response

An array of MultiGetResponse objects

interface MultiRangeResponse {
    key: string;
    labels: Label[];
    data: Sample;
}

if withLabels is true, labels in MultiRangeResponse will be empty

If for a key returned because matches the filter but doesn't contain any data, MultiGetResponse.data will contain a sample with value = 0 and timestamp = 0;

Example

import { 
    FilterBuilder, 
    Sample, 
    Label, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = new Date(2020, 1, 6, 11).getTime();
    const label1 = new Label("label", "1");
    const sensor1 = new Label("sensor", "1");
    const sensor2 = new Label("sensor", "2");

    await redisTimeSeries.create("multiget1", [label1, sensor1]);
    await redisTimeSeries.create("multiget2", [label1, sensor2]);

    for (let i = 0; i < 10; i++) {
        await redisTimeSeries.add(new Sample("multiget1", 20 + i, date + i * 1000));
        await redisTimeSeries.add(new Sample("multiget2", 30 + i, date + i * 1000));
    }

    const filter = new FilterBuilder("label", 1);
    const multiGets = await redisTimeSeries.multiGet(filter);

    const multiGet1 = multiGets.shift();
    console.log(multiGet1.key); // multiget1
    
    const labels1 = multiGet1.labels;
    console.log(labels1.shift()); // Label { name: 'label', value: '1' }
    console.log(labels1.shift()); // Label { name: 'sensor', value: '1' }
    
    const sample1 = multiGet1.data;
    
    console.log(sample1.getValue()); // 29
    console.log(sample1.getTimestamp()); // 1580983209000

    const multiGet2 = multiGets.shift();
    
    console.log(multiGet2.key); // multiget2
    
    const labels2 = multiGet2.labels;
    console.log(labels2.shift()); // Label { name: 'label', value: '1' }
    console.log(labels2.shift()); // Label { name: 'sensor', value: '2' }

    const sample2 = multiGet2.data;
    
    console.log(sample2.getValue()); // 39
    console.log(sample2.getTimestamp()); // 1580983209000

    await redisTimeSeries.delete("multiget1", "multiget2");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.MULTIGET

.queryIndex

Get all time-series keys matching the filter list.

redisTimeSeries.mueryIndex(filters: FilterBuilder): Promise<string[]>

The filters param is a FilterBuilder which will generate an array of filter to be applied across multiple time-series

Response

An array of time-series keys

Example

import { 
    Label, 
    Sample, 
    FilterBuilder, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = new Date(2020, 1, 6, 11).getTime();
    const label1 = new Label("label", "1");
    const sensor1 = new Label("sensor", "1");
    const sensor2 = new Label("sensor", "2");

    await redisTimeSeries.create("query1", [label1, sensor1]);
    await redisTimeSeries.create("query2", [label1, sensor2]);

    for (let i = 0; i < 10; i++) {
        await redisTimeSeries.add(new Sample("query1", 20 + i, date + i * 1000));
        await redisTimeSeries.add(new Sample("query2", 30 + i, date + i * 1000));
    }

    const filter = new FilterBuilder("label", 1);
    const keys = await redisTimeSeries.queryIndex(filter);

    // @ts-ignore
    console.log(keys.shift()); // query1
    // @ts-ignore
    console.log(keys.shift()); // query2

    await redisTimeSeries.delete("query1", "query2");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.QUERYINDEX

.info

Returns information and statistics a time-series specified by key.

redisTimeSeries.info(key: string): Promise<InfoResponse>

Response

An InfoResponse object

interface InfoResponse {
    totalSamples: number;
    memoryUsage: number;
    firstTimestamp: number;
    lastTimestamp: number;
    retentionTime: number;
    chunkCount: number;
    chunkSize: number;
    chunkType: string;
    labels: Label[];
    duplicatePolicy: string;
    sourceKey?: string;
    rules: AggregationByKey;
}

interface AggregationByKey {
    [key: string]: Aggregation;
}

Example

import { 
    Label, 
    Sample, 
    RedisTimeSeriesFactory 
} from "redis-time-series-ts";

const example = async () => {
    const factory = new RedisTimeSeriesFactory({ port: 6381, db: 15 });
    const redisTimeSeries = factory.create();
    const date = new Date(2020, 1, 6, 11).getTime();
    const label = new Label("label", "1");

    await redisTimeSeries.add(new Sample("info", 20, date), [label], 50000);

    const info = await redisTimeSeries.info("info");

    console.log(info.totalSamples); // 1
    console.log(info.memoryUsage); // 1
    console.log(info.firstTimestamp); // 1580983200000
    console.log(info.lastTimestamp); // 1580983200000
    console.log(info.retentionTime); // 50000
    console.log(info.sourceKey); // undefined
    console.log(info.labels.shift()); // Label { name: 'label', value: '1' }
    console.log(info.chunkSize); // 256
    console.log(info.chunkCount); // 1
    console.log(info.chunkType); // 'uncompressed'
    console.log(info.duplicatePolicy); // 'LAST'
    console.log(info.rules); // {}

    await redisTimeSeries.delete("info");
    await redisTimeSeries.disconnect();
};

example();

More info: TS.INFO

.expire

Expires a time-series key by using Redis expire command.

redisTimeSeries.expire(key: string, seconds: number): Promise<boolean>

.delete

Deletes a time-series key by using Redis del command.

redisTimeSeries.delete(key: string): Promise<boolean>

Response

It returns true if the key was deleted successfully, otherwise false.

.deleteAll

Deletes all keys in the current Redis database by using Redis flushdb command.

redisTimeSeries.deleteAll(): Promise<boolean>

Response

It returns true if all keys were deleted successfully, otherwise false.

.reset

Resets a time-series key by deleting and then recreating the time-series with the labels and retention specified, if any.

redisTimeSeries.reset(key: string, labels?: Label[], retention?: number): Promise<boolean>

Response

It returns true if key was created successfully, otherwise false. If key doesn't exist or could not be deleted and error is thrown.

.disconnect

Disconnects the RedisTimeSeries client from Redis server.

redisTimeSeries.disconnect(): Promise<boolean>

Response

It returns true if the client was disconnected successfully, otherwise false.

Testing

Tests can be run locally with docker. docker.compose.yml file will build two services:

You can follow these steps to build the Docker services and run the tests:

License

Redis-time-series code is distributed under MIT license, see LICENSE file