Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InfluxDB Flux query to display data #37

Open
YeomansIII opened this issue Aug 24, 2020 · 15 comments
Open

InfluxDB Flux query to display data #37

YeomansIII opened this issue Aug 24, 2020 · 15 comments
Labels

Comments

@YeomansIII
Copy link

YeomansIII commented Aug 24, 2020

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.

@pR0Ps
Copy link
Owner

pR0Ps commented Aug 29, 2020

This comment might be helpful to you: #3 (comment) . 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 pR0Ps added the question label Aug 29, 2020
@MatinF
Copy link

MatinF commented Sep 22, 2020

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")

@etwobben
Copy link

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.

@MatinF
Copy link

MatinF commented Oct 28, 2020

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

@etwobben
Copy link

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?

@MatinF
Copy link

MatinF commented Oct 28, 2020

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.

@YeomansIII
Copy link
Author

@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.

@raul-parada
Copy link

How to show this in a map on InfluxDB 2.0?

@MatinF
Copy link

MatinF commented Dec 7, 2020

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.

@raul-parada
Copy link

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

@MatinF
Copy link

MatinF commented Dec 7, 2020

Hi again,

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

best,

@marcel4042
Copy link

marcel4042 commented Mar 11, 2022

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 :-)

@blurpiflurp
Copy link

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

@Fr4nc3sc0NL
Copy link

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")

@ignamendi21
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants