@coherentglobal/spark-execute-sdk 0.4.9 → 0.4.10

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/dist/browser.js CHANGED
@@ -7164,7 +7164,15 @@ var Spark = /*#__PURE__*/function () {
7164
7164
  */
7165
7165
  function Spark(config) {
7166
7166
  var _this$config$sparkEnd, _this$config, _this$config$sparkEnd2, _this$config$sparkEnd3, _this$config2, _this$config2$sparkEn, _ref, _this$config$sparkEnd4, _this$config3, _this$config3$sparkEn, _this$config4, _this$config4$sparkEn, _this$config$sparkEnd5, _this$config5, _this$config5$sparkEn, _this$config6;
7167
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
7167
7168
  (0, _classCallCheck2["default"])(this, Spark);
7169
+ /**
7170
+ * @private
7171
+ */
7172
+ if (options !== null && options !== void 0 && options.log) {
7173
+ this.logger = options.log;
7174
+ }
7175
+
7168
7176
  /**
7169
7177
  * @private
7170
7178
  */
@@ -7190,25 +7198,51 @@ var Spark = /*#__PURE__*/function () {
7190
7198
  * @private
7191
7199
  */
7192
7200
  this.isCompatible = utils.isWasmSupported();
7193
- /**
7194
- * @private
7195
- */
7196
- this.model = this._models((_this$config6 = this.config) === null || _this$config6 === void 0 ? void 0 : _this$config6.nodeGenModels);
7201
+
7197
7202
  /**
7198
7203
  * @private
7199
7204
  */
7200
7205
  this.priority = ["model", "onlineEndpoint"];
7201
- }
7202
7206
 
7203
- /**
7204
- *
7205
- * @param {object} nodegen
7206
- * @returns {object}
7207
- */
7207
+ /**
7208
+ * @private
7209
+ */
7210
+ this.model = this._models((_this$config6 = this.config) === null || _this$config6 === void 0 ? void 0 : _this$config6.nodeGenModels);
7211
+ }
7208
7212
  (0, _createClass2["default"])(Spark, [{
7213
+ key: "log",
7214
+ value: function log(msg) {
7215
+ this.logger && this.logger(msg);
7216
+ }
7217
+
7218
+ /**
7219
+ *
7220
+ * @param {object} nodegen
7221
+ * @returns {object}
7222
+ */
7223
+ }, {
7209
7224
  key: "_models",
7210
7225
  value: function _models(nodegen) {
7211
- var models = nodegen.map(processModels);
7226
+ this.log({
7227
+ eventType: "spark._models",
7228
+ msg: 'Parse models'
7229
+ });
7230
+ var models = [];
7231
+ for (var i = 0; i < nodegen.length; i++) {
7232
+ var startDate = Date.now();
7233
+ this.log({
7234
+ eventType: "spark._models.processModels.start",
7235
+ msg: "Start process model ".concat(i)
7236
+ });
7237
+ var model = processModels(nodegen[i], i, this.log.bind(this));
7238
+ var endDate = Date.now();
7239
+ this.log({
7240
+ eventType: "spark._models.processModels.end",
7241
+ msg: "End process model ".concat(i),
7242
+ duration: endDate - startDate
7243
+ });
7244
+ models.push(model);
7245
+ }
7212
7246
  return models;
7213
7247
  }
7214
7248
  /**
@@ -7219,13 +7253,27 @@ var Spark = /*#__PURE__*/function () {
7219
7253
  }, {
7220
7254
  key: "validateConfig",
7221
7255
  value: function validateConfig(config) {
7256
+ var start = Date.now();
7257
+ this.log({
7258
+ eventType: 'spark.validateconfig.start'
7259
+ });
7222
7260
  var _validate = validate(config),
7223
7261
  value = _validate.value,
7224
7262
  error = _validate.error;
7225
7263
  if (!error) {
7264
+ var end = Date.now();
7265
+ this.log({
7266
+ eventType: 'spark.validateconfig.startend',
7267
+ duration: end - start
7268
+ });
7226
7269
  return value;
7227
7270
  } else {
7228
7271
  var message = JSON.stringify(error.details);
7272
+ var _end = Date.now();
7273
+ this.log({
7274
+ eventType: 'spark.validateconfig.startend.error',
7275
+ duration: _end - start
7276
+ });
7229
7277
  throw new Error("ValidationError: ".concat(message));
7230
7278
  }
7231
7279
  }
@@ -7271,26 +7319,32 @@ var Spark = /*#__PURE__*/function () {
7271
7319
  while (1) {
7272
7320
  switch (_context.prev = _context.next) {
7273
7321
  case 0:
7322
+ if (!(this.model == undefined)) {
7323
+ _context.next = 2;
7324
+ break;
7325
+ }
7326
+ throw new WasmRunnerErrors.BadRequestError('Spark initialize is required. Please call initialize method');
7327
+ case 2:
7274
7328
  versionID = version_id || (input === null || input === void 0 ? void 0 : (_input$request_meta = input.request_meta) === null || _input$request_meta === void 0 ? void 0 : _input$request_meta.version_id) || (input === null || input === void 0 ? void 0 : (_input$request_meta2 = input.request_meta) === null || _input$request_meta2 === void 0 ? void 0 : _input$request_meta2.version_uuid);
7275
7329
  lookup = {
7276
7330
  model: this.offlineModel,
7277
7331
  onlineEndpoint: this.onlineModelEndpoint
7278
7332
  };
7279
- _context.next = 4;
7333
+ _context.next = 6;
7280
7334
  return lookup[this.priority[0]](input, versionID, this);
7281
- case 4:
7335
+ case 6:
7282
7336
  response = _context.sent;
7283
7337
  if (!(response instanceof Error && this.config.sparkEndpoint !== undefined)) {
7284
- _context.next = 9;
7338
+ _context.next = 11;
7285
7339
  break;
7286
7340
  }
7287
- _context.next = 8;
7341
+ _context.next = 10;
7288
7342
  return lookup[this.priority[1]](input, versionID, this);
7289
- case 8:
7343
+ case 10:
7290
7344
  response = _context.sent;
7291
- case 9:
7345
+ case 11:
7292
7346
  return _context.abrupt("return", response);
7293
- case 10:
7347
+ case 12:
7294
7348
  case "end":
7295
7349
  return _context.stop();
7296
7350
  }
@@ -7667,14 +7721,29 @@ var findModel = function findModel(requestMeta, nodegenModels) {
7667
7721
  }));
7668
7722
  }).reverse();
7669
7723
  }
7670
- } else if (requestMeta.service_id && requestMeta.version) {
7724
+ } else if (requestMeta.servicename) {
7725
+ var transactionDate = new Date();
7671
7726
  model = nodegenModels.filter(function (_ref4) {
7672
7727
  var metaData = _ref4.metaData;
7673
- return metaData.EngineInformation.ServiceGuid === requestMeta.service_id && metaData.EngineInformation.Revision === requestMeta.version;
7728
+ return metaData.EngineInformation.ServiceName === requestMeta.servicename && transactionDate >= new Date(metaData.EffectiveStartDate) && transactionDate <= new Date(metaData.EffectiveEndDate);
7674
7729
  });
7675
- } else if (requestMeta.service_id && requestMeta.transaction_date) {
7730
+ if (model.length > 1) {
7731
+ return model.sort(function (versionA, versionB) {
7732
+ return versionA.metaData.EngineInformation.Revision.replace(/\d+/g, function (n) {
7733
+ return +n + 100000;
7734
+ }).localCompare(versionB.metaData.EngineInformation.Revision.replace(/\d+/g, function (n) {
7735
+ return +n + 100000;
7736
+ }));
7737
+ }).reverse();
7738
+ }
7739
+ } else if (requestMeta.service_id && requestMeta.version) {
7676
7740
  model = nodegenModels.filter(function (_ref5) {
7677
7741
  var metaData = _ref5.metaData;
7742
+ return metaData.EngineInformation.ServiceGuid === requestMeta.service_id && metaData.EngineInformation.Revision === requestMeta.version;
7743
+ });
7744
+ } else if (requestMeta.service_id && requestMeta.transaction_date) {
7745
+ model = nodegenModels.filter(function (_ref6) {
7746
+ var metaData = _ref6.metaData;
7678
7747
  return metaData.EngineInformation.ServiceGuid === requestMeta.service_id && new Date(requestMeta.transaction_date) >= new Date(metaData.EffectiveStartDate) && new Date(requestMeta.transaction_date) <= new Date(metaData.EffectiveEndDate);
7679
7748
  });
7680
7749
  if (model.length > 1) {
@@ -7686,6 +7755,21 @@ var findModel = function findModel(requestMeta, nodegenModels) {
7686
7755
  }));
7687
7756
  }).reverse();
7688
7757
  }
7758
+ } else if (requestMeta.service_id) {
7759
+ var _transactionDate = new Date();
7760
+ model = nodegenModels.filter(function (_ref7) {
7761
+ var metaData = _ref7.metaData;
7762
+ return metaData.EngineInformation.ServiceGuid === requestMeta.service_id && _transactionDate >= new Date(metaData.EffectiveStartDate) && _transactionDate <= new Date(metaData.EffectiveEndDate);
7763
+ });
7764
+ if (model.length > 1) {
7765
+ return model.sort(function (versionA, versionB) {
7766
+ return versionA.metaData.EngineInformation.Revision.replace(/\d+/g, function (n) {
7767
+ return +n + 100000;
7768
+ }).localCompare(versionB.metaData.EngineInformation.Revision.replace(/\d+/g, function (n) {
7769
+ return +n + 100000;
7770
+ }));
7771
+ }).reverse();
7772
+ }
7689
7773
  } else if (requestMeta.service_uri && requestMeta.transaction_date) {
7690
7774
  var regex = /folders\/(.+)\/services\/(.+)/gi;
7691
7775
  var parts = regex.exec(requestMeta.service_uri);
@@ -7694,8 +7778,8 @@ var findModel = function findModel(requestMeta, nodegenModels) {
7694
7778
  folder = decodeURI(parts[1]);
7695
7779
  service = decodeURI(parts[2]);
7696
7780
  }
7697
- model = nodegenModels.filter(function (_ref6) {
7698
- var metaData = _ref6.metaData;
7781
+ model = nodegenModels.filter(function (_ref8) {
7782
+ var metaData = _ref8.metaData;
7699
7783
  return metaData.EngineInformation.ProductName === folder && metaData.EngineInformation.ServiceName === service && new Date(requestMeta.transaction_date) >= new Date(metaData.EffectiveStartDate) && new Date(requestMeta.transaction_date) <= new Date(metaData.EffectiveEndDate);
7700
7784
  });
7701
7785
  if (model.length > 1) {
@@ -7715,10 +7799,32 @@ var findModel = function findModel(requestMeta, nodegenModels) {
7715
7799
  _folder = decodeURI(_parts[1]);
7716
7800
  _service = decodeURI(_parts[2]);
7717
7801
  }
7718
- model = nodegenModels.filter(function (_ref7) {
7719
- var metaData = _ref7.metaData;
7802
+ model = nodegenModels.filter(function (_ref9) {
7803
+ var metaData = _ref9.metaData;
7720
7804
  return metaData.EngineInformation.ProductName === _folder && metaData.EngineInformation.ServiceName === _service && metaData.EngineInformation.Revision === requestMeta.version;
7721
7805
  });
7806
+ } else if (requestMeta.service_uri) {
7807
+ var _transactionDate2 = new Date();
7808
+ var _regex2 = /folders\/(.+)\/services\/(.+)/gi;
7809
+ var _parts2 = _regex2.exec(requestMeta.service_uri);
7810
+ var _folder2, _service2;
7811
+ if (_parts2) {
7812
+ _folder2 = decodeURI(_parts2[1]);
7813
+ _service2 = decodeURI(_parts2[2]);
7814
+ }
7815
+ model = nodegenModels.filter(function (_ref10) {
7816
+ var metaData = _ref10.metaData;
7817
+ return metaData.EngineInformation.ProductName === _folder2 && metaData.EngineInformation.ServiceName === _service2 && _transactionDate2 >= new Date(metaData.EffectiveStartDate) && _transactionDate2 <= new Date(metaData.EffectiveEndDate);
7818
+ });
7819
+ if (model.length > 1) {
7820
+ return model.sort(function (versionA, versionB) {
7821
+ return versionA.metaData.EngineInformation.Revision.replace(/\d+/g, function (n) {
7822
+ return +n + 100000;
7823
+ }).localCompare(versionB.metaData.EngineInformation.Revision.replace(/\d+/g, function (n) {
7824
+ return +n + 100000;
7825
+ }));
7826
+ }).reverse();
7827
+ }
7722
7828
  } else {
7723
7829
  throw new Error("Model not found. requestMeta: " + JSON.stringify(requestMeta));
7724
7830
  }
@@ -7818,13 +7924,35 @@ var path = require("path");
7818
7924
  * @param {object} nodegen
7819
7925
  * @returns
7820
7926
  */
7927
+
7821
7928
  var processModels = function processModels(nodegen, index) {
7929
+ var logFn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
7930
+ var instanceid = arguments.length > 3 ? arguments[3] : undefined;
7931
+ var startDate = Date.now();
7932
+ logFn && logFn({
7933
+ eventType: 'processModels.start',
7934
+ nodegenType: nodegen.type
7935
+ });
7822
7936
  var processedModels = _objectSpread({}, nodegen);
7937
+ var endSpreadDate = Date.now();
7938
+ logFn && logFn({
7939
+ eventType: 'processModels.spread',
7940
+ nodegenType: nodegen.type,
7941
+ duration: endSpreadDate - startDate
7942
+ });
7823
7943
  switch (nodegen.type) {
7824
7944
  case "binary":
7825
7945
  case "uint8array":
7826
7946
  case "base64":
7827
- if ((0, _typeof2["default"])(nodegen.binary) === "object") {
7947
+ if (Buffer.isBuffer(nodegen.binary)) {
7948
+ processedModels.binary = nodegen.binary;
7949
+ var _endCreateBufferDate = Date.now();
7950
+ logFn && logFn({
7951
+ eventType: 'processModels.type.buffer',
7952
+ nodegenType: nodegen.type,
7953
+ duration: _endCreateBufferDate - startDate
7954
+ });
7955
+ } else if ((0, _typeof2["default"])(nodegen.binary) === "object") {
7828
7956
  var model = {
7829
7957
  servicename: nodegen.binary.servicename,
7830
7958
  wasm: Buffer.from(nodegen.binary.wasm, "base64"),
@@ -7832,6 +7960,12 @@ var processModels = function processModels(nodegen, index) {
7832
7960
  data: Buffer.from(nodegen.binary.data, "base64"),
7833
7961
  metadata: Buffer.from(nodegen.binary.metadata, "base64")
7834
7962
  };
7963
+ var endCreateobjectDate = Date.now();
7964
+ logFn && logFn({
7965
+ eventType: 'processModels.type.object',
7966
+ nodegenType: nodegen.type,
7967
+ duration: endCreateobjectDate - startDate
7968
+ });
7835
7969
  processedModels.binary = model;
7836
7970
  } else if (typeof nodegen.binaryPath !== 'undefined') {
7837
7971
  processModels.binaryPath = nodegen.binaryPath;
@@ -7847,7 +7981,7 @@ var processModels = function processModels(nodegen, index) {
7847
7981
  fs.mkdirSync(folder);
7848
7982
  }
7849
7983
  }
7850
- var filePath = path.join(__dirname, "resolver_temp/model-".concat(nodegen.versionId || nodegen.VersionI || uuidv4(), ".binary"));
7984
+ var filePath = path.join(__dirname, "resolver_temp/".concat(instanceid, "_model_").concat(nodegen.versionId || nodegen.VersionId || uuidv4(), ".binary"));
7851
7985
  fs.writeFileSync(filePath, Buffer.from(nodegen.binary, 'base64'));
7852
7986
  processedModels.binaryPath = filePath;
7853
7987
  nodegen.binaryPath = filePath;
@@ -7856,6 +7990,12 @@ var processModels = function processModels(nodegen, index) {
7856
7990
  }
7857
7991
  } else {
7858
7992
  var _model = Buffer.from(nodegen.binary, "base64");
7993
+ var _endCreateBufferDate2 = Date.now();
7994
+ logFn && logFn({
7995
+ eventType: 'processModels.type.bufferb64',
7996
+ nodegenType: nodegen.type,
7997
+ duration: _endCreateBufferDate2 - startDate
7998
+ });
7859
7999
  processedModels.binary = _model;
7860
8000
  }
7861
8001
  }
@@ -7885,9 +8025,24 @@ var processModels = function processModels(nodegen, index) {
7885
8025
  } else {
7886
8026
  processedModels.binary = func();
7887
8027
  }
8028
+ break;
8029
+ case "url":
8030
+ processedModels.binary = nodegen.binary;
8031
+ var endCreateBufferDate = Date.now();
8032
+ logFn && logFn({
8033
+ eventType: 'processModels.type.url',
8034
+ nodegenType: nodegen.type,
8035
+ duration: endCreateBufferDate - startDate
8036
+ });
7888
8037
  default:
7889
8038
  break;
7890
8039
  }
8040
+ var endDate = Date.now();
8041
+ logFn && logFn({
8042
+ eventType: 'processModels.complete',
8043
+ nodegenType: nodegen.type,
8044
+ duration: endDate - startDate
8045
+ });
7891
8046
  return processedModels;
7892
8047
  };
7893
8048
  module.exports = processModels;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coherentglobal/spark-execute-sdk",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
4
4
  "description": "",
5
5
  "main": "src/node.js",
6
6
  "browser": "dist/browser.js",
package/src/browser.js CHANGED
@@ -11,7 +11,14 @@ class Spark {
11
11
  /**
12
12
  * @param {object} config
13
13
  */
14
- constructor(config) {
14
+ constructor(config, options = {}) {
15
+ /**
16
+ * @private
17
+ */
18
+ if (options?.log) {
19
+ this.logger = options.log
20
+ }
21
+
15
22
  /**
16
23
  * @private
17
24
  */
@@ -40,14 +47,20 @@ class Spark {
40
47
  * @private
41
48
  */
42
49
  this.isCompatible = utils.isWasmSupported();
43
- /**
44
- * @private
45
- */
46
- this.model = this._models(this.config?.nodeGenModels);
50
+
47
51
  /**
48
52
  * @private
49
53
  */
50
54
  this.priority = ["model", "onlineEndpoint"];
55
+
56
+ /**
57
+ * @private
58
+ */
59
+ this.model = this._models(this.config?.nodeGenModels);
60
+ }
61
+
62
+ log(msg) {
63
+ this.logger && this.logger(msg)
51
64
  }
52
65
 
53
66
  /**
@@ -56,7 +69,17 @@ class Spark {
56
69
  * @returns {object}
57
70
  */
58
71
  _models(nodegen) {
59
- const models = nodegen.map(processModels);
72
+ this.log({ eventType: "spark._models", msg: 'Parse models' })
73
+ const models = [];
74
+
75
+ for (let i = 0; i < nodegen.length; i++) {
76
+ const startDate = Date.now();
77
+ this.log({ eventType: "spark._models.processModels.start", msg: `Start process model ${i}` });
78
+ const model = processModels(nodegen[i], i, this.log.bind(this));
79
+ const endDate = Date.now();
80
+ this.log({ eventType: "spark._models.processModels.end", msg: `End process model ${i}`, duration: endDate - startDate });
81
+ models.push(model)
82
+ }
60
83
 
61
84
  return models;
62
85
  }
@@ -66,11 +89,17 @@ class Spark {
66
89
  * @returns {object}
67
90
  */
68
91
  validateConfig(config) {
92
+ const start = Date.now();
93
+ this.log({ eventType: 'spark.validateconfig.start' })
69
94
  const { value, error } = validate(config);
70
95
  if (!error) {
96
+ const end = Date.now();
97
+ this.log({ eventType: 'spark.validateconfig.startend', duration: end - start })
71
98
  return value;
72
99
  } else {
73
100
  const message = JSON.stringify(error.details);
101
+ const end = Date.now();
102
+ this.log({ eventType: 'spark.validateconfig.startend.error', duration: end - start })
74
103
  throw new Error(`ValidationError: ${message}`);
75
104
  }
76
105
  }
@@ -105,6 +134,10 @@ class Spark {
105
134
  * @returns {object}
106
135
  */
107
136
  async execute(input, version_id) {
137
+ if (this.model == undefined) {
138
+ throw new WasmRunnerErrors.BadRequestError('Spark initialize is required. Please call initialize method')
139
+ }
140
+
108
141
  const versionID =
109
142
  version_id ||
110
143
  input?.request_meta?.version_id ||
@@ -189,7 +222,7 @@ class Spark {
189
222
  },
190
223
  });
191
224
  }
192
- const result = await registry.execute(input, modelVersionId, () => {});
225
+ const result = await registry.execute(input, modelVersionId, () => { });
193
226
  return result;
194
227
  } catch (err) {
195
228
  return err;
package/src/findModels.js CHANGED
@@ -17,9 +17,35 @@ const findModel = (requestMeta, nodegenModels) => {
17
17
  ({ metaData }) =>
18
18
  metaData.EngineInformation.ServiceName === requestMeta.servicename &&
19
19
  new Date(requestMeta.transaction_date) >=
20
- new Date(metaData.EffectiveStartDate) &&
20
+ new Date(metaData.EffectiveStartDate) &&
21
21
  new Date(requestMeta.transaction_date) <=
22
- new Date(metaData.EffectiveEndDate)
22
+ new Date(metaData.EffectiveEndDate)
23
+ );
24
+
25
+ if (model.length > 1) {
26
+ return model
27
+ .sort((versionA, versionB) =>
28
+ versionA.metaData.EngineInformation.Revision.replace(
29
+ /\d+/g,
30
+ (n) => +n + 100000
31
+ ).localCompare(
32
+ versionB.metaData.EngineInformation.Revision.replace(
33
+ /\d+/g,
34
+ (n) => +n + 100000
35
+ )
36
+ )
37
+ )
38
+ .reverse();
39
+ }
40
+ } else if (requestMeta.servicename) {
41
+ const transactionDate = new Date();
42
+ model = nodegenModels.filter(
43
+ ({ metaData }) =>
44
+ metaData.EngineInformation.ServiceName === requestMeta.servicename &&
45
+ transactionDate >=
46
+ new Date(metaData.EffectiveStartDate) &&
47
+ transactionDate <=
48
+ new Date(metaData.EffectiveEndDate)
23
49
  );
24
50
 
25
51
  if (model.length > 1) {
@@ -48,9 +74,35 @@ const findModel = (requestMeta, nodegenModels) => {
48
74
  ({ metaData }) =>
49
75
  metaData.EngineInformation.ServiceGuid === requestMeta.service_id &&
50
76
  new Date(requestMeta.transaction_date) >=
51
- new Date(metaData.EffectiveStartDate) &&
77
+ new Date(metaData.EffectiveStartDate) &&
52
78
  new Date(requestMeta.transaction_date) <=
53
- new Date(metaData.EffectiveEndDate)
79
+ new Date(metaData.EffectiveEndDate)
80
+ );
81
+
82
+ if (model.length > 1) {
83
+ return model
84
+ .sort((versionA, versionB) =>
85
+ versionA.metaData.EngineInformation.Revision.replace(
86
+ /\d+/g,
87
+ (n) => +n + 100000
88
+ ).localCompare(
89
+ versionB.metaData.EngineInformation.Revision.replace(
90
+ /\d+/g,
91
+ (n) => +n + 100000
92
+ )
93
+ )
94
+ )
95
+ .reverse();
96
+ }
97
+ } else if (requestMeta.service_id) {
98
+ const transactionDate = new Date();
99
+ model = nodegenModels.filter(
100
+ ({ metaData }) =>
101
+ metaData.EngineInformation.ServiceGuid === requestMeta.service_id &&
102
+ transactionDate >=
103
+ new Date(metaData.EffectiveStartDate) &&
104
+ transactionDate <=
105
+ new Date(metaData.EffectiveEndDate)
54
106
  );
55
107
 
56
108
  if (model.length > 1) {
@@ -82,9 +134,9 @@ const findModel = (requestMeta, nodegenModels) => {
82
134
  metaData.EngineInformation.ProductName === folder &&
83
135
  metaData.EngineInformation.ServiceName === service &&
84
136
  new Date(requestMeta.transaction_date) >=
85
- new Date(metaData.EffectiveStartDate) &&
137
+ new Date(metaData.EffectiveStartDate) &&
86
138
  new Date(requestMeta.transaction_date) <=
87
- new Date(metaData.EffectiveEndDate)
139
+ new Date(metaData.EffectiveEndDate)
88
140
  );
89
141
 
90
142
  if (model.length > 1) {
@@ -116,6 +168,41 @@ const findModel = (requestMeta, nodegenModels) => {
116
168
  metaData.EngineInformation.ServiceName === service &&
117
169
  metaData.EngineInformation.Revision === requestMeta.version
118
170
  );
171
+ } else if (requestMeta.service_uri) {
172
+ const transactionDate = new Date();
173
+ const regex = /folders\/(.+)\/services\/(.+)/gi;
174
+ const parts = regex.exec(requestMeta.service_uri);
175
+ let folder, service;
176
+ if (parts) {
177
+ folder = decodeURI(parts[1]);
178
+ service = decodeURI(parts[2]);
179
+ }
180
+
181
+ model = nodegenModels.filter(
182
+ ({ metaData }) =>
183
+ metaData.EngineInformation.ProductName === folder &&
184
+ metaData.EngineInformation.ServiceName === service &&
185
+ transactionDate >=
186
+ new Date(metaData.EffectiveStartDate) &&
187
+ transactionDate <=
188
+ new Date(metaData.EffectiveEndDate)
189
+ );
190
+
191
+ if (model.length > 1) {
192
+ return model
193
+ .sort((versionA, versionB) =>
194
+ versionA.metaData.EngineInformation.Revision.replace(
195
+ /\d+/g,
196
+ (n) => +n + 100000
197
+ ).localCompare(
198
+ versionB.metaData.EngineInformation.Revision.replace(
199
+ /\d+/g,
200
+ (n) => +n + 100000
201
+ )
202
+ )
203
+ )
204
+ .reverse();
205
+ }
119
206
  } else {
120
207
  throw new Error(
121
208
  "Model not found. requestMeta: " + JSON.stringify(requestMeta)
package/src/models.js CHANGED
@@ -7,14 +7,24 @@ const path = require("path");
7
7
  * @param {object} nodegen
8
8
  * @returns
9
9
  */
10
- const processModels = (nodegen, index) => {
10
+
11
+ const processModels = (nodegen, index, logFn = undefined, instanceid) => {
12
+ const startDate = Date.now();
13
+ logFn && logFn({ eventType: 'processModels.start', nodegenType: nodegen.type });
14
+
11
15
  const processedModels = { ...nodegen };
16
+ const endSpreadDate = Date.now();
17
+ logFn && logFn({ eventType: 'processModels.spread', nodegenType: nodegen.type, duration: endSpreadDate - startDate });
12
18
 
13
19
  switch (nodegen.type) {
14
20
  case "binary":
15
21
  case "uint8array":
16
22
  case "base64":
17
- if (typeof nodegen.binary === "object") {
23
+ if (Buffer.isBuffer(nodegen.binary)) {
24
+ processedModels.binary = nodegen.binary;
25
+ const endCreateBufferDate = Date.now();
26
+ logFn && logFn({ eventType: 'processModels.type.buffer', nodegenType: nodegen.type, duration: endCreateBufferDate - startDate });
27
+ } else if (typeof nodegen.binary === "object") {
18
28
  const model = {
19
29
  servicename: nodegen.binary.servicename,
20
30
  wasm: Buffer.from(nodegen.binary.wasm, "base64"),
@@ -22,6 +32,8 @@ const processModels = (nodegen, index) => {
22
32
  data: Buffer.from(nodegen.binary.data, "base64"),
23
33
  metadata: Buffer.from(nodegen.binary.metadata, "base64")
24
34
  };
35
+ const endCreateobjectDate = Date.now();
36
+ logFn && logFn({ eventType: 'processModels.type.object', nodegenType: nodegen.type, duration: endCreateobjectDate - startDate });
25
37
  processedModels.binary = model;
26
38
  } else if (typeof nodegen.binaryPath !== 'undefined') {
27
39
  processModels.binaryPath = nodegen.binaryPath
@@ -39,7 +51,7 @@ const processModels = (nodegen, index) => {
39
51
  }
40
52
  const filePath = path.join(
41
53
  __dirname,
42
- `resolver_temp/model-${nodegen.versionId || nodegen.VersionI || uuidv4()}.binary`
54
+ `resolver_temp/${instanceid}_model_${nodegen.versionId || nodegen.VersionId || uuidv4()}.binary`
43
55
  );
44
56
 
45
57
  fs.writeFileSync(filePath, Buffer.from(nodegen.binary, 'base64'));
@@ -50,6 +62,8 @@ const processModels = (nodegen, index) => {
50
62
  }
51
63
  } else {
52
64
  const model = Buffer.from(nodegen.binary, "base64");
65
+ const endCreateBufferDate = Date.now();
66
+ logFn && logFn({ eventType: 'processModels.type.bufferb64', nodegenType: nodegen.type, duration: endCreateBufferDate - startDate });
53
67
  processedModels.binary = model;
54
68
  }
55
69
  }
@@ -67,10 +81,17 @@ const processModels = (nodegen, index) => {
67
81
  } else {
68
82
  processedModels.binary = func();
69
83
  }
84
+ break;
85
+ case "url":
86
+ processedModels.binary = nodegen.binary;
87
+ const endCreateBufferDate = Date.now();
88
+ logFn && logFn({ eventType: 'processModels.type.url', nodegenType: nodegen.type, duration: endCreateBufferDate - startDate });
70
89
  default:
71
90
  break;
72
91
  }
73
92
 
93
+ const endDate = Date.now();
94
+ logFn && logFn({ eventType: 'processModels.complete', nodegenType: nodegen.type, duration: endDate - startDate })
74
95
  return processedModels;
75
96
  };
76
97
 
package/src/node.js CHANGED
@@ -13,31 +13,34 @@ const { object } = require("joi");
13
13
  const { P } = require("pino");
14
14
  const findModel = require("./findModels.js");
15
15
 
16
- let registry = [];
17
-
18
16
  class Spark {
19
17
  /**
20
18
  * @param {object} config
21
19
  */
22
- constructor(config, option = {}, apmInstance) {
23
- // REMOVE ME!!
24
- // console.log('init spark instance serialize:', option?.serialize)
20
+ constructor(config, option = undefined, apmInstance = undefined) {
21
+ /**
22
+ * @private
23
+ */
24
+ this._registry = undefined;
25
+
25
26
  if (apmInstance) {
26
- // console.log(apmInstance)
27
27
  this.apm = apmInstance
28
28
  }
29
+
30
+ /**
31
+ * @private
32
+ */
33
+ this.instanceid = option && option.instanceid ? option.instanceid : uuidv4()
34
+
29
35
  /**
30
36
  * @private
31
37
  */
32
- if (option.serialize) {
38
+ if (option && option.serialize) {
33
39
  const buffer = fs.readFileSync(option.configPath);
34
40
  const toBase = buffer.toString("utf8");
35
41
  const deserialize = serializeObject.deserialize(toBase);
36
42
  const validate = this.validateConfig(deserialize);
37
43
  this.config = validate;
38
- logger.info({
39
- message: "DESERIALIZED"
40
- });
41
44
  this.isSerializedConfig;
42
45
  } else {
43
46
  this.config = this.validateConfig(config);
@@ -88,7 +91,7 @@ class Spark {
88
91
  * @returns {object}
89
92
  */
90
93
  _models(nodegen) {
91
- const models = nodegen.map(processModels);
94
+ const models = nodegen.map((n, i) => processModels(n, i, undefined, this.instanceid));
92
95
  return models;
93
96
  }
94
97
 
@@ -105,6 +108,7 @@ class Spark {
105
108
  const message = JSON.stringify(error.details);
106
109
  logger.error({
107
110
  EventType: "ValidationError",
111
+ InstanceId: this.instanceid,
108
112
  TextMessage: message,
109
113
  });
110
114
  throw new Error(`ValidationError: ${message}`);
@@ -125,15 +129,15 @@ class Spark {
125
129
  input.request_meta.version_uuid;
126
130
 
127
131
  let lookup = {
128
- model: this.offlineModel,
129
- onlineEndpoint: this.onlineModelEndpoint,
132
+ model: this.offlineModel.bind(this),
133
+ onlineEndpoint: this.onlineModelEndpoint.bind(this),
130
134
  };
131
135
 
132
136
  let response;
133
137
 
134
- response = await lookup[this.priority[0]](input, versionID, this);
138
+ response = await lookup[this.priority[0]](input, versionID);
135
139
  if (response instanceof Error && this.config.sparkEndpoint !== undefined && false) {
136
- response = await lookup[this.priority[1]](input, versionID, this);
140
+ response = await lookup[this.priority[1]](input, versionID);
137
141
  }
138
142
 
139
143
  return response;
@@ -148,12 +152,12 @@ class Spark {
148
152
  }
149
153
  const filePath = path.join(
150
154
  __dirname,
151
- `resolver_temp/resolver-${uuidv4()}.js`
155
+ `resolver_temp/${this.instanceid}_resolver-${uuidv4()}.js`
152
156
  );
153
157
 
154
158
  const configPath = path.join(
155
159
  __dirname,
156
- `resolver_temp/model_config`
160
+ `resolver_temp/${this.instanceid}_config`
157
161
  );
158
162
 
159
163
  if (fs.existsSync(filePath)) {
@@ -173,9 +177,11 @@ class Spark {
173
177
  template = template.replace(
174
178
  "%%config_to_replace%%",
175
179
  configPath
180
+ ).replace(
181
+ "%%instanceid%%",
182
+ this.instanceid
176
183
  );
177
184
 
178
-
179
185
  fs.writeFileSync(filePath, template);
180
186
  if (!this.isSerializedConfig) {
181
187
  const config = { ...this.config }
@@ -185,7 +191,7 @@ class Spark {
185
191
  if (m.binary) {
186
192
  const filePath = path.join(
187
193
  __dirname,
188
- `resolver_temp/model-${m.versionId || m.VersionId || uuidv4()}.binary`
194
+ `resolver_temp/${this.instanceid}_model_${m.versionId || m.VersionId || uuidv4()}.binary`
189
195
  );
190
196
 
191
197
  fs.writeFileSync(filePath, Buffer.from(m.binary, 'base64'));
@@ -198,6 +204,26 @@ class Spark {
198
204
  fs.writeFileSync(configPath, Buffer.from(data))
199
205
  }
200
206
 
207
+ process.once('SIGINT', () => {
208
+ try {
209
+ fs.rmSync(path.join(
210
+ __dirname,
211
+ `resolver_temp`), { recursive: true, force: true });
212
+ } catch (err) {
213
+ // Just ignore
214
+ }
215
+ });
216
+
217
+ process.once('exit', () => {
218
+ try {
219
+ fs.rmSync(path.join(
220
+ __dirname,
221
+ `resolver_temp`), { recursive: true, force: true });
222
+ } catch (err) {
223
+ // Just ignore
224
+ }
225
+ });
226
+
201
227
  return require.resolve(filePath);
202
228
  }
203
229
 
@@ -207,7 +233,9 @@ class Spark {
207
233
  m?.metaData?.EngineInformation?.ProductName === folderName &&
208
234
  m?.metaData?.EngineInformation?.ServiceName === serviceName
209
235
  );
210
- if (!model) throw new WasmRunnerErrors.MissingModelError(serviceName);
236
+ if (!model) {
237
+ throw new WasmRunnerErrors.MissingModelError(`folder ${folderName} service ${serviceName}`);
238
+ }
211
239
  return model?.metaData?.VersionId || model?.versionId;
212
240
  }
213
241
 
@@ -216,7 +244,6 @@ class Spark {
216
244
  console.err(err)
217
245
  );
218
246
  return require.resolve("config.json");
219
- // return JSON.stringify(this.config)
220
247
  }
221
248
 
222
249
  /**
@@ -224,55 +251,61 @@ class Spark {
224
251
  */
225
252
  // async remove(versionId) {}
226
253
 
227
- async offlineModel(input, versionID, ds) {
254
+ async offlineModel(input, versionID) {
228
255
  try {
229
- const model = findModel(input.request_meta, ds.model);
256
+ const model = findModel(input.request_meta, this.model);
230
257
 
231
258
  const versionId = versionID || (model && model?.versionId);
232
259
 
233
260
  logger.info({
234
261
  EventType: "ExecuteModel",
262
+ InstanceId: this.instanceid,
235
263
  TextMessage: `Execute uuid: ${versionId}`,
236
264
  });
237
- if (!(registry instanceof WasmRunner)) {
238
- registry = new WasmRunner(undefined, ds.externalResolver, "");
265
+
266
+ if (this._registry === undefined) {
267
+ this._registry = new WasmRunner(undefined, this.externalResolver, "");
239
268
  }
240
269
 
241
- if (!registry.isExist(versionId)) {
242
- await registry.append({
270
+ if (!this._registry.isExist(versionId)) {
271
+ await this._registry.append({
243
272
  id: versionId,
244
273
  url: model?.binary || model?.binaryPath || "",
245
274
  });
246
275
  }
247
276
 
248
- let result = await registry.execute(input, versionId);
277
+ let result = await this._registry.execute(input, versionId);
249
278
 
250
279
  return result;
251
280
  } catch (err) {
252
- console.log('----ERRORR')
253
- console.log(err)
281
+ logger.error({
282
+ EventType: "ExecutionError",
283
+ InstanceId: this.instanceid,
284
+ TextMessage: err.message,
285
+ });
254
286
  return err;
255
287
  }
256
288
  }
257
289
 
258
- async onlineModelEndpoint(input, versionID, ds) {
290
+ async onlineModelEndpoint(input, versionID) {
259
291
  logger.info({
260
292
  EventType: "ExecuteOnlineModel",
293
+ InstanceId: this.instanceid,
261
294
  TextMessage: `Execute uuid: ${versionID}`,
262
295
  });
263
296
 
264
- const isPublic = ds.authType === "public";
297
+ const isPublic = this.authType === "public";
265
298
  let token = "";
266
299
  if (!isPublic) {
267
- if (ds.token.constructor.name === "AsyncFunction") {
268
- token = await ds.token();
269
- } else if (typeof ds.token === "function") {
270
- token = ds.token();
300
+ if (this.token.constructor.name === "AsyncFunction") {
301
+ token = await this.token();
302
+ } else if (typeof this.token === "function") {
303
+ token = this.token();
271
304
  } else {
272
- token = ds.token;
305
+ token = this.token;
273
306
  }
274
307
 
275
- if (utils.isEmpty(ds.token)) {
308
+ if (utils.isEmpty(this.token)) {
276
309
  throw new WasmRunnerErrors.UnauthorizedError();
277
310
  }
278
311
  }
@@ -280,7 +313,7 @@ class Spark {
280
313
  try {
281
314
  const options = {
282
315
  headers: !isPublic
283
- ? utils.getAuthHeaders(token, ds.authType, ds.tenant)
316
+ ? utils.getAuthHeaders(token, this.authType, this.tenant)
284
317
  : {},
285
318
  json: {
286
319
  request_data: {},
@@ -289,8 +322,8 @@ class Spark {
289
322
  };
290
323
 
291
324
  let modelUrl = new URL(
292
- `/${ds.tenant}/api/v3/${!isPublic ? "execute" : "public"}`,
293
- ds.url
325
+ `/${this.tenant}/api/v3/${!isPublic ? "execute" : "public"}`,
326
+ this.url
294
327
  );
295
328
 
296
329
  const result = await got.post(modelUrl, options).json();
@@ -60,7 +60,7 @@ const sparkServiceVersionOne = async (requestData, context) => {
60
60
  const options = {}
61
61
 
62
62
  if (!instance) {
63
- instance = new Spark({}, { serialize: true, configPath: "%%config_to_replace%%" })
63
+ instance = new Spark({}, { serialize: true, configPath: "%%config_to_replace%%", instanceid: "%%instanceid%%" })
64
64
  }
65
65
 
66
66
  const modelId = instance._getModelByMeta(folderName, serviceName)
@@ -3,7 +3,8 @@ declare class Spark {
3
3
  /**
4
4
  * @param {object} config
5
5
  */
6
- constructor(config: object);
6
+ constructor(config: object, options?: {});
7
+ logger: any;
7
8
  /**
8
9
  * @private
9
10
  */
@@ -28,14 +29,15 @@ declare class Spark {
28
29
  * @private
29
30
  */
30
31
  private isCompatible;
31
- /**
32
- * @private
33
- */
34
- private model;
35
32
  /**
36
33
  * @private
37
34
  */
38
35
  private priority;
36
+ /**
37
+ * @private
38
+ */
39
+ private model;
40
+ log(msg: any): void;
39
41
  /**
40
42
  *
41
43
  * @param {object} nodegen
@@ -1 +1 @@
1
- {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.js"],"names":[],"mappings":";AASA;IACE;;OAEG;IACH,oBAFW,MAAM,EAuChB;IApCC;;OAEG;IACH,eAAyC;IAEzC;;OAEG;IACH,eAAsD;IACtD;;OAEG;IACH,iBAA0D;IAC1D;;OAEG;IACH,cAGI;IACJ;;OAEG;IACH,YAAgD;IAChD;;OAEG;IACH,qBAA2C;IAC3C;;OAEG;IACH,cAAqD;IACrD;;OAEG;IACH,iBAA2C;IAG7C;;;;OAIG;IACH,iBAHW,MAAM,GACJ,MAAM,CAMlB;IACD;;;;OAIG;IACH,uBAHW,MAAM,GACJ,MAAM,CAUlB;IACD;;;;OAIG;IACH,cAHW,MAAM,GACJ,OAAO,CAOnB;IAED,wDASC;IAED;;;;;;OAMG;IACH,eAJW,MAAM,oCAEJ,MAAM,CAmBlB;IAED,gEAuEC;IAED,uEA6CC;CACF"}
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.js"],"names":[],"mappings":";AASA;IACE;;OAEG;IACH,oBAFW,MAAM,gBAgDhB;IAzCG,YAAyB;IAG3B;;OAEG;IACH,eAAyC;IAEzC;;OAEG;IACH,eAAsD;IACtD;;OAEG;IACH,iBAA0D;IAC1D;;OAEG;IACH,cAGI;IACJ;;OAEG;IACH,YAAgD;IAChD;;OAEG;IACH,qBAA2C;IAE3C;;OAEG;IACH,iBAA2C;IAE3C;;MAEE;IACF,cAAqD;IAGvD,oBAEC;IAED;;;;OAIG;IACH,iBAHW,MAAM,GACJ,MAAM,CAgBlB;IACD;;;;OAIG;IACH,uBAHW,MAAM,GACJ,MAAM,CAgBlB;IACD;;;;OAIG;IACH,cAHW,MAAM,GACJ,OAAO,CAOnB;IAED,wDASC;IAED;;;;;;OAMG;IACH,eAJW,MAAM,oCAEJ,MAAM,CAuBlB;IAED,gEAuEC;IAED,uEA6CC;CACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"findModels.d.ts","sourceRoot":"","sources":["../src/findModels.js"],"names":[],"mappings":";AAEA,sEA+HC"}
1
+ {"version":3,"file":"findModels.d.ts","sourceRoot":"","sources":["../src/findModels.js"],"names":[],"mappings":";AAEA,sEAsNC"}
package/types/models.d.ts CHANGED
@@ -4,5 +4,5 @@ export = processModels;
4
4
  * @param {object} nodegen
5
5
  * @returns
6
6
  */
7
- declare function processModels(nodegen: object, index: any): {};
7
+ declare function processModels(nodegen: object, index: any, logFn: undefined, instanceid: any): {};
8
8
  //# sourceMappingURL=models.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../src/models.js"],"names":[],"mappings":";AAIA;;;;GAIG;AACH,wCAHW,MAAM,kBAoEhB"}
1
+ {"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../src/models.js"],"names":[],"mappings":";AAIA;;;;GAIG;AAEH,wCAJW,MAAM,qDAyFhB"}
package/types/node.d.ts CHANGED
@@ -3,9 +3,14 @@ declare class Spark {
3
3
  /**
4
4
  * @param {object} config
5
5
  */
6
- constructor(config: object, option: {} | undefined, apmInstance: any);
6
+ constructor(config: object, option?: undefined, apmInstance?: undefined);
7
+ /**
8
+ * @private
9
+ */
10
+ private _registry;
7
11
  apm: any;
8
12
  config: object;
13
+ instanceid: any;
9
14
  /**
10
15
  * @private
11
16
  */
@@ -64,7 +69,7 @@ declare class Spark {
64
69
  /**
65
70
  * Remove Model
66
71
  */
67
- offlineModel(input: any, versionID: any, ds: any): Promise<any>;
68
- onlineModelEndpoint(input: any, versionID: any, ds: any): Promise<any>;
72
+ offlineModel(input: any, versionID: any): Promise<unknown>;
73
+ onlineModelEndpoint(input: any, versionID: any): Promise<any>;
69
74
  }
70
75
  //# sourceMappingURL=node.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.js"],"names":[],"mappings":";AAiBA;IACE;;OAEG;IACH,oBAFW,MAAM,4CA8DhB;IAxDG,SAAsB;IAUtB,eAAsB;IASxB;;OAEG;IACH,eAAsD;IACtD;;OAEG;IACH,iBAA0D;IAC1D;;OAEG;IACH,cAGI;IACJ;;OAEG;IACH,YAAgD;IAChD;;OAEG;IACH,qBAA2C;IAC3C;;OAEG;IACH,cAAqD;IAErD;;OAEG;IACH,yBAAmD;IAEnD;;OAEG;IACH,iBAA2C;IAG7C;;;;OAIG;IACH,iBAHW,MAAM,GACJ,MAAM,CAKlB;IAED;;;;OAIG;IACH,uBAHW,MAAM,GACJ,MAAM,CAclB;IAED;;;;;;OAMG;IACH,eAJW,MAAM,oCAEJ,MAAM,CAqBlB;IAED,+BA4DC;IAED,wDAQC;IAED,uBAMC;IAED;;OAEG;IAGH,gEA6BC;IAED,uEA4CC;CACF"}
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.js"],"names":[],"mappings":";AAeA;IACE;;OAEG;IACH,oBAFW,MAAM,+CAmEhB;IAhEC;;OAEG;IACH,kBAA0B;IAGxB,SAAsB;IAUtB,eAAsB;IASxB,gBAA4E;IAE5E;;OAEG;IACH,eAAsD;IACtD;;OAEG;IACH,iBAA0D;IAC1D;;OAEG;IACH,cAGI;IACJ;;OAEG;IACH,YAAgD;IAChD;;OAEG;IACH,qBAA2C;IAC3C;;OAEG;IACH,cAAqD;IAErD;;OAEG;IACH,yBAAmD;IAEnD;;OAEG;IACH,iBAA2C;IAG7C;;;;OAIG;IACH,iBAHW,MAAM,GACJ,MAAM,CAKlB;IAED;;;;OAIG;IACH,uBAHW,MAAM,GACJ,MAAM,CAclB;IAED;;;;;;OAMG;IACH,eAJW,MAAM,oCAEJ,MAAM,CAqBlB;IAED,+BAkFC;IAED,wDAWC;IAED,uBAKC;IAED;;OAEG;IAGH,2DA0CC;IAED,8DA4CC;CACF"}