iobroker.sql 4.7.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 +17 -6
- package/build/lib/connection-factory.js +19 -0
- package/build/lib/connection-factory.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/main.js +266 -73
- package/build/main.js.map +1 -1
- package/io-package.json +14 -14
- package/package.json +3 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgresql-client.js","sourceRoot":"","sources":["../../src/lib/postgresql-client.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAyD;AACzD,8DAAqC;AACrC,uDAAmE;
|
|
1
|
+
{"version":3,"file":"postgresql-client.js","sourceRoot":"","sources":["../../src/lib/postgresql-client.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAyD;AACzD,8DAAqC;AACrC,uDAAmE;AAOnE,8EAA8E;AAC9E,MAAM,2BAA4B,SAAQ,sCAAiB;IAC/C,MAAM,CAA4B;IAC1C,yEAAyE;IACjE,QAAQ,GAAG,KAAK,CAAC;IACzB,qFAAqF;IAC7E,MAAM,CAA4B;IAC1C,gFAAgF;IACxE,iBAAiB,GAAG,KAAK,CAAC;IAElC,cAAc,CAAC,aAAgC,EAAE,QAA0D;QACvG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACf,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAClB,EAAE,CAAC,EAAE;gBACD,MAAM,YAAY,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;gBAC/C,IAAI,CAAC,MAAM,GAAG,YAAY,IAAI,EAAE,CAAC,OAA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I,CAAC,CAAC,CAAC;gBACrC,CAAC;qBAAM,CAAC;oBACJ,MAAM,CAAC,IAAI,CAAC,CAAC;oBACb,IAAI,EAAE,CAAC;gBACX,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;QACF,IAAI,EAAE,CAAC;IACX,CAAC;CACJ;AAED,MAAa,gBAAiB,SAAQ,oBAAS;IAC3C,YAAY,aAAgC;QACxC,KAAK,CAAC,aAAa,EAAE,IAAI,2BAA2B,EAAE,CAAC,CAAC;IAC5D,CAAC;CACJ;AAJD,4CAIC;AAED,MAAa,oBAAqB,SAAQ,+BAAa;IACnD,YAAY,WAAuB,EAAE,aAAgC;QACjE,KAAK,CAAC,WAAW,EAAE,aAAa,EAAE,IAAI,2BAA2B,EAAE,CAAC,CAAC;IACzE,CAAC;CACJ;AAJD,oDAIC"}
|
package/build/lib/sql-client.js
CHANGED
|
@@ -155,6 +155,32 @@ class SQLClient extends node_events_1.EventEmitter {
|
|
|
155
155
|
}
|
|
156
156
|
});
|
|
157
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Read the rows of a query batch by batch, see `ConnectionFactory.executeStreamed()`.
|
|
160
|
+
*
|
|
161
|
+
* @param sql plain SQL without bound values
|
|
162
|
+
* @param batchSize maximum number of rows per `onRows` call
|
|
163
|
+
* @param onRows receives the rows batch by batch
|
|
164
|
+
* @param callback `streamed` is false if the driver cannot stream; then nothing was read
|
|
165
|
+
*/
|
|
166
|
+
executeStreamed(sql, batchSize, onRows, callback) {
|
|
167
|
+
if (!this.connection) {
|
|
168
|
+
return this.connect(err => {
|
|
169
|
+
if (err) {
|
|
170
|
+
callback(err, false);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
this.executeStreamed(sql, batchSize, onRows, callback);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
this.factory.executeStreamed(this.connection, sql, batchSize, onRows, (err, streamed) => {
|
|
178
|
+
if (err) {
|
|
179
|
+
this.#flagIfConnectionError(err);
|
|
180
|
+
}
|
|
181
|
+
callback(err || null, streamed);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
158
184
|
async executeAsync(statement) {
|
|
159
185
|
if (!this.connection) {
|
|
160
186
|
await this.connectAsync();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-client.js","sourceRoot":"","sources":["../../src/lib/sql-client.ts"],"names":[],"mappings":";;AACA,6CAA2C;AAC3C,qCAA6C;AAC7C,2CAA+C;AAG/C,MAAqB,SAAU,SAAQ,0BAAY;IAC9B,OAAO,CAAM;IACtB,OAAO,CAAoB;IAC5B,SAAS,GAAkB,IAAI,CAAC;IAChC,WAAW,GAAkB,IAAI,CAAC;IAClC,YAAY,GAAkB,IAAI,CAAC;IAClC,UAAU,CAAgB;IAC1B,MAAM,GAAG,KAAK,CAAC;IACf,SAAS,GAAiB,IAAI,CAAC;IAEvC,YAAY,OAAY,EAAE,iBAAoC;QAC1D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,iBAAiB,CAAC;IACrC,CAAC;IAED;;;;;;;;;;;OAWG;IACH,gBAAgB,CAAC,UAAyB;QACtC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QAEtB,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,CAAC,EAAE,KAAK,UAAU,EAAE,CAAC;YACrD,OAAO;QACX,CAAC;QAED,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAY,EAAQ,EAAE;YAC1C,kFAAkF;YAClF,mFAAmF;YACnF,uFAAuF;YACvF,kCAAkC;YAClC,IAAI,IAAI,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC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;;;OAQG;IACH,sBAAsB,CAAC,GAAU;QAC7B,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,IAAA,0BAAiB,EAAC,GAAG,CAAC,EAAE,CAAC;YACzC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;YACnB,IAAI,CAAC,SAAS,GAAG,GAAG,CAAC;YACrB,wFAAwF;YACxF,0FAA0F;YAC1F,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC9B,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;YAC5B,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AA9PD,4BA8PC"}
|
package/build/lib/sql-string.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.quoteSqlString = quoteSqlString;
|
|
4
|
+
exports.quoteNString = quoteNString;
|
|
5
|
+
exports.quoteMsIdentifier = quoteMsIdentifier;
|
|
4
6
|
exports.quotePgString = quotePgString;
|
|
5
7
|
exports.numberOrNull = numberOrNull;
|
|
6
8
|
exports.boolOrNull = boolOrNull;
|
|
@@ -28,6 +30,28 @@ function quoteSqlString(value) {
|
|
|
28
30
|
const text = typeof value === 'string' ? value : String(value);
|
|
29
31
|
return `'${text.replace(/\0/g, '').replace(/'/g, "''")}'`;
|
|
30
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Return `value` as an MS SQL Unicode string literal (`N'...'`), including the prefix and quotes.
|
|
35
|
+
*
|
|
36
|
+
* A plain `'...'` literal is converted to the code page of the database collation before it reaches an
|
|
37
|
+
* `nvarchar` column: `Кухня` or an emoji arrived as `?????`. With `N` the text stays Unicode.
|
|
38
|
+
*
|
|
39
|
+
* @param value the text to quote; anything else is converted with `String()`
|
|
40
|
+
*/
|
|
41
|
+
function quoteNString(value) {
|
|
42
|
+
return `N${quoteSqlString(value)}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Return `name` as an MS SQL identifier in brackets, e.g. a database name.
|
|
46
|
+
*
|
|
47
|
+
* Without brackets a name with `-`, a space or a reserved word (`io-broker`) is a syntax error. A `]`
|
|
48
|
+
* in the name is doubled.
|
|
49
|
+
*
|
|
50
|
+
* @param name the identifier
|
|
51
|
+
*/
|
|
52
|
+
function quoteMsIdentifier(name) {
|
|
53
|
+
return `[${String(name).replace(/]/g, ']]')}]`;
|
|
54
|
+
}
|
|
31
55
|
/**
|
|
32
56
|
* Return `value` as a PostgreSQL escape string literal (`E'...'`), including the prefix and quotes.
|
|
33
57
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-string.js","sourceRoot":"","sources":["../../src/lib/sql-string.ts"],"names":[],"mappings":";;AAqBA,wCAGC;AAaD,sCAGC;AAUD,oCAEC;AASD,gCASC;
|
|
1
|
+
{"version":3,"file":"sql-string.js","sourceRoot":"","sources":["../../src/lib/sql-string.ts"],"names":[],"mappings":";;AAqBA,wCAGC;AAUD,oCAEC;AAUD,8CAEC;AAaD,sCAGC;AAUD,oCAEC;AASD,gCASC;AA9FD,qCAAkC;AAClC;;;;;;;;;;;;;GAaG;AAEH;;;;GAIG;AACH,SAAgB,cAAc,CAAC,KAAc;IACzC,MAAM,IAAI,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/D,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC;AAC9D,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAC,KAAc;IACvC,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;AACvC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,iBAAiB,CAAC,IAAY;IAC1C,OAAO,IAA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X_RAW_ENTRIES = 5000;
|
|
210
210
|
/** How often an unchanged connection error is repeated as error in the log */
|
|
211
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;
|
|
212
214
|
/** How long `aggregate` and `lastChange` wait for the answer of the own `getHistory` */
|
|
213
215
|
const STANDARD_TIMEOUT = 120_000;
|
|
214
216
|
function sortByTs(a, b) {
|
|
@@ -227,6 +229,8 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
227
229
|
finished = false;
|
|
228
230
|
sqlConnected = null;
|
|
229
231
|
multiRequests = true;
|
|
232
|
+
/** The driver cannot stream (pg-native, or pg-cursor not installed): read buffered without trying again */
|
|
233
|
+
streamingUnavailable = false;
|
|
230
234
|
subscribeAll = false;
|
|
231
235
|
clientPool = null;
|
|
232
236
|
reconnectTimeout = null;
|
|
@@ -235,7 +239,10 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
235
239
|
bufferChecker = null;
|
|
236
240
|
activeConnections = 0;
|
|
237
241
|
poolBorrowGuard = []; // required until https://github.com/intellinote/sql-client/issues/7 is fixed
|
|
238
|
-
|
|
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
|
+
}
|
|
239
246
|
postgresDbCreated = false;
|
|
240
247
|
lockTasks = false;
|
|
241
248
|
sqlFuncs = null;
|
|
@@ -370,6 +377,13 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
370
377
|
},
|
|
371
378
|
});
|
|
372
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
|
+
}
|
|
373
387
|
/**
|
|
374
388
|
* Log a statement on debug level.
|
|
375
389
|
*
|
|
@@ -379,7 +393,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
379
393
|
* @param statement plain SQL or SQL with values
|
|
380
394
|
*/
|
|
381
395
|
#debugStatement(statement) {
|
|
382
|
-
if (this
|
|
396
|
+
if (this.#isDebug()) {
|
|
383
397
|
this.log.debug((0, statement_1.statementToString)(statement));
|
|
384
398
|
}
|
|
385
399
|
}
|
|
@@ -871,7 +885,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
871
885
|
this.resetConnectionError();
|
|
872
886
|
this.log.info(`Connected to ${this.config.dbtype}`);
|
|
873
887
|
// read all DB IDs and all FROM ids
|
|
874
|
-
this.getAllIds(() => this.getAllFroms(callback));
|
|
888
|
+
this.#readNameColumnTypes(() => this.getAllIds(() => this.getAllFroms(callback)));
|
|
875
889
|
}
|
|
876
890
|
});
|
|
877
891
|
}
|
|
@@ -1818,6 +1832,39 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1818
1832
|
// the converted value must not replace it.
|
|
1819
1833
|
this.pushValueIntoDB(_id, { ...state, val }, false, false, cb);
|
|
1820
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
|
+
}
|
|
1821
1868
|
getAllIds(cb) {
|
|
1822
1869
|
const query = this.sqlFuncs.getIdSelect(this.config.dbname);
|
|
1823
1870
|
this.#debugStatement(query);
|
|
@@ -2128,10 +2175,14 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2128
2175
|
}
|
|
2129
2176
|
task.cb?.();
|
|
2130
2177
|
task.cb = null;
|
|
2131
|
-
|
|
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(() => {
|
|
2132
2183
|
this.tasksReadType.shift();
|
|
2133
2184
|
this.processReadTypes();
|
|
2134
|
-
}
|
|
2185
|
+
});
|
|
2135
2186
|
}
|
|
2136
2187
|
prepareTaskReadDbId(id, state, isCounter, cb) {
|
|
2137
2188
|
if (!this.sqlDPs[id]) {
|
|
@@ -2263,12 +2314,17 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2263
2314
|
if (!this.sqlDPs[id] || !state) {
|
|
2264
2315
|
return cb?.();
|
|
2265
2316
|
}
|
|
2266
|
-
|
|
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
|
+
}
|
|
2267
2321
|
this.prepareTaskCheckTypeAndDbId(id, state, isCounter, (err) => {
|
|
2268
2322
|
if (!this.sqlDPs[id]) {
|
|
2269
2323
|
return cb?.();
|
|
2270
2324
|
}
|
|
2271
|
-
|
|
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
|
+
}
|
|
2272
2328
|
if (err) {
|
|
2273
2329
|
return cb?.(err);
|
|
2274
2330
|
}
|
|
@@ -2408,8 +2464,16 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2408
2464
|
#keptErrors = new WeakSet();
|
|
2409
2465
|
storeCached(onlyId, cb) {
|
|
2410
2466
|
let count = 0;
|
|
2411
|
-
|
|
2412
|
-
|
|
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]) {
|
|
2413
2477
|
continue;
|
|
2414
2478
|
}
|
|
2415
2479
|
const _settings = this.sqlDPs[id].config || {};
|
|
@@ -2883,32 +2947,153 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2883
2947
|
client.execute(query, (err, rows /* , fields */) => {
|
|
2884
2948
|
this.returnClientToPool(client);
|
|
2885
2949
|
if (!err && rows) {
|
|
2886
|
-
|
|
2887
|
-
if (typeof rows[c].ts === 'string') {
|
|
2888
|
-
rows[c].ts = parseInt(rows[c].ts, 10);
|
|
2889
|
-
}
|
|
2890
|
-
if (this.common?.loglevel === 'debug') {
|
|
2891
|
-
rows[c].date = new Date(rows[c].ts);
|
|
2892
|
-
}
|
|
2893
|
-
if (options.ack) {
|
|
2894
|
-
rows[c].ack = !!rows[c].ack;
|
|
2895
|
-
}
|
|
2896
|
-
if (typeof rows[c].val === 'number' && isFinite(rows[c].val) && options.round) {
|
|
2897
|
-
rows[c].val = Math.round(rows[c].val * options.round) / options.round;
|
|
2898
|
-
}
|
|
2899
|
-
if (options.id && this.sqlDPs[options.id] && this.sqlDPs[options.id].type === 2) {
|
|
2900
|
-
// 2 === boolean
|
|
2901
|
-
rows[c].val = !!rows[c].val;
|
|
2902
|
-
}
|
|
2903
|
-
if (options.addId && !rows[c].id && options.id) {
|
|
2904
|
-
rows[c].id = options.id;
|
|
2905
|
-
}
|
|
2906
|
-
}
|
|
2950
|
+
this.#normalizeRows(rows, options);
|
|
2907
2951
|
}
|
|
2908
2952
|
callback?.(err, rows);
|
|
2909
2953
|
});
|
|
2910
2954
|
});
|
|
2911
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
|
+
}
|
|
2912
3097
|
getDataFromDB(table, options, callback) {
|
|
2913
3098
|
const query = this.sqlFuncs.getHistory(this.config.dbname, table, options);
|
|
2914
3099
|
this.#debugStatement(query);
|
|
@@ -3182,53 +3367,61 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
3182
3367
|
}, msg.callback);
|
|
3183
3368
|
}
|
|
3184
3369
|
else {
|
|
3185
|
-
const
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
if (debugLog) {
|
|
3201
|
-
this.log.debug(`${logId} after getDataFromDB: length = ${data.length}`);
|
|
3202
|
-
}
|
|
3203
|
-
if (options.count &&
|
|
3204
|
-
data.length > options.count &&
|
|
3205
|
-
options.aggregate === 'none' &&
|
|
3206
|
-
!options.returnNewestEntries) {
|
|
3207
|
-
if (options.start) {
|
|
3208
|
-
for (let i = 0; i < data.length; i++) {
|
|
3209
|
-
if (data[i].ts < options.start) {
|
|
3210
|
-
data.splice(i, 1);
|
|
3211
|
-
i--;
|
|
3212
|
-
}
|
|
3213
|
-
else {
|
|
3214
|
-
break;
|
|
3215
|
-
}
|
|
3216
|
-
}
|
|
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);
|
|
3217
3385
|
}
|
|
3218
|
-
data.splice(options.count);
|
|
3219
3386
|
if (debugLog) {
|
|
3220
|
-
this.log.debug(`${logId}
|
|
3387
|
+
this.log.debug(`${logId} after getDataFromDB: length = ${data.length}`);
|
|
3221
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);
|
|
3222
3410
|
}
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
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
|
+
}
|
|
3232
3425
|
}
|
|
3233
3426
|
});
|
|
3234
3427
|
}
|