cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

How to structure a query to InfluxDB with the Flux API

randerson-3
14-Alexandrite

How to structure a query to InfluxDB with the Flux API

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

3 REPLIES 3

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?

 

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.

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.

Top Tags