redis-time-series-ts
Version:
Javascript RedisTimeSeries client
622 lines (621 loc) • 34.2 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisTimeSeries = void 0;
const commandName_1 = require("./enum/commandName");
const sample_1 = require("./entity/sample");
const disconnectCommand_1 = require("./command/disconnectCommand");
const timeSeriesCommand_1 = require("./command/timeSeriesCommand");
const expireCommand_1 = require("./command/expireCommand");
const deleteCommand_1 = require("./command/deleteCommand");
const deleteAllCommand_1 = require("./command/deleteAllCommand");
class RedisTimeSeries {
constructor(provider, receiver, invoker, director, renderFactory) {
this.provider = provider;
this.receiver = receiver;
this.invoker = invoker;
this.director = director;
this.renderFactory = renderFactory;
}
/**
* Create a new time-series.
*
* Docs: [TS.CREATE](https://oss.redislabs.com/redistimeseries/commands/#tscreate).
*
* @param key Key name for timeseries.
* @param labels Array of Label objects (label-value pairs) that represent metadata labels of the key.
* Use `new Label('label', value)` to create a new Label object.
* @param retention Maximum age for samples compared to last event time (in milliseconds).
* Default: The global retention secs configuration of the database (by default, 0 ).
* When set to 0, the series is not trimmed at all.
* @param chunkSize Amount of memory, in bytes, allocated for data. Default: 4000.
* @param duplicatePolicy Configure what to do on duplicate sample.
* See more on [DUPLICATE_POLICY](https://oss.redislabs.com/redistimeseries/configuration/#DUPLICATE_POLICY).
*
* When this is not set, the server-wide default will be used.
*
* - BLOCK - an error will occur for any out of order sample.
* - FIRST - ignore the new value.
* - LAST - override with latest value.
* - MIN - only override if the value is lower than the existing value.
* - MAX - only override if the value is higher than the existing value.
* @param uncompressed Cince version 1.2, both timestamps and values are compressed by default.
* Adding this flag will keep data in an uncompressed form.
* Compression not only saves memory but usually improve performance due to lower number of memory accesses.
* @returns `true` if timeseries created successfully. `false` otherwise.
*
* @remarks
* Complexity -- O(1)
*/
create(key, labels, retention, chunkSize, duplicatePolicy, uncompressed) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director
.create(key, labels, retention, chunkSize, duplicatePolicy, uncompressed)
.get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.CREATE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return response === "OK";
});
}
/**
* Update the retention, labels of an existing key.
*
* Docs: [TS.ALTER](https://oss.redislabs.com/redistimeseries/commands/#tsalter).
*
* @param key Key name for timeseries
* @param labels Array of Label objects (label-value pairs) that represent metadata labels of the key.
* Use `new Label('label', value)` to create a new Label object.
* @param retention Maximum age for samples compared to last event time (in milliseconds).
* Default: The global retention secs configuration of the database (by default, 0 ).
* When set to 0, the series is not trimmed at all.
* @param chunkSize Amount of memory, in bytes, allocated for data. Default: 4000.
* @param duplicatePolicy Configure what to do on duplicate sample.
* See more on [DUPLICATE_POLICY](https://oss.redislabs.com/redistimeseries/configuration/#DUPLICATE_POLICY)
*
* When this is not set, the server-wide default will be used.
*
* - BLOCK - an error will occur for any out of order sample.
* - FIRST - ignore the new value.
* - LAST - override with latest value.
* - MIN - only override if the value is lower than the existing value.
* - MAX - only override if the value is higher than the existing value.
* @param uncompressed Since version 1.2, both timestamps and values are compressed by default.
* Adding this flag will keep data in an uncompressed form.
* Compression not only saves memory but usually improve performance due to lower number of memory accesses.
* @returns `true` if timeseries altered successfully. `false` otherwise.
*/
alter(key, labels, retention, chunkSize, duplicatePolicy, uncompressed) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director
.alter(key, labels, retention, chunkSize, duplicatePolicy, uncompressed)
.get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.ALTER, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return response === "OK";
});
}
/**
* Append (or create and append) a new sample to the series.
*
* Docs: [TS.ADD](https://oss.redislabs.com/redistimeseries/commands/#tsadd).
*
* @param sample The sample to add to the timeseries. Use `new Sample(key, timestamp, value)` to create it
* @param labels Array of Label objects (label-value pairs) that represent metadata labels of the key.
* Use `new Label('label', value)` to create a new Label object
* @param retention Maximum age for samples compared to last event time (in milliseconds).
* Default: The global retention secs configuration of the database (by default, 0 ).
* When set to 0, the series is not trimmed at all
* @param chunkSize Amount of memory, in bytes, allocated for data. Default: 4000.
* @param onDuplicate Configure what to do on duplicate sample.
* See more on [DUPLICATE_POLICY](https://oss.redislabs.com/redistimeseries/configuration/#DUPLICATE_POLICY)
*
* When this is not set, the server-wide default will be used.
*
* - BLOCK - an error will occur for any out of order sample.
* - FIRST - ignore the new value.
* - LAST - override with latest value.
* - MIN - only override if the value is lower than the existing value.
* - MAX - only override if the value is higher than the existing value.
* @param uncompressed Since version 1.2, both timestamps and values are compressed by default.
* Adding this flag will keep data in an uncompressed form.
* Compression not only saves memory but usually improve performance due to lower number of memory accesses.
* @returns The timestamp of the added Sample.
*
* @remarks
* Complexity:
*
* If a compaction rule exits on a timeseries, TS.ADD performance might be reduced.
* The complexity of TS.ADD is always O(M) when M is the amount of compaction rules or O(1) with no compaction.
*/
add(sample, labels, retention, chunkSize, onDuplicate, uncompressed) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director
.add(sample, labels, retention, chunkSize, onDuplicate, uncompressed)
.get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.ADD, params);
return yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
});
}
/**
* Append new samples to a list of series.
*
* Docs: [TS.MADD](https://oss.redislabs.com/redistimeseries/commands/#tsmadd)
*
* @param samples The array of samples to add to the timeseries. Use `new Sample(key, timestamp, value)` to create a sample
* @returns the timestamp of the added Samples
*
* @remarks
* Complexity:
*
* If a compaction rule exits on a timeseries, multiAdd (TS.MADD) performance might be reduced.
* The complexity of TS.MADD is always O(N*M) when N is the amount of series updated
* and M is the amount of compaction rules or O(N) with no compaction.
*/
multiAdd(samples) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.multiAdd(samples).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.MADD, params);
return yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
});
}
/**
* Creates a new sample that increments the latest sample's value.
*
* Docs: [TS.INCRBY](https://oss.redislabs.com/redistimeseries/commands/#tsincrbytsdecrby)
*
* @param sample The sample to add to the timeseries. Use `new Sample(key, timestamp, value)` to create it
* @param labels Array of Label objects (label-value pairs) that represent metadata labels of the key.
* Use `new Label('label', value)` to create a new Label object
* @param retention Maximum age for samples compared to last event time (in milliseconds).
* Default: The global retention secs configuration of the database (by default, 0 ).
* When set to 0, the series is not trimmed at all
* @param uncompressed Since version 1.2, both timestamps and values are compressed by default.
* Adding this flag will keep data in an uncompressed form.
* Compression not only saves memory but usually improve performance due to lower number of memory accesses.
* @param chunkSize Amount of memory, in bytes, allocated for data. Default: 4000.
*
* @remarks
* - You can use this command to add data to an non existing timeseries in a single command.
* This is the reason why labels and retentionTime are optional arguments.
*
* - When specified and the key doesn't exist, RedisTimeSeries will create the key with the specified labels and or retentionTime .
* Setting the labels and retentionTime introduces additional time complexity.
*/
incrementBy(sample, labels, retention, uncompressed, chunkSize) {
return __awaiter(this, void 0, void 0, function* () {
return this.changeBy(commandName_1.CommandName.INCRBY, sample, labels, retention, uncompressed, chunkSize);
});
}
/**
* Creates a new sample that decrements the latest sample's value.
*
* Docs: [TS.DECRBY](https://oss.redislabs.com/redistimeseries/commands/#tsincrbytsdecrby).
*
* @param sample The sample to add to the timeseries. Use `new Sample(key, timestamp, value)` to create it.
* @param labels Array of Label objects (label-value pairs) that represent metadata labels of the key.
* Use `new Label('label', value)` to create a new Label object.
* @param retention Maximum age for samples compared to last event time (in milliseconds).
* Default: The global retention secs configuration of the database (by default, 0 ).
* When set to 0, the series is not trimmed at all.
* @param uncompressed Since version 1.2, both timestamps and values are compressed by default.
* Adding this flag will keep data in an uncompressed form.
* Compression not only saves memory but usually improve performance due to lower number of memory accesses.
* @param chunkSize Amount of memory, in bytes, allocated for data. Default: 4000.
*
* @remarks
* - You can use this command to add data to an non existing timeseries in a single command.
* This is the reason why labels and retentionTime are optional arguments.
*
* - When specified and the key doesn't exist, RedisTimeSeries will create the key with the specified labels and or retentionTime .
* Setting the labels and retentionTime introduces additional time complexity.
*/
decrementBy(sample, labels, retention, uncompressed, chunkSize) {
return __awaiter(this, void 0, void 0, function* () {
return this.changeBy(commandName_1.CommandName.DECRBY, sample, labels, retention, uncompressed, chunkSize);
});
}
/**
* Create a compaction rule.
*
* Docs: [TS.CREATERULE](https://oss.redislabs.com/redistimeseries/commands/#tscreaterule).
*
* @param sourceKey Key name for source time series.
* @param destKey Key name for destination time series.
* @param aggregation Aggregation Object -- avg, sum, min, max, range, count, first, last, std.p, std.s, var.p, var.s.
* Create with `new Aggregation(type,timeBucketinMs)`
* @returns `true` if rule created. `false` otherwise.
*
* @remarks
* - Currently, only new samples that are added into the source series after creation of the rule will be aggregated.
* - `destKey` should be of a timeseries type, and should be created before `createRule` is called.
*/
createRule(sourceKey, destKey, aggregation) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.createRule(sourceKey, destKey, aggregation).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.CREATE_RULE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return response === "OK";
});
}
/**
* Delete a compaction rule.
*
* Docs: [TS.DELETERULE](https://oss.redislabs.com/redistimeseries/commands/#tsdeleterule)
*
* @param sourceKey Key name for source time series
* @param destKey Key name for destination time series
* @returns `true` if rule deleted. `false` otherwise
*/
deleteRule(sourceKey, destKey) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.deleteRule(sourceKey, destKey).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.DELETE_RULE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return response === "OK";
});
}
/**
* Query a range in the forward direction.
*
* Docs: [TS.RANGE](https://oss.redislabs.com/redistimeseries/commands/#tsrangetsrevrange).
*
* @param key Key name for timeseries.
* @param range A TimestampRange object. Contains Start and End timestamps for the range query.
* Create with `new TimestampRange(from, to)`. Leave both params `from` and `to` as `undefined` i.e. `new TimestampRange()`.
* to express the minimum possible timestamp (`-`) and the maximum possible timestamp (`+`).
* @param count Maximum number of returned results
* @param aggregation Aggregation Object -- avg, sum, min, max, range, count, first, last, std.p, std.s, var.p, var.s.
* Create with `new Aggregation(type,timeBucketinMs)`.
* @returns An array of `Sample` objects containing the timestamp and value.
*
* @remarks
* Complexity:
*
* TS.RANGE complexity is O(n/m+k).
* n = Number of data points m = Chunk size (data points per chunk) k = Number of data points that are in the requested range.
* This can be improved in the future by using binary search to find the start of the range, which makes this O(Log(n/m)+k*m).
* But because m is pretty small, we can neglect it and look at the operation as O(Log(n) + k).
*/
range(key, range, count, aggregation) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.range(key, range, count, aggregation).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.RANGE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
const samples = [];
for (const sample of response) {
samples.push(new sample_1.Sample(key, Number(sample[1]), sample[0]));
}
return samples;
});
}
/**
* Query a range in the reverse direction.
*
* Docs: [TS.REVRANGE](https://oss.redislabs.com/redistimeseries/commands/#tsrangetsrevrange).
*
* @param key Key name for timeseries
* @param range A TimestampRange object. Contains Start and End timestamps for the range query.
* Create with `new TimestampRange(from, to)`. Leave both params `from` and `to` as `undefined` i.e. `new TimestampRange()`.
* to express the minimum possible timestamp (`-`) and the maximum possible timestamp (`+`).
* @param count Maximum number of returned results.
* @param aggregation Aggregation Object -- avg, sum, min, max, range, count, first, last, std.p, std.s, var.p, var.s.
* Create with `new Aggregation(type,timeBucketinMs)`.
* @returns An array of `Sample` objects containing the timestamp and value.
*
* @remarks
* Complexity:
*
* TS.REVRANGE complexity is O(n/m+k).
* n = Number of data points m = Chunk size (data points per chunk) k = Number of data points that are in the requested range.
* This can be improved in the future by using binary search to find the start of the range, which makes this O(Log(n/m)+k*m).
* But because m is pretty small, we can neglect it and look at the operation as O(Log(n) + k).
*/
revRange(key, range, count, aggregation) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.range(key, range, count, aggregation).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.REV_RANGE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
const samples = [];
for (const sample of response) {
samples.push(new sample_1.Sample(key, Number(sample[1]), sample[0]));
}
return samples;
});
}
/**
* Query a range across multiple time-series by filters in the forward direction.
*
* Docs: [TS.MRANGE](https://oss.redislabs.com/redistimeseries/commands/#tsmrangetsmrevrange).
*
* @param range A TimestampRange object. Contains Start and End timestamps for the range query.
* Create with `new TimestampRange(from, to)`. Leave both params `from` and `to` as `undefined` i.e. `new TimestampRange()`
* to express the minimum possible timestamp (`-`) and the maximum possible timestamp (`+`)
* @param filters A filters object. Create with `new FilterBuilder(label, value)`. Chain methods to make more complex filters.
* See docs on [filtering](https://oss.redislabs.com/redistimeseries/commands/#filtering)
*
* Example:
*
* ```ts
* // Filter timeseries with labels `device=raspberry_23` and `sensor=temperature_1`:
* const filter = new FilterBuilder("device", "raspberry_23").equal("sensor", "temperature_1");
* ```
* Methods that can be chained: `equal`,`notEqual`, `exists`, `notExists`, `in`, `notIn`. See README for more examples on filter usage.
*
* @param count Maximum number of returned results per time-series
* @param aggregation Aggregation Object -- avg, sum, min, max, range, count, first, last, std.p, std.s, var.p, var.s.
* Create with `new Aggregation(type,timeBucketinMs)`
* @param withLabels Include in the reply the label-value pairs that represent metadata labels of the time-series.
* If this argument is not set, by default, an empty Array will be replied on the labels array position.
* @returns a promise containing an array of multi-range response objects i.e. `{ key: key, labels: Label[], data: Sample[] }`
*/
multiRange(range, filters, count, aggregation, withLabels) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director
.multiRange(range, filters, count, aggregation, withLabels)
.get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.MULTI_RANGE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return this.renderFactory.getMultiRangeRender().render(response);
});
}
/**
* Query a range across multiple time-series by filters in the reverse direction.
*
* Docs: [TS.MREVRANGE](https://oss.redislabs.com/redistimeseries/commands/#tsmrangetsmrevrange).
*
* @param range A TimestampRange object. Contains Start and End timestamps for the range query.
* Create with `new TimestampRange(from, to)`. Leave both params `from` and `to` as `undefined` i.e. `new TimestampRange()`.
* to express the minimum possible timestamp (`-`) and the maximum possible timestamp (`+`).
* @param filters A filters object. Create with `new FilterBuilder(label, value)`. Chain methods to make more complex filters.
* See docs on [filtering](https://oss.redislabs.com/redistimeseries/commands/#filtering).
*
* Examples:
*
* ```ts
* // Filter timeseries with labels `device=raspberry_23` and `sensor=temperature_1`:
* const filter = new FilterBuilder("device", "raspberry_23").equal("sensor", "temperature_1");
* ```
* Methods that can be chained: `equal`,`notEqual`, `exists`, `notExists`, `in`, `notIn`. See README for more examples on filter usage.
*
* @param count Maximum number of returned results per time-series.
* @param aggregation Aggregation Object -- avg, sum, min, max, range, count, first, last, std.p, std.s, var.p, var.s.
* Create with `new Aggregation(type,timeBucketinMs)`.
* @param withLabels Include in the reply the label-value pairs that represent metadata labels of the time-series.
* If this argument is not set, by default, an empty Array will be replied on the labels array position.
* @returns a promise containing an array of multi-range response objects i.e. `{ key: key, labels: Label[], data: Sample[] }`.
*/
multiRevRange(range, filters, count, aggregation, withLabels) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director
.multiRange(range, filters, count, aggregation, withLabels)
.get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.MULTI_REV_RANGE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return this.renderFactory.getMultiRangeRender().render(response);
});
}
/**
* Get the last sample.
*
* Docs: [TS.GET](https://oss.redislabs.com/redistimeseries/commands/#tsget).
*
* @param key Key name for timeseries.
* @returns the Sample object containing the lastest sample.
*/
get(key) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.getKey(key).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.GET, params);
const sample = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return new sample_1.Sample(key, Number(sample[1]), sample[0]);
});
}
/**
* Get the last samples matching the specific filter.
*
* Docs: [TS.MGET](https://oss.redislabs.com/redistimeseries/commands/#tsmget).
*
* @param filters A filters object. Create with `new FilterBuilder(label, value)`. Chain methods to make more complex filters.
* See docs on [filtering](https://oss.redislabs.com/redistimeseries/commands/#filtering).
*
* Examples:
*
* Filter timeseries with labels `device=raspberry_23` and `sensor=temperature_1`:
* ```ts
* const filter = new FilterBuilder("device", "raspberry_23").equal("sensor", "temperature_1");
* ```
* Methods that can be chained: `equal`,`notEqual`, `exists`, `notExists`, `in`, `notIn`.
*
* See README for more examples on filter usage.
*
* @param withLabels Include in the reply the label-value pairs that represent metadata labels of the time-series.
* If this argument is not set, by default, an empty Array will be replied on the labels array position.
* @returns An array of Sample objects containing the lastest samples across the specified series.
*
* @remarks
* TS.MGET complexity is O(n). n = Number of time-series that match the filters.
*/
multiGet(filters, withLabels) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.multiGet(filters, withLabels).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.MULTI_GET, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return this.renderFactory.getMultiGetRender().render(response);
});
}
/**
* Returns information and statistics on the time-series.
*
* Docs: [TS.INFO](https://oss.redislabs.com/redistimeseries/commands/#tsinfo).
*
* Complexity -- O(1)
*
* @param key Key name for timeseries
* @returns An `InfoResponse` object containing information about the timeseries i.e.
* ```
* // 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;
* }
* ```
*/
info(key) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.getKey(key).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.INFO, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return this.renderFactory.getInfoRender().render(response);
});
}
/**
* Get all the keys matching the filter list.
*
* Docs: [TS.QUERYINDEX](https://oss.redislabs.com/redistimeseries/commands/#tsqueryindex).
*
* @param filters A filters object. Create with `new FilterBuilder(label, value)`. Chain methods to make more complex filters.
* See docs on [filtering](https://oss.redislabs.com/redistimeseries/commands/#filtering).
*
* Example:
* ```ts
* // Filter timeseries with labels `device=raspberry_23` and `sensor=temperature_1`:
* const filter = new FilterBuilder("device", "raspberry_23").equal("sensor", "temperature_1");
* ```
* Methods that can be chained: `equal`,`notEqual`, `exists`, `notExists`, `in`, `notIn`. See README for more examples on filter usage
* @returns An array of keys matching the filters.
*/
queryIndex(filters) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director.queryIndex(filters).get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.QUERY_INDEX, params);
return yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
});
}
/**
* Set a timeout on a Key. After the timeout has expired, the key will automatically be deleted.
*
* Docs: [EXPIRE](https://redis.io/commands/expire)
*
* Note: Timeout can be set for a series using redis EXPIRE command when creating the series.
*
* @param keys The Key of for the series to be expired.
* @param seconds The timeout in seconds.
* @returns `true` if expiry on Key set successfully. `false` otherwise.
*/
expire(key, seconds) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.invoker
.setCommand(new expireCommand_1.ExpireCommand(this.provider.getRTSClient(), key, seconds))
.run();
return response === 1;
});
}
/**
* Delete the specified series.
*
* Docs: [TS.DEL](https://oss.redislabs.com/redistimeseries/commands/#del)
*
* Note: Timeout can be set for a series using redis EXPIRE command when creating the series.
*
* @param keys An array of Keys for the series to be deleted.
* @returns `true` if Keys deleted. `false` otherwise.
*/
delete(...keys) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.invoker.setCommand(new deleteCommand_1.DeleteCommand(this.provider.getRTSClient(), keys)).run();
return response === 1;
});
}
/**
* Delete all series.
*
* Note: This is an alias for the ioredis `flushdb()` command.
*
* @param keys An array of Keys to be deleted.
* @returns `true` if all series deleted.
*/
deleteAll() {
return __awaiter(this, void 0, void 0, function* () {
yield this.invoker.setCommand(new deleteAllCommand_1.DeleteAllCommand(this.provider.getRTSClient())).run();
return true;
});
}
/**
* Reset a timeseries i.e. `delete()` then `create()`. Takes the same arguments as `create()`
*
* @param key Key name for timeseries
* @param labels Array of Label objects (label-value pairs) that represent metadata labels of the key.
* Use `new Label('label', value)` to create a new Label object
* @param retention Maximum age for samples compared to last event time (in milliseconds).
* Default: The global retention secs configuration of the database (by default, 0 ).
* When set to 0, the series is not trimmed at all
* @param chunkSize Amount of memory, in bytes, allocated for data. Default: 4000.
* @param duplicatePolicy Configure what to do on duplicate sample.
* See more on [DUPLICATE_POLICY](https://oss.redislabs.com/redistimeseries/configuration/#DUPLICATE_POLICY)
*
* When this is not set, the server-wide default will be used.
*
* - BLOCK - an error will occur for any out of order sample
* - FIRST - ignore the new value
* - LAST - override with latest value
* - MIN - only override if the value is lower than the existing value
* - MAX - only override if the value is higher than the existing value
* @param uncompressed Since version 1.2, both timestamps and values are compressed by default.
* Adding this flag will keep data in an uncompressed form.
* Compression not only saves memory but usually improve performance due to lower number of memory accesses.
* @returns `true` if timeseries reset successfully. `false` otherwise
*/
reset(key, labels, retention, chunkSize, duplicatePolicy, uncompressed) {
return __awaiter(this, void 0, void 0, function* () {
const deleted = yield this.invoker.setCommand(new deleteCommand_1.DeleteCommand(this.provider.getRTSClient(), [key])).run();
if (deleted !== 1) {
throw new Error(`redis time series with key ${key} could not be deleted`);
}
const params = this.director
.create(key, labels, retention, chunkSize, duplicatePolicy, uncompressed)
.get();
const commandData = this.provider.getCommandData(commandName_1.CommandName.CREATE, params);
const response = yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
return response === "OK";
});
}
/**
* Disconnect from the client
*
* @returns `true` if client disconnected successfully. `false` otherwise
*/
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
const disconnected = yield this.invoker.setCommand(new disconnectCommand_1.DisconnectCommand(this.provider.getRTSClient())).run();
return disconnected === "OK";
});
}
changeBy(command, sample, labels = [], retention, uncompressed, chunkSize) {
return __awaiter(this, void 0, void 0, function* () {
const params = this.director
.changeBy(sample, labels, retention, uncompressed, chunkSize)
.get();
const commandData = this.provider.getCommandData(command, params);
return yield this.invoker.setCommand(new timeSeriesCommand_1.TimeSeriesCommand(commandData, this.receiver)).run();
});
}
}
exports.RedisTimeSeries = RedisTimeSeries;