在使用node-redistimeSeries的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.

推荐答案

看到Retrieving Time Series data from Redis

作为聚合类型,使用TimeSeriesAggregationType.MIN,作为聚合桶使用较大的值,这样您的时间范围就不会被分割成桶.

阅读更多关于聚合here.

返回的时间戳是每个bucket的开始、结束或中间时间(由[BUCKETTIMESTAMP bt]可选参数指定).目前,您无法检索测量报告的最小/最大值的时间戳.

然而,一旦检索到最小值,您可以运行另一个查询并检索测量最小值时的所有时间戳:TS.RANGE有一个可选参数[FILTER_BY_VALUE min max],因此您可以在同一时间框架内查询,这次不进行聚合,并将检索到的值指定为minmax(因此您将检索测量该精确值时的所有时间戳).

Typescript相关问答推荐

您可以创建一个类型来表示打字员吗

处理API响应中的日期对象

Angular中的其他服务仅获取默认值

当使用`type B = A`时,B的类型显示为A.为什么当`type B = A时显示为`any `|用A代替?

对深度对象键/路径进行适当的智能感知和类型判断,作为函数参数,而不会触发递归类型限制器

如果请求是流,如何等待所有响应块(Axios)

CDK从可选的Lambda角色属性承担角色/复合主体中没有非空断言

为什么&;(交集)运算符会删除不相交的属性?

Angular 自定义验证控件未获得表单值

使用条件类型的类型保护

在类型脚本中将泛型字符串数组转换为字符串字母并集

声明遵循映射类型的接口

有没有办法传递泛型类型数组,以便推断每个元素的泛型类型?

三重融合视角与正交阵

垫子工具栏不起作用的Angular 布线

有没有一种更好的方法来存储内存中的数据,而不是在类型脚本中使用数组和基于索引的函数?

我可以将一个类型数组映射到TypeScrip中的另一个类型数组吗?

在tabel管理区域react js上设置特定顺序

广义泛型类型不加载抽象类型上下文

有没有办法从不同长度的元组的联合中提取带有类型的最后一个元素?