跳到主要内容

时间序列数据

从支持时间序列存储的设备参数中查询历史数据。

信息

对于不在节点配置内的参数,参见自定义参数时间序列。

本模块的功能​

查询设备参数的时间序列历史数据,例如能耗、温度和传感器读数,可指定时间范围,并支持分页和聚合。

适用于图表、趋势分析和报表。参数的 properties 数组中需要包含 "time_series" 或 "simple_ts"。

不在节点配置内? 请使用自定义参数时间序列。

预期结果: 请求范围内的时间戳/数值数据点(可选分页)。

常见工作流​

获取最近 24 小时​

const { nodes } = await userInstance.getUserNodes();
const nodeConfig = nodes[0].nodeConfig ?? (await nodes[0].getNodeConfig());

const param = nodeConfig.devices[0].params.find(
(p) => p.properties?.includes("time_series")
);

const response = await param.getRawTSData({
startTime: Date.now() - 24 * 60 * 60 * 1000,
endTime: Date.now(),
resultCount: 100,
});

response.tsData.forEach((point) => {
console.log(new Date(point.timestamp), point.value);
});

查找时间序列参数​

device.params.forEach((param) => {
if (param.properties?.includes("time_series") || param.properties?.includes("simple_ts")) {
console.log(`${param.name} supports time series`);
}
});

带分页的原始数据​

let tsResponse = await param.getRawTSData({
startTime: startTimestamp,
endTime: endTimestamp,
resultCount: 100,
timezone: "America/Los_Angeles",
});

while (tsResponse.hasNext) {
tsResponse = await tsResponse.fetchNext();
}

聚合数据​

const tsResponse = await param.getTSData({
startTime: startTimestamp,
endTime: endTimestamp,
numIntervals: 24,
aggregate: "avg",
aggregationInterval: "hour",
});

简单查询​

const tsResponse = await param.getSimpleTSData({
startTime: startTimestamp,
endTime: endTimestamp,
resultCount: 50,
});

错误处理​

try {
if (!param.properties?.includes("time_series")) return;
await param.getRawTSData(request);
} catch (error) {
console.error("Time series query failed:", error);
}

进阶概念​

选择查询方法​

最佳实践​

  1. 查询前检查 properties
  2. 时间范围较大时分页获取
  3. 设置 timezone 以获得准确的边界
  4. 优先使用 getSimpleTSData(),除非需要聚合

相关资源​

On this page