grafanafluxword-cloudinfluxdb-2

Influxdb2 influx grafana word cloud from multiple results


I am getting into influxdb2 and flux currently. My playground is, to scrape a public website and be able to visualize the words on the website in a word cloud in Grafana. This works, but when my range selection in Grafana results in multiple results, The words show up twice or more times - depending on the number of results.

My current flux query

  from(bucket: v.bucket)
    |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
    |> filter(fn: (r) => r._measurement == "wordcount" )
    |> group(columns: ["items"])
    |> sort(columns: ["_value"], desc: true)

My goal would be to combine all resulting rows into one result that holds the average occurrence of the words in the selected range. Example rows fields value:

{"field1": 2, "field2": 1, "field3": 4}
{"field1": 2, "field2": 1, "field4": 0}

Wanted result:

{"field1": 4, "field2": 2, "field3": 4, "field4": 0}

How would that be done?


Solution

  • With the help of the Grafana forum, I was able to piece together a working solution for the problem. The complete query now looks as follows.

    timestart = uint(v: v.timeRangeStart)
    timestop = uint(v: v.timeRangeStop)
    duration = duration(v:timestop - timestart)
    
    from(bucket: v.bucket)
      |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
      |> filter(fn: (r) => r._measurement == "wordcount" )
      |> aggregateWindow(every: duration, fn: sum)
      |> group(columns: ["_time","_measurement"])
      |> sort(columns: ["_value"], desc: true)