timeSeriesRateToGrid
引入版本:v25.6.0
该聚合函数接收由时间戳和值组成的时间序列数据,并在由起始时间戳、结束时间戳和 step 定义的规则时间网格上,根据这些数据计算 类 PromQL rate。对于网格上的每个点,计算 rate 时会使用指定时间窗口内的样本。
如果多个样本具有相同的时间戳,则仅使用其中一个:值最大的样本。NaN 值会输给任何其他值,因此仅当该时间戳处的所有样本均为 NaN 时,才会使用 NaN 值。
语法
timeSeriesRateToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)参数
start_timestamp— 指定网格的起始时间。使用DateTime64时间戳实参时,也可以是小数、包含数字的字符串或日期时间文本。UInt32或DateTime或DateTime64或Float*或Decimal*或Stringend_timestamp— 指定网格的结束时间。使用DateTime64时间戳实参时,也可以是小数、包含数字的字符串或日期时间文本。UInt32或DateTime或DateTime64或Float*或Decimal*或Stringgrid_step— 指定网格步长,单位为秒。使用DateTime64时间戳实参时,也可以是小数、包含数字的字符串,或类似 '15s' 或 '1m' 的时长。UInt32或Float*或Decimal*或Stringstaleness— 指定参与计算的样本允许的最大陈旧时间,单位为秒。陈旧时间窗口为左开右闭区间。使用DateTime64时间戳实参时,也可以是小数、包含数字的字符串,或类似 '15s' 或 '1m' 的时长。UInt32或Float*或Decimal*或String
实参
timestamp— 样本的时间戳。可以是单个值或数组。UInt32或DateTime或Array(UInt32)或Array(DateTime)value— 与该时间戳对应的时间序列值。可以是单个值或数组。Float*或Array(Float*)
返回值
返回指定网格上的速率值。返回的数组中,每个时间网格点对应一个值。如果在窗口内没有足够的样本来计算某个网格点的速率值,则该值为 NULL。Array(Nullable(Float64))
示例
使用单个时间戳-值对的基本用法
SET allow_experimental_time_series_aggregate_functions = 1;
WITH
-- NOTE: the gap between 140 and 190 is to show how values are filled for ts = 150, 165, 180 according to window parameter
[110, 120, 130, 140, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
[1, 1, 3, 4, 5, 5, 8, 12, 13]::Array(Float32) AS values, -- array of values corresponding to timestamps above
90 AS start_ts, -- start of timestamp grid
90 + 120 AS end_ts, -- end of timestamp grid
15 AS step_seconds, -- step of timestamp grid
45 AS window_seconds -- "staleness" window
SELECT timeSeriesRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM
(
-- This subquery converts arrays of timestamps and values into rows of `timestamp`, `value`
SELECT
arrayJoin(arrayZip(timestamps, values)) AS ts_and_val,
ts_and_val.1 AS timestamp,
ts_and_val.2 AS value
);┌─timeSeriesRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,0.06666667,0.1,0.055555556,NULL,NULL,0.083333336] │
└────────────────────────────────────────────────────────────────────────────────────────┘使用数组类型实参
SET allow_experimental_time_series_aggregate_functions = 1;
WITH
[110, 120, 130, 140, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
[1, 1, 3, 4, 5, 5, 8, 12, 13]::Array(Float32) AS values,
90 AS start_ts,
90 + 120 AS end_ts,
15 AS step_seconds,
45 AS window_seconds
SELECT timeSeriesRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);┌─timeSeriesRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,0,0.06666667,0.1,0.055555556,NULL,NULL,0.083333336] │
└──────────────────────────────────────────────────────────────────────────────────────────┘