Skip to main content
14-Alexandrite
July 24, 2023
Solved

How to structure a query to InfluxDB with the Flux API

  • July 24, 2023
  • 2 replies
  • 2614 views

I am also interested in how to structure a query to InfluxDB with the Flux API, mainly for the time grouping and functions that are available OOTB. If anyone has a sample of a PostJSON that is working, it would be great to see, I think the issue is how the fluxQuery part is structured, I have tried multiple formats with the same failure result. Below is a sample of what I am trying, but the response I get is "An internal error has occurred - check server logs"

 

 

// Construct the Flux query as a string
let fluxQuery = "data = from(bucket: \"thingworx\")\n |> range(start: -60m, stop: -1m)\n |> filter(fn: (r) => r[\"_measurement\"] == \"Winder_Thing01\")\n |> filter(fn: (r) => r[\"_field\"] == \"KibbleWinderAmps\")\n |> filter(fn: (r) => r[\"valuestreamname\"] == \"Winder_Thing01_VS\")\n |> aggregateWindow(every: 1m, fn: median, createEmpty: false)\n |> yield(name: \"median\")";

var headers = {
 "Content-Type": "application/json", // Correct the Content-Type
 "Authorization": "Token ourToken"
};

let params = {
 proxyScheme: undefined /* STRING {"defaultValue":"http"} */,
 headers: headers /* JSON */,
 ignoreSSLErrors: undefined /* BOOLEAN */,
 useNTLM: undefined /* BOOLEAN {"defaultValue":false} */,
 workstation: undefined /* STRING {"defaultValue":""} */,
 useProxy: undefined /* BOOLEAN {"defaultValue":false} */,
 withCookies: undefined /* BOOLEAN {"defaultValue":false} */,
 proxyHost: undefined /* STRING {"defaultValue":""} */,
 url: 'http://localhost:8086/api/v2/query/analyze?orgID=OrgID' /* STRING */,
 content: fluxQuery /* JSON */,
 timeout: undefined /* NUMBER {"defaultValue":60} */,
 proxyPort: undefined /* INTEGER {"defaultValue":8080} */,
 password: undefined /* STRING */,
 domain: undefined /* STRING {"defaultValue":""} */,
 username: undefined /* STRING */
};

let result = Resources["ContentLoaderFunctions"].PostJSON(params);

 

Question separated from Original topic.

Best answer by randerson-3

Sorry about my terribly slow response @Bikash_Panda 
You were correct in suggesting structuring the query part like that to get it onto multiple lines.

But this did not resolve the issue. I eventually found a way to do it.

The content loader function to use is PostText

and "Content-Type": "application/vnd.flux",

 

The data then comes back in CSV format, so I am converting it into an info table.

2 replies

19-Tanzanite
July 24, 2023

Is the question if the query is valid from a Flux perspective, or you know it's correct, but translating in a ThingWorx service is not working?

 

14-Alexandrite
July 27, 2023

Can you try arranging your flux query like following, 

 

'data = from(bucket: "thingworx")'+
'|> range(start: -60m, stop: -1m)'+
'|>.....................

 

I have used like this in another JS platform, and this works.

 

May be you can try from POSTMAN or simillar tool, just to avoid any intermediary issues.

randerson-314-AlexandriteAuthorAnswer
14-Alexandrite
November 11, 2023

Sorry about my terribly slow response @Bikash_Panda 
You were correct in suggesting structuring the query part like that to get it onto multiple lines.

But this did not resolve the issue. I eventually found a way to do it.

The content loader function to use is PostText

and "Content-Type": "application/vnd.flux",

 

The data then comes back in CSV format, so I am converting it into an info table.