@transitive-sdk/clickhouse 0.5.0 → 0.5.1
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/index.js +9 -13
- package/package.json +1 -1
- package/test/clickhouse.test.js +1 -1
package/index.js
CHANGED
|
@@ -48,22 +48,21 @@ class ClickHouse {
|
|
|
48
48
|
mqttHistoryTable = null; // name of the table used for MQTT history, if used
|
|
49
49
|
topics = {}; // list of topics registered for storage, as object for de-duplication
|
|
50
50
|
rowCache = {}; // cache of rows awaiting insertion, by table
|
|
51
|
-
insertionInterval = null; // the actual interval
|
|
52
51
|
|
|
53
52
|
/** Create the client, connecting to Clickhouse */
|
|
54
|
-
async init({ url, dbName, user, password } = {}) {
|
|
53
|
+
async init({ url, dbName, user, password, interval } = {}) {
|
|
55
54
|
|
|
56
55
|
const _url = url || process.env.CLICKHOUSE_URL || 'http://clickhouse:8123';
|
|
57
56
|
const _dbName = dbName || process.env.CLICKHOUSE_DB || 'default';
|
|
58
57
|
const _user = user || process.env.CLICKHOUSE_USER || 'default';
|
|
58
|
+
interval ||= 10_000;
|
|
59
59
|
|
|
60
60
|
const {hostname, port} = URL.parse(_url);
|
|
61
|
-
const
|
|
62
|
-
await waitPort({ host: hostname, port: Number(port || 80),
|
|
61
|
+
const waitInterval = 200;
|
|
62
|
+
await waitPort({ host: hostname, port: Number(port || 80), waitInterval },
|
|
63
|
+
10000);
|
|
63
64
|
await new Promise(done => setTimeout(done, 200));
|
|
64
65
|
|
|
65
|
-
// console.debug(`Creating ClickHouse client for URL: ${_url}, DB: ${_dbName}, User: ${_user}`);
|
|
66
|
-
|
|
67
66
|
this._client = createClient({
|
|
68
67
|
url: _url,
|
|
69
68
|
max_open_connections: 10,
|
|
@@ -88,6 +87,9 @@ class ClickHouse {
|
|
|
88
87
|
});
|
|
89
88
|
|
|
90
89
|
await this._client.query({ query: 'SELECT 1' });
|
|
90
|
+
|
|
91
|
+
// start interval for batch insertion
|
|
92
|
+
setInterval(this.batchInsertCache.bind(this), interval);
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
/** Get the Clickhouse client (from @clickhouse/client) */
|
|
@@ -165,7 +167,7 @@ class ClickHouse {
|
|
|
165
167
|
* @param {object} options = {dataCache, tableName, ttlDays}
|
|
166
168
|
* @param {number} interval = ms interval between batch insertions
|
|
167
169
|
*/
|
|
168
|
-
async enableHistory(options
|
|
170
|
+
async enableHistory(options) {
|
|
169
171
|
const { dataCache, tableName = 'mqtt_history' } = options;
|
|
170
172
|
|
|
171
173
|
if (this.mqttHistoryTable != tableName) {
|
|
@@ -263,12 +265,6 @@ class ClickHouse {
|
|
|
263
265
|
}
|
|
264
266
|
|
|
265
267
|
this.mqttHistoryTable = tableName;
|
|
266
|
-
|
|
267
|
-
// start interval for batch insertion
|
|
268
|
-
if (!this.insertionInterval) {
|
|
269
|
-
this.insertionInterval =
|
|
270
|
-
setInterval(this.batchInsertCache.bind(this), interval);
|
|
271
|
-
}
|
|
272
268
|
}
|
|
273
269
|
|
|
274
270
|
/** Add the given rows to the cache for batch-insertion to the given table */
|
package/package.json
CHANGED
package/test/clickhouse.test.js
CHANGED
|
@@ -48,7 +48,7 @@ describe('ClickHouse', function() {
|
|
|
48
48
|
const dataCache = new DataCache({});
|
|
49
49
|
|
|
50
50
|
before(async () => {
|
|
51
|
-
await clickhouse.init({ url: CLICKHOUSE_URL });
|
|
51
|
+
await clickhouse.init({ url: CLICKHOUSE_URL, interval: 100 });
|
|
52
52
|
/* Register for `insert` events on ClickHouse client */
|
|
53
53
|
emitter = interceptInserts();
|
|
54
54
|
|