Use the API to query a set of tagged points for rollups over a time period
-
Someone recently asked over the phone for the pure JavaScript example of getting a set of point values through the API based on tags. The solution (currently) is to query the data points first, then compose a list to submit to the point-values endpoint, like,
var token = "Token auth!"; var baseUrl = 'http://localhost:8082/rest'; function newHttpRequest(method, url, headers) { var http = new XMLHttpRequest(); http.open(method, url, true); http.setRequestHeader("Accept", "application/json") http.setRequestHeader("Authorization", "Bearer " + token) if(headers != null) { for(var header in headers) http.setRequestHeader(header, headers[header]); } return http; } valuesRequestBody = { "dateTimeFormat": "YYYY-MM-dd'T'HH:mm:ss.SSSZ", "fields": [ "XID", "VALUE" ], "from": "2018-10-16T18:43:20.111Z", "limit": 1000, "timePeriod": { "periods": 1, "type": "HOURS" }, "timezone": "America/Denver", "to": "2018-10-19T18:43:20.111Z", "truncate": true, "xids": [ ] } getDataPoints = newHttpRequest('GET', baseUrl + '/v2/data-points?eq(tags.device,example)&limit(100)', null); getDataPoints.onreadystatechange = function() { if (getDataPoints.readyState == XMLHttpRequest.DONE) { var dataPoints = JSON.parse(this.responseText).items; var dpXids = [] for(var k = 0; k < dataPoints.length; k+=1) dpXids.push(dataPoints[k].xid); valuesRequestBody.xids = dpXids; getPointValues.send(JSON.stringify(valuesRequestBody)); } } getPointValues = newHttpRequest('POST', baseUrl + '/v2/point-values/multiple-arrays/time-period/ALL', {"Content-Type": "application/json"}); getPointValues.onreadystatechange = function() { if (getPointValues.readyState == XMLHttpRequest.DONE) { //var response = JSON.parse(getPointValues.responseText); //Then do stuff console.log(getPointValues.responseText); } } getDataPoints.send(null);
This JavaScript will query the data points for up to 100 with the device tag 'example' and then request up to 1000 values for each (because it's a multiple arrays, single arrays the limit applies to all), where a 'value' in this context is a one hour rollup of ALL statistics options.