|
| 1 | +// Add data to a Redis TimeSeries and query it. |
| 2 | +// Requires the RedisTimeSeries module: https://redistimeseries.io/ |
| 3 | + |
| 4 | +import { createClient } from 'redis'; |
| 5 | +import { TimeSeriesDuplicatePolicies, TimeSeriesEncoding, TimeSeriesAggregationType } from '@node-redis/time-series'; |
| 6 | + |
| 7 | +async function timeSeries() { |
| 8 | + const client = createClient(); |
| 9 | + |
| 10 | + await client.connect(); |
| 11 | + await client.del('mytimeseries'); |
| 12 | + |
| 13 | + try { |
| 14 | + // Create a timeseries |
| 15 | + // https://oss.redis.com/redistimeseries/commands/#tscreate |
| 16 | + const created = await client.ts.create('mytimeseries', { |
| 17 | + RETENTION: 86400000, // 1 day in milliseconds |
| 18 | + ENCODING: TimeSeriesEncoding.UNCOMPRESSED, // No compression |
| 19 | + DUPLICATE_POLICY: TimeSeriesDuplicatePolicies.BLOCK // No duplicates |
| 20 | + }); |
| 21 | + |
| 22 | + if (created === 'OK') { |
| 23 | + console.log('Created timeseries.'); |
| 24 | + } else { |
| 25 | + console.log('Error creating timeseries :('); |
| 26 | + process.exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + let value = Math.floor(Math.random() * 1000) + 1; // Random data point value |
| 30 | + let currentTimestamp = 1640995200000; // Jan 1 2022 00:00:00 |
| 31 | + let num = 0; |
| 32 | + |
| 33 | + while (num < 10000) { |
| 34 | + // Add a new value to the timeseries, providing our own timestamp: |
| 35 | + // https://oss.redis.com/redistimeseries/commands/#tsadd |
| 36 | + await client.ts.add('mytimeseries', currentTimestamp, value); |
| 37 | + console.log(`Added timestamp ${currentTimestamp}, value ${value}.`); |
| 38 | + |
| 39 | + num += 1; |
| 40 | + value = Math.floor(Math.random() * 1000) + 1; // Get another random value |
| 41 | + currentTimestamp += 1000; // Move on one second. |
| 42 | + } |
| 43 | + |
| 44 | + // Add multiple values to the timeseries in round trip to the server: |
| 45 | + // https://oss.redis.com/redistimeseries/commands/#tsmadd |
| 46 | + const response = await client.ts.mAdd([{ |
| 47 | + key: 'mytimeseries', |
| 48 | + timestamp: currentTimestamp + 60000, |
| 49 | + value: Math.floor(Math.random() * 1000) + 1 |
| 50 | + }, { |
| 51 | + key: 'mytimeseries', |
| 52 | + timestamp: currentTimestamp + 120000, |
| 53 | + value: Math.floor(Math.random() * 1000) + 1 |
| 54 | + }]); |
| 55 | + |
| 56 | + // response = array of timestamps added by TS.MADD command. |
| 57 | + if (response.length === 2) { |
| 58 | + console.log('Added 2 entries to timeseries with TS.MADD.'); |
| 59 | + } |
| 60 | + |
| 61 | + // Update timeseries retention with TS.ALTER: |
| 62 | + // https://oss.redis.com/redistimeseries/commands/#tsalter |
| 63 | + const alterResponse = await client.ts.alter('mytimeseries', { |
| 64 | + RETENTION: 0 // Keep the entries forever |
| 65 | + }); |
| 66 | + |
| 67 | + if (alterResponse === 'OK') { |
| 68 | + console.log('Timeseries retention settings altered successfully.'); |
| 69 | + } |
| 70 | + |
| 71 | + // Query the timeseries with TS.RANGE: |
| 72 | + // https://oss.redis.com/redistimeseries/commands/#tsrangetsrevrange |
| 73 | + const fromTimestamp = 1640995200000; // Jan 1 2022 00:00:00 |
| 74 | + const toTimestamp = 1640995260000; // Jan 1 2022 00:01:00 |
| 75 | + const rangeResponse = await client.ts.range('mytimeseries', fromTimestamp, toTimestamp, { |
| 76 | + // Group into 10 second averages. |
| 77 | + AGGREGATION: { |
| 78 | + type: TimeSeriesAggregationType.AVERAGE, |
| 79 | + timeBucket: 10000 |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + console.log('RANGE RESPONSE:'); |
| 84 | + // rangeResponse looks like: |
| 85 | + // [ |
| 86 | + // { timestamp: 1640995200000, value: 356.8 }, |
| 87 | + // { timestamp: 1640995210000, value: 534.8 }, |
| 88 | + // { timestamp: 1640995220000, value: 481.3 }, |
| 89 | + // { timestamp: 1640995230000, value: 437 }, |
| 90 | + // { timestamp: 1640995240000, value: 507.3 }, |
| 91 | + // { timestamp: 1640995250000, value: 581.2 }, |
| 92 | + // { timestamp: 1640995260000, value: 600 } |
| 93 | + // ] |
| 94 | + |
| 95 | + console.log(rangeResponse); |
| 96 | + |
| 97 | + // Get some information about the state of the timeseries. |
| 98 | + // https://oss.redis.com/redistimeseries/commands/#tsinfo |
| 99 | + const tsInfo = await client.ts.info('mytimeseries'); |
| 100 | + |
| 101 | + // tsInfo looks like this: |
| 102 | + // { |
| 103 | + // totalSamples: 1440, |
| 104 | + // memoryUsage: 28904, |
| 105 | + // firstTimestamp: 1641508920000, |
| 106 | + // lastTimestamp: 1641595320000, |
| 107 | + // retentionTime: 86400000, |
| 108 | + // chunkCount: 7, |
| 109 | + // chunkSize: 4096, |
| 110 | + // chunkType: 'uncompressed', |
| 111 | + // duplicatePolicy: 'block', |
| 112 | + // labels: [], |
| 113 | + // sourceKey: null, |
| 114 | + // rules: [] |
| 115 | + // } |
| 116 | + |
| 117 | + console.log('Timeseries info:'); |
| 118 | + console.log(tsInfo); |
| 119 | + } catch (e) { |
| 120 | + console.error(e); |
| 121 | + } |
| 122 | + |
| 123 | + await client.quit(); |
| 124 | +} |
| 125 | + |
| 126 | +timeSeries(); |
0 commit comments