Extract bit from integer Influx DB

Hallo,

I use Grafana 9.1.6 for the visualisation of data from hardware equipment. I receive a status word (integer value) which every bit is the status of a specific error.

  • Data Source is InfluxDB

*Example value ist 255 (1111 1111) Each bit represents differents Status.

is it possible to extract every bit and map the value

Thanks

Hi @kabs

This is a tricky one and I’m not totally sure this would work, but I believe you should be able to use Go and Flux to do the bit extraction and then send that info to Grafana for display.

First convert the integer value to its binary representation using the strconv.FormatInt() function in Go. For example, if the integer value is 255, you can convert it to binary representation as follows:

statusWord := 255
binaryWord := strconv.FormatInt(int64(statusWord), 2) // "11111111"

Next, once you have the binary representation, you can extract each bit using the strings.Split() function and then iterate over the resulting array to map each bit to its corresponding status. For example, let’s say you have the following mapping of bits to statuses:

Bit Status
0 Error A
1 Error B
2 Error C
3 Error D
4 Error E
5 Error F
6 Error G
7 Error H

You can use the following code to extract each bit and map it to its corresponding status:

bits := strings.Split(binaryWord, "")
for i, bit := range bits {
    if bit == "1" {
        // Map the bit to its corresponding status
        switch i {
        case 0:
            // Bit 0 corresponds to Error A
            // Do something here
        case 1:
            // Bit 1 corresponds to Error B
            // Do something here
        case 2:
            // Bit 2 corresponds to Error C
            // Do something here
        // Add more cases for the remaining bits and statuses
        }
    }
}

Finally, you can use the map() function in Flux to create a new table with the mapped statuses. For example, let’s say you want to create a table with two columns: Status and Count in Flux:

from(bucket: "my-bucket")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "my-measurement")
  |> map(fn: (r) => ({
      status: r.status,
      count: 1
  }))
  |> reduce(
      fn: (r, accumulator) => ({
          status: r.status,
          count: r.count + accumulator.count
      })
  )
  |> map(fn: (r) => ({
      _time: now(),
      _value: r.count,
      _field: "Count",
      Status: r.status
  }))

The above creates a new table with the mapped statuses and the count of occurrences of each status in the original table. You can then use this table to create a visualization (e.g. Stat Panel) in Grafana.

I think it could be simpler than that, aren’t there bit masking operations on influxdB already?, I think I have seen those. if that is the case you could just mask the bit and use and and operation to extract the value of each bit.

I’ll be back in no time with more info, I’m reading the documentation.

import "array"
import "experimental/bitwise"




rows = [
{int: 8,  binary: "1000"},
{int: 5,  binary: "0101"},
{int: 6,  binary: "0110"},
{int: 12, binary: "1100"},
{int: 10, binary: "1010"},
{int: 1,  binary: "0001"},
{int: 14, binary: "1110"},
{int: 11, binary: "1011"},

]

array.from(rows: rows)
  |> map(fn: (r) => ({ r with 
  
  bit3: bool(v: bitwise.sand(a: r.int, b: 8) / 8) ,  
  bit2: bool(v: bitwise.sand(a: r.int, b: 4) / 4) , 
  bit1: bool(v: bitwise.sand(a: r.int, b: 2) / 2) ,  
  bit0: bool(v: bitwise.sand(a: r.int, b: 1) / 1) 
  
  
  }))

:grafana:reatl! Thanks for sharing.

What is pushing the data to influxdb?

Is it possible to extract bits in rows, not in columns?

For example, I have integer field Alarm:
ALARM

And I would like to extract all bits from it like this:
ALARM2

I.e. to have all extracted bit values in one column, bit numbers in the other one.

Sorry I was on a forced “hiatus” from all this type of stuff.

I am kind of confused because you already did it on the second picture?

so, what do you need help for?

Second picture shows what I need to achieve having the data shown on the first picture returned from my query.