VOLTTRON发布消息到总线并在Grafana中查看数据。

huangapple go评论93阅读模式
英文:

VOLTTRON publish message to bus and see data in Grafana

问题

在我的VOLTTRON代理中,我进行了一些计算,然后将一些浮点数据发布到VOLTTRON消息总线,然后转发代理应该获取它,并且SQL代理将数据摄入到时间刻度SQL中。

有人能帮我看看如果我在代理代码中这样发布到总线:

#发布到消息总线
self.vip.pubsub.publish(
    peer="pubsub",
    topic=f"LBS_AGENT",
    headers={headers.TIMESTAMP: format_timestamp(get_aware_utc_now())},
    message=f"TOTAL_AIR_FLOW/{total_flow_temp}",
)

我在Grafana中如何查询它?以下查询不起作用:

SELECT
  "time" AS "time",
  metric AS metric,
  value
FROM slipstream_volttron
WHERE
  $__timeFilter("time") AND
  metric LIKE 'TOTAL_AIR_FLOW'
ORDER BY 1,2
英文:

In my VOLTTRON agent I am making some calculations and then publishing some float data to the VOLTTRON message bus where then the forward agent should pick it up and SQL agent ingest the data into the time scale SQL.

Can someone help me with if I publish to the bus with this in my agent code:

#publish to message bus
self.vip.pubsub.publish(
    peer="pubsub",
    topic=
    f"LBS_AGENT",
    headers={headers.TIMESTAMP: format_timestamp(get_aware_utc_now())},
    message=f"TOTAL_AIR_FLOW/{total_flow_temp}",
)

How would I query for it in Grafana? This doesn't work:

SELECT
  "time" AS "time",
  metric AS metric,
  value
FROM slipstream_volttron
WHERE
  $__timeFilter("time") AND
  metric LIKE 'TOTAL_AIR_FLOW'
ORDER BY 1,2

答案1

得分: 2

以下是您要翻译的内容:

  1. 一些必要的操作需要在此处从头到尾完成。(请注意,这假定当您说“SQL代理”时,您指的是SQLHistorian代理,而不是自定义数据库解决方案。)
    历史记录器的默认行为是保存/转发以以下四个前缀之一开头的主题: "devices /", "analysis /", "datalogger /" 或 "record /"。可以在历史记录器代理的配置中添加其他主题,但除非您这样做,否则它们将忽略此发布,因为您的主题以 'LBS_AGENT' 开头。最好选择历史记录器已经理解的主题前缀(即分析),如我们将在接下来看到的。

  2. 消息的格式需要适用于发布到SQL Historian的主题,以便其能够解释它。基本历史记录器不知道以 'LBS_AGENT' 开头的主题的数据结构可能是什么。设备、分析和数据记录器主题都需要消息具有特定的结构。这些可以在此处找到:Volttron Historian主题语法。设备和分析主题都需要使用字典发布,所以最好这样发布:

    self.vip.pubsub.publish(
      peer="pubsub",
      topic=f"analysis/LBS_AGENT",
      headers={headers.TIMESTAMP: format_timestamp(get_aware_utc_now())},
      message={"TOTAL_AIR_FLOW": total_flow_temp}
    )
    
  3. **Grafana中的数据源查询需要匹配历史记录器数据库的结构。**对于SQLHistorian,有两个表: "topics" 和 "data"。第一次存储给定主题时,它将在 "topics" 表中创建一个具有 "topic_name" 和 "topic_id" 的条目。 "topic_name" 字段是您发布的主题(没有分析前缀)加上消息中字典的键,即 "LBS_AGENT/TOTAL_AIR_FLOW"。topic_id也包含在数据表中作为外键。 "data" 表具有 "ts"(您的时间信息), "topic_id" 和 "value_string"(其中包含您的数据)等列。如果您在创建Grafana中的查询时已知主题ID,您可以使用以下查询:

    SELECT
      ts AS "time",
      value_string AS value
    FROM slipstream_volttron 
    WHERE
      $__timeFilter("time") AND
      topic_id = <the_actual_topic_id>
    

如果您不知道主题ID,或需要更加动态,您可以在主题和数据表上连接topic_id或使用Grafana查询变量获取主题ID,并在查询中使用该变量。

英文:

To get from start to finish here, a few things need to happen. (Note that this assumes that when you say "SQL agent" you are referring to the SQLHistorian agent, and not a custom database solution.)

  1. Once you publish the data, the historians (both the ForwardHistorian and the SQLHistorian) need to recognize the topic as one they should archive/forward.
    The default behavior for the historian is to save/forward topics which begin with one of four prefixes: "devices/", "analysis/", "datalogger/", or "record/". Additional topics may be added in the configuration of the historian agents, but unless you have done so, they will ignore this publish, since your topic begins with 'LBS_AGENT'. It is probably better to choose a topic prefix which the historians already understand (i.e., analysis), as we will see in the next bit.

  2. The format of the message needs to be appropriate to the topic on which it is published for the SQL Historian to be able to interpret it. The base historian is not aware of what structure data of topics beginning with LBS_AGENT might have. Devices, analysis, and datalogger topics all require the message to have a particular structure. These can be found here: https://volttron.readthedocs.io/en/main/agent-framework/historian-agents/historian-topic-syntax.html. Devices and analysis topics take dictionaries, so you are probably better off publishing like this:

    self.vip.pubsub.publish(
      peer=&quot;pubsub&quot;,
      topic=f&quot;analysis/LBS_AGENT&quot;,
      headers={headers.TIMESTAMP: format_timestamp(get_aware_utc_now())},
      message={&quot;TOTAL_AIR_FLOW&quot;: total_flow_temp}
    )
    
  3. Your datasource query in Grafana needs to match the structure of the historian database. In the case of the SQLHistorian, there are two tables: "topics", and "data". The first time a given topic is stored, it will create an entry in the "topics" table with a "topic_name" and a "topic_id". The "topic_name" field is the topic you published with (without the analysis prefix) plus the key of the dictionary in your message -- i.e., "LBS_AGENT/TOTAL_AIR_FLOW". The topic_id is also contained as a foreign key in the data table. The "data" table, meanwhile, has the columns "ts" (your time information), "topic_id", and "value_string" (which contains your data). If you know the topic_id already when you create your query in Grafana, you can use a query along the lines of:

    SELECT
      ts AS &quot;time&quot;,
      value_string AS value
    FROM slipstream_volttron 
    WHERE
      $__timeFilter(&quot;time&quot;) AND
      topic_id = &lt;the_actual_topic_id&gt;
    

If you are not going to know the topic_id, or need to be more dynamic, you can join the topics and data tables on topic_id or use a Grafana query variable to get the topic_ids and use that variable in the query.

答案2

得分: 1

以下是已翻译的内容:

我做的方式是:

  1. 找到主题(将“fake”更改为您想要的内容)
SELECT * FROM topics WHERE topic_name LIKE ('%fake%');

你会看到类似的结果:

1 | campus/building/fake/OutsideAirTemperature1 | {"units": "F", "type": "integer", "tz": "US/Pacific"}
2 | campus/building/fake/SampleLong1 | {"units": "Enumeration", "type": "integer", "tz": "US/Pacific"}
3 | campus/building/fake/SampleBool1 | {"units": "On / Off", "type": "integer", "tz": "US/Pacific"}
4 | campus/building/fake/OutsideAirTemperature2 | {"units": "F", "type": "integer", "tz": "US/Pacific"}
5 | campus/building/fake/SampleLong2 | {"units": "Enumeration", "type": "integer", "tz": "US/Pacific"}
6 | campus/building/fake/SampleBool2 | {"units": "On / Off", "type": "integer", "tz": "US/Pacific"}

假设你对campus/building/fake/SampleLong2感兴趣

然后在Grafana中应该是:

  SELECT
    $__time(data.ts),
    CAST (data.value_string AS DECIMAL) as "Label"
  FROM data, topics
  WHERE
    $__timeFilter(ts) AND
    data.topic_id = topics.topic_id AND
    topics.topic_name LIKE ('campus/building/fake/SampleLong2')
  ORDER BY 1,2

这将适用于Postgres数据库。

英文:

The way I do it is:

  1. Find the topic (change fake to what you want)
SELECT * FROM topics WHERE topic_name LIKE (&#39;%fake%&#39;);

You will see something like

1 | campus/building/fake/OutsideAirTemperature1 | {"units": "F", "type": "integer", "tz": "US/Pacific"}
2 | campus/building/fake/SampleLong1 | {"units": "Enumeration", "type": "integer", "tz": "US/Pacific"}
3 | campus/building/fake/SampleBool1 | {"units": "On / Off", "type": "integer", "tz": "US/Pacific"}
4 | campus/building/fake/OutsideAirTemperature2 | {"units": "F", "type": "integer", "tz": "US/Pacific"}
5 | campus/building/fake/SampleLong2 | {"units": "Enumeration", "type": "integer", "tz": "US/Pacific"}
6 | campus/building/fake/SampleBool2 | {"units": "On / Off", "type": "integer", "tz": "US/Pacific"}

Let say you are interested in campus/building/fake/SampleLong2

Then Grafana would be:

  SELECT
    $__time(data.ts),
    CAST (data.value_string AS DECIMAL) as &quot;Label&quot;
  FROM data, topics
  WHERE
    $__timeFilter(ts) AND
    data.topic_id = topics.topic_id AND
    topics.topic_name LIKE (&#39;campus/building/fake/SampleLong2&#39;)
  ORDER BY 1,2

This will work for a Postgres db.

huangapple
  • 本文由 发表于 2023年3月21日 03:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定