@transitive-sdk/utils-web 0.16.1 → 0.16.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.
Files changed (2) hide show
  1. package/dist/utils-web.js +57 -7
  2. package/package.json +1 -1
package/dist/utils-web.js CHANGED
@@ -469,6 +469,14 @@ var require_common = __commonJS({
469
469
  !parts.h && (rtv += `${parts.s}s`);
470
470
  return rtv.trim();
471
471
  };
472
+ var doOnceCache = {};
473
+ var doOnce = (fn, key = void 0) => {
474
+ key ||= fn.toString();
475
+ if (doOnceCache[key])
476
+ return;
477
+ fn();
478
+ doOnceCache[key] = true;
479
+ };
472
480
  module2.exports = {
473
481
  parseMQTTUsername,
474
482
  parseMQTTTopic,
@@ -503,7 +511,8 @@ var require_common = __commonJS({
503
511
  metaPathToSelectorPath,
504
512
  selectorPathToMetaPath,
505
513
  metaTopicToSelector,
506
- selectorToMetaTopic
514
+ selectorToMetaTopic,
515
+ doOnce
507
516
  };
508
517
  }
509
518
  });
@@ -723,7 +732,8 @@ var require_MqttSync = __commonJS({
723
732
  encodeTopicElement,
724
733
  visitAncestor,
725
734
  getRandomId,
726
- selectorPathToMetaPath
735
+ selectorPathToMetaPath,
736
+ selectorToMetaTopic
727
737
  } = require_common();
728
738
  var { DataCache } = require_DataCache();
729
739
  var log2 = getLogger2("MqttSync");
@@ -753,7 +763,7 @@ var require_MqttSync = __commonJS({
753
763
  achieve the "should-be" state. Note that we cannot use a structured document
754
764
  for storing these publishedMessages since we need to be able to store separate
755
765
  values at non-leaf nodes in the object (just like mqtt, where you can have
756
- /a/b = 1 and /a/b/c = 1 at the same time). Note: not used in atomic mode.
766
+ `/a/b = 1` and `/a/b/c = 1` at the same time). Note: not used in atomic mode.
757
767
  Note: we use specialKey in this DataCache to allow overlapping
758
768
  topics (e.g., `/a/b/$_ = 1` and `/a/$_ = {b: 2}`)
759
769
  */
@@ -807,9 +817,6 @@ var require_MqttSync = __commonJS({
807
817
  path = path.slice(sliceTopic);
808
818
  topic = pathToTopic2(path);
809
819
  }
810
- if (!inclMeta && path.some((field) => field[0] == "$")) {
811
- return;
812
- }
813
820
  const rpcHandler = this.getRPCHandler(topic);
814
821
  if (rpcHandler) {
815
822
  const json = mqttParsePayload(payload);
@@ -818,6 +825,9 @@ var require_MqttSync = __commonJS({
818
825
  const json = mqttParsePayload(payload);
819
826
  this.handleRPCResponse(topic, json);
820
827
  } else if (packet.retain || ignoreRetain) {
828
+ if (!inclMeta && path.slice(0, 5).some((field) => field[0] == "$")) {
829
+ return;
830
+ }
821
831
  if (this.isPublished(topic)) {
822
832
  const json = mqttParsePayload(payload);
823
833
  this.publishedMessages.updateFromArray([...path, specialKey], json);
@@ -843,8 +853,8 @@ var require_MqttSync = __commonJS({
843
853
  /**
844
854
  * Publish all values at the given level of the given object under the given
845
855
  * topic (plus sub-key, of course).
846
- * TODO: Is this OK, or do we need to go through this.publish?
847
856
  */
857
+ // TODO: Is this OK, or do we need to go through this.publish?
848
858
  publishAtLevel(topic, value, level) {
849
859
  log2.debug(`publishingAtLevel ${level}`, topic, value);
850
860
  if (level > 0) {
@@ -1317,6 +1327,46 @@ var require_MqttSync = __commonJS({
1317
1327
  const storageRequest = pathToTopic2(path);
1318
1328
  this.mqtt.publish(storageRequest, String(ttl), { retain: true });
1319
1329
  }
1330
+ /** Query a topics history (if stored). Convenience function to make RPC call
1331
+ * to the mqtt2clickhouse service. For details see `clickhouse.queryMQTTHistory`
1332
+ * in utils/clickhouse.
1333
+ * @param {object} params
1334
+ * @param {object} params.topic - A topic with wildcards selecting what
1335
+ * to retrieve.
1336
+ * @param {number} [params.since] - A time (seconds since epoch) from when on
1337
+ * to retrieve history.
1338
+ * @param {number} [params.until] - A time (seconds since epoch) until when on
1339
+ * to retrieve history.
1340
+ * @param {[string]} [params.path] - A path into the payload to extract, e.g.,
1341
+ * `['a', 'b']` would retrieve the value 123 from `{a: {b: 123}}`. Requires `type`.
1342
+ * @param {string} [params.type] - Type of element to extract using `path`.
1343
+ * For available types, see https://clickhouse.com/docs/sql-reference/data-types.
1344
+ * @param {string} [params.orderBy] - an `ORDER BY` statement to use for sorting
1345
+ * results.
1346
+ * @param {integer} [params.limit] - Max number of results to return, after grouping.
1347
+ * @param {integer} [params.bins] - Into how many bins to aggregate (if given,
1348
+ * requires `since`).
1349
+ * @param {string} [params.agg] - Aggregation function to use (if `aggSeconds`
1350
+ * or `bins` and `since` are given). Defaults to `count` (which works for any
1351
+ * data type). See
1352
+ * https://clickhouse.com/docs/sql-reference/aggregate-functions/reference.
1353
+ * @param {integer} [params.aggSeconds] - How many seconds to group together
1354
+ * (alternative to `bins` + `since`).
1355
+ * */
1356
+ async queryHistory(params) {
1357
+ const path = topicToPath2(params.topic);
1358
+ const rpc = selectorToMetaTopic(`${pathToTopic2(path.slice(0, 5))}/$queryMQTTHistory`);
1359
+ const query = {
1360
+ subtopic: pathToTopic2(path.slice(5)),
1361
+ ...params
1362
+ };
1363
+ delete query.topic;
1364
+ query.since && (query.since = query.since.getTime());
1365
+ query.until && (query.until = query.until.getTime());
1366
+ log2.info("queryHistory", { rpc, query });
1367
+ const result = await this.call(rpc, query);
1368
+ return result;
1369
+ }
1320
1370
  };
1321
1371
  module2.exports = MqttSync3;
1322
1372
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transitive-sdk/utils-web",
3
- "version": "0.16.1",
3
+ "version": "0.16.3",
4
4
  "description": "Web utils for the Transitive framework",
5
5
  "homepage": "https://transitiverobotics.com",
6
6
  "repository": {