Search code examples
grafanainfluxdb

create Grafana dashboard dynamically from influxdb-measurements


I have measurements like these in influx

weather_sensor,crop=blueberries,plot=1,temp=13.6 1472515200000000000
weather_sensor,crop=blueberries,plot=2,temp=14.0 1472515200000000000
weather_sensor,crop=blueberries,plot=1,rain=0 1472515200000000000
weather_sensor,crop=blueberries,plot=2,rain=37 1472515200000000000
weather_sensor,crop=apples,plot=3,temp=15.4 1472515200000000000
weather_sensor,crop=apples,plot=4,temp=15.8 1472515200000000000
weather_sensor,crop=apples,plot=3,rain=102 1472515200000000000
weather_sensor,crop=apples,plot=4,rain=44 1472515200000000000

Is it possible to dynamically create the following dashboard from this data alone:

  • dropdown to select the crop
  • when crop "blueberries" is selected: charts for all blueberry plots are displayed
  • the charts show temperature and rainfall for each plot

When a new measurement arrives like

weather_sensor,crop=strawberries,plot=9,temp=12.2 1572515200000000000

There is a new element "strawberries" in the dropdown, leading to a dashboard with one chart ("strawberries plot 9")

All this without knowing beforehand which crops and plots there are (like reading from a DB the structure of the farming operation)


Solution

  • Yes, crop must be Influxdb tag. Create Grafana dashboard variable, e.g. crop from the query, e.g. SHOW TAG VALUES WITH KEY = "crop". Then use this variable in the graph panel query, e.g.

    SELECT mean("temp"), mean("rain") 
    FROM "metrics" 
    WHERE "crop" =~ /^$crop$/ AND $timeFilter 
    GROUP BY time($__interval), "plot" fill(null)
    

    I don't have your data, so this query may still need some minor tweaks. It is to give you and idea.

    If you need dedicated panels per plot, then define also plot variable, with crop filter condition, e.g. SHOW TAG VALUES WITH KEY = "plot" WHERE "crop" =~ /^$crop$/ and use panel repeating feature for this plot variable + use also filtering with $plot in the panel query.