Query a counter of distinct values

influxdb
#1

Hi, I’m looking for an influx version of Python’s Counter.
Basically, a way of counting the number of values for each distinct values (also called occurrences).

I already found two topics related to my question:

With no conclusive answers.

#2

Hello @Thomas_Jalabert,

I was able to do this in flux with the following script:

   from(bucket: "test")
      |> range(start: dashboardTime)
      |> filter(fn: (r) => r._field == "value")
      |> group(columns: ["_value"])
      |> map(fn: (r) => ({_time: r._time, index: 1}))
      |> cumulativeSum(columns: ["index"]) 
      |> last()

Where my data in line protocol looks like:

test,tagkey=occ, value=1
test,tagkey=occ, value=5
test,tagkey=occ, value=5
test,tagkey=occ, value=1
test,tagkey=occ, value=1

This feels a little hacky, but hopefully, it will carry you through while I look for a more elegant solution.

The distinct() function could be useful too.

1 Like
#3

@Thomas_Jalabert at the very least, at least now you’ve been introduced to some pretty useful flux functions. If you want to write more flux scripts, this will be your best resource.

#4

Also, please take a look at this testdata repo . It has examples of how each flux function transforms small example datasets.

#5

Thank you @Anaisdg!

I was hoping for a solution using InfluxQL.
I never used flux before, surely I will take a look!

Thanks again for the various ressources.