You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: packages/time-series/README.md
+145-1
Original file line number
Diff line number
Diff line change
@@ -4,4 +4,148 @@ This package provides support for the [RedisTimeSeries](https://redistimeseries.
4
4
5
5
To use these extra commands, your Redis server must have the RedisTimeSeries module installed.
6
6
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`":
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:
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
+
constresponse=awaitclient.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
+
constfromTimestamp=1640995200000; // Jan 1 2022 00:00:00
84
+
consttoTimestamp=1640995260000; // Jan 1 2022 00:01:00
0 commit comments