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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Querying influx DB to get the standard deviation for a property's history

Janicen
16-Pearl

Querying influx DB to get the standard deviation for a property's history

Hi, All

 

I have queried influx db and got min, max and other aggregate functions for some properties. I am however in need of figuring out how to query influx in order to get the standard deviation for a property's history. Does anyone perhaps know an article that I can reference? If so please drop the link in here.

 

Many Thanks.

3 REPLIES 3
VladimirN
24-Ruby II
(To:Janicen)

Hi,

 

I am trying to query influx db by means of writing a flux query on thingworx and getting the result. The key thing here is that it has to be on a THINGWORX service in order for me visualize the results on a mashup.

This is what I have written thus far: 


let fluxQuery = 'from(bucket: "thingworx")' +
'|> range(start: ' + start + ', stop: ' + end + ')' +
'|> filter(fn: (r) => ' + Measurements_Inputs.map(m => 'r["_measurement"] == "' + m + '"').join(' or ') + ')' +
'|> filter(fn: (r) => ' + Fields.map(f => 'r["_field"] == "' + f + '"').join(' or ') + ')';
var headers = {
"Content-Type": "application/vnd.flux",
"Authorization": "Token "+token // Replace with your actual token
};

let params = {
headers: headers,
url: 'http://localhost:8086/api/v2/query?org=1worx',
content: fluxQuery,
method: 'POST'
};
let jresult = Resources["ContentLoaderFunctions"].PostText(params);

var csvData = jresult;


var dataObjects = csvToArrayOfObjects(csvData);


result = dataObjects;

 

function csvToArrayOfObjects(csv) {
var lines = csv.split("\n");
// Update the headers array to match the exact order and names of your CSV headers
var headers = ["","result", "table", "_start", "_stop", "_time", "_value", "_field", "_measurement", "valuestreamname"];
var headerLine = lines[0].trim(); // Save the header line for comparison
var result = [];

for (var i = 1; i < lines.length; i++) {
var line = lines[i].trim();

// Skip non-CSV lines and repeated header lines
if (line === headerLine || line.startsWith("^Authorization=") || line.startsWith("ResponseStatus =") || line.startsWith("ResponseHeaders =")) {
continue;
}

var obj = {};
var currentline = line.split(",");

if (currentline.length >= headers.length) {
for (var j = 0; j < headers.length; j++) {
var value = currentline[j].trim(); // Get value
if (headers[j] === "_value") {
// Convert _value to a float if it's numeric
obj[headers[j]] = isNaN(parseFloat(value)) ? value : parseFloat(value);
} else {
obj[headers[j]] = value;
}
}
result.push(obj);
}
}
return result;
}

 

This code returns an empty array. How can I get the properties history by means of a flux query?

result of dataObjects.png

image 1 is the result of dataObjects

result of csvData.png

  

Image 2 is the result of csvData

slangley
23-Emerald II
(To:Janicen)

Hi @Janicen.

 

Have you taken a look at the Influxdb APIs?  We can send you a link if you provide the version of Influx you're running.

 

Regards.

 

--Sharon

Announcements


Top Tags