Search code examples
grafanainfluxdb

display a count of the last calendar month entries from influxdb 2 on grafana


Not sure I got my title right, apologies.

Recently discovered influxdb 2.0 and grafana 7, vast improvement from previous version.

I wondered if something is possible to do, I have a system that posts to influxdb the time it took to do a task, is it possible to count the number of entries for the last 30 days or calendar month ideally and display it as a gauge or text on grafana?

the Flux syntax is not like anything I have seen before so no idea where to start and any obvious googling I have done doesn't seem to bare fruit

Could be I need to collect the data via python, work it out and post it to a new measurement, seems kludgey

Thanks


Solution

  • I finally figured this out

    In order to count you need to group the data and then only keep the columns you need, for my table I grouped and kept the "serial" column, then ran a count on this. the Range -1mo is the previous calendar month

    from(bucket: "provisioning")
      |> range(start: -1mo, stop: now())
      |> filter(fn: (r) => r["_measurement"] == "provision")
      |> filter(fn: (r) => r["_field"] == "ttl")
      |> group()
      |> keep(columns: ["serial"])
      |> count(column: "serial")