Skip to main content

timeSeriesLinearRegressionToGrid

引入版本:v26.9.0 聚合函数,接收由时间戳和值对组成的时间序列数据,并在由起始时间戳、结束时间戳和步长定义的等间隔时间网格上对这些值进行直线拟合。对于网格上的每个点,函数会用一条直线拟合指定时间窗口内的样本,并返回 Tuple (intercept, slope):intercept 是该直线在网格点时间戳处的值,slope 是其每秒斜率。如果窗口内的样本不足,两者均为 NULL。 当偏移量为 t 时,timeSeriesPredictLinearToGrid 的结果等于 intercept + slope * t,而 timeSeriesDerivToGrid 的结果等于 slope。PromQL 函数 predict_linear 正是基于此函数实现的,因此不同网格点可以使用不同的预测偏移量,例如 predict_linear(v, time())。 样本可以通过以下三种形式之一传入:
  • 两个参数 timestamp 和 value,每行包含一个样本;
  • 时间戳数组和值数组,每行包含一整条时间序列;
  • 由 (timestamp, value) Tuple 组成的单个数组,每行包含一整条时间序列。
如果多个样本的时间戳相同,则只使用其中值最大的那个样本。NaN 值小于任何其他值,因此只有当该时间戳上的所有样本均为 NaN 时,才会使用 NaN 值。
此函数目前处于私有预览阶段,可通过设置 enable_time_series_aggregate_functions=true 启用。
语法
参数
  • start_timestamp — 指定网格的起点。也可以是小数,或包含数字或日期时间文本的字符串。UInt32 或 DateTime 或 DateTime64 或 Float* 或 Decimal* 或 String
  • end_timestamp — 指定网格的终点。也可以是小数,或包含数字或日期时间文本的字符串。UInt32 或 DateTime 或 DateTime64 或 Float* 或 Decimal* 或 String
  • grid_step — 指定网格的步长,单位为秒。也可以是小数,或包含数字或时长 (如 ’15s’ 或 ‘1m’) 的字符串。UInt32 或 Float* 或 Decimal* 或 String
  • staleness — 指定参与计算的样本的最大“陈旧时长” (staleness) ,单位为秒。staleness 窗口为左开右闭区间。也可以是小数,或包含数字或时长 (如 ’15s’ 或 ‘1m’) 的字符串。UInt32 或 Float* 或 Decimal* 或 String
参数 返回值 每个网格点对应一个 Tuple (intercept, slope):即拟合直线在该网格点时间戳处的值,以及其每秒斜率。如果某个网格点的窗口内样本不足,则两个元素均为 NULL。Array(Tuple(intercept Nullable(Float64), slope Nullable(Float64))) 示例 在网格 [100, 110, 120] 上拟合直线,并与 timeSeriesPredictLinearToGrid 和 timeSeriesDerivToGrid 进行对比
Query
Response
最后修改于 2026年10月14日