简体   繁体   English

使用用户定义的事件将实时传感器数据记录到RDBMS中

[英]Recording real-time sensor data in to RDBMS with user defined events

I have a system composed of two main components, sensors and triggers. 我有一个由两个主要组件,传感器和触发器组成的系统。

The system is responsible for recording information sent from the sensors, and reporting to users the times when certain triggers went active or inactive. 该系统负责记录从传感器发送的信息,并向用户报告某些触发器处于活动或不活动状态的时间。

A few pieces of information about sensors: 有关传感器的一些信息:

  • All sensors record the same set of fields 所有传感器记录同一组字段
  • Sensors send their recordings with a timestamp of when they were recorded 传感器发送记录时带有时间戳记的时间戳

Triggers: 触发条件:

  • Triggers are user defined 触发器是用户定义的
  • input is only one reading for one sensor (ie, not readings over time or multiple sensors) 一个传感器的输入仅为一个读数(即不是随时间推移的读数或多个传感器的读数)
  • only have active / inactive states 仅具有活动/非活动状态

Sensors are assigned to triggers on a many to many basis. 传感器在许多基础上分配给触发器。

The problem I have relates to tracking "when" the triggers went active or inactive. 我遇到的问题与跟踪“何时”触发器变为活动状态或非活动状态有关。

for example, for a trigger checking value > 1 例如,对于触发检查值> 1

sensor_id | reading             | value | trigger_value
1           2011-04-25T20:09:00   0       false
1           2011-04-25T20:11:00   1       false
1           2011-04-25T20:13:00   4       true
1           2011-04-25T20:15:00   5       true
1           2011-04-25T20:17:00   3       true
1           2011-04-25T20:19:00   6       true
1           2011-04-25T20:21:00   1       false

it might return: 它可能会返回:

sensor_id | reading             | event
1           2011-04-25T20:13:00   1 -- 'went active'
1           2011-04-25T20:21:00   0 -- 'went in-active'

My current approach involves recording the current state of the set of triggers for each sensor. 我当前的方法涉及为每个传感器记录一组触发器的当前状态。 When the next input comes in for a sensor, and the triggers are evaluated, it compares this with the list of current trigger states for that sensor. 当传感器的下一个输入输入并评估触发器时,它将与该传感器的当前触发器状态列表进行比较。 For each change in state the 'activation' or 'de-activation' is recorded. 对于每个状态变化,都会记录“激活”或“取消激活”。

This approach is quite simple, but it generates data about state changes that is "already there" in the database; 这种方法非常简单,但是它生成有关状态更改的数据,该状态更改已在数据库中“存在”。 however the data isn't in a single tuple, it resides in the relationship between tuples (in the same table). 但是,数据不在单个元组中,而是位于元组之间的关系中(在同一表中)。

So, the question is: 因此,问题是:

Do I change my approach because the data is 'already there'; 我是否因为数据已经“存在”而改变了我的方法? by changing the schema to make it less dependent on the relationship between tuples, or creating views / stored procs to analyse it apon request 通过更改模式以减少对元组之间关系的依赖,或创建视图/存储的过程来分析它

OR 要么

Do I keep with this system because it solves the problem and hides the fact there is a temporal relationship between tuples in the same table (which I know is bad). 我是否继续使用该系统,因为它解决了问题并且掩盖了同一表中元组之间存在时间关系的事实(我知道这是不好的)。

In a more general form: 以更一般的形式:

How do you store time based statistics / data in tables, and analyse the differences between consecutive statistics without bastardising RDBMS. 如何在表中存储基于时间的统计信息/数据,以及如何在不破坏RDBMS的情况下分析连续统计信息之间的差异。

Example of current implementation structure: 当前实现结构示例:

-- log incoming sensor data, keyed by sensor and when it was recorded
create table sensor_log (
  sensor_id integer references sensors (sensor_id),
  reading timestamp,
  data_point_a integer NOT NULL, -- example name only
  data_point_b integer NOT NULL,
  -- ...  various other data points
  primary key(sensor_id, reading)
);
-- data storage for trigger configuration
create table triggers (
  trigger_id integer,
  -- ...  configuration for the triggers
  primary key(trigger_id)
);
-- associate triggers with particular sensors on a many to many basis
create table sensor_triggers (
  sensor_id integer references sensors (sensor_id),
  trigger_id integer references triggers (trigger_id),
  -- ...  configuration for the triggers
  primary key(sensor_id, trigger_id)
);
-- record which triggers were active for a particular sensor input
-- not necessary, unless to save on recomputing past trigger activations
create table sensor_trigger_activations (
  sensor_id integer,
  reading timestamp,
  trigger_id integer references triggers (trigger_id),
  primary key (sensor_id, reading, trigger_id),
  foreign key (sensor_id, reading) references sensor_log (sensor_id, reading)
);
-- record trigger 'activations' & 'deactivations'
-- activation: active state preceded by in-active state (for a particular trigger)
-- deactivation: in-active state preceded by an active state ""
-- absense
create table sensor_trigger_events (
  sensor_id integer,
  reading timestamp,
  trigger_id integer,
  event_type smallint CHECK (event_type = 0 OR event_type = 1), -- 0 = deactivation, 1 = activation
  primary_key (sensor_id, reading, trigger_id),
  foreign key (sensor_id, reading) references sensor_log (sensor_id, reading)
);

First, I don't think a temporal relationship between tuples in the same table is necessarily bad. 首先,我认为同一张表中元组之间的时间关系不一定很糟糕。 It isn't really direct and so hiding it is ok. 它不是直接的,所以可以隐藏它。

I don't really see much that can be improved given your set of requirements. 鉴于您的要求,我真的看不到有什么可以改善的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM