Skip to content

Commit

Permalink
* Added support aggregate and count for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Apr 14, 2022
1 parent f8615eb commit 93eeff2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Call in browser `http://ipaddress:8087/help` to get the help about API. The resu
"query": "http://ipaddress:8087/query/stateID1,stateID2/?prettyPrint"
"query": "http://ipaddress:8087/query/stateID1,stateID2/?noHistory=true&prettyPrint"
"query": "http://ipaddress:8087/query/stateID1,stateID2/?dateFrom=2019-06-06T12:00:00.000Z&d&prettyPrint"
"query": "http://ipaddress:8087/query/stateID1,stateID2/?dateFrom=2019-06-06T12:00:00.000Z&dateTo=2019-06-06T12:00:00.000Z&prettyPrint"
"query": "http://ipaddress:8087/query/stateID1,stateID2/?dateFrom=2019-06-06T12:00:00.000Z&dateTo=2019-06-06T12:00:00.000Z&aggregate=minmax&count=2000&prettyPrint"
}
```

Expand Down Expand Up @@ -585,9 +585,12 @@ If no data source was specified or the noHistory parameter is passed, then only
```
<!--
Placeholder for the next version (at the beginning of the line):
### __WORK IN PROGRESS__
### **WORK IN PROGRESS**
-->
## Changelog
### **WORK IN PROGRESS**
* Added support aggregate and count for queries

### 2.6.4 (2022-03-17)
* (Apollon77) Optimize performance, especially when using names instead of object ids

Expand Down
55 changes: 37 additions & 18 deletions lib/simpleapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,18 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {
dateFrom = Date.parse(range.from);
dateTo = Date.parse(range.to);
}
const options = {
start: dateFrom,
end: dateTo,
aggregate: values.aggregate || 'onchange',
};

if (values.count) {
options.count = parseInt(values.count, 10);
}
if (values.step) {
options.step = parseInt(values.step, 10);
}

oId = [];
Array.isArray(targets) && targets.forEach(t => oId.push(t.target));
Expand All @@ -468,11 +480,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {

that.adapter.sendTo(that.adapter.config.dataSource, 'getHistory', {
id: targets[b].target,
options: {
start: dateFrom,
end: dateTo,
aggregate: 'onchange'
}
options
}, (result, step, error) => {
if (!error) status = 200;

Expand Down Expand Up @@ -535,7 +543,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {
break;

case 'annotations':
// iobroker does not support annontations
// iobroker does not support annotations
that.adapter.log.debug('[ANNOTATIONS]');
doResponse(res, responseType, 200, headers, [], values.prettyPrint);
break;
Expand Down Expand Up @@ -1421,28 +1429,39 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {
if (values.dateTo) {
dateTo = Date.parse(values.dateTo);
}
const options = {
start: dateFrom,
end: dateTo,
aggregate: values.aggregate || 'onchange',
};

if (values.count) {
options.count = parseInt(values.count, 10);
}
if (values.step) {
options.step = parseInt(values.step, 10);
}

if (!oId.length || !oId[0]) {
doResponse(res, responseType, status, headers, {error: 'no datapoints given'}, values.prettyPrint);
break;
}
let tcnt = oId.length;
let tCnt = oId.length;
response = [];
let tDone = false;

for (let b = 0; b < oId.length; b++) {
const resIndex = b;
if (that.adapter.config.dataSource && !(values.noHistory && values.noHistory === 'true')) {
that.adapter.log.debug(`Read data from: ${that.adapter.config.dataSource}`);

that.adapter.sendTo(that.adapter.config.dataSource, 'getHistory', {
id: oId[b],
options: {
start: dateFrom,
end: dateTo,
aggregate: 'onchange'
}
options
}, (result, step, error) => {
if (!error) status = 200;
if (!error) {
status = 200;
}

that.adapter.log.debug(`[QUERY] sendTo result = ${JSON.stringify(result)}`);

Expand All @@ -1457,7 +1476,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {

response[resIndex] = element;

if (!--tcnt && !tDone) {
if (!--tCnt && !tDone) {
tDone = true;
that.adapter.log.debug(`[QUERY] response = ${JSON.stringify(response)}`);
doResponse(res, responseType, status, headers, response, values.prettyPrint);
Expand All @@ -1472,7 +1491,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {
element.datapoints = [];

if (err) {
tcnt = 0;
tCnt = 0;
if (err instanceof Error) {
err = err.message;
}
Expand All @@ -1492,7 +1511,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {

response[resIndex] = element;

if (!--tcnt && !tDone) {
if (!--tCnt && !tDone) {
that.adapter.log.debug(`[QUERY] response = ${JSON.stringify(response)}`);
tDone = true;
doResponse(res, responseType, status, headers, response, values.prettyPrint);
Expand All @@ -1501,7 +1520,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {
});
}
}
if (!tcnt && !tDone) {
if (!tCnt && !tDone) {
tDone = true;
doResponse(res, responseType, status, headers, response, values.prettyPrint);
}
Expand Down Expand Up @@ -1537,7 +1556,7 @@ function SimpleAPI(server, webSettings, adapter, instanceSettings, app) {
_obj.objects = `${request}/objects?pattern=system.adapter.admin.0*&prettyPrint${auth ? `&${auth}` : ''}`;
_obj.states = `${request}/states?pattern=system.adapter.admin.0*&prettyPrint${auth ? `&${auth}` : ''}`;
_obj.search = `${request}/search?pattern=system.adapter.admin.0*&prettyPrint${auth ? `&${auth}` : ''}`;
_obj.query = `${request}/query/stateID1,stateID2/?dateFrom=2019-06-06T12:00:00.000Z&dateTo=2019-06-06T12:00:00.000Z&noHistory=false&prettyPrint${auth ? `&${auth}` : ''}`;
_obj.query = `${request}/query/stateID1,stateID2/?dateFrom=2019-06-06T12:00:00.000Z&dateTo=2019-06-06T12:00:00.000Z&noHistory=false&aggregate=minmax&count=3000&prettyPrint${auth ? `&${auth}` : ''}`;

doResponse(res, responseType, status, headers, _obj, true);
break;
Expand Down

0 comments on commit 93eeff2

Please sign in to comment.