iobroker.influxdb 2.4.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +80 -23
- package/io-package.json +38 -44
- package/lib/Database.js +6 -5
- package/lib/DatabaseInfluxDB1x.js +18 -17
- package/lib/DatabaseInfluxDB2x.js +65 -70
- package/lib/aggregate.js +141 -37
- package/main.js +475 -169
- package/package.json +9 -9
|
@@ -12,7 +12,7 @@ class FakeConnectionPool {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
activatePool () {
|
|
15
|
-
this.connections.push(
|
|
15
|
+
this.connections.push('fakehost');
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
getHostsAvailable () {
|
|
@@ -42,11 +42,13 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
42
42
|
this.connect();
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
connect
|
|
46
|
-
const url = this.protocol
|
|
45
|
+
connect() {
|
|
46
|
+
const url = `${this.protocol}://${this.host}:${this.port}/`;
|
|
47
|
+
|
|
48
|
+
this.log.debug(`Connect InfluxDB1: ${url} [${this.database}]`);
|
|
47
49
|
|
|
48
50
|
this.connection = new InfluxClient.InfluxDB({
|
|
49
|
-
url
|
|
51
|
+
url,
|
|
50
52
|
token: this.token,
|
|
51
53
|
});
|
|
52
54
|
|
|
@@ -63,19 +65,19 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
63
65
|
this.request.activatePool();
|
|
64
66
|
}
|
|
65
67
|
|
|
66
|
-
async getDatabaseNames
|
|
67
|
-
this.log.debug(
|
|
68
|
+
async getDatabaseNames(callback) {
|
|
69
|
+
this.log.debug(`Organization being checked: ${this.organization}`);
|
|
68
70
|
|
|
69
71
|
try {
|
|
70
72
|
const organizations = await this.orgsApi.getOrgs({ org: this.organization });
|
|
71
|
-
this.log.debug(
|
|
73
|
+
this.log.debug(`Organizations: ${JSON.stringify(organizations)}`);
|
|
72
74
|
if (organizations && organizations.orgs && !organizations.orgs.length) {
|
|
73
75
|
throw new Error('No organizations exists or the token do not have proper permissions. Please check the token!')
|
|
74
76
|
}
|
|
75
77
|
this.organizationId = organizations.orgs[0].id;
|
|
76
78
|
|
|
77
79
|
const buckets = await this.bucketsApi.getBuckets({ orgID: this.organizationId });
|
|
78
|
-
this.log.debug(
|
|
80
|
+
this.log.debug(`Buckets: ${JSON.stringify(buckets)}`);
|
|
79
81
|
|
|
80
82
|
const foundDatabases = [];
|
|
81
83
|
|
|
@@ -90,21 +92,21 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
90
92
|
}
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
async getRetentionPolicyForDB
|
|
94
|
-
this.log.debug(
|
|
95
|
+
async getRetentionPolicyForDB(dbname, callback) {
|
|
96
|
+
this.log.debug(`Getting retention policy for ${dbname}`);
|
|
95
97
|
try {
|
|
96
98
|
const bucketData = await this.bucketsApi.getBucketsID({ bucketID: this.bucketIds[dbname] });
|
|
97
|
-
this.log.debug(
|
|
98
|
-
callback({
|
|
99
|
+
this.log.debug(`Found retention policy: ${bucketData.retentionRules[0].everySeconds} seconds`);
|
|
100
|
+
callback({time: bucketData.retentionRules[0].everySeconds });
|
|
99
101
|
} catch (error) {
|
|
100
102
|
this.log.error(error);
|
|
101
103
|
callback(null);
|
|
102
104
|
}
|
|
103
105
|
}
|
|
104
106
|
|
|
105
|
-
async applyRetentionPolicyToDB
|
|
107
|
+
async applyRetentionPolicyToDB(dbname, retention, callback_error) {
|
|
106
108
|
const shardGroupDuration = this.calculateShardGroupDuration(retention);
|
|
107
|
-
this.log.info(
|
|
109
|
+
this.log.info(`Applying retention policy for ${dbname} to ${!retention ? "infinity" : retention + " seconds"}. Shard Group Duration (calculated): ${shardGroupDuration} seconds`);
|
|
108
110
|
try {
|
|
109
111
|
await this.bucketsApi.patchBucketsID({
|
|
110
112
|
bucketID: this.bucketIds[dbname],
|
|
@@ -123,9 +125,9 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
|
|
126
|
-
async createDatabase
|
|
128
|
+
async createDatabase(dbname, callback_error) {
|
|
127
129
|
try {
|
|
128
|
-
this.log.info(
|
|
130
|
+
this.log.info(`Creating database ${dbname} for orgId ${this.organizationId}`);
|
|
129
131
|
const newBucket = await this.bucketsApi.postBuckets({
|
|
130
132
|
body: {
|
|
131
133
|
orgID: this.organizationId,
|
|
@@ -141,9 +143,9 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
141
143
|
}
|
|
142
144
|
}
|
|
143
145
|
|
|
144
|
-
async dropDatabase
|
|
146
|
+
async dropDatabase(dbname, callback_error) {
|
|
145
147
|
try {
|
|
146
|
-
this.log.info(
|
|
148
|
+
this.log.info(`Dropping database ${dbname} for orgId ${this.organizationId}`);
|
|
147
149
|
await this.bucketsApi.deleteBucketsID({ bucketID: this.bucketIds[dbname] });
|
|
148
150
|
|
|
149
151
|
callback_error(false);
|
|
@@ -153,8 +155,8 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
157
|
|
|
156
|
-
async writeSeries
|
|
157
|
-
this.log.debug(
|
|
158
|
+
async writeSeries(series, callback_error) {
|
|
159
|
+
this.log.debug(`Write series: ${JSON.stringify(series)}`);
|
|
158
160
|
|
|
159
161
|
const points = [];
|
|
160
162
|
for (const [pointId, valueSets] of Object.entries(series)) {
|
|
@@ -168,15 +170,15 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
168
170
|
try {
|
|
169
171
|
this.writeApi.writePoints(points);
|
|
170
172
|
await this.writeApi.flush();
|
|
171
|
-
this.log.debug(
|
|
173
|
+
this.log.debug(`Points written to ${this.database}`);
|
|
172
174
|
callback_error();
|
|
173
175
|
} catch (error) {
|
|
174
176
|
callback_error(error);
|
|
175
177
|
}
|
|
176
178
|
}
|
|
177
179
|
|
|
178
|
-
async writePoints
|
|
179
|
-
this.log.debug(
|
|
180
|
+
async writePoints(pointId, pointsToSend, callback_error) {
|
|
181
|
+
this.log.debug(`Write Points: ${pointId} pointstoSend:${JSON.stringify(pointsToSend)}`);
|
|
180
182
|
|
|
181
183
|
const points = [];
|
|
182
184
|
pointsToSend.forEach((values) => {
|
|
@@ -188,62 +190,62 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
188
190
|
try {
|
|
189
191
|
this.writeApi.writePoints(points);
|
|
190
192
|
await this.writeApi.flush();
|
|
191
|
-
this.log.debug(
|
|
193
|
+
this.log.debug(`Points written to ${this.database}`);
|
|
192
194
|
callback_error();
|
|
193
195
|
} catch (error) {
|
|
194
196
|
callback_error(error);
|
|
195
197
|
}
|
|
196
198
|
}
|
|
197
199
|
|
|
198
|
-
async writePoint
|
|
199
|
-
this.log.debug(
|
|
200
|
+
async writePoint(pointId, values, options, callback_error) {
|
|
201
|
+
this.log.debug(`Write Point: ${pointId} values:${JSON.stringify(values)} options: ${JSON.stringify(options)}`);
|
|
200
202
|
|
|
201
203
|
try {
|
|
202
204
|
this.writeApi.writePoint(this.stateValueToPoint(pointId, values));
|
|
203
205
|
await this.writeApi.flush();
|
|
204
|
-
this.log.debug(
|
|
206
|
+
this.log.debug(`Point written to ${this.database}`);
|
|
205
207
|
callback_error();
|
|
206
208
|
} catch (error) {
|
|
207
|
-
this.log.warn(
|
|
209
|
+
this.log.warn(`Point could not be written to database: ${this.database}`);
|
|
208
210
|
callback_error(error);
|
|
209
211
|
}
|
|
210
212
|
|
|
211
213
|
}
|
|
212
214
|
|
|
213
|
-
stateValueToPoint
|
|
215
|
+
stateValueToPoint(pointName, stateValue) {
|
|
214
216
|
let point = null;
|
|
215
217
|
|
|
216
218
|
if (this.useTags) {
|
|
217
219
|
point = new InfluxClient.Point(pointName)
|
|
218
220
|
.timestamp(stateValue.time)
|
|
219
|
-
.tag(
|
|
220
|
-
.tag(
|
|
221
|
-
.tag(
|
|
221
|
+
.tag('q', String(stateValue.q))
|
|
222
|
+
.tag('ack', String(stateValue.ack))
|
|
223
|
+
.tag('from', stateValue.from);
|
|
222
224
|
} else {
|
|
223
225
|
point = new InfluxClient.Point(pointName)
|
|
224
226
|
.timestamp(stateValue.time)
|
|
225
|
-
.floatField(
|
|
226
|
-
.booleanField(
|
|
227
|
-
.stringField(
|
|
227
|
+
.floatField('q', stateValue.q)
|
|
228
|
+
.booleanField('ack', stateValue.ack)
|
|
229
|
+
.stringField('from', stateValue.from);
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
switch (typeof stateValue.value) {
|
|
231
|
-
case
|
|
232
|
-
point.booleanField(
|
|
233
|
+
case 'boolean':
|
|
234
|
+
point.booleanField('value', stateValue.value);
|
|
233
235
|
break;
|
|
234
|
-
case
|
|
235
|
-
point.floatField(
|
|
236
|
+
case 'number':
|
|
237
|
+
point.floatField('value', parseFloat(stateValue.value));
|
|
236
238
|
break;
|
|
237
|
-
case
|
|
239
|
+
case 'string':
|
|
238
240
|
default:
|
|
239
|
-
point.stringField(
|
|
241
|
+
point.stringField('value', stateValue.value);
|
|
240
242
|
break;
|
|
241
243
|
}
|
|
242
244
|
return point;
|
|
243
245
|
}
|
|
244
246
|
|
|
245
|
-
query
|
|
246
|
-
this.log.debug(
|
|
247
|
+
query(query, callback) {
|
|
248
|
+
this.log.debug(`Query to execute: ${query}`);
|
|
247
249
|
let rows = [];
|
|
248
250
|
|
|
249
251
|
this.queryApi.queryRows(query, {
|
|
@@ -251,9 +253,9 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
251
253
|
const fields = tableMeta.toObject(row);
|
|
252
254
|
|
|
253
255
|
//Columns "_time" and "_value" are mapped to "time" and "value" for backwards compatibility
|
|
254
|
-
if (fields[
|
|
255
|
-
fields[
|
|
256
|
-
|
|
256
|
+
if (fields['_time'] !== null)
|
|
257
|
+
fields['time'] = fields['_time'];
|
|
258
|
+
|
|
257
259
|
//if (fields["_value"] !== null)
|
|
258
260
|
// fields["value"] = fields["_value"];
|
|
259
261
|
|
|
@@ -262,9 +264,9 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
262
264
|
// console.log(JSON.stringify(o, null, 2))
|
|
263
265
|
},
|
|
264
266
|
error (error) {
|
|
265
|
-
//Ignore errors that are related to an empty range. The handling of this currently seems
|
|
267
|
+
//Ignore errors that are related to an empty range. The handling of this currently seems inconsistent for flux.
|
|
266
268
|
//See also https://github.com/influxdata/flux/issues/3543
|
|
267
|
-
if (error.message.match(
|
|
269
|
+
if (error.message.match('.*cannot query an empty range.*'))
|
|
268
270
|
callback(null, rows);
|
|
269
271
|
else
|
|
270
272
|
callback(error, null);
|
|
@@ -278,8 +280,8 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
278
280
|
|
|
279
281
|
getMetaDataStorageType (callback) {
|
|
280
282
|
const queries = [
|
|
281
|
-
|
|
282
|
-
|
|
283
|
+
`import "influxdata/influxdb/schema" schema.tagKeys(bucket: "${this.database}")`,
|
|
284
|
+
`import "influxdata/influxdb/schema" schema.fieldKeys(bucket: "${this.database}")`,
|
|
283
285
|
];
|
|
284
286
|
|
|
285
287
|
this.queries(queries, (error, result) => {
|
|
@@ -287,21 +289,21 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
287
289
|
if (error) {
|
|
288
290
|
callback(error, null);
|
|
289
291
|
} else {
|
|
290
|
-
this.log.debug(
|
|
291
|
-
for (let i=0; i <= 1; i++) {
|
|
292
|
-
result[i].forEach(
|
|
292
|
+
this.log.debug(`Result of metadata storage type check: ${JSON.stringify(result)}`);
|
|
293
|
+
for (let i = 0; i <= 1; i++) {
|
|
294
|
+
result[i].forEach(row => {
|
|
293
295
|
switch(row._value) {
|
|
294
|
-
case
|
|
295
|
-
case
|
|
296
|
-
case
|
|
297
|
-
storageType =
|
|
296
|
+
case 'q':
|
|
297
|
+
case 'ack':
|
|
298
|
+
case 'from':
|
|
299
|
+
storageType = i == 0 ? 'tags' : 'fields';
|
|
298
300
|
return;
|
|
299
301
|
}
|
|
300
302
|
});
|
|
301
303
|
}
|
|
302
304
|
callback(null, storageType);
|
|
303
305
|
}
|
|
304
|
-
|
|
306
|
+
|
|
305
307
|
});
|
|
306
308
|
}
|
|
307
309
|
|
|
@@ -324,6 +326,7 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
324
326
|
});
|
|
325
327
|
});
|
|
326
328
|
}
|
|
329
|
+
|
|
327
330
|
let retError = null;
|
|
328
331
|
if (errors.length) {
|
|
329
332
|
retError = new Error(`${errors.length} Error happened while processing ${queries.length} queries`);
|
|
@@ -332,18 +335,10 @@ class DatabaseInfluxDB2x extends Database {
|
|
|
332
335
|
callback(retError, success ? collectedRows : null);
|
|
333
336
|
}
|
|
334
337
|
|
|
335
|
-
ping
|
|
336
|
-
//can't do much with interval, so ignoring it for compatibility reasons
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
this.healthApi.getHealth().then(result => {
|
|
340
|
-
resolve(result.status === 'pass' ? { online: true } : { online: false });
|
|
341
|
-
})
|
|
342
|
-
.catch(error => {
|
|
343
|
-
reject(error);
|
|
344
|
-
});
|
|
345
|
-
}));
|
|
346
|
-
return Promise.all(promises);
|
|
338
|
+
ping(interval) {
|
|
339
|
+
// can't do much with interval, so ignoring it for compatibility reasons
|
|
340
|
+
return this.healthApi.getHealth()
|
|
341
|
+
.then(result => result.status === 'pass' ? [{ online: true }] : [{ online: false }]);
|
|
347
342
|
}
|
|
348
343
|
}
|
|
349
344
|
|
package/lib/aggregate.js
CHANGED
|
@@ -22,16 +22,18 @@ function initAggregate(options) {
|
|
|
22
22
|
options.overallLength = 0;
|
|
23
23
|
|
|
24
24
|
// pre-fill the result with timestamps (add one before start and one after end)
|
|
25
|
-
for (let i = 0; i <= options.maxIndex + 2; i++){
|
|
25
|
+
for (let i = 0; i <= options.maxIndex + 2; i++) {
|
|
26
26
|
options.result[i] = {
|
|
27
|
-
val:
|
|
28
|
-
max:
|
|
29
|
-
min:
|
|
30
|
-
start:
|
|
31
|
-
end:
|
|
27
|
+
val: {ts: null, val: null},
|
|
28
|
+
max: {ts: null, val: null},
|
|
29
|
+
min: {ts: null, val: null},
|
|
30
|
+
start: {ts: null, val: null},
|
|
31
|
+
end: {ts: null, val: null}
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
if (options.aggregate === 'average')
|
|
34
|
+
if (options.aggregate === 'average') {
|
|
35
|
+
options.averageCount[i] = 0;
|
|
36
|
+
}
|
|
35
37
|
}
|
|
36
38
|
return options;
|
|
37
39
|
}
|
|
@@ -40,9 +42,12 @@ function aggregation(options, data) {
|
|
|
40
42
|
let index;
|
|
41
43
|
let preIndex;
|
|
42
44
|
for (let i = 0; i < data.length; i++) {
|
|
43
|
-
if (!data[i])
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
if (!data[i]) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (typeof data[i].ts !== 'number') {
|
|
49
|
+
data[i].ts = parseInt(data[i].ts, 10);
|
|
50
|
+
}
|
|
46
51
|
|
|
47
52
|
preIndex = Math.floor((data[i].ts - options.start) / options.step);
|
|
48
53
|
|
|
@@ -62,17 +67,29 @@ function aggregation(options, data) {
|
|
|
62
67
|
}
|
|
63
68
|
|
|
64
69
|
if (options.aggregate === 'max') {
|
|
65
|
-
if (!options.result[index].val.ts)
|
|
66
|
-
|
|
70
|
+
if (!options.result[index].val.ts) {
|
|
71
|
+
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
72
|
+
}
|
|
73
|
+
if (options.result[index].val.val === null || options.result[index].val.val < data[i].val) {
|
|
74
|
+
options.result[index].val.val = data[i].val;
|
|
75
|
+
}
|
|
67
76
|
} else if (options.aggregate === 'min') {
|
|
68
|
-
if (!options.result[index].val.ts)
|
|
69
|
-
|
|
77
|
+
if (!options.result[index].val.ts) {
|
|
78
|
+
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
79
|
+
}
|
|
80
|
+
if (options.result[index].val.val === null || options.result[index].val.val > data[i].val) {
|
|
81
|
+
options.result[index].val.val = data[i].val;
|
|
82
|
+
}
|
|
70
83
|
} else if (options.aggregate === 'average') {
|
|
71
|
-
if (!options.result[index].val.ts)
|
|
84
|
+
if (!options.result[index].val.ts) {
|
|
85
|
+
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
86
|
+
}
|
|
72
87
|
options.result[index].val.val += parseFloat(data[i].val);
|
|
73
88
|
options.averageCount[index]++;
|
|
74
89
|
} else if (options.aggregate === 'total') {
|
|
75
|
-
if (!options.result[index].val.ts)
|
|
90
|
+
if (!options.result[index].val.ts) {
|
|
91
|
+
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
92
|
+
}
|
|
76
93
|
options.result[index].val.val += parseFloat(data[i].val);
|
|
77
94
|
} else if (options.aggregate === 'minmax') {
|
|
78
95
|
if (options.result[index].min.ts === null) {
|
|
@@ -195,7 +212,6 @@ function finishAggregation(options) {
|
|
|
195
212
|
};
|
|
196
213
|
} // start, min, max == end
|
|
197
214
|
else {
|
|
198
|
-
|
|
199
215
|
options.result.splice(ii + 1, 0, {
|
|
200
216
|
ts: options.result[ii].min.ts,
|
|
201
217
|
val: options.result[ii].min.val
|
|
@@ -261,8 +277,8 @@ function finishAggregation(options) {
|
|
|
261
277
|
for (let k = options.result.length - 1; k >= 0; k--) {
|
|
262
278
|
if (options.result[k].val.ts) {
|
|
263
279
|
options.result[k] = {
|
|
264
|
-
ts:
|
|
265
|
-
val:
|
|
280
|
+
ts: options.result[k].val.ts,
|
|
281
|
+
val: options.result[k].val.val !== null ? Math.round(options.result[k].val.val / options.averageCount[k] * 100) / 100 : null
|
|
266
282
|
};
|
|
267
283
|
} else {
|
|
268
284
|
// no one value in this interval
|
|
@@ -282,16 +298,26 @@ function finishAggregation(options) {
|
|
|
282
298
|
}
|
|
283
299
|
}
|
|
284
300
|
}
|
|
301
|
+
|
|
285
302
|
beautify(options);
|
|
286
303
|
}
|
|
287
304
|
|
|
288
305
|
function beautify(options) {
|
|
289
306
|
let preFirstValue = null;
|
|
290
307
|
let postLastValue = null;
|
|
291
|
-
|
|
292
|
-
if (options.ignoreNull === '
|
|
293
|
-
|
|
294
|
-
|
|
308
|
+
|
|
309
|
+
if (options.ignoreNull === 'true') { // include nulls and replace them with last value
|
|
310
|
+
options.ignoreNull = true;
|
|
311
|
+
} else
|
|
312
|
+
if (options.ignoreNull === 'false') { // include nulls
|
|
313
|
+
options.ignoreNull = false;
|
|
314
|
+
} else
|
|
315
|
+
if (options.ignoreNull === '0') { // include nulls and replace them with 0
|
|
316
|
+
options.ignoreNull = 0;
|
|
317
|
+
} else
|
|
318
|
+
if (options.ignoreNull !== true && options.ignoreNull !== false && options.ignoreNull !== 0) {
|
|
319
|
+
options.ignoreNull = false;
|
|
320
|
+
}
|
|
295
321
|
|
|
296
322
|
// process null values, remove points outside the span and find first points after end and before start
|
|
297
323
|
for (let i = 0; i < options.result.length; i++) {
|
|
@@ -348,7 +374,7 @@ function beautify(options) {
|
|
|
348
374
|
if (firstY !== null) {
|
|
349
375
|
// interpolate
|
|
350
376
|
const y = preFirstValue.val + (firstY - preFirstValue.val) * (options.start - preFirstValue.ts) / (firstTS - preFirstValue.ts);
|
|
351
|
-
options.result.unshift({ts: options.start, val: y});
|
|
377
|
+
options.result.unshift({ts: options.start, val: y, i: true});
|
|
352
378
|
} else {
|
|
353
379
|
options.result.unshift({ts: options.start, val: null});
|
|
354
380
|
}
|
|
@@ -386,7 +412,7 @@ function beautify(options) {
|
|
|
386
412
|
if (lastY !== null) {
|
|
387
413
|
// make interpolation
|
|
388
414
|
const _y = lastY + (postLastValue.val - lastY) * (options.end - lastTS) / (postLastValue.ts - lastTS);
|
|
389
|
-
options.result.push({ts: options.end, val: _y});
|
|
415
|
+
options.result.push({ts: options.end, val: _y, i: true});
|
|
390
416
|
} else {
|
|
391
417
|
options.result.push({ts: options.end, val: null});
|
|
392
418
|
}
|
|
@@ -407,9 +433,8 @@ function beautify(options) {
|
|
|
407
433
|
}
|
|
408
434
|
}
|
|
409
435
|
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if ((options.count) && (options.result.length > options.count)) {
|
|
436
|
+
} else if (options.aggregate === 'none') {
|
|
437
|
+
if (options.count && options.result.length > options.count) {
|
|
413
438
|
options.result = options.result.slice(0, options.count);
|
|
414
439
|
}
|
|
415
440
|
}
|
|
@@ -443,8 +468,10 @@ function sendResponse(adapter, msg, options, data, startTime) {
|
|
|
443
468
|
}
|
|
444
469
|
}
|
|
445
470
|
options.result = aggregateData.result;
|
|
471
|
+
|
|
446
472
|
beautify(options);
|
|
447
|
-
|
|
473
|
+
|
|
474
|
+
if (options.aggregate === 'none' && options.count && options.result.length > options.count) {
|
|
448
475
|
options.result = options.result.slice(0, options.count);
|
|
449
476
|
}
|
|
450
477
|
aggregateData.result = options.result;
|
|
@@ -455,10 +482,11 @@ function sendResponse(adapter, msg, options, data, startTime) {
|
|
|
455
482
|
aggregateData.result = options.result;
|
|
456
483
|
}
|
|
457
484
|
|
|
458
|
-
adapter.log.debug(
|
|
485
|
+
adapter.log.debug(`Send: ${aggregateData.result.length} of: ${aggregateData.sourceLength} in: ${Date.now() - startTime}ms`);
|
|
486
|
+
|
|
459
487
|
adapter.sendTo(msg.from, msg.command, {
|
|
460
|
-
result:
|
|
461
|
-
step:
|
|
488
|
+
result: aggregateData.result,
|
|
489
|
+
step: aggregateData.step,
|
|
462
490
|
error: null,
|
|
463
491
|
sessionId: options.sessionId
|
|
464
492
|
}, msg.callback);
|
|
@@ -468,8 +496,84 @@ function sendResponse(adapter, msg, options, data, startTime) {
|
|
|
468
496
|
}
|
|
469
497
|
}
|
|
470
498
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
499
|
+
function sendResponseCounter(adapter, msg, options, data, startTime) {
|
|
500
|
+
// data
|
|
501
|
+
// 1586713810000 100
|
|
502
|
+
// 1586713810010 200
|
|
503
|
+
// 1586713810040 500
|
|
504
|
+
// 1586713810050 0
|
|
505
|
+
// 1586713810090 400
|
|
506
|
+
// 1586713810100 0
|
|
507
|
+
// 1586713810110 100
|
|
508
|
+
if (typeof data === 'string') {
|
|
509
|
+
adapter.log.error(data);
|
|
510
|
+
return adapter.sendTo(msg.from, msg.command, {
|
|
511
|
+
result: [],
|
|
512
|
+
error: data,
|
|
513
|
+
sessionId: options.sessionId
|
|
514
|
+
}, msg.callback);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (data[0] && data[1]) {
|
|
518
|
+
// first | start | afterFirst | ...... | last | end | afterLast
|
|
519
|
+
// 5 | | 8 | 9 | 1 | 3 | | 5
|
|
520
|
+
// | 5+(8-5/tsDiff) | | 9 | 1 | | 3+(5-3/tsDiff) |
|
|
521
|
+
// (9 - 6.5) + (4 - 1)
|
|
522
|
+
|
|
523
|
+
if (data[1].ts === options.start) {
|
|
524
|
+
data.splice(0, 1);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (data[0].ts < options.start && data[0].val > data[1].val) {
|
|
528
|
+
data.splice(0, 1);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// interpolate from first to start time
|
|
532
|
+
if (data[0].ts < options.start) {
|
|
533
|
+
const val = data[0].val + (data[1].val - data[0].val) * ((options.start - data[0].ts) / (data[1].ts - data[0].ts));
|
|
534
|
+
data.splice(0, 1);
|
|
535
|
+
data.unshift({ts: options.start, val, i: true});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
if (data[data.length - 2].ts === options.end) {
|
|
539
|
+
data.splice(-1, 1);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const veryLast = data[data.length - 1];
|
|
543
|
+
const beforeLast = data[data.length - 2];
|
|
544
|
+
|
|
545
|
+
// interpolate from end time to last
|
|
546
|
+
if (options.end < veryLast.ts) {
|
|
547
|
+
const val = beforeLast.val + (veryLast.val - beforeLast.val) * ((options.end - beforeLast.ts) / (veryLast.ts - beforeLast.ts));
|
|
548
|
+
data.splice(-1, 1);
|
|
549
|
+
data.push({ts: options.end, val, i: true});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// at this point we expect [6.5, 9, 1, 4]
|
|
553
|
+
// at this point we expect [150, 200, 500, 0, 400, 0, 50]
|
|
554
|
+
let val = data[data.length - 1].val;
|
|
555
|
+
let sum = 0;
|
|
556
|
+
for (let i = data.length - 2; i >= 0; i--) {
|
|
557
|
+
if (data[i].val < val) {
|
|
558
|
+
sum += val - data[i].val;
|
|
559
|
+
}
|
|
560
|
+
val = data[i].val;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
adapter.sendTo(msg.from, msg.command, {
|
|
564
|
+
result: sum,
|
|
565
|
+
error: null,
|
|
566
|
+
sessionId: options.sessionId
|
|
567
|
+
}, msg.callback);
|
|
568
|
+
} else {
|
|
569
|
+
adapter.log.info('No Data');
|
|
570
|
+
adapter.sendTo(msg.from, msg.command, {result: 0, step: null, error: null, sessionId: options.sessionId}, msg.callback);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
module.exports.sendResponseCounter = sendResponseCounter;
|
|
575
|
+
module.exports.sendResponse = sendResponse;
|
|
576
|
+
module.exports.initAggregate = initAggregate;
|
|
577
|
+
module.exports.aggregation = aggregation;
|
|
578
|
+
module.exports.beautify = beautify;
|
|
579
|
+
module.exports.finishAggregation = finishAggregation;
|