我们的传感器以不规则的时间间隔产生值:
12:00 10 12:02 20 12:22 30 12:29 40
我正在尝试找到一个时间序列数据库,它可以自动计算某些常规时间间隔(例如10分钟)的平均值。当然,在该区间内值有效的时间越长,它在平均值中的权重就越大(时间加权平均值)。 (例如12:00-12:10:(10 * 2 + 20 * 8)/ 10 = 18))
我现在正在互联网上搜索几个小时,发现很多时间序列数据库都在讨论不规则的时间序列(例如InfluxDB,OpenTDSB等),而且大多数数据库都有一些类似SQL的查询语言和聚合函数。 / p>
不幸的是,他们并没有说出如何平均不规则的时间间隔。由于我不想尝试所有这些,有人可以告诉我哪些数据库支持计算时间加权平均值?谢谢!
答案 0 :(得分:2)
OpenTSDB在查询隐含的时间内对查询中的所有系列执行聚合。对于任何在时间戳上没有数据值的系列,它会从前后的值线性插值。它在查询时进行“上采样” - 原始数据始终存储在它到达时。你可以执行尾随窗口时间平均值,但不是指数加权移动平均线(我相信你的时间加权是什么意思?)
http://opentsdb.net/docs/build/html/user_guide/query/aggregators.html
(我应该补充一点,这不是对你应该使用的数据库的OpenTSDB的全面推荐,我只是回答你的问题)
答案 1 :(得分:1)
Axibase时间序列数据库支持加权时间平均聚合器(wtavg):http://axibase.com/products/axibase-time-series-database/visualization/widgets/configuring-the-widgets/aggregators/
与当前时间相比, wtavg
对旧样本的权重进行了线性递减。
REST API,SQL层和规则引擎支持此聚合器。
编辑2016-06-15T12:52:00Z :支持interpolation functions:
披露:我为Axibase工作。
答案 2 :(得分:0)
我最近不得不为我们自己的SCADA / IoT产品提供加权平均加权平均值的解决方案,数据存储在PostgreSQL中。如果你想自己动手,那就是你可以做到的。
我们假设下表:
create table samples (
stamp timestamptz,
series integer,
value float
);
insert into samples values
('2018-04-30 23:00:00+02', 1, 12.3),
('2018-05-01 01:45:00+02', 1, 22.2),
('2018-05-01 02:13:00+02', 1, 21.6),
('2018-05-01 02:26:00+02', 1, 14.9),
('2018-05-01 03:02:00+02', 1, 16.9);
要计算常规加权平均值,我们需要执行以下操作:
在提交代码之前,我们将做出以下假设:
假设我们有兴趣计算系列2018-05-01 00:00:00+02
的{{1}}和2018-05-01 04:00:00+02
之间的每小时加权平均值。我们首先查询给定的时间范围,添加一个对齐的标记:
1
这给了我们:
select
stamp,
to_timestamp(extract (epoch from stamp)::integer / 3600 * 3600)
as stamp_aligned,
value
from samples
where
series = 1 and
stamp >= '2018-05-01 00:00:00+02' and
stamp <= '2018-05-01 04:00:00+02';
我们会注意到:
stamp | stamp_aligned | value
------------------------+------------------------+-------
2018-05-01 01:45:00+02 | 2018-05-01 01:00:00+02 | 22.2
2018-05-01 02:13:00+02 | 2018-05-01 02:00:00+02 | 21.6
2018-05-01 02:26:00+02 | 2018-05-01 02:00:00+02 | 14.9
2018-05-01 03:02:00+02 | 2018-05-01 03:00:00+02 | 16.9
(4 rows)
的值,也无法告诉00:00:00
的值。01:00:00
列告诉我们该记录属于哪个时间段,但事实上该表缺少每个时段开头的值。要解决这些问题,我们将查询给定时间范围之前的最后一个已知值,并添加圆周时间的记录,我们稍后将使用正确的值填充:
stamp_aligned
这给了我们
with
t_values as (
select * from (
-- select last value prior to time range
(select
stamp,
to_timestamp(extract(epoch from stamp)::integer / 3600 * 3600)
as stamp_aligned,
value,
false as filled_in
from samples
where
series = 1 and
stamp < '2018-05-01 00:00:00+02'
order by
stamp desc
limit 1) union
-- select records from given time range
(select
stamp,
to_timestamp(extract(epoch from stamp)::integer / 3600 * 3600)
as stamp_aligned,
value,
false as filled_in
from samples
where
series = 1 and
stamp >= '2018-05-01 00:00:00+02' and
stamp <= '2018-05-01 04:00:00+02'
order by
stamp) union
-- select all regular periods for given time range
(select
stamp,
stamp as stamp_aligned,
null as value,
true as filled_in
from generate_series(
'2018-05-01 00:00:00+02',
'2018-05-01 04:00:00+02',
interval '3600 seconds'
) stamp)
) states
order by stamp
)
select * from t_values;
因此,我们每个时间段至少有一条记录,但我们仍需填写填写记录的值:
stamp | stamp_aligned | value | filled_in
------------------------+------------------------+-------+-----------
2018-04-30 23:00:00+02 | 2018-04-30 23:00:00+02 | 12.3 | f
2018-05-01 00:00:00+02 | 2018-05-01 00:00:00+02 | ¤ | t
2018-05-01 01:00:00+02 | 2018-05-01 01:00:00+02 | ¤ | t
2018-05-01 01:45:00+02 | 2018-05-01 01:00:00+02 | 22.2 | f
2018-05-01 02:00:00+02 | 2018-05-01 02:00:00+02 | ¤ | t
2018-05-01 02:13:00+02 | 2018-05-01 02:00:00+02 | 21.6 | f
2018-05-01 02:26:00+02 | 2018-05-01 02:00:00+02 | 14.9 | f
2018-05-01 03:00:00+02 | 2018-05-01 03:00:00+02 | ¤ | t
2018-05-01 03:02:00+02 | 2018-05-01 03:00:00+02 | 16.9 | f
2018-05-01 04:00:00+02 | 2018-05-01 04:00:00+02 | ¤ | t
(10 rows)
这给了我们以下内容:
with
t_values as (
...
),
-- since records generated using generate_series do not contain values,
-- we need to copy the value from the last non-generated record.
t_with_filled_in_values as (
-- the outer query serves to remove any record prior to the given
-- time range
select *
from (
select
stamp,
stamp_aligned,
-- fill in value from last non-filled record (the first record
-- having the same filled_in_partition value)
(case when filled_in then
first_value(value) over (partition by filled_in_partition
order by stamp) else value end) as value
from (
select
stamp,
stamp_aligned,
value,
filled_in,
-- this field is incremented on every non-filled record
sum(case when filled_in then 0 else 1 end)
over (order by stamp) as filled_in_partition
from
t_values
) t_filled_in_partition
) t_filled_in_values
-- we wrap the filling-in query in order to remove any record before the
-- beginning of the given time range
where stamp >= '2018-05-01 00:00:00+02'
order by stamp
)
select * from t_with_filled_in_values;
所以我们都很好 - 我们已经为所有圆周时间添加了正确值的记录,我们还删除了第一条记录,它给了我们时间范围开头的值,但是它位于它之外。不,我们已准备好进行下一步。
我们将继续计算每条记录的持续时间:
stamp | stamp_aligned | value
------------------------+------------------------+-------
2018-05-01 00:00:00+02 | 2018-05-01 00:00:00+02 | 12.3
2018-05-01 01:00:00+02 | 2018-05-01 01:00:00+02 | 12.3
2018-05-01 01:45:00+02 | 2018-05-01 01:00:00+02 | 22.2
2018-05-01 02:00:00+02 | 2018-05-01 02:00:00+02 | 22.2
2018-05-01 02:13:00+02 | 2018-05-01 02:00:00+02 | 21.6
2018-05-01 02:26:00+02 | 2018-05-01 02:00:00+02 | 14.9
2018-05-01 03:00:00+02 | 2018-05-01 03:00:00+02 | 14.9
2018-05-01 03:02:00+02 | 2018-05-01 03:00:00+02 | 16.9
2018-05-01 04:00:00+02 | 2018-05-01 04:00:00+02 | 16.9
(9 rows)
这给了我们:
with
t_values as (
...
),
t_with_filled_in_values (
...
),
t_with_weight as (
select
stamp,
stamp_aligned,
value,
-- use window to get stamp from next record in order to calculate
-- the duration of the record which, divided by the period, gives
-- us the weight.
coalesce(extract(epoch from (lead(stamp)
over (order by stamp) - stamp)), 3600)::float / 3600 as weight
from t_with_filled_in_values
order by stamp
)
select * from t_with_weight;
剩下的就是总结一下:
stamp | stamp_aligned | value | weight
------------------------+------------------------+-------+--------------------
2018-05-01 00:00:00+02 | 2018-05-01 00:00:00+02 | 12.3 | 1
2018-05-01 01:00:00+02 | 2018-05-01 01:00:00+02 | 12.3 | 0.75
2018-05-01 01:45:00+02 | 2018-05-01 01:00:00+02 | 22.2 | 0.25
2018-05-01 02:00:00+02 | 2018-05-01 02:00:00+02 | 22.2 | 0.216666666666667
2018-05-01 02:13:00+02 | 2018-05-01 02:00:00+02 | 21.6 | 0.216666666666667
2018-05-01 02:26:00+02 | 2018-05-01 02:00:00+02 | 14.9 | 0.566666666666667
2018-05-01 03:00:00+02 | 2018-05-01 03:00:00+02 | 14.9 | 0.0333333333333333
2018-05-01 03:02:00+02 | 2018-05-01 03:00:00+02 | 16.9 | 0.966666666666667
2018-05-01 04:00:00+02 | 2018-05-01 04:00:00+02 | 16.9 | 1
(9 rows)
结果:
with
t_values as (
...
),
t_with_filled_in_values (
...
),
t_with_weight as (
...
)
select
stamp_aligned as stamp,
sum(value * weight) as avg
from t_with_weight
group by stamp_aligned
order by stamp_aligned;
您可以在this gist中找到完整的代码。
答案 3 :(得分:0)
如果 TSDB 支持给定时间范围内的值积分功能,则可以计算时间加权平均值 (TWA)。然后 TWA 可以计算为给定持续时间的积分除以持续时间。例如,以下查询计算 https://docs.mongodb.com/realm/ 中过去一小时指标 power
的时间加权平均值:
integrate(power[1h])/1h
在 VictoriaMetrics 查看有关 integrate()
函数的更多详细信息。