@transitive-sdk/clickhouse 0.3.1 → 0.3.3

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 CHANGED
@@ -26,7 +26,7 @@ const path2where = (path) => {
26
26
  _.forEach(path, (value, i) => {
27
27
  if (!['+','#'].includes(value[0])) {
28
28
  // it's a constant, filter by it
29
- where.push(`((TopicParts[${i + 1}]) = '${value}')`);
29
+ where.push(`TopicParts[${i + 1}] = '${value}'`);
30
30
  // Note that ClickHouse/SQL index starting at 1, not 0
31
31
  }
32
32
  });
@@ -260,36 +260,26 @@ class ClickHouse {
260
260
  * @param {string} topic - MQTT topic to register
261
261
  */
262
262
  async registerMqttTopicForStorage(selector, ttlDays = DEFAULT_TTL_DAYS) {
263
- this.topics[selector] = true;
264
-
265
- // ---------------------------------------------------------------
266
- // Set/update TTL for this capability and sub-topic
267
263
 
268
264
  const path = topicToPath(selector);
269
265
 
270
266
  if (path.length < 4) {
271
267
  // underspecified, don't set TTL
268
+ console.warn('Not registering topic as it is too short', selector);
272
269
  return;
273
270
  }
274
271
 
275
- // list of TopicParts indices and selected value to use in WHERE statement
276
- // const topicPartSelectors = [
277
- // [2, path[2]],
278
- // [3, path[3]]
279
- // ];
272
+ this.topics[selector] = true;
280
273
 
281
- // path.slice(5).forEach((value, i) => topicPartSelectors.push([i + 5, value]));
274
+ // ---------------------------------------------------------------
275
+ // Set/update TTL for this capability and sub-topic
282
276
 
283
- // const where = topicPartSelectors
284
- // // filter out wildcards
285
- // .filter(([i, value]) => !['+','#'].includes(value[0]))
286
- // // map to WHERE conditions
287
- // .map(([i, value]) => `((TopicParts[${i + 1}]) = '${value}')`);
277
+ // Derive WHERE conditions for TTL expression from non-wildcards
288
278
  const where = path2where(path);
289
279
 
290
-
291
280
  if (where.length == 0) {
292
281
  // underspecified, don't set TTL
282
+ console.warn('Not setting TTL as topic is under specified', selector);
293
283
  return;
294
284
  }
295
285
 
@@ -305,19 +295,18 @@ class ClickHouse {
305
295
  const ttls = matched ? matched[1].split(',').map(x => x.trim()) : [];
306
296
 
307
297
  const whereStatement = `WHERE ${where.join(' AND ')}`;
308
- let present = false;
309
-
310
- // check if TTL statement already present on table definiton
311
- ttls.forEach((ttl, i) => {
312
- if (ttl.endsWith(whereStatement)) {
313
- // condition already present, just replace it to update time
314
- ttls[i] = newTTLStatement;
315
- present = true;
316
- }
317
- });
318
-
319
- if (!present) {
320
- ttls.push(`toDateTime(Timestamp) + toIntervalDay(${ttlDays}) ${whereStatement}`);
298
+ const newTTLStatement =
299
+ `toDateTime(Timestamp) + toIntervalDay(${ttlDays}) ${whereStatement}`;
300
+
301
+ const currentIndex =
302
+ ttls.findIndex(ttl => ttl.replace(/[()]/g, '').endsWith(whereStatement));
303
+
304
+ if (currentIndex >= 0) {
305
+ // replace existing
306
+ ttls[currentIndex] = newTTLStatement;
307
+ } else {
308
+ // add new
309
+ ttls.push(newTTLStatement);
321
310
  }
322
311
 
323
312
  await this.client.command({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transitive-sdk/clickhouse",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "A tiny ClickHouse utility class for use in the Transitive framework.",
5
5
  "homepage": "https://transitiverobotics.com",
6
6
  "repository": {
@@ -193,6 +193,10 @@ describe('ClickHouse', function() {
193
193
  assert.equal(rows.length, 1);
194
194
  });
195
195
 
196
+ it('updates TTL without crashing', async () => {
197
+ await clickhouse.registerMqttTopicForStorage('/+/+/myscope/+/+/#', 13);
198
+ await clickhouse.registerMqttTopicForStorage('/+/+/myscope/+/+/#', 15);
199
+ });
196
200
  });
197
201
 
198
202