iobroker.sql 4.6.0 → 4.8.0
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/README.md +25 -21
- package/build/lib/connection-factory.js +19 -0
- package/build/lib/connection-factory.js.map +1 -1
- package/build/lib/help.js +23 -5
- package/build/lib/help.js.map +1 -1
- package/build/lib/mssql.js +149 -65
- package/build/lib/mssql.js.map +1 -1
- package/build/lib/postgresql-client.js +57 -1
- package/build/lib/postgresql-client.js.map +1 -1
- package/build/lib/sql-client.js +26 -0
- package/build/lib/sql-client.js.map +1 -1
- package/build/lib/sql-string.js +24 -0
- package/build/lib/sql-string.js.map +1 -1
- package/build/lib/standardHistory.js +303 -0
- package/build/lib/standardHistory.js.map +1 -0
- package/build/main.js +290 -75
- package/build/main.js.map +1 -1
- package/io-package.json +27 -27
- package/package.json +3 -1
package/build/main.js
CHANGED
|
@@ -55,6 +55,7 @@ const postgresql_client_1 = require("./lib/postgresql-client");
|
|
|
55
55
|
const sqlite3_client_1 = require("./lib/sqlite3-client");
|
|
56
56
|
const errors_1 = require("./lib/errors");
|
|
57
57
|
const help_1 = require("./lib/help");
|
|
58
|
+
const standardHistory_1 = require("./lib/standardHistory");
|
|
58
59
|
const write_buffer_1 = require("./lib/write-buffer");
|
|
59
60
|
const SQLFuncs = {
|
|
60
61
|
mssql: {
|
|
@@ -208,6 +209,10 @@ const KEPT_LOG_INTERVAL = 60_000;
|
|
|
208
209
|
const MAX_RAW_ENTRIES = 5000;
|
|
209
210
|
/** How often an unchanged connection error is repeated as error in the log */
|
|
210
211
|
const REPEATED_ERROR_INTERVAL = 3_600_000;
|
|
212
|
+
/** Rows per batch when an aggregated PostgreSQL read is streamed instead of buffered */
|
|
213
|
+
const STREAM_BATCH_SIZE = 2000;
|
|
214
|
+
/** How long `aggregate` and `lastChange` wait for the answer of the own `getHistory` */
|
|
215
|
+
const STANDARD_TIMEOUT = 120_000;
|
|
211
216
|
function sortByTs(a, b) {
|
|
212
217
|
const aTs = a.ts;
|
|
213
218
|
const bTs = b.ts;
|
|
@@ -224,6 +229,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
224
229
|
finished = false;
|
|
225
230
|
sqlConnected = null;
|
|
226
231
|
multiRequests = true;
|
|
232
|
+
/** The driver cannot stream (pg-native, or pg-cursor not installed): read buffered without trying again */
|
|
233
|
+
streamingUnavailable = false;
|
|
227
234
|
subscribeAll = false;
|
|
228
235
|
clientPool = null;
|
|
229
236
|
reconnectTimeout = null;
|
|
@@ -232,7 +239,10 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
232
239
|
bufferChecker = null;
|
|
233
240
|
activeConnections = 0;
|
|
234
241
|
poolBorrowGuard = []; // required until https://github.com/intellinote/sql-client/issues/7 is fixed
|
|
235
|
-
|
|
242
|
+
/** Borrow/return of every connection on debug level; the texts are only built when debug is on. */
|
|
243
|
+
get logConnectionUsage() {
|
|
244
|
+
return this.#isDebug();
|
|
245
|
+
}
|
|
236
246
|
postgresDbCreated = false;
|
|
237
247
|
lockTasks = false;
|
|
238
248
|
sqlFuncs = null;
|
|
@@ -367,6 +377,13 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
367
377
|
},
|
|
368
378
|
});
|
|
369
379
|
}
|
|
380
|
+
/**
|
|
381
|
+
* Whether debug lines are written at all. A template string passed to `log.debug()` is built before the
|
|
382
|
+
* call, also when the level is `info`; on the paths that run for every value, check this first.
|
|
383
|
+
*/
|
|
384
|
+
#isDebug() {
|
|
385
|
+
return this.log.level === 'debug' || this.log.level === 'silly';
|
|
386
|
+
}
|
|
370
387
|
/**
|
|
371
388
|
* Log a statement on debug level.
|
|
372
389
|
*
|
|
@@ -376,7 +393,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
376
393
|
* @param statement plain SQL or SQL with values
|
|
377
394
|
*/
|
|
378
395
|
#debugStatement(statement) {
|
|
379
|
-
if (this
|
|
396
|
+
if (this.#isDebug()) {
|
|
380
397
|
this.log.debug((0, statement_1.statementToString)(statement));
|
|
381
398
|
}
|
|
382
399
|
}
|
|
@@ -868,7 +885,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
868
885
|
this.resetConnectionError();
|
|
869
886
|
this.log.info(`Connected to ${this.config.dbtype}`);
|
|
870
887
|
// read all DB IDs and all FROM ids
|
|
871
|
-
this.getAllIds(() => this.getAllFroms(callback));
|
|
888
|
+
this.#readNameColumnTypes(() => this.getAllIds(() => this.getAllFroms(callback)));
|
|
872
889
|
}
|
|
873
890
|
});
|
|
874
891
|
}
|
|
@@ -1394,6 +1411,12 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1394
1411
|
else if (msg.command === 'getCounter') {
|
|
1395
1412
|
this.getCounterDiff(msg);
|
|
1396
1413
|
}
|
|
1414
|
+
else if (msg.command === 'aggregate') {
|
|
1415
|
+
void this.answerStandard(msg, standardHistory_1.aggregate);
|
|
1416
|
+
}
|
|
1417
|
+
else if (msg.command === 'lastChange') {
|
|
1418
|
+
void this.answerStandard(msg, standardHistory_1.lastChange);
|
|
1419
|
+
}
|
|
1397
1420
|
else if (msg.command === 'test') {
|
|
1398
1421
|
void this.testConnection(msg);
|
|
1399
1422
|
}
|
|
@@ -1809,6 +1832,39 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1809
1832
|
// the converted value must not replace it.
|
|
1810
1833
|
this.pushValueIntoDB(_id, { ...state, val }, false, false, cb);
|
|
1811
1834
|
}
|
|
1835
|
+
/**
|
|
1836
|
+
* MS SQL: find out whether the `name` columns are still `varchar` (tables created before #588), so that the names
|
|
1837
|
+
* are compared with the right kind of literal (see `varcharNames` in lib/mssql.ts). Other databases: nothing to do.
|
|
1838
|
+
* An error is logged and the start goes on with `N'...'` literals, as for new tables.
|
|
1839
|
+
*
|
|
1840
|
+
* @param cb called when done
|
|
1841
|
+
*/
|
|
1842
|
+
#readNameColumnTypes(cb) {
|
|
1843
|
+
if (this.config.dbtype !== 'mssql') {
|
|
1844
|
+
return cb();
|
|
1845
|
+
}
|
|
1846
|
+
const query = MSSQL.getNameColumnTypes(this.config.dbname);
|
|
1847
|
+
this.borrowClientFromPool((err, client) => {
|
|
1848
|
+
if (err || !client) {
|
|
1849
|
+
this.returnClientToPool(client);
|
|
1850
|
+
this.log.warn(`Cannot read the column types of the tables: ${err?.message || 'no client'}`);
|
|
1851
|
+
return cb();
|
|
1852
|
+
}
|
|
1853
|
+
client.execute(query, (err, rows) => {
|
|
1854
|
+
this.returnClientToPool(client);
|
|
1855
|
+
if (err) {
|
|
1856
|
+
this.log.warn(`Cannot read the column types of the tables: ${err.message}`);
|
|
1857
|
+
}
|
|
1858
|
+
else {
|
|
1859
|
+
MSSQL.setNameColumnTypes(this.config.dbname, rows || []);
|
|
1860
|
+
if (rows?.some(row => !String(row.type).toLowerCase().startsWith('n'))) {
|
|
1861
|
+
this.log.debug('The tables were created by an older version (varchar names). Names with characters outside the code page of the database are stored as "?". The README explains how to convert the tables.');
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
cb();
|
|
1865
|
+
});
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1812
1868
|
getAllIds(cb) {
|
|
1813
1869
|
const query = this.sqlFuncs.getIdSelect(this.config.dbname);
|
|
1814
1870
|
this.#debugStatement(query);
|
|
@@ -2119,10 +2175,14 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2119
2175
|
}
|
|
2120
2176
|
task.cb?.();
|
|
2121
2177
|
task.cb = null;
|
|
2122
|
-
|
|
2178
|
+
// The queue has one entry per value, not per datapoint: every value that arrives before the type
|
|
2179
|
+
// of its datapoint is known waits here. A pause of 50 ms per entry limited the start with many
|
|
2180
|
+
// datapoints to 20 values per second (2000 datapoints: more than a minute and a half, with the
|
|
2181
|
+
// values piling up in memory). The lookups still run one after the other.
|
|
2182
|
+
setImmediate(() => {
|
|
2123
2183
|
this.tasksReadType.shift();
|
|
2124
2184
|
this.processReadTypes();
|
|
2125
|
-
}
|
|
2185
|
+
});
|
|
2126
2186
|
}
|
|
2127
2187
|
prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
2128
2188
|
if (!this.sqlDPs[id]) {
|
|
@@ -2254,12 +2314,17 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2254
2314
|
if (!this.sqlDPs[id] || !state) {
|
|
2255
2315
|
return cb?.();
|
|
2256
2316
|
}
|
|
2257
|
-
|
|
2317
|
+
// runs for every value: no JSON.stringify unless the line is written
|
|
2318
|
+
if (this.#isDebug()) {
|
|
2319
|
+
this.log.debug(`pushValueIntoDB called for ${id} (type: ${this.sqlDPs[id].type}, ID: ${this.sqlDPs[id].index}) and state: ${JSON.stringify(state)}`);
|
|
2320
|
+
}
|
|
2258
2321
|
this.prepareTaskCheckTypeAndDbId(id, state, isCounter, (err) => {
|
|
2259
2322
|
if (!this.sqlDPs[id]) {
|
|
2260
2323
|
return cb?.();
|
|
2261
2324
|
}
|
|
2262
|
-
|
|
2325
|
+
if (this.#isDebug()) {
|
|
2326
|
+
this.log.debug(`pushValueIntoDB-prepareTaskCheckTypeAndDbId RESULT for ${id} (type: ${this.sqlDPs[id].type}, ID: ${this.sqlDPs[id].index}) and state: ${JSON.stringify(state)}: ${err}`);
|
|
2327
|
+
}
|
|
2263
2328
|
if (err) {
|
|
2264
2329
|
return cb?.(err);
|
|
2265
2330
|
}
|
|
@@ -2399,8 +2464,16 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2399
2464
|
#keptErrors = new WeakSet();
|
|
2400
2465
|
storeCached(onlyId, cb) {
|
|
2401
2466
|
let count = 0;
|
|
2402
|
-
|
|
2403
|
-
|
|
2467
|
+
// With `onlyId` (a full buffer, for every value with maxLength 0) only that datapoint: looping
|
|
2468
|
+
// over all datapoints to find it cost O(n) per value.
|
|
2469
|
+
const ids = onlyId !== undefined
|
|
2470
|
+
? Object.prototype.hasOwnProperty.call(this.sqlDPs, onlyId)
|
|
2471
|
+
? [onlyId]
|
|
2472
|
+
: []
|
|
2473
|
+
: Object.keys(this.sqlDPs);
|
|
2474
|
+
for (const id of ids) {
|
|
2475
|
+
// `ids` is a snapshot; `for...in` skipped datapoints that were deleted during the loop
|
|
2476
|
+
if (!this.sqlDPs[id]) {
|
|
2404
2477
|
continue;
|
|
2405
2478
|
}
|
|
2406
2479
|
const _settings = this.sqlDPs[id].config || {};
|
|
@@ -2874,32 +2947,153 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2874
2947
|
client.execute(query, (err, rows /* , fields */) => {
|
|
2875
2948
|
this.returnClientToPool(client);
|
|
2876
2949
|
if (!err && rows) {
|
|
2877
|
-
|
|
2878
|
-
if (typeof rows[c].ts === 'string') {
|
|
2879
|
-
rows[c].ts = parseInt(rows[c].ts, 10);
|
|
2880
|
-
}
|
|
2881
|
-
if (this.common?.loglevel === 'debug') {
|
|
2882
|
-
rows[c].date = new Date(rows[c].ts);
|
|
2883
|
-
}
|
|
2884
|
-
if (options.ack) {
|
|
2885
|
-
rows[c].ack = !!rows[c].ack;
|
|
2886
|
-
}
|
|
2887
|
-
if (typeof rows[c].val === 'number' && isFinite(rows[c].val) && options.round) {
|
|
2888
|
-
rows[c].val = Math.round(rows[c].val * options.round) / options.round;
|
|
2889
|
-
}
|
|
2890
|
-
if (options.id && this.sqlDPs[options.id] && this.sqlDPs[options.id].type === 2) {
|
|
2891
|
-
// 2 === boolean
|
|
2892
|
-
rows[c].val = !!rows[c].val;
|
|
2893
|
-
}
|
|
2894
|
-
if (options.addId && !rows[c].id && options.id) {
|
|
2895
|
-
rows[c].id = options.id;
|
|
2896
|
-
}
|
|
2897
|
-
}
|
|
2950
|
+
this.#normalizeRows(rows, options);
|
|
2898
2951
|
}
|
|
2899
2952
|
callback?.(err, rows);
|
|
2900
2953
|
});
|
|
2901
2954
|
});
|
|
2902
2955
|
}
|
|
2956
|
+
/**
|
|
2957
|
+
* Prepare the rows of a history read in place: numeric `ts`, `date` for the debug log, `ack` as boolean,
|
|
2958
|
+
* rounding, boolean values of boolean datapoints and `id` for `addId`. The buffered and the streamed
|
|
2959
|
+
* read use the same code, so both deliver the same rows.
|
|
2960
|
+
*
|
|
2961
|
+
* @param rows rows as the driver returned them
|
|
2962
|
+
* @param options options of the read
|
|
2963
|
+
*/
|
|
2964
|
+
#normalizeRows(rows, options) {
|
|
2965
|
+
const isDebug = this.common?.loglevel === 'debug';
|
|
2966
|
+
const isBoolean = !!(options.id && this.sqlDPs[options.id] && this.sqlDPs[options.id].type === 2);
|
|
2967
|
+
for (let c = 0; c < rows.length; c++) {
|
|
2968
|
+
if (typeof rows[c].ts === 'string') {
|
|
2969
|
+
rows[c].ts = parseInt(rows[c].ts, 10);
|
|
2970
|
+
}
|
|
2971
|
+
if (isDebug) {
|
|
2972
|
+
rows[c].date = new Date(rows[c].ts);
|
|
2973
|
+
}
|
|
2974
|
+
if (options.ack) {
|
|
2975
|
+
rows[c].ack = !!rows[c].ack;
|
|
2976
|
+
}
|
|
2977
|
+
if (typeof rows[c].val === 'number' && isFinite(rows[c].val) && options.round) {
|
|
2978
|
+
rows[c].val = Math.round(rows[c].val * options.round) / options.round;
|
|
2979
|
+
}
|
|
2980
|
+
if (isBoolean) {
|
|
2981
|
+
// 2 === boolean
|
|
2982
|
+
rows[c].val = !!rows[c].val;
|
|
2983
|
+
}
|
|
2984
|
+
if (options.addId && !rows[c].id && options.id) {
|
|
2985
|
+
rows[c].id = options.id;
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
/**
|
|
2990
|
+
* Whether an aggregated read of one datapoint can be streamed from PostgreSQL instead of buffered (#516).
|
|
2991
|
+
*
|
|
2992
|
+
* The query of an aggregated read with a start returns every row in one `ORDER BY ts`, with at most one
|
|
2993
|
+
* row before the start and one after the end. Fed batch by batch, `aggregation()` then produces exactly
|
|
2994
|
+
* what it produces for the whole array, because all its state lives in the options. Values from the RAM
|
|
2995
|
+
* cache would have to be merged in by time, so a read with cached values stays buffered.
|
|
2996
|
+
*
|
|
2997
|
+
* `integralTotal` stays buffered: `aggregation()` adds a row from far before the start at the end of the
|
|
2998
|
+
* call that contains it, and `integralTotal` keeps one series in the order the rows were added.
|
|
2999
|
+
*
|
|
3000
|
+
* @param options options of the read
|
|
3001
|
+
* @param cachedValues number of values of this datapoint in the RAM cache
|
|
3002
|
+
* @param includesInFlightData whether values are being written right now
|
|
3003
|
+
*/
|
|
3004
|
+
#canStream(options, cachedValues, includesInFlightData) {
|
|
3005
|
+
return (this.config.dbtype === 'postgresql' &&
|
|
3006
|
+
this.multiRequests &&
|
|
3007
|
+
!this.streamingUnavailable &&
|
|
3008
|
+
!!options.start &&
|
|
3009
|
+
!cachedValues &&
|
|
3010
|
+
!includesInFlightData &&
|
|
3011
|
+
!!options.aggregate &&
|
|
3012
|
+
options.aggregate !== 'none' &&
|
|
3013
|
+
options.aggregate !== 'onchange' &&
|
|
3014
|
+
options.aggregate !== 'integralTotal');
|
|
3015
|
+
}
|
|
3016
|
+
/**
|
|
3017
|
+
* Aggregated read of one datapoint, streamed from PostgreSQL in batches of `STREAM_BATCH_SIZE` rows, so the
|
|
3018
|
+
* memory of the adapter does not grow with the requested range (#516). Each batch is folded into the
|
|
3019
|
+
* aggregation right away. The answer is the one `sendResponse()` builds for the whole array.
|
|
3020
|
+
*
|
|
3021
|
+
* @param table table of the datapoint
|
|
3022
|
+
* @param options options of the read
|
|
3023
|
+
* @param msg the request
|
|
3024
|
+
* @param startTime when the request arrived
|
|
3025
|
+
* @param debugLog whether to log the aggregation steps
|
|
3026
|
+
* @param logId prefix of the debug log
|
|
3027
|
+
* @param readBuffered the buffered read, used when the driver cannot stream
|
|
3028
|
+
*/
|
|
3029
|
+
#getAggregatedDataStreamed(table, options, msg, startTime, debugLog, logId, readBuffered) {
|
|
3030
|
+
const query = this.sqlFuncs.getHistory(this.config.dbname, table, options);
|
|
3031
|
+
if (typeof query !== 'string') {
|
|
3032
|
+
readBuffered();
|
|
3033
|
+
return;
|
|
3034
|
+
}
|
|
3035
|
+
this.#debugStatement(query);
|
|
3036
|
+
// like sendResponse(): the step of the request, before initAggregate() calculates one
|
|
3037
|
+
const responseStep = options.step || 0;
|
|
3038
|
+
const log = debugLog ? (text) => this.log.debug(`${logId}: ${text}`) : undefined;
|
|
3039
|
+
// Initialized with the first batch: if the driver cannot stream, `options` stays untouched for the buffered read
|
|
3040
|
+
let aggOptions;
|
|
3041
|
+
let rowCount = 0;
|
|
3042
|
+
let batches = 0;
|
|
3043
|
+
// An exception must not escape into the callback of the cursor: the connection would never be returned
|
|
3044
|
+
let aggError = null;
|
|
3045
|
+
this.borrowClientFromPool((err, client) => {
|
|
3046
|
+
if (err || !client) {
|
|
3047
|
+
this.returnClientToPool(client);
|
|
3048
|
+
(0, aggregate_1.sendResponse)(this, msg, options.id, options, (err || new Error('No client')).toString(), startTime);
|
|
3049
|
+
return;
|
|
3050
|
+
}
|
|
3051
|
+
client.executeStreamed(query, STREAM_BATCH_SIZE, rows => {
|
|
3052
|
+
if (aggError) {
|
|
3053
|
+
return;
|
|
3054
|
+
}
|
|
3055
|
+
try {
|
|
3056
|
+
aggOptions ??= (0, aggregate_1.initAggregate)(options, options.id, undefined, log);
|
|
3057
|
+
this.#normalizeRows(rows, options);
|
|
3058
|
+
rowCount += rows.length;
|
|
3059
|
+
batches++;
|
|
3060
|
+
(0, aggregate_1.aggregation)(aggOptions, rows);
|
|
3061
|
+
}
|
|
3062
|
+
catch (e) {
|
|
3063
|
+
aggError = e instanceof Error ? e : new Error(String(e));
|
|
3064
|
+
}
|
|
3065
|
+
}, (streamErr, streamed) => {
|
|
3066
|
+
this.returnClientToPool(client);
|
|
3067
|
+
if (!streamed) {
|
|
3068
|
+
if (!streamErr) {
|
|
3069
|
+
// pg-native, or pg-cursor is not installed: that does not change while the adapter runs
|
|
3070
|
+
this.streamingUnavailable = true;
|
|
3071
|
+
this.log.debug('PostgreSQL reads are not streamed: pg-native is used or pg-cursor is missing');
|
|
3072
|
+
}
|
|
3073
|
+
readBuffered();
|
|
3074
|
+
return;
|
|
3075
|
+
}
|
|
3076
|
+
if (streamErr || aggError) {
|
|
3077
|
+
(0, aggregate_1.sendResponse)(this, msg, options.id, options, (streamErr || aggError).toString(), startTime);
|
|
3078
|
+
return;
|
|
3079
|
+
}
|
|
3080
|
+
if (!aggOptions) {
|
|
3081
|
+
// no rows: the same "No Data" answer as for an empty buffered read
|
|
3082
|
+
(0, aggregate_1.sendResponse)(this, msg, options.id, options, [], startTime);
|
|
3083
|
+
return;
|
|
3084
|
+
}
|
|
3085
|
+
try {
|
|
3086
|
+
(0, aggregate_1.finishAggregation)(aggOptions);
|
|
3087
|
+
const result = aggOptions.result;
|
|
3088
|
+
this.log.debug(`Send after aggregation (streamed in ${batches} batches): ${result.length} of: ${rowCount} in: ${Date.now() - startTime}ms`);
|
|
3089
|
+
this.sendTo(msg.from, msg.command, { result, step: responseStep, sessionId: options.sessionId }, msg.callback);
|
|
3090
|
+
}
|
|
3091
|
+
catch (e) {
|
|
3092
|
+
(0, aggregate_1.sendResponse)(this, msg, options.id, options, e.toString(), startTime);
|
|
3093
|
+
}
|
|
3094
|
+
});
|
|
3095
|
+
});
|
|
3096
|
+
}
|
|
2903
3097
|
getDataFromDB(table, options, callback) {
|
|
2904
3098
|
const query = this.sqlFuncs.getHistory(this.config.dbname, table, options);
|
|
2905
3099
|
this.#debugStatement(query);
|
|
@@ -2939,6 +3133,19 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2939
3133
|
this.#getDataFromDB(query, options, callback);
|
|
2940
3134
|
}
|
|
2941
3135
|
}
|
|
3136
|
+
/**
|
|
3137
|
+
* Answer `aggregate` or `lastChange` of the shared catalog of sendTo commands (family history): computed from the
|
|
3138
|
+
* stored values, read with the own `getHistory` (as a message to this instance, so getHistory stays as it is)
|
|
3139
|
+
*/
|
|
3140
|
+
async answerStandard(msg, command) {
|
|
3141
|
+
const answer = await command(msg.message, {
|
|
3142
|
+
read: (0, standardHistory_1.readThroughGetHistory)(message => this.sendToAsync(this.namespace, 'getHistory', message, { timeout: STANDARD_TIMEOUT })),
|
|
3143
|
+
unit: async (id) => (await this.getForeignObjectAsync(id))?.common?.unit,
|
|
3144
|
+
});
|
|
3145
|
+
if (msg.callback) {
|
|
3146
|
+
this.sendTo(msg.from, msg.command, answer, msg.callback);
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
2942
3149
|
getCounterDiff(msg) {
|
|
2943
3150
|
const id = msg.message?.id;
|
|
2944
3151
|
const opts = msg.message?.options || {};
|
|
@@ -3070,11 +3277,11 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3070
3277
|
else {
|
|
3071
3278
|
options.round = configuredRound;
|
|
3072
3279
|
}
|
|
3073
|
-
if (
|
|
3280
|
+
if (options.aggregate === 'percentile' && (options.percentile < 0 || options.percentile > 100)) {
|
|
3074
3281
|
this.log.error(`Invalid percentile value: ${options.percentile}, use 50 as default`);
|
|
3075
3282
|
options.percentile = 50;
|
|
3076
3283
|
}
|
|
3077
|
-
if (
|
|
3284
|
+
if (options.aggregate === 'quantile' && (options.quantile < 0 || options.quantile > 1)) {
|
|
3078
3285
|
this.log.error(`Invalid quantile value: ${options.quantile}, use 0.5 as default`);
|
|
3079
3286
|
options.quantile = 0.5;
|
|
3080
3287
|
}
|
|
@@ -3160,53 +3367,61 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3160
3367
|
}, msg.callback);
|
|
3161
3368
|
}
|
|
3162
3369
|
else {
|
|
3163
|
-
const
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
if (debugLog) {
|
|
3179
|
-
this.log.debug(`${logId} after getDataFromDB: length = ${data.length}`);
|
|
3180
|
-
}
|
|
3181
|
-
if (options.count &&
|
|
3182
|
-
data.length > options.count &&
|
|
3183
|
-
options.aggregate === 'none' &&
|
|
3184
|
-
!options.returnNewestEntries) {
|
|
3185
|
-
if (options.start) {
|
|
3186
|
-
for (let i = 0; i < data.length; i++) {
|
|
3187
|
-
if (data[i].ts < options.start) {
|
|
3188
|
-
data.splice(i, 1);
|
|
3189
|
-
i--;
|
|
3190
|
-
}
|
|
3191
|
-
else {
|
|
3192
|
-
break;
|
|
3193
|
-
}
|
|
3194
|
-
}
|
|
3370
|
+
const readBuffered = () => {
|
|
3371
|
+
const origEnd = options.end;
|
|
3372
|
+
if (includesInFlightData && earliestTs) {
|
|
3373
|
+
options.end = earliestTs;
|
|
3374
|
+
}
|
|
3375
|
+
// if not all data read
|
|
3376
|
+
this.getDataFromDB(dbNames[type], options, (err, data) => {
|
|
3377
|
+
if (!err && data) {
|
|
3378
|
+
options.end = origEnd;
|
|
3379
|
+
if (options.aggregate === 'none' && options.count && options.returnNewestEntries) {
|
|
3380
|
+
cacheData = cacheData.reverse();
|
|
3381
|
+
data = cacheData.concat(data);
|
|
3382
|
+
}
|
|
3383
|
+
else {
|
|
3384
|
+
data = data.concat(cacheData);
|
|
3195
3385
|
}
|
|
3196
|
-
data.splice(options.count);
|
|
3197
3386
|
if (debugLog) {
|
|
3198
|
-
this.log.debug(`${logId}
|
|
3387
|
+
this.log.debug(`${logId} after getDataFromDB: length = ${data.length}`);
|
|
3199
3388
|
}
|
|
3389
|
+
if (options.count &&
|
|
3390
|
+
data.length > options.count &&
|
|
3391
|
+
options.aggregate === 'none' &&
|
|
3392
|
+
!options.returnNewestEntries) {
|
|
3393
|
+
if (options.start) {
|
|
3394
|
+
for (let i = 0; i < data.length; i++) {
|
|
3395
|
+
if (data[i].ts < options.start) {
|
|
3396
|
+
data.splice(i, 1);
|
|
3397
|
+
i--;
|
|
3398
|
+
}
|
|
3399
|
+
else {
|
|
3400
|
+
break;
|
|
3401
|
+
}
|
|
3402
|
+
}
|
|
3403
|
+
}
|
|
3404
|
+
data.splice(options.count);
|
|
3405
|
+
if (debugLog) {
|
|
3406
|
+
this.log.debug(`${logId} pre-cut data to ${options.count} oldest values`);
|
|
3407
|
+
}
|
|
3408
|
+
}
|
|
3409
|
+
data.sort(sortByTs);
|
|
3200
3410
|
}
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3411
|
+
try {
|
|
3412
|
+
(0, aggregate_1.sendResponse)(this, msg, options.id, options, err?.toString() || data || [], startTime, debugLog ? logId : undefined);
|
|
3413
|
+
}
|
|
3414
|
+
catch (e) {
|
|
3415
|
+
(0, aggregate_1.sendResponse)(this, msg, options.id, options, e.toString(), startTime);
|
|
3416
|
+
}
|
|
3417
|
+
});
|
|
3418
|
+
};
|
|
3419
|
+
if (this.#canStream(options, cacheData.length, includesInFlightData)) {
|
|
3420
|
+
this.#getAggregatedDataStreamed(dbNames[type], options, msg, startTime, debugLog, logId, readBuffered);
|
|
3421
|
+
}
|
|
3422
|
+
else {
|
|
3423
|
+
readBuffered();
|
|
3424
|
+
}
|
|
3210
3425
|
}
|
|
3211
3426
|
});
|
|
3212
3427
|
}
|