在使用node-redis和timeSeries的Typescript 中,我想得到一个armin的范围,但我找不到一个方法来做到这一点.
BARCH是一个简单的例子,它有一个timeSeries(时间戳0、1、2、3、4、5上的6个点,每个点都有一个随机值).我想要时间戳范围的argmin [3,5]
import * as rd from 'redis';
// Connect to redis
const { REDIS_ENDPOINT, REDIS_PASSWORD, REDIS_PORT } = process.env;
if (!REDIS_ENDPOINT || !REDIS_PASSWORD || !REDIS_PORT) throw new Error('missing env variable');
const client = rd.createClient({ socket: { host: REDIS_ENDPOINT, port: parseInt(REDIS_PORT, 10) }, password: REDIS_PASSWORD });
await client.connect();
// Create a time series with timestamp 0, 1, 2, 3, 4, 5 (random value on each point)
const savePipeline = client.multi();
Array.from(Array(6).keys()).forEach((timestamp) => {
savePipeline.ts.add('test_time_serie_key', timestamp, Math.random());
});
await savePipeline.exec();
// Query argmin: I want timestamp and value of point with min value in timestamp range [3, 5]
const fromTimestamp = 3;
const toTimestamp = 5;
const argmin = { timestamp: null, value: null }; // How to get It ?
console.log(`Argmin in range [${fromTimestamp}, ${toTimestamp}] is: ${JSON.stringify(argmin)}`);
我try 了aggregation,但它不起作用,使用聚合,我可以检索min,但不能检索对应于该min的时间戳(因为范围被分成块,返回范围的开始时间戳). 这里有一个例子.它总是返回一个时间戳为0的点(即使在时间戳,我们没有min值,并且查询在时间戳3和5之间,0是不可能的).
const pipeline = client.multi();
pipeline.ts.range('test_time_serie_key', fromTimestamp, toTimestamp, {
AGGREGATION: { type: rd.TimeSeriesAggregationType.MIN, timeBucket: 1_000000 },
});
const results = await pipeline.exec() as [{ timestamp: number, value: number } | null][];
console.log(results);
Note: I know one way to get the argmin with the aggregation: I could use a very low timeBucket (which would return all points in range) and then find the desired point using javascript code. But I would like to avoid fetching all points. In reality, the range is quite long, there are a lot of points and this task needs to be repeated frequently (around every 10 seconds). So fetching a huge amount of points to find a single point every 10 seconds seems a bad idea.