@redis/time-series
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,5 +0,7 @@ | ||
import { Labels } from '.'; | ||
import { Labels, TimeSeriesDuplicatePolicies } from '.'; | ||
export declare const FIRST_KEY_INDEX = 1; | ||
interface AlterOptions { | ||
RETENTION?: number; | ||
CHUNK_SIZE?: number; | ||
DUPLICATE_POLICY?: TimeSeriesDuplicatePolicies; | ||
LABELS?: Labels; | ||
@@ -6,0 +8,0 @@ } |
@@ -9,2 +9,4 @@ "use strict"; | ||
(0, _1.pushRetentionArgument)(args, options?.RETENTION); | ||
(0, _1.pushChunkSizeArgument)(args, options?.CHUNK_SIZE); | ||
(0, _1.pushDuplicatePolicy)(args, options?.DUPLICATE_POLICY); | ||
(0, _1.pushLabelsArgument)(args, options?.LABELS); | ||
@@ -11,0 +13,0 @@ return args; |
@@ -11,5 +11,3 @@ "use strict"; | ||
(0, _1.pushChunkSizeArgument)(args, options?.CHUNK_SIZE); | ||
if (options?.DUPLICATE_POLICY) { | ||
args.push('DUPLICATE_POLICY', options.DUPLICATE_POLICY); | ||
} | ||
(0, _1.pushDuplicatePolicy)(args, options?.DUPLICATE_POLICY); | ||
(0, _1.pushLabelsArgument)(args, options?.LABELS); | ||
@@ -16,0 +14,0 @@ return args; |
import { TimeSeriesAggregationType } from '.'; | ||
export declare const FIRST_KEY_INDEX = 1; | ||
export declare function transformArguments(sourceKey: string, destinationKey: string, aggregationType: TimeSeriesAggregationType, timeBucket: number): Array<string>; | ||
export declare function transformArguments(sourceKey: string, destinationKey: string, aggregationType: TimeSeriesAggregationType, bucketDuration: number, alignTimestamp?: number): Array<string>; | ||
export declare function transformReply(): 'OK'; |
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.FIRST_KEY_INDEX = 1; | ||
function transformArguments(sourceKey, destinationKey, aggregationType, timeBucket) { | ||
return [ | ||
function transformArguments(sourceKey, destinationKey, aggregationType, bucketDuration, alignTimestamp) { | ||
const args = [ | ||
'TS.CREATERULE', | ||
@@ -13,5 +13,9 @@ sourceKey, | ||
aggregationType, | ||
timeBucket.toString() | ||
bucketDuration.toString() | ||
]; | ||
if (alignTimestamp) { | ||
args.push(alignTimestamp.toString()); | ||
} | ||
return args; | ||
} | ||
exports.transformArguments = transformArguments; |
@@ -9,5 +9,5 @@ "use strict"; | ||
sourceKey, | ||
destinationKey, | ||
destinationKey | ||
]; | ||
} | ||
exports.transformArguments = transformArguments; |
@@ -0,5 +1,10 @@ | ||
import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; | ||
import { SampleRawReply, SampleReply } from '.'; | ||
export declare const FIRST_KEY_INDEX = 1; | ||
export declare const IS_READ_ONLY = true; | ||
export declare function transformArguments(key: string): Array<string>; | ||
interface GetOptions { | ||
LATEST?: boolean; | ||
} | ||
export declare function transformArguments(key: string, options?: GetOptions): RedisCommandArguments; | ||
export declare function transformReply(reply: [] | SampleRawReply): null | SampleReply; | ||
export {}; |
@@ -7,4 +7,4 @@ "use strict"; | ||
exports.IS_READ_ONLY = true; | ||
function transformArguments(key) { | ||
return ['TS.GET', key]; | ||
function transformArguments(key, options) { | ||
return (0, _1.pushLatestArgument)(['TS.GET', key], options?.LATEST); | ||
} | ||
@@ -11,0 +11,0 @@ exports.transformArguments = transformArguments; |
@@ -69,14 +69,18 @@ import * as ADD from './ADD'; | ||
export declare enum TimeSeriesAggregationType { | ||
AVERAGE = "avg", | ||
SUM = "sum", | ||
MINIMUM = "min", | ||
MAXIMUM = "max", | ||
RANGE = "range", | ||
COUNT = "count", | ||
FIRST = "first", | ||
LAST = "last", | ||
STD_P = "std.p", | ||
STD_S = "std.s", | ||
VAR_P = "var.p", | ||
VAR_S = "var.s" | ||
AVG = "AVG", | ||
AVERAGE = "AVG", | ||
FIRST = "FIRST", | ||
LAST = "LAST", | ||
MIN = "MIN", | ||
MINIMUM = "MIN", | ||
MAX = "MAX", | ||
MAXIMUM = "MAX", | ||
SUM = "SUM", | ||
RANGE = "RANGE", | ||
COUNT = "COUNT", | ||
STD_P = "STD.P", | ||
STD_S = "STD.S", | ||
VAR_P = "VAR.P", | ||
VAR_S = "VAR.S", | ||
TWA = "TWA" | ||
} | ||
@@ -92,5 +96,14 @@ export declare enum TimeSeriesDuplicatePolicies { | ||
export declare enum TimeSeriesReducers { | ||
SUM = "sum", | ||
MINIMUM = "min", | ||
MAXIMUM = "max" | ||
AVG = "AVG", | ||
SUM = "SUM", | ||
MIN = "MIN", | ||
MINIMUM = "MIN", | ||
MAX = "MAX", | ||
MAXIMUM = "MAX", | ||
RANGE = "range", | ||
COUNT = "COUNT", | ||
STD_P = "STD.P", | ||
STD_S = "STD.S", | ||
VAR_P = "VAR.P", | ||
VAR_S = "VAR.S" | ||
} | ||
@@ -106,2 +119,3 @@ export declare type Timestamp = number | Date | string; | ||
export declare function pushChunkSizeArgument(args: RedisCommandArguments, chunkSize?: number): RedisCommandArguments; | ||
export declare function pushDuplicatePolicy(args: RedisCommandArguments, duplicatePolicy?: TimeSeriesDuplicatePolicies): RedisCommandArguments; | ||
export declare type RawLabels = Array<[label: string, value: string]>; | ||
@@ -127,3 +141,9 @@ export declare type Labels = { | ||
export declare function transformSampleReply(reply: SampleRawReply): SampleReply; | ||
export declare enum TimeSeriesBucketTimestamp { | ||
LOW = "-", | ||
HIGH = "+", | ||
MID = "~" | ||
} | ||
export interface RangeOptions { | ||
LATEST?: boolean; | ||
FILTER_BY_TS?: Array<Timestamp>; | ||
@@ -139,2 +159,4 @@ FILTER_BY_VALUE?: { | ||
timeBucket: Timestamp; | ||
BUCKETTIMESTAMP?: TimeSeriesBucketTimestamp; | ||
EMPTY?: boolean; | ||
}; | ||
@@ -175,1 +197,2 @@ } | ||
export declare function transformMRangeWithLabelsReply(reply: MRangeRawReply): Array<MRangeWithLabelsReplyItem>; | ||
export declare function pushLatestArgument(args: RedisCommandArguments, latest?: boolean): RedisCommandArguments; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transformMRangeWithLabelsReply = exports.transformMRangeReply = exports.transformRangeReply = exports.pushMRangeWithLabelsArguments = exports.pushWithLabelsArgument = exports.pushMRangeArguments = exports.pushFilterArgument = exports.pushMRangeGroupByArguments = exports.pushRangeArguments = exports.transformSampleReply = exports.transformIncrDecrArguments = exports.pushLabelsArgument = exports.transformLablesReply = exports.pushChunkSizeArgument = exports.pushEncodingArgument = exports.TimeSeriesEncoding = exports.pushRetentionArgument = exports.transformTimestampArgument = exports.TimeSeriesReducers = exports.TimeSeriesDuplicatePolicies = exports.TimeSeriesAggregationType = void 0; | ||
exports.pushLatestArgument = exports.transformMRangeWithLabelsReply = exports.transformMRangeReply = exports.transformRangeReply = exports.pushMRangeWithLabelsArguments = exports.pushWithLabelsArgument = exports.pushMRangeArguments = exports.pushFilterArgument = exports.pushMRangeGroupByArguments = exports.pushRangeArguments = exports.TimeSeriesBucketTimestamp = exports.transformSampleReply = exports.transformIncrDecrArguments = exports.pushLabelsArgument = exports.transformLablesReply = exports.pushDuplicatePolicy = exports.pushChunkSizeArgument = exports.pushEncodingArgument = exports.TimeSeriesEncoding = exports.pushRetentionArgument = exports.transformTimestampArgument = exports.TimeSeriesReducers = exports.TimeSeriesDuplicatePolicies = exports.TimeSeriesAggregationType = void 0; | ||
const ADD = require("./ADD"); | ||
@@ -72,14 +72,21 @@ const ALTER = require("./ALTER"); | ||
(function (TimeSeriesAggregationType) { | ||
TimeSeriesAggregationType["AVERAGE"] = "avg"; | ||
TimeSeriesAggregationType["SUM"] = "sum"; | ||
TimeSeriesAggregationType["MINIMUM"] = "min"; | ||
TimeSeriesAggregationType["MAXIMUM"] = "max"; | ||
TimeSeriesAggregationType["RANGE"] = "range"; | ||
TimeSeriesAggregationType["COUNT"] = "count"; | ||
TimeSeriesAggregationType["FIRST"] = "first"; | ||
TimeSeriesAggregationType["LAST"] = "last"; | ||
TimeSeriesAggregationType["STD_P"] = "std.p"; | ||
TimeSeriesAggregationType["STD_S"] = "std.s"; | ||
TimeSeriesAggregationType["VAR_P"] = "var.p"; | ||
TimeSeriesAggregationType["VAR_S"] = "var.s"; | ||
TimeSeriesAggregationType["AVG"] = "AVG"; | ||
// @deprecated | ||
TimeSeriesAggregationType["AVERAGE"] = "AVG"; | ||
TimeSeriesAggregationType["FIRST"] = "FIRST"; | ||
TimeSeriesAggregationType["LAST"] = "LAST"; | ||
TimeSeriesAggregationType["MIN"] = "MIN"; | ||
// @deprecated | ||
TimeSeriesAggregationType["MINIMUM"] = "MIN"; | ||
TimeSeriesAggregationType["MAX"] = "MAX"; | ||
// @deprecated | ||
TimeSeriesAggregationType["MAXIMUM"] = "MAX"; | ||
TimeSeriesAggregationType["SUM"] = "SUM"; | ||
TimeSeriesAggregationType["RANGE"] = "RANGE"; | ||
TimeSeriesAggregationType["COUNT"] = "COUNT"; | ||
TimeSeriesAggregationType["STD_P"] = "STD.P"; | ||
TimeSeriesAggregationType["STD_S"] = "STD.S"; | ||
TimeSeriesAggregationType["VAR_P"] = "VAR.P"; | ||
TimeSeriesAggregationType["VAR_S"] = "VAR.S"; | ||
TimeSeriesAggregationType["TWA"] = "TWA"; | ||
})(TimeSeriesAggregationType = exports.TimeSeriesAggregationType || (exports.TimeSeriesAggregationType = {})); | ||
@@ -97,5 +104,16 @@ var TimeSeriesDuplicatePolicies; | ||
(function (TimeSeriesReducers) { | ||
TimeSeriesReducers["SUM"] = "sum"; | ||
TimeSeriesReducers["MINIMUM"] = "min"; | ||
TimeSeriesReducers["MAXIMUM"] = "max"; | ||
TimeSeriesReducers["AVG"] = "AVG"; | ||
TimeSeriesReducers["SUM"] = "SUM"; | ||
TimeSeriesReducers["MIN"] = "MIN"; | ||
// @deprecated | ||
TimeSeriesReducers["MINIMUM"] = "MIN"; | ||
TimeSeriesReducers["MAX"] = "MAX"; | ||
// @deprecated | ||
TimeSeriesReducers["MAXIMUM"] = "MAX"; | ||
TimeSeriesReducers["RANGE"] = "range"; | ||
TimeSeriesReducers["COUNT"] = "COUNT"; | ||
TimeSeriesReducers["STD_P"] = "STD.P"; | ||
TimeSeriesReducers["STD_S"] = "STD.S"; | ||
TimeSeriesReducers["VAR_P"] = "VAR.P"; | ||
TimeSeriesReducers["VAR_S"] = "VAR.S"; | ||
})(TimeSeriesReducers = exports.TimeSeriesReducers || (exports.TimeSeriesReducers = {})); | ||
@@ -136,2 +154,9 @@ function transformTimestampArgument(timestamp) { | ||
exports.pushChunkSizeArgument = pushChunkSizeArgument; | ||
function pushDuplicatePolicy(args, duplicatePolicy) { | ||
if (duplicatePolicy) { | ||
args.push('DUPLICATE_POLICY', duplicatePolicy); | ||
} | ||
return args; | ||
} | ||
exports.pushDuplicatePolicy = pushDuplicatePolicy; | ||
function transformLablesReply(reply) { | ||
@@ -180,4 +205,11 @@ const labels = {}; | ||
exports.transformSampleReply = transformSampleReply; | ||
var TimeSeriesBucketTimestamp; | ||
(function (TimeSeriesBucketTimestamp) { | ||
TimeSeriesBucketTimestamp["LOW"] = "-"; | ||
TimeSeriesBucketTimestamp["HIGH"] = "+"; | ||
TimeSeriesBucketTimestamp["MID"] = "~"; | ||
})(TimeSeriesBucketTimestamp = exports.TimeSeriesBucketTimestamp || (exports.TimeSeriesBucketTimestamp = {})); | ||
function pushRangeArguments(args, fromTimestamp, toTimestamp, options) { | ||
args.push(transformTimestampArgument(fromTimestamp), transformTimestampArgument(toTimestamp)); | ||
pushLatestArgument(args, options?.LATEST); | ||
if (options?.FILTER_BY_TS) { | ||
@@ -200,2 +232,8 @@ args.push('FILTER_BY_TS'); | ||
args.push('AGGREGATION', options.AGGREGATION.type, transformTimestampArgument(options.AGGREGATION.timeBucket)); | ||
if (options.AGGREGATION.BUCKETTIMESTAMP) { | ||
args.push('BUCKETTIMESTAMP', options.AGGREGATION.BUCKETTIMESTAMP); | ||
} | ||
if (options.AGGREGATION.EMPTY) { | ||
args.push('EMPTY'); | ||
} | ||
} | ||
@@ -214,11 +252,9 @@ return args; | ||
args.push('FILTER'); | ||
(0, generic_transformers_1.pushVerdictArguments)(args, filter); | ||
return args; | ||
return (0, generic_transformers_1.pushVerdictArguments)(args, filter); | ||
} | ||
exports.pushFilterArgument = pushFilterArgument; | ||
function pushMRangeArguments(args, fromTimestamp, toTimestamp, filter, options) { | ||
pushRangeArguments(args, fromTimestamp, toTimestamp, options); | ||
pushFilterArgument(args, filter); | ||
pushMRangeGroupByArguments(args, options?.GROUPBY); | ||
return args; | ||
args = pushRangeArguments(args, fromTimestamp, toTimestamp, options); | ||
args = pushFilterArgument(args, filter); | ||
return pushMRangeGroupByArguments(args, options?.GROUPBY); | ||
} | ||
@@ -232,3 +268,3 @@ exports.pushMRangeArguments = pushMRangeArguments; | ||
args.push('SELECTED_LABELS'); | ||
(0, generic_transformers_1.pushVerdictArguments)(args, selectedLabels); | ||
args = (0, generic_transformers_1.pushVerdictArguments)(args, selectedLabels); | ||
} | ||
@@ -239,7 +275,6 @@ return args; | ||
function pushMRangeWithLabelsArguments(args, fromTimestamp, toTimestamp, filter, options) { | ||
pushRangeArguments(args, fromTimestamp, toTimestamp, options); | ||
pushWithLabelsArgument(args, options?.SELECTED_LABELS); | ||
pushFilterArgument(args, filter); | ||
pushMRangeGroupByArguments(args, options?.GROUPBY); | ||
return args; | ||
args = pushRangeArguments(args, fromTimestamp, toTimestamp, options); | ||
args = pushWithLabelsArgument(args, options?.SELECTED_LABELS); | ||
args = pushFilterArgument(args, filter); | ||
return pushMRangeGroupByArguments(args, options?.GROUPBY); | ||
} | ||
@@ -274,1 +309,8 @@ exports.pushMRangeWithLabelsArguments = pushMRangeWithLabelsArguments; | ||
exports.transformMRangeWithLabelsReply = transformMRangeWithLabelsReply; | ||
function pushLatestArgument(args, latest) { | ||
if (latest) { | ||
args.push('LATEST'); | ||
} | ||
return args; | ||
} | ||
exports.pushLatestArgument = pushLatestArgument; |
@@ -5,17 +5,17 @@ import { InfoRawReply, InfoReply } from './INFO'; | ||
declare type InfoDebugRawReply = [ | ||
...infoArgs: InfoRawReply, | ||
_: string, | ||
keySelfName: string, | ||
_: string, | ||
chunks: Array<[ | ||
_: string, | ||
startTimestamp: number, | ||
_: string, | ||
endTimestamp: number, | ||
_: string, | ||
samples: number, | ||
_: string, | ||
size: number, | ||
_: string, | ||
bytesPerSample: string | ||
...InfoRawReply, | ||
'keySelfName', | ||
string, | ||
'chunks', | ||
Array<[ | ||
'startTimestamp', | ||
number, | ||
'endTimestamp', | ||
number, | ||
'samples', | ||
number, | ||
'size', | ||
number, | ||
'bytesPerSample', | ||
string | ||
]> | ||
@@ -22,0 +22,0 @@ ]; |
@@ -6,26 +6,26 @@ import { TimeSeriesAggregationType, TimeSeriesDuplicatePolicies } from '.'; | ||
export declare type InfoRawReply = [ | ||
_: string, | ||
totalSamples: number, | ||
_: string, | ||
memoryUsage: number, | ||
_: string, | ||
firstTimestamp: number, | ||
_: string, | ||
lastTimestamp: number, | ||
_: string, | ||
retentionTime: number, | ||
_: string, | ||
chunkCount: number, | ||
_: string, | ||
chunkSize: number, | ||
_: string, | ||
chunkType: string, | ||
_: string, | ||
duplicatePolicy: TimeSeriesDuplicatePolicies | null, | ||
_: string, | ||
labels: Array<[name: string, value: string]>, | ||
_: string, | ||
sourceKey: string | null, | ||
_: string, | ||
rules: Array<[key: string, timeBucket: number, aggregationType: TimeSeriesAggregationType]> | ||
'totalSamples', | ||
number, | ||
'memoryUsage', | ||
number, | ||
'firstTimestamp', | ||
number, | ||
'lastTimestamp', | ||
number, | ||
'retentionTime', | ||
number, | ||
'chunkCount', | ||
number, | ||
'chunkSize', | ||
number, | ||
'chunkType', | ||
string, | ||
'duplicatePolicy', | ||
TimeSeriesDuplicatePolicies | null, | ||
'labels', | ||
Array<[name: string, value: string]>, | ||
'sourceKey', | ||
string | null, | ||
'rules', | ||
Array<[key: string, timeBucket: number, aggregationType: TimeSeriesAggregationType]> | ||
]; | ||
@@ -32,0 +32,0 @@ export interface InfoReply { |
import { SelectedLabels, Labels, Filter } from '.'; | ||
import { MGetRawReply, MGetReply } from './MGET'; | ||
import { MGetOptions, MGetRawReply, MGetReply } from './MGET'; | ||
import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; | ||
export declare const IS_READ_ONLY = true; | ||
interface MGetWithLabelsOptions { | ||
interface MGetWithLabelsOptions extends MGetOptions { | ||
SELECTED_LABELS?: SelectedLabels; | ||
} | ||
export declare function transformArguments(filter: Filter, options?: MGetWithLabelsOptions): Array<string>; | ||
export declare function transformArguments(filter: Filter, options?: MGetWithLabelsOptions): RedisCommandArguments; | ||
export interface MGetWithLabelsReply extends MGetReply { | ||
@@ -9,0 +10,0 @@ labels: Labels; |
@@ -7,6 +7,4 @@ "use strict"; | ||
function transformArguments(filter, options) { | ||
const args = ['TS.MGET']; | ||
(0, _1.pushWithLabelsArgument)(args, options?.SELECTED_LABELS); | ||
(0, _1.pushFilterArgument)(args, filter); | ||
return args; | ||
const args = (0, _1.pushWithLabelsArgument)(['TS.MGET'], options?.SELECTED_LABELS); | ||
return (0, _1.pushFilterArgument)(args, filter); | ||
} | ||
@@ -13,0 +11,0 @@ exports.transformArguments = transformArguments; |
import { RedisCommandArguments } from '@redis/client/dist/lib/commands'; | ||
import { Filter, RawLabels, SampleRawReply, SampleReply } from '.'; | ||
export declare const IS_READ_ONLY = true; | ||
export declare function transformArguments(filter: Filter): RedisCommandArguments; | ||
export interface MGetOptions { | ||
LATEST?: boolean; | ||
} | ||
export declare function transformArguments(filter: Filter, options?: MGetOptions): RedisCommandArguments; | ||
export declare type MGetRawReply = Array<[ | ||
@@ -6,0 +9,0 @@ key: string, |
@@ -6,4 +6,5 @@ "use strict"; | ||
exports.IS_READ_ONLY = true; | ||
function transformArguments(filter) { | ||
return (0, _1.pushFilterArgument)(['TS.MGET'], filter); | ||
function transformArguments(filter, options) { | ||
const args = (0, _1.pushLatestArgument)(['TS.MGET'], options?.LATEST); | ||
return (0, _1.pushFilterArgument)(args, filter); | ||
} | ||
@@ -10,0 +11,0 @@ exports.transformArguments = transformArguments; |
export { default } from './commands'; | ||
export { TimeSeriesDuplicatePolicies, TimeSeriesEncoding, TimeSeriesAggregationType } from './commands'; | ||
export { TimeSeriesDuplicatePolicies, TimeSeriesEncoding, TimeSeriesAggregationType, TimeSeriesReducers, TimeSeriesBucketTimestamp } from './commands'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TimeSeriesAggregationType = exports.TimeSeriesEncoding = exports.TimeSeriesDuplicatePolicies = exports.default = void 0; | ||
exports.TimeSeriesBucketTimestamp = exports.TimeSeriesReducers = exports.TimeSeriesAggregationType = exports.TimeSeriesEncoding = exports.TimeSeriesDuplicatePolicies = exports.default = void 0; | ||
var commands_1 = require("./commands"); | ||
@@ -10,1 +10,3 @@ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return commands_1.default; } }); | ||
Object.defineProperty(exports, "TimeSeriesAggregationType", { enumerable: true, get: function () { return commands_2.TimeSeriesAggregationType; } }); | ||
Object.defineProperty(exports, "TimeSeriesReducers", { enumerable: true, get: function () { return commands_2.TimeSeriesReducers; } }); | ||
Object.defineProperty(exports, "TimeSeriesBucketTimestamp", { enumerable: true, get: function () { return commands_2.TimeSeriesBucketTimestamp; } }); |
{ | ||
"name": "@redis/time-series", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"license": "MIT", | ||
@@ -21,10 +21,10 @@ "main": "./dist/index.js", | ||
"@redis/test-utils": "*", | ||
"@types/node": "^17.0.31", | ||
"@types/node": "^18.11.6", | ||
"nyc": "^15.1.0", | ||
"release-it": "^15.0.0", | ||
"release-it": "^15.5.0", | ||
"source-map-support": "^0.5.21", | ||
"ts-node": "^10.7.0", | ||
"typedoc": "^0.22.15", | ||
"typescript": "^4.6.4" | ||
"ts-node": "^10.9.1", | ||
"typedoc": "^0.23.18", | ||
"typescript": "^4.8.4" | ||
} | ||
} |
146
README.md
@@ -7,2 +7,146 @@ # @redis/time-series | ||
For an example of how to add values to a time series, query a time series, and perform aggregated queries against a time series, see `time-series.js` in the Node Redis examples folder. | ||
## Usage | ||
For a complete example, see [`time-series.js`](https://github.com/redis/node-redis/blob/master/examples/time-series.js) in the Node Redis examples folder. | ||
### Creating Time Series data structure in Redis | ||
The [`TS.CREATE`](https://oss.redis.com/redistimeseries/commands/#tscreate) command creates a new time series. | ||
Here, we'll create a new time series "`temperature`": | ||
```javascript | ||
import { createClient } from 'redis'; | ||
import { TimeSeriesDuplicatePolicies, TimeSeriesEncoding, TimeSeriesAggregationType } from '@redis/time-series'; | ||
... | ||
const created = await client.ts.create('temperature', { | ||
RETENTION: 86400000, // 1 day in milliseconds | ||
ENCODING: TimeSeriesEncoding.UNCOMPRESSED, // No compression - When not specified, the option is set to COMPRESSED | ||
DUPLICATE_POLICY: TimeSeriesDuplicatePolicies.BLOCK, // No duplicates - When not specified: set to the global DUPLICATE_POLICY configuration of the database (which by default, is BLOCK). | ||
}); | ||
if (created === 'OK') { | ||
console.log('Created timeseries.'); | ||
} else { | ||
console.log('Error creating timeseries :('); | ||
process.exit(1); | ||
} | ||
``` | ||
### Adding new value to a Time Series data structure in Redis | ||
With RedisTimeSeries, we can add a single value to time series data structure using the [`TS.ADD`](https://redis.io/commands/ts.add/) command and if we would like to add multiple values we can use the [`TS.MADD`](https://redis.io/commands/ts.madd/) command. | ||
```javascript | ||
let value = Math.floor(Math.random() * 1000) + 1; // Random data point value | ||
let currentTimestamp = 1640995200000; // Jan 1 2022 00:00:00 | ||
let num = 0; | ||
while (num < 10000) { | ||
// Add a new value to the timeseries, providing our own timestamp: | ||
// https://redis.io/commands/ts.add/ | ||
await client.ts.add('temperature', currentTimestamp, value); | ||
console.log(`Added timestamp ${currentTimestamp}, value ${value}.`); | ||
num += 1; | ||
value = Math.floor(Math.random() * 1000) + 1; // Get another random value | ||
currentTimestamp += 1000; // Move on one second. | ||
} | ||
// Add multiple values to the timeseries in round trip to the server: | ||
// https://redis.io/commands/ts.madd/ | ||
const response = await client.ts.mAdd([{ | ||
key: 'temperature', | ||
timestamp: currentTimestamp + 60000, | ||
value: Math.floor(Math.random() * 1000) + 1 | ||
}, { | ||
key: 'temperature', | ||
timestamp: currentTimestamp + 120000, | ||
value: Math.floor(Math.random() * 1000) + 1 | ||
}]); | ||
``` | ||
### Retrieving Time Series data from Redis | ||
With RedisTimeSeries, we can retrieve the time series data using the [`TS.RANGE`](https://redis.io/commands/ts.range/) command by passing the criteria as follows: | ||
```javascript | ||
// Query the timeseries with TS.RANGE: | ||
// https://redis.io/commands/ts.range/ | ||
const fromTimestamp = 1640995200000; // Jan 1 2022 00:00:00 | ||
const toTimestamp = 1640995260000; // Jan 1 2022 00:01:00 | ||
const rangeResponse = await client.ts.range('temperature', fromTimestamp, toTimestamp, { | ||
// Group into 10 second averages. | ||
AGGREGATION: { | ||
type: TimeSeriesAggregationType.AVERAGE, | ||
timeBucket: 10000 | ||
} | ||
}); | ||
console.log('RANGE RESPONSE:'); | ||
// rangeResponse looks like: | ||
// [ | ||
// { timestamp: 1640995200000, value: 356.8 }, | ||
// { timestamp: 1640995210000, value: 534.8 }, | ||
// { timestamp: 1640995220000, value: 481.3 }, | ||
// { timestamp: 1640995230000, value: 437 }, | ||
// { timestamp: 1640995240000, value: 507.3 }, | ||
// { timestamp: 1640995250000, value: 581.2 }, | ||
// { timestamp: 1640995260000, value: 600 } | ||
// ] | ||
``` | ||
### Altering Time Series data Stored in Redis | ||
RedisTimeSeries includes commands that can update values in a time series data structure. | ||
Using the [`TS.ALTER`](https://redis.io/commands/ts.alter/) command, we can update time series retention like this: | ||
```javascript | ||
// https://redis.io/commands/ts.alter/ | ||
const alterResponse = await client.ts.alter('temperature', { | ||
RETENTION: 0 // Keep the entries forever | ||
}); | ||
``` | ||
### Retrieving Information about the timeseries Stored in Redis | ||
RedisTimeSeries also includes commands that can help to view the information on the state of a time series. | ||
Using the [`TS.INFO`](https://redis.io/commands/ts.info/) command, we can view timeseries information like this: | ||
```javascript | ||
// Get some information about the state of the timeseries. | ||
// https://redis.io/commands/ts.info/ | ||
const tsInfo = await client.ts.info('temperature'); | ||
// tsInfo looks like this: | ||
// { | ||
// totalSamples: 1440, | ||
// memoryUsage: 28904, | ||
// firstTimestamp: 1641508920000, | ||
// lastTimestamp: 1641595320000, | ||
// retentionTime: 86400000, | ||
// chunkCount: 7, | ||
// chunkSize: 4096, | ||
// chunkType: 'uncompressed', | ||
// duplicatePolicy: 'block', | ||
// labels: [], | ||
// sourceKey: null, | ||
// rules: [] | ||
// } | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48692
1061
152