Skip to content

Commit e0e96ae

Browse files
fix(#2231): created doc for using timeseries (#2312)
* fix(doc): created doc for using timeseries * fix(doc): created doc for using timeseries * Apply suggestions from code review Co-authored-by: Simon Prickett <[email protected]> Co-authored-by: Simon Prickett <[email protected]>
1 parent 2a5dc75 commit e0e96ae

File tree

1 file changed

+145
-1
lines changed

1 file changed

+145
-1
lines changed

packages/time-series/README.md

+145-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,148 @@ This package provides support for the [RedisTimeSeries](https://redistimeseries.
44

55
To use these extra commands, your Redis server must have the RedisTimeSeries module installed.
66

7-
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.
7+
## Usage
8+
9+
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.
10+
11+
### Creating Time Series data structure in Redis
12+
13+
The [`TS.CREATE`](https://oss.redis.com/redistimeseries/commands/#tscreate) command creates a new time series.
14+
15+
Here, we'll create a new time series "`temperature`":
16+
17+
```javascript
18+
19+
import { createClient } from 'redis';
20+
import { TimeSeriesDuplicatePolicies, TimeSeriesEncoding, TimeSeriesAggregationType } from '@redis/time-series';
21+
22+
...
23+
24+
const created = await client.ts.create('temperature', {
25+
RETENTION: 86400000, // 1 day in milliseconds
26+
ENCODING: TimeSeriesEncoding.UNCOMPRESSED, // No compression - When not specified, the option is set to COMPRESSED
27+
DUPLICATE_POLICY: TimeSeriesDuplicatePolicies.BLOCK, // No duplicates - When not specified: set to the global DUPLICATE_POLICY configuration of the database (which by default, is BLOCK).
28+
});
29+
30+
if (created === 'OK') {
31+
console.log('Created timeseries.');
32+
} else {
33+
console.log('Error creating timeseries :(');
34+
process.exit(1);
35+
}
36+
37+
```
38+
39+
### Adding new value to a Time Series data structure in Redis
40+
41+
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.
42+
43+
```javascript
44+
45+
let value = Math.floor(Math.random() * 1000) + 1; // Random data point value
46+
let currentTimestamp = 1640995200000; // Jan 1 2022 00:00:00
47+
let num = 0;
48+
49+
while (num < 10000) {
50+
// Add a new value to the timeseries, providing our own timestamp:
51+
// https://redis.io/commands/ts.add/
52+
await client.ts.add('temperature', currentTimestamp, value);
53+
console.log(`Added timestamp ${currentTimestamp}, value ${value}.`);
54+
55+
num += 1;
56+
value = Math.floor(Math.random() * 1000) + 1; // Get another random value
57+
currentTimestamp += 1000; // Move on one second.
58+
}
59+
60+
// Add multiple values to the timeseries in round trip to the server:
61+
// https://redis.io/commands/ts.madd/
62+
const response = await client.ts.mAdd([{
63+
key: 'temperature',
64+
timestamp: currentTimestamp + 60000,
65+
value: Math.floor(Math.random() * 1000) + 1
66+
}, {
67+
key: 'temperature',
68+
timestamp: currentTimestamp + 120000,
69+
value: Math.floor(Math.random() * 1000) + 1
70+
}]);
71+
72+
73+
```
74+
75+
### Retrieving Time Series data from Redis
76+
77+
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:
78+
79+
```javascript
80+
81+
// Query the timeseries with TS.RANGE:
82+
// https://redis.io/commands/ts.range/
83+
const fromTimestamp = 1640995200000; // Jan 1 2022 00:00:00
84+
const toTimestamp = 1640995260000; // Jan 1 2022 00:01:00
85+
const rangeResponse = await client.ts.range('temperature', fromTimestamp, toTimestamp, {
86+
// Group into 10 second averages.
87+
AGGREGATION: {
88+
type: TimeSeriesAggregationType.AVERAGE,
89+
timeBucket: 10000
90+
}
91+
});
92+
93+
console.log('RANGE RESPONSE:');
94+
// rangeResponse looks like:
95+
// [
96+
// { timestamp: 1640995200000, value: 356.8 },
97+
// { timestamp: 1640995210000, value: 534.8 },
98+
// { timestamp: 1640995220000, value: 481.3 },
99+
// { timestamp: 1640995230000, value: 437 },
100+
// { timestamp: 1640995240000, value: 507.3 },
101+
// { timestamp: 1640995250000, value: 581.2 },
102+
// { timestamp: 1640995260000, value: 600 }
103+
// ]
104+
105+
```
106+
107+
### Altering Time Series data Stored in Redis
108+
109+
RedisTimeSeries includes commands that can update values in a time series data structure.
110+
111+
Using the [`TS.ALTER`](https://redis.io/commands/ts.alter/) command, we can update time series retention like this:
112+
113+
```javascript
114+
115+
// https://redis.io/commands/ts.alter/
116+
const alterResponse = await client.ts.alter('temperature', {
117+
RETENTION: 0 // Keep the entries forever
118+
});
119+
120+
```
121+
122+
### Retrieving Information about the timeseries Stored in Redis
123+
124+
RedisTimeSeries also includes commands that can help to view the information on the state of a time series.
125+
126+
Using the [`TS.INFO`](https://redis.io/commands/ts.info/) command, we can view timeseries information like this:
127+
128+
```javascript
129+
130+
// Get some information about the state of the timeseries.
131+
// https://redis.io/commands/ts.info/
132+
const tsInfo = await client.ts.info('temperature');
133+
134+
// tsInfo looks like this:
135+
// {
136+
// totalSamples: 1440,
137+
// memoryUsage: 28904,
138+
// firstTimestamp: 1641508920000,
139+
// lastTimestamp: 1641595320000,
140+
// retentionTime: 86400000,
141+
// chunkCount: 7,
142+
// chunkSize: 4096,
143+
// chunkType: 'uncompressed',
144+
// duplicatePolicy: 'block',
145+
// labels: [],
146+
// sourceKey: null,
147+
// rules: []
148+
// }
149+
150+
```
151+

0 commit comments

Comments
 (0)