如何在InfluxDB 1.x仪表板中将数字转换为字符串?

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

How to transform Numbers to strings in InfluxDB 1.x Dashboards?

问题

以下是翻译好的部分:

The visualization templates I have for creating a dashboard in Chronograf always expect numbers (except table). How can I make it so that I get text instead of a number in certain cases?

在Chronograf中,我用于创建仪表板的可视化模板始终期望数字(除了表格)。如何使某些情况下获得文本而不是数字呢?

For instance:

例如:

The logic I want to implement is:

我想要实现的逻辑是:

if result eq 0 then return "Idle" else return value

如果结果等于0,则返回"Idle",否则返回值

That's my Influx-Query, which results in 0.

这是我的Influx查询,其结果为0

SELECT last("DischargingBattery") AS "DischargingBattery" FROM "wallbox"."autogen"."battery"

从"wallbox"."autogen"."battery"中选择最后一个"DischargingBattery"作为"DischargingBattery"

This ends up with this result on the Dashboard:

这在仪表板上显示为以下结果:

如何在InfluxDB 1.x仪表板中将数字转换为字符串?

当此查询结果为0时,我希望在我的仪表板上看到字符串"Idle",而不是数字零。

That is what I would like to achieve:

这是我想要实现的目标:

如何在InfluxDB 1.x仪表板中将数字转换为字符串?

英文:

The visualization templates I have for creating a dashboard in Chronograf always expect numbers (except table). How can I make it so that I get text instead of a number in certain cases?

For instance:

The logic I want implement is:

if result eq 0 then return "Idle" else return value

That's my Influx-Query, which results into 0.

SELECT last("DischargingBattery") AS "DischargingBattery" FROM "wallbox"."autogen"."battery"

This ends up into this result on the Dashboard:

如何在InfluxDB 1.x仪表板中将数字转换为字符串?

When this query result into 0, I want to see the string "Idle" on my Dashboard instead of a number Zero.

That is what I would like to achieve:

如何在InfluxDB 1.x仪表板中将数字转换为字符串?

答案1

得分: 3

使用InfluxQL可能无法帮助您在这方面。

您可以尝试Flux,这是InfluxData的新功能数据脚本语言,专为查询、分析和处理数据而设计。
查看Flux文档:https://docs.influxdata.com/flux/v0.x/

Map函数:https://docs.influxdata.com/flux/v0.x/stdlib/universe/map

步骤1:通过此文档在InfluxDB 1.X中启用Flux:https://docs.influxdata.com/influxdb/v1.8/flux/installation/
步骤2:将您的查询更改为以下内容:

from(bucket: "wallbox/autogen")
|> range(start: "2022-02-22T20:22:02Z", stop: now())
|> filter(fn: (r) => r._measurement == "battery")
|> filter(fn: (r) => r._field == "DischargingBattery")
|> last()
|> map(fn: (r) => ({r with _value: if r._value > 0 then "Idle" else _value}))
英文:

Using InfuxQL might not be able to help you there.

You could try Flux which is InfluxData’s new functional data scripting language designed for querying, analyzing, and acting on data.
See flux documentation: https://docs.influxdata.com/flux/v0.x/

Map function: https://docs.influxdata.com/flux/v0.x/stdlib/universe/map

Step 1: enable Flux in 1.X via this doc: https://docs.influxdata.com/influxdb/v1.8/flux/installation/
Step 2: Change your query similar to following:

from(bucket: "wallbox/autogen")
|> range(start: "2022-02-22T20:22:02Z", stop: now())
|> filter(fn: (r) => r._measurement == "battery")
|> filter(fn: (r) => r._field == "DischargingBattery")
|> last()
|> map(fn: (r) => ({r with _value: if r._value > 0 then "Idle" else _value}))

答案2

得分: 0

这是我用来将0和1转换为"night"和"day"的方法。toString()对我来说是解决方案,它不喜欢在映射中使用整数类型的条件。我还需要在“value options -> Fields”下选择“Tarif”字段,这也将解决您在面板中关于字符串与整数的其他问题。

from(bucket: "p1_meter")
  |> range(start: -15m)
  |> filter(fn: (r) =>
    r._measurement == "P1_measurements" and
    r._field == "tarif"
  )
  |> last()
  |> toString()
  |> map(fn: (r) => ({r with _value: if r._value == "0" then "night" else "day" }))

您也可以将这个方法用于您的类似情况:

...
|> last()
|> toString()
|> map(fn: (r) => ({r with _value: if r._value == "0" then "IDLE" else _value }))

panel value options


<details>
<summary>英文:</summary>

this is what I used to transform 0 and 1 to &quot;night&quot; and &quot;day&quot;
The toString() was the solution for me, it didn&#39;t like to use Integer type for the condition in the map.
I needed also to select under &#39;value options -&gt; Fields&#39; the &#39;Tarif&#39; field,  which will also solve your other issue about string vs int in the panel.

        from(bucket: &quot;p1_meter&quot;)
      |&gt; range(start: -15m)
      |&gt; filter(fn: (r) =&gt;
        r._measurement == &quot;P1_measurements&quot; and
        r._field == &quot;tarif&quot;
      )
      |&gt; last()
      |&gt; toString()
      |&gt; map(fn: (r) =&gt; ({r with _value: if r._value == &quot;0&quot; then &quot;night&quot; else &quot;day&quot; }))

You could use this to your analogy as well:

    ...
    |&gt; last()
    |&gt; toString()
    |&gt; map(fn: (r) =&gt; ({r with _value: if r._value == &quot;0&quot; then &quot;IDLE&quot; else _value }))

[panel value options][1]


  [1]: https://i.stack.imgur.com/fo2iS.png

</details>



huangapple
  • 本文由 发表于 2023年3月7日 18:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660720.html
匿名

发表评论

匿名网友

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

确定