grafana-trackmap-panel icon indicating copy to clipboard operation
grafana-trackmap-panel copied to clipboard

InfluxDB Flux query to display data

Open YeomansIII opened this issue 5 years ago • 15 comments

I am trying to display a geo track using this plugin. My InfluxDB data is stored on InfluxDB 2.0 and I am querying using a Flux query in Grafana. My data contains a lat and lon field. What is the proper way of displaying this data on this plugin map?

I have tried many different queries and transformations, I am currently using this, which still doesn't display any data.

Query A:

from(bucket: "geodata")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "geo" and
    r._field == "lat" and
    r.source == "12345" and
    r.userid == "416481000"
  )
  |> set(key: "_field", value: "latitude")

Query B:

from(bucket: "geodata")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "geo" and
    r._field == "lon" and
    r.source == "12345" and
    r.userid == "416481000"
  )
  |> set(key: "_field", value: "longitude")

This results in a Grafana data table with columns Time, latitude, and longitude.

YeomansIII avatar Aug 24 '20 19:08 YeomansIII

This comment might be helpful to you: https://github.com/pR0Ps/grafana-trackmap-panel/issues/3#issuecomment-384655784 . I don't have any experience with Flux queries so I don't have any direct advice, sorry. If you end up figuring it out, let me know and I'll add an example to the readme for others.

pR0Ps avatar Aug 29 '20 17:08 pR0Ps

I'm using a query as below, which works great:

from(bucket: "test-bucket-new")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "958D2219")
  |> filter(fn: (r) => r["_field"] == "Latitude" or r["_field"] == "Longitude")
  |> aggregateWindow(every: v.windowPeriod, fn: median)
  |> yield(name: "median")

MatinF avatar Sep 22 '20 09:09 MatinF

I have tried the above query, but it doesn't render anything. Any pointers what could be the issue? Or any updates about the support for flux.

etwobben avatar Oct 28 '20 08:10 etwobben

Note that my query is rather specific to the way my Influx data is structured, so it should be viewed as an example only

MatinF avatar Oct 28 '20 09:10 MatinF

Ofcourse I modified it to my needs. But I have a very simple dataset I use for this. It has 2 fields (latitude and longitude) and 2 tags (geohash and source) within one measurement (position). So your query (changing bucket and measurement) for this should work right?

etwobben avatar Oct 28 '20 09:10 etwobben

I'm not sure what might be the reason - just shared my example in case it might be helpful for some. I have it working just fine with Flux on latest Grafana and using InfluxDB 1.8 or InfluxDB 2.0 (cloud starter) editions.

MatinF avatar Oct 28 '20 10:10 MatinF

@etwobben The two queries I posted in my initial question actually ended up working. The "set" function was only required to change the name of the columns from lat -> latitude, etc.

Try using two separate queries for lat and lon.

YeomansIII avatar Nov 04 '20 15:11 YeomansIII

How to show this in a map on InfluxDB 2.0?

raul-parada avatar Dec 07 '20 11:12 raul-parada

You can see an example in our playground (see the maps queries): https://grafana.csselectronics.stellarhosted.com/d/hXdWa0VMk/css-playground

Done using Flux query language in InfluxDB 2.0 cloud version.

MatinF avatar Dec 07 '20 12:12 MatinF

@MatinF Can I see the code? (no confidential data)

raul-parada avatar Dec 07 '20 12:12 raul-parada

Hi again,

The query code is available if you click the maps panel. Not sure what other code you're looking for?

best,

MatinF avatar Dec 07 '20 12:12 MatinF

Hi there! I have more or less a similar problem. The "lat" and "lon" is in one field as a string and I split this with my Influx query. The result seems OK but the map is showing nothing neither an error message.

Here is my query: import "strings" from(bucket: "source") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "LOCATION") |> filter(fn: (r) => r["_field"] == "value") |> map (fn: (r) => ({ r with latitude: float (v: strings.split(v: r._value, t: ",")[0]), longitude: float (v: strings.split(v: r._value, t: ",")[1]) }))

The data / result: _time | _value | latitude | longitude 2022-03-11T18:17:01.329Z | 49.4692537xxxxx,11.056303982xxxxx | 49.469253797xxxxx | 11.0563039xxxxx

Any thoughts? Thanks in advance! Best :-)

marcel4042 avatar Mar 11 '22 18:03 marcel4042

I'm using a query as below, which works great:

from(bucket: "test-bucket-new")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "958D2219")
  |> filter(fn: (r) => r["_field"] == "Latitude" or r["_field"] == "Longitude")
  |> aggregateWindow(every: v.windowPeriod, fn: median)
  |> yield(name: "median")

This worked great for me thanks! The only thing I've added was a filter for which device I want to display. from(bucket: "myBucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "findmy") |> filter(fn: (r) => r["name"] == "${Device}") |> filter(fn: (r) => r["_field"] == "latitude" or r["_field"] == "longitude") |> aggregateWindow(every: v.windowPeriod, fn: median) |> yield(name: "median")

I'm pulling in data using telegraf / starlark parsing of JSON files. new_metric.tags["serialnumber"] = group["serialNumber"] new_metric.fields["longitude"] = group["location"]["longitude"] new_metric.fields["altitude"] = group["location"]["altitude"] new_metric.fields["latitude"] = group["location"]["latitude"] new_metric.fields["positiontype"] = group["location"]["positionType"] new_metric.fields["address"] = group["address"]["mapItemFullAddress"] new_metric.fields["isold"] = group["location"]["isOld"] new_metric.tags["name"] = group["name"] new_metric.time = int(group["location"]["timeStamp"]*1000000)

So my measurements looks like this _time, name, serialnumber, address, altitude, isOld, latitude, longitude, positionType

blurpiflurp avatar Nov 28 '22 15:11 blurpiflurp

If anyone is working with the sample data provided by Influx, I had to drop the columns not expected by TrackMap. The following works for me with the birds migration dataset:

from(bucket: "Tutorial")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "migration")
  |> filter(fn: (r) => r["_field"] == "lat" or r["_field"] == "lon")
  |> filter(fn: (r) => r["id"] == "91761A")
  |> drop(columns: ["s2_cell_id"])
  |> aggregateWindow(every: v.windowPeriod, fn: median)
  |> yield(name: "median")

Fr4nc3sc0NL avatar Apr 25 '23 20:04 Fr4nc3sc0NL

THIS WORKS:

from(bucket: "ANDROMAXA") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "navigation.position") |> filter(fn: (r) => r["source"] == "Navi.1") |> filter(fn: (r) => r["_field"] == "lat" or r["_field"] == "lon") |> aggregateWindow(every: v.windowPeriod, fn: median) |> yield(name: "median")

HOWEVER, THE POINTER NOT. I navigate in the other grafs but the pointer is not appearing and not movin through the track.. Anyone knows why?

ignamendi21 avatar Dec 24 '23 14:12 ignamendi21