@xyo-network/diviner-forecasting-memory 2.84.1 → 2.84.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.
@@ -43,7 +43,7 @@ var import_diviner_forecasting_abstract = require("@xyo-network/diviner-forecast
43
43
  var import_diviner_forecasting_method_arima = require("@xyo-network/diviner-forecasting-method-arima");
44
44
  var import_diviner_forecasting_model = require("@xyo-network/diviner-forecasting-model");
45
45
  var import_diviner_model = require("@xyo-network/diviner-model");
46
- var import_jsonpath = __toESM(require("jsonpath"));
46
+ var import_jsonpath = __toESM(require("jsonpath"), 1);
47
47
  var getJsonPathTransformer = (pathExpression) => {
48
48
  const transformer = (x) => {
49
49
  const ret = import_jsonpath.default.value(x, pathExpression);
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ MemoryForecastingDiviner: () => MemoryForecastingDiviner
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+
37
+ // src/MemoryForecastingDiviner.ts
38
+ var import_assert = require("@xylabs/assert");
39
+ var import_exists = require("@xylabs/exists");
40
+ var import_archivist_model = require("@xyo-network/archivist-model");
41
+ var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
42
+ var import_diviner_forecasting_abstract = require("@xyo-network/diviner-forecasting-abstract");
43
+ var import_diviner_forecasting_method_arima = require("@xyo-network/diviner-forecasting-method-arima");
44
+ var import_diviner_forecasting_model = require("@xyo-network/diviner-forecasting-model");
45
+ var import_diviner_model = require("@xyo-network/diviner-model");
46
+ var import_jsonpath = __toESM(require("jsonpath"), 1);
47
+ var getJsonPathTransformer = (pathExpression) => {
48
+ const transformer = (x) => {
49
+ const ret = import_jsonpath.default.value(x, pathExpression);
50
+ if (typeof ret === "number")
51
+ return ret;
52
+ throw new Error("Parsed invalid payload value");
53
+ };
54
+ return transformer;
55
+ };
56
+ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_diviner_forecasting_abstract.AbstractForecastingDiviner {
57
+ static configSchemas = [import_diviner_forecasting_model.ForecastingDivinerConfigSchema];
58
+ static forecastingMethodDict = {
59
+ arimaForecasting: import_diviner_forecasting_method_arima.arimaForecastingMethod,
60
+ seasonalArimaForecasting: import_diviner_forecasting_method_arima.seasonalArimaForecastingMethod
61
+ };
62
+ /**
63
+ * The max number of records to search during the batch query
64
+ */
65
+ batchLimit = 1e3;
66
+ // TODO: Inject via config
67
+ maxTrainingLength = 1e4;
68
+ get forecastingMethod() {
69
+ const forecastingMethodName = (0, import_assert.assertEx)(this.config.forecastingMethod, "Missing forecastingMethod in config");
70
+ const forecastingMethod = _MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName];
71
+ if (forecastingMethod)
72
+ return forecastingMethod;
73
+ throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`);
74
+ }
75
+ get transformer() {
76
+ const pathExpression = (0, import_assert.assertEx)(this.config.jsonPathExpression, "Missing jsonPathExpression in config");
77
+ return getJsonPathTransformer(pathExpression);
78
+ }
79
+ async getPayloadsInWindow(startTimestamp, stopTimestamp) {
80
+ const addresses = this.config.witnessAddresses;
81
+ const payload_schemas = [(0, import_assert.assertEx)(this.config.witnessSchema, "Missing witnessSchema in config")];
82
+ const payloads = [];
83
+ const archivist = (0, import_archivist_model.asArchivistInstance)(await this.readArchivist(), "Unable to resolve archivist");
84
+ const bwDiviner = (0, import_diviner_model.asDivinerInstance)((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
85
+ const limit = this.batchLimit;
86
+ const witnessSchema = (0, import_assert.assertEx)(this.config.witnessSchema, "Missing witnessSchema in config");
87
+ let timestamp = stopTimestamp;
88
+ let more = true;
89
+ while (more || payloads.length < this.maxTrainingLength) {
90
+ const query = { addresses, limit, payload_schemas, schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema, timestamp };
91
+ const boundWitnesses = (await bwDiviner.divine([query])).filter(
92
+ (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp
93
+ );
94
+ if (boundWitnesses.length === 0)
95
+ break;
96
+ timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(import_exists.exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
97
+ if (timestamp === Number.MAX_SAFE_INTEGER)
98
+ break;
99
+ more = boundWitnesses.length === limit;
100
+ const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(import_exists.exists);
101
+ if (hashes.length !== 0) {
102
+ const batchPayloads = (await archivist.get(hashes)).filter(import_exists.exists);
103
+ payloads.push(...batchPayloads);
104
+ }
105
+ }
106
+ return payloads;
107
+ }
108
+ };
109
+ // Annotate the CommonJS export names for ESM import in node:
110
+ 0 && (module.exports = {
111
+ MemoryForecastingDiviner
112
+ });
113
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/MemoryForecastingDiviner.ts"],"sourcesContent":["export * from './MemoryForecastingDiviner'\n","import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { AbstractForecastingDiviner, ForecastingDivinerParams } from '@xyo-network/diviner-forecasting-abstract'\nimport {\n arimaForecastingMethod,\n arimaForecastingName,\n seasonalArimaForecastingMethod,\n seasonalArimaForecastingName,\n} from '@xyo-network/diviner-forecasting-method-arima'\nimport { ForecastingDivinerConfigSchema, ForecastingMethod, PayloadValueTransformer } from '@xyo-network/diviner-forecasting-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport jsonpath from 'jsonpath'\n\nexport type SupportedForecastingType = typeof arimaForecastingName | typeof seasonalArimaForecastingName\n\nconst getJsonPathTransformer = (pathExpression: string): PayloadValueTransformer => {\n const transformer = (x: Payload): number => {\n // eslint-disable-next-line import/no-named-as-default-member\n const ret = jsonpath.value(x, pathExpression)\n if (typeof ret === 'number') return ret\n throw new Error('Parsed invalid payload value')\n }\n return transformer\n}\n\nexport class MemoryForecastingDiviner<\n TParams extends ForecastingDivinerParams = ForecastingDivinerParams,\n> extends AbstractForecastingDiviner<TParams> {\n static override configSchemas = [ForecastingDivinerConfigSchema]\n\n protected static readonly forecastingMethodDict: Record<SupportedForecastingType, ForecastingMethod> = {\n arimaForecasting: arimaForecastingMethod,\n seasonalArimaForecasting: seasonalArimaForecastingMethod,\n }\n\n /**\n * The max number of records to search during the batch query\n */\n protected readonly batchLimit = 1_000\n\n // TODO: Inject via config\n protected readonly maxTrainingLength = 10_000\n\n protected override get forecastingMethod(): ForecastingMethod {\n const forecastingMethodName = assertEx(this.config.forecastingMethod, 'Missing forecastingMethod in config') as SupportedForecastingType\n const forecastingMethod = MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName]\n if (forecastingMethod) return forecastingMethod\n throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`)\n }\n\n protected override get transformer(): PayloadValueTransformer {\n const pathExpression = assertEx(this.config.jsonPathExpression, 'Missing jsonPathExpression in config')\n return getJsonPathTransformer(pathExpression)\n }\n\n protected override async getPayloadsInWindow(startTimestamp: number, stopTimestamp: number): Promise<Payload[]> {\n const addresses = this.config.witnessAddresses\n const payload_schemas = [assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')]\n const payloads: Payload[] = []\n const archivist = asArchivistInstance(await this.readArchivist(), 'Unable to resolve archivist')\n const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), 'Unable to resolve boundWitnessDiviner')\n const limit = this.batchLimit\n const witnessSchema = assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')\n let timestamp = stopTimestamp\n let more = true\n\n // TODO: Window size vs sample size\n // Loop until there are no more BWs to process or we've got enough payloads to satisfy the training window\n while (more || payloads.length < this.maxTrainingLength) {\n const query: BoundWitnessDivinerQueryPayload = { addresses, limit, payload_schemas, schema: BoundWitnessDivinerQuerySchema, timestamp }\n const boundWitnesses = ((await bwDiviner.divine([query])) as BoundWitness[]).filter(\n (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp,\n )\n if (boundWitnesses.length === 0) break\n\n // Update the timestamp value for the next batch\n timestamp = boundWitnesses\n .map((bw) => bw.timestamp)\n .filter(exists)\n .reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER)\n if (timestamp === Number.MAX_SAFE_INTEGER) break\n\n // Set the more flag to false if there are fewer documents returned than the batch size\n more = boundWitnesses.length === limit\n\n // Get the corresponding payload hashes from the BWs\n const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(exists)\n\n // Get the payloads corresponding to the BW hashes from the archivist\n if (hashes.length !== 0) {\n const batchPayloads = (await archivist.get(hashes)).filter(exists)\n payloads.push(...batchPayloads)\n }\n }\n return payloads\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,oBAAuB;AACvB,6BAAoC;AAEpC,wCAAgF;AAChF,0CAAqE;AACrE,8CAKO;AACP,uCAA2F;AAC3F,2BAAkC;AAElC,sBAAqB;AAIrB,IAAM,yBAAyB,CAAC,mBAAoD;AAClF,QAAM,cAAc,CAAC,MAAuB;AAE1C,UAAM,MAAM,gBAAAA,QAAS,MAAM,GAAG,cAAc;AAC5C,QAAI,OAAO,QAAQ;AAAU,aAAO;AACpC,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AACA,SAAO;AACT;AAEO,IAAM,2BAAN,MAAM,kCAEH,+DAAoC;AAAA,EAC5C,OAAgB,gBAAgB,CAAC,+DAA8B;AAAA,EAE/D,OAA0B,wBAA6E;AAAA,IACrG,kBAAkB;AAAA,IAClB,0BAA0B;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKmB,aAAa;AAAA;AAAA,EAGb,oBAAoB;AAAA,EAEvC,IAAuB,oBAAuC;AAC5D,UAAM,4BAAwB,wBAAS,KAAK,OAAO,mBAAmB,qCAAqC;AAC3G,UAAM,oBAAoB,0BAAyB,sBAAsB,qBAAqB;AAC9F,QAAI;AAAmB,aAAO;AAC9B,UAAM,IAAI,MAAM,mCAAmC,qBAAqB,EAAE;AAAA,EAC5E;AAAA,EAEA,IAAuB,cAAuC;AAC5D,UAAM,qBAA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AM;AACjE,iBAAS,KAAK,GAAG,aAAa;AAAA,MAChC;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;","names":["jsonpath"]}
@@ -1,63 +1,30 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- MemoryForecastingDiviner: () => MemoryForecastingDiviner
34
- });
35
- module.exports = __toCommonJS(src_exports);
36
-
37
1
  // src/MemoryForecastingDiviner.ts
38
- var import_assert = require("@xylabs/assert");
39
- var import_exists = require("@xylabs/exists");
40
- var import_archivist_model = require("@xyo-network/archivist-model");
41
- var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
42
- var import_diviner_forecasting_abstract = require("@xyo-network/diviner-forecasting-abstract");
43
- var import_diviner_forecasting_method_arima = require("@xyo-network/diviner-forecasting-method-arima");
44
- var import_diviner_forecasting_model = require("@xyo-network/diviner-forecasting-model");
45
- var import_diviner_model = require("@xyo-network/diviner-model");
46
- var import_jsonpath = __toESM(require("jsonpath"));
2
+ import { assertEx } from "@xylabs/assert";
3
+ import { exists } from "@xylabs/exists";
4
+ import { asArchivistInstance } from "@xyo-network/archivist-model";
5
+ import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
6
+ import { AbstractForecastingDiviner } from "@xyo-network/diviner-forecasting-abstract";
7
+ import {
8
+ arimaForecastingMethod,
9
+ seasonalArimaForecastingMethod
10
+ } from "@xyo-network/diviner-forecasting-method-arima";
11
+ import { ForecastingDivinerConfigSchema } from "@xyo-network/diviner-forecasting-model";
12
+ import { asDivinerInstance } from "@xyo-network/diviner-model";
13
+ import jsonpath from "jsonpath";
47
14
  var getJsonPathTransformer = (pathExpression) => {
48
15
  const transformer = (x) => {
49
- const ret = import_jsonpath.default.value(x, pathExpression);
16
+ const ret = jsonpath.value(x, pathExpression);
50
17
  if (typeof ret === "number")
51
18
  return ret;
52
19
  throw new Error("Parsed invalid payload value");
53
20
  };
54
21
  return transformer;
55
22
  };
56
- var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_diviner_forecasting_abstract.AbstractForecastingDiviner {
57
- static configSchemas = [import_diviner_forecasting_model.ForecastingDivinerConfigSchema];
23
+ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractForecastingDiviner {
24
+ static configSchemas = [ForecastingDivinerConfigSchema];
58
25
  static forecastingMethodDict = {
59
- arimaForecasting: import_diviner_forecasting_method_arima.arimaForecastingMethod,
60
- seasonalArimaForecasting: import_diviner_forecasting_method_arima.seasonalArimaForecastingMethod
26
+ arimaForecasting: arimaForecastingMethod,
27
+ seasonalArimaForecasting: seasonalArimaForecastingMethod
61
28
  };
62
29
  /**
63
30
  * The max number of records to search during the batch query
@@ -66,48 +33,47 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_di
66
33
  // TODO: Inject via config
67
34
  maxTrainingLength = 1e4;
68
35
  get forecastingMethod() {
69
- const forecastingMethodName = (0, import_assert.assertEx)(this.config.forecastingMethod, "Missing forecastingMethod in config");
36
+ const forecastingMethodName = assertEx(this.config.forecastingMethod, "Missing forecastingMethod in config");
70
37
  const forecastingMethod = _MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName];
71
38
  if (forecastingMethod)
72
39
  return forecastingMethod;
73
40
  throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`);
74
41
  }
75
42
  get transformer() {
76
- const pathExpression = (0, import_assert.assertEx)(this.config.jsonPathExpression, "Missing jsonPathExpression in config");
43
+ const pathExpression = assertEx(this.config.jsonPathExpression, "Missing jsonPathExpression in config");
77
44
  return getJsonPathTransformer(pathExpression);
78
45
  }
79
46
  async getPayloadsInWindow(startTimestamp, stopTimestamp) {
80
47
  const addresses = this.config.witnessAddresses;
81
- const payload_schemas = [(0, import_assert.assertEx)(this.config.witnessSchema, "Missing witnessSchema in config")];
48
+ const payload_schemas = [assertEx(this.config.witnessSchema, "Missing witnessSchema in config")];
82
49
  const payloads = [];
83
- const archivist = (0, import_archivist_model.asArchivistInstance)(await this.readArchivist(), "Unable to resolve archivist");
84
- const bwDiviner = (0, import_diviner_model.asDivinerInstance)((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
50
+ const archivist = asArchivistInstance(await this.readArchivist(), "Unable to resolve archivist");
51
+ const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
85
52
  const limit = this.batchLimit;
86
- const witnessSchema = (0, import_assert.assertEx)(this.config.witnessSchema, "Missing witnessSchema in config");
53
+ const witnessSchema = assertEx(this.config.witnessSchema, "Missing witnessSchema in config");
87
54
  let timestamp = stopTimestamp;
88
55
  let more = true;
89
56
  while (more || payloads.length < this.maxTrainingLength) {
90
- const query = { addresses, limit, payload_schemas, schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema, timestamp };
57
+ const query = { addresses, limit, payload_schemas, schema: BoundWitnessDivinerQuerySchema, timestamp };
91
58
  const boundWitnesses = (await bwDiviner.divine([query])).filter(
92
59
  (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp
93
60
  );
94
61
  if (boundWitnesses.length === 0)
95
62
  break;
96
- timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(import_exists.exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
63
+ timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
97
64
  if (timestamp === Number.MAX_SAFE_INTEGER)
98
65
  break;
99
66
  more = boundWitnesses.length === limit;
100
- const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(import_exists.exists);
67
+ const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(exists);
101
68
  if (hashes.length !== 0) {
102
- const batchPayloads = (await archivist.get(hashes)).filter(import_exists.exists);
69
+ const batchPayloads = (await archivist.get(hashes)).filter(exists);
103
70
  payloads.push(...batchPayloads);
104
71
  }
105
72
  }
106
73
  return payloads;
107
74
  }
108
75
  };
109
- // Annotate the CommonJS export names for ESM import in node:
110
- 0 && (module.exports = {
76
+ export {
111
77
  MemoryForecastingDiviner
112
- });
78
+ };
113
79
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/MemoryForecastingDiviner.ts"],"sourcesContent":["export * from './MemoryForecastingDiviner'\n","import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { AbstractForecastingDiviner, ForecastingDivinerParams } from '@xyo-network/diviner-forecasting-abstract'\nimport {\n arimaForecastingMethod,\n arimaForecastingName,\n seasonalArimaForecastingMethod,\n seasonalArimaForecastingName,\n} from '@xyo-network/diviner-forecasting-method-arima'\nimport { ForecastingDivinerConfigSchema, ForecastingMethod, PayloadValueTransformer } from '@xyo-network/diviner-forecasting-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport jsonpath from 'jsonpath'\n\nexport type SupportedForecastingType = typeof arimaForecastingName | typeof seasonalArimaForecastingName\n\nconst getJsonPathTransformer = (pathExpression: string): PayloadValueTransformer => {\n const transformer = (x: Payload): number => {\n // eslint-disable-next-line import/no-named-as-default-member\n const ret = jsonpath.value(x, pathExpression)\n if (typeof ret === 'number') return ret\n throw new Error('Parsed invalid payload value')\n }\n return transformer\n}\n\nexport class MemoryForecastingDiviner<\n TParams extends ForecastingDivinerParams = ForecastingDivinerParams,\n> extends AbstractForecastingDiviner<TParams> {\n static override configSchemas = [ForecastingDivinerConfigSchema]\n\n protected static readonly forecastingMethodDict: Record<SupportedForecastingType, ForecastingMethod> = {\n arimaForecasting: arimaForecastingMethod,\n seasonalArimaForecasting: seasonalArimaForecastingMethod,\n }\n\n /**\n * The max number of records to search during the batch query\n */\n protected readonly batchLimit = 1_000\n\n // TODO: Inject via config\n protected readonly maxTrainingLength = 10_000\n\n protected override get forecastingMethod(): ForecastingMethod {\n const forecastingMethodName = assertEx(this.config.forecastingMethod, 'Missing forecastingMethod in config') as SupportedForecastingType\n const forecastingMethod = MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName]\n if (forecastingMethod) return forecastingMethod\n throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`)\n }\n\n protected override get transformer(): PayloadValueTransformer {\n const pathExpression = assertEx(this.config.jsonPathExpression, 'Missing jsonPathExpression in config')\n return getJsonPathTransformer(pathExpression)\n }\n\n protected override async getPayloadsInWindow(startTimestamp: number, stopTimestamp: number): Promise<Payload[]> {\n const addresses = this.config.witnessAddresses\n const payload_schemas = [assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')]\n const payloads: Payload[] = []\n const archivist = asArchivistInstance(await this.readArchivist(), 'Unable to resolve archivist')\n const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), 'Unable to resolve boundWitnessDiviner')\n const limit = this.batchLimit\n const witnessSchema = assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')\n let timestamp = stopTimestamp\n let more = true\n\n // TODO: Window size vs sample size\n // Loop until there are no more BWs to process or we've got enough payloads to satisfy the training window\n while (more || payloads.length < this.maxTrainingLength) {\n const query: BoundWitnessDivinerQueryPayload = { addresses, limit, payload_schemas, schema: BoundWitnessDivinerQuerySchema, timestamp }\n const boundWitnesses = ((await bwDiviner.divine([query])) as BoundWitness[]).filter(\n (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp,\n )\n if (boundWitnesses.length === 0) break\n\n // Update the timestamp value for the next batch\n timestamp = boundWitnesses\n .map((bw) => bw.timestamp)\n .filter(exists)\n .reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER)\n if (timestamp === Number.MAX_SAFE_INTEGER) break\n\n // Set the more flag to false if there are fewer documents returned than the batch size\n more = boundWitnesses.length === limit\n\n // Get the corresponding payload hashes from the BWs\n const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(exists)\n\n // Get the payloads corresponding to the BW hashes from the archivist\n if (hashes.length !== 0) {\n const batchPayloads = (await archivist.get(hashes)).filter(exists)\n payloads.push(...batchPayloads)\n }\n }\n return payloads\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACA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AAG,uCAAuC;AACxI,UAAM,QAAQ,KAAK;AACnB,UAAM,oBAAgB,wBAAS,KAAK,OAAO,eAAe,iCAAiC;AAC3F,QAAI,YAAY;AAChB,QAAI,OAAO;AAIX,WAAO,QAAQ,SAAS,SAAS,KAAK,mBAAmB;AACvD,YAAM,QAAyC,EAAE,WAAW,OAAO,iBAAiB,QAAQ,kEAAgC,UAAU;AACtI,YAAM,kBAAmB,MAAM,UAAU,OAAO,CAAC,KAAK,CAAC,GAAsB;AAAA,QAC3E,CAAC,OAAO,GAAG,aAAa,GAAG,aAAa,kBAAkB,GAAG,aAAa;AAAA,MAC5E;AACA,UAAI,eAAe,WAAW;AAAG;AAGjC,kBAAY,eACT,IAAI,CAAC,OAAO,GAAG,SAAS,EACxB,OAAO,oBAAM,EACb,OAAO,CAAC,GAAG,MAAM,KAAK,IAAI,GAAG,CAAC,GAAG,OAAO,gBAAgB;AAC3D,UAAI,cAAc,OAAO;AAAkB;AAG3C,aAAO,eAAe,WAAW;AAGjC,YAAM,SAAS,eAAe,IAAI,CAAC,OAAO,GAAG,eAAe,GAAG,gBAAgB,UAAU,CAAC,MAAM,MAAM,aAAa,CAAC,CAAC,EAAE,OAAO,oBAAM;AAGpI,UAAI,OAAO,WAAW,GAAG;AACvB,cAAM,iBAAiB,MAAM,UAAU,IAAI,MAAM,GAAG,OAAO,oBAAM;AACjE,iBAAS,KAAK,GAAG,aAAa;AAAA,MAChC;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;","names":["jsonpath"]}
1
+ {"version":3,"sources":["../../src/MemoryForecastingDiviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { AbstractForecastingDiviner, ForecastingDivinerParams } from '@xyo-network/diviner-forecasting-abstract'\nimport {\n arimaForecastingMethod,\n arimaForecastingName,\n seasonalArimaForecastingMethod,\n seasonalArimaForecastingName,\n} from '@xyo-network/diviner-forecasting-method-arima'\nimport { ForecastingDivinerConfigSchema, ForecastingMethod, PayloadValueTransformer } from '@xyo-network/diviner-forecasting-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport jsonpath from 'jsonpath'\n\nexport type SupportedForecastingType = typeof arimaForecastingName | typeof seasonalArimaForecastingName\n\nconst getJsonPathTransformer = (pathExpression: string): PayloadValueTransformer => {\n const transformer = (x: Payload): number => {\n // eslint-disable-next-line import/no-named-as-default-member\n const ret = jsonpath.value(x, pathExpression)\n if (typeof ret === 'number') return ret\n throw new Error('Parsed invalid payload value')\n }\n return transformer\n}\n\nexport class MemoryForecastingDiviner<\n TParams extends ForecastingDivinerParams = ForecastingDivinerParams,\n> extends AbstractForecastingDiviner<TParams> {\n static override configSchemas = [ForecastingDivinerConfigSchema]\n\n protected static readonly forecastingMethodDict: Record<SupportedForecastingType, ForecastingMethod> = {\n arimaForecasting: arimaForecastingMethod,\n seasonalArimaForecasting: seasonalArimaForecastingMethod,\n }\n\n /**\n * The max number of records to search during the batch query\n */\n protected readonly batchLimit = 1_000\n\n // TODO: Inject via config\n protected readonly maxTrainingLength = 10_000\n\n protected override get forecastingMethod(): ForecastingMethod {\n const forecastingMethodName = assertEx(this.config.forecastingMethod, 'Missing forecastingMethod in config') as SupportedForecastingType\n const forecastingMethod = MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName]\n if (forecastingMethod) return forecastingMethod\n throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`)\n }\n\n protected override get transformer(): PayloadValueTransformer {\n const pathExpression = assertEx(this.config.jsonPathExpression, 'Missing jsonPathExpression in config')\n return getJsonPathTransformer(pathExpression)\n }\n\n protected override async getPayloadsInWindow(startTimestamp: number, stopTimestamp: number): Promise<Payload[]> {\n const addresses = this.config.witnessAddresses\n const payload_schemas = [assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')]\n const payloads: Payload[] = []\n const archivist = asArchivistInstance(await this.readArchivist(), 'Unable to resolve archivist')\n const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), 'Unable to resolve boundWitnessDiviner')\n const limit = this.batchLimit\n const witnessSchema = assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')\n let timestamp = stopTimestamp\n let more = true\n\n // TODO: Window size vs sample size\n // Loop until there are no more BWs to process or we've got enough payloads to satisfy the training window\n while (more || payloads.length < this.maxTrainingLength) {\n const query: BoundWitnessDivinerQueryPayload = { addresses, limit, payload_schemas, schema: BoundWitnessDivinerQuerySchema, timestamp }\n const boundWitnesses = ((await bwDiviner.divine([query])) as BoundWitness[]).filter(\n (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp,\n )\n if (boundWitnesses.length === 0) break\n\n // Update the timestamp value for the next batch\n timestamp = boundWitnesses\n .map((bw) => bw.timestamp)\n .filter(exists)\n .reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER)\n if (timestamp === Number.MAX_SAFE_INTEGER) break\n\n // Set the more flag to false if there are fewer documents returned than the batch size\n more = boundWitnesses.length === limit\n\n // Get the corresponding payload hashes from the BWs\n const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(exists)\n\n // Get the payloads corresponding to the BW hashes from the archivist\n if (hashes.length !== 0) {\n const batchPayloads = (await archivist.get(hashes)).filter(exists)\n payloads.push(...batchPayloads)\n }\n }\n return payloads\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,cAAc;AACvB,SAAS,2BAA2B;AAEpC,SAA0C,sCAAsC;AAChF,SAAS,kCAA4D;AACrE;AAAA,EACE;AAAA,EAEA;AAAA,OAEK;AACP,SAAS,sCAAkF;AAC3F,SAAS,yBAAyB;AAElC,OAAO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,WAAO,QAAQ,SAAS,SAAS,KAAK,mBAAmB;AACvD,YAAM,QAAyC,EAAE,WAAW,OAAO,iBAAiB,QAAQ,gCAAgC,UAAU;AACtI,YAAM,kBAAmB,MAAM,UAAU,OAAO,CAAC,KAAK,CAAC,GAAsB;AAAA,QAC3E,CAAC,OAAO,GAAG,aAAa,GAAG,aAAa,kBAAkB,GAAG,aAAa;AAAA,MAC5E;AACA,UAAI,eAAe,WAAW;AAAG;AAGjC,kBAAY,eACT,IAAI,CAAC,OAAO,GAAG,SAAS,EACxB,OAAO,MAAM,EACb,OAAO,CAAC,GAAG,MAAM,KAAK,IAAI,GAAG,CAAC,GAAG,OAAO,gBAAgB;AAC3D,UAAI,cAAc,OAAO;AAAkB;AAG3C,aAAO,eAAe,WAAW;AAGjC,YAAM,SAAS,eAAe,IAAI,CAAC,OAAO,GAAG,eAAe,GAAG,gBAAgB,UAAU,CAAC,MAAM,MAAM,aAAa,CAAC,CAAC,EAAE,OAAO,MAAM;AAGpI,UAAI,OAAO,WAAW,GAAG;AACvB,cAAM,iBAAiB,MAAM,UAAU,IAAI,MAAM,GAAG,OAAO,MAAM;AACjE,iBAAS,KAAK,GAAG,aAAa;AAAA,MAChC;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
package/package.json CHANGED
@@ -12,24 +12,23 @@
12
12
  "dependencies": {
13
13
  "@xylabs/assert": "^2.13.20",
14
14
  "@xylabs/exists": "^2.13.20",
15
- "@xyo-network/archivist-model": "~2.84.1",
16
- "@xyo-network/boundwitness-model": "~2.84.1",
17
- "@xyo-network/diviner-boundwitness-model": "~2.84.1",
18
- "@xyo-network/diviner-forecasting-abstract": "~2.84.1",
19
- "@xyo-network/diviner-forecasting-method-arima": "~2.84.1",
20
- "@xyo-network/diviner-forecasting-model": "~2.84.1",
21
- "@xyo-network/diviner-model": "~2.84.1",
22
- "@xyo-network/payload-model": "~2.84.1",
15
+ "@xyo-network/archivist-model": "~2.84.3",
16
+ "@xyo-network/boundwitness-model": "~2.84.3",
17
+ "@xyo-network/diviner-boundwitness-model": "~2.84.3",
18
+ "@xyo-network/diviner-forecasting-abstract": "~2.84.3",
19
+ "@xyo-network/diviner-forecasting-method-arima": "~2.84.3",
20
+ "@xyo-network/diviner-forecasting-model": "~2.84.3",
21
+ "@xyo-network/diviner-model": "~2.84.3",
22
+ "@xyo-network/payload-model": "~2.84.3",
23
23
  "jsonpath": "^1.1.1"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/jsonpath": "^0.2.4",
27
- "@xylabs/ts-scripts-yarn3": "^3.2.10",
28
- "@xylabs/tsconfig": "^3.2.10",
27
+ "@xylabs/ts-scripts-yarn3": "^3.2.19",
28
+ "@xylabs/tsconfig": "^3.2.19",
29
29
  "typescript": "^5.3.3"
30
30
  },
31
31
  "description": "Primary SDK for using XYO Protocol 2.0",
32
- "docs": "dist/docs.json",
33
32
  "types": "dist/node/index.d.ts",
34
33
  "exports": {
35
34
  ".": {
@@ -45,19 +44,19 @@
45
44
  },
46
45
  "node": {
47
46
  "require": {
48
- "types": "./dist/node/index.d.ts",
49
- "default": "./dist/node/index.js"
47
+ "types": "./dist/node/index.d.cts",
48
+ "default": "./dist/node/index.cjs"
50
49
  },
51
50
  "import": {
52
51
  "types": "./dist/node/index.d.mts",
53
- "default": "./dist/node/index.mjs"
52
+ "default": "./dist/node/index.js"
54
53
  }
55
54
  }
56
55
  },
57
56
  "./package.json": "./package.json"
58
57
  },
59
- "main": "dist/node/index.js",
60
- "module": "dist/node/index.mjs",
58
+ "main": "dist/node/index.cjs",
59
+ "module": "dist/node/index.js",
61
60
  "homepage": "https://xyo.network",
62
61
  "license": "LGPL-3.0-only",
63
62
  "publishConfig": {
@@ -68,5 +67,6 @@
68
67
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
69
68
  },
70
69
  "sideEffects": false,
71
- "version": "2.84.1"
70
+ "version": "2.84.3",
71
+ "type": "module"
72
72
  }
@@ -1,79 +0,0 @@
1
- // src/MemoryForecastingDiviner.ts
2
- import { assertEx } from "@xylabs/assert";
3
- import { exists } from "@xylabs/exists";
4
- import { asArchivistInstance } from "@xyo-network/archivist-model";
5
- import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
6
- import { AbstractForecastingDiviner } from "@xyo-network/diviner-forecasting-abstract";
7
- import {
8
- arimaForecastingMethod,
9
- seasonalArimaForecastingMethod
10
- } from "@xyo-network/diviner-forecasting-method-arima";
11
- import { ForecastingDivinerConfigSchema } from "@xyo-network/diviner-forecasting-model";
12
- import { asDivinerInstance } from "@xyo-network/diviner-model";
13
- import jsonpath from "jsonpath";
14
- var getJsonPathTransformer = (pathExpression) => {
15
- const transformer = (x) => {
16
- const ret = jsonpath.value(x, pathExpression);
17
- if (typeof ret === "number")
18
- return ret;
19
- throw new Error("Parsed invalid payload value");
20
- };
21
- return transformer;
22
- };
23
- var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractForecastingDiviner {
24
- static configSchemas = [ForecastingDivinerConfigSchema];
25
- static forecastingMethodDict = {
26
- arimaForecasting: arimaForecastingMethod,
27
- seasonalArimaForecasting: seasonalArimaForecastingMethod
28
- };
29
- /**
30
- * The max number of records to search during the batch query
31
- */
32
- batchLimit = 1e3;
33
- // TODO: Inject via config
34
- maxTrainingLength = 1e4;
35
- get forecastingMethod() {
36
- const forecastingMethodName = assertEx(this.config.forecastingMethod, "Missing forecastingMethod in config");
37
- const forecastingMethod = _MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName];
38
- if (forecastingMethod)
39
- return forecastingMethod;
40
- throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`);
41
- }
42
- get transformer() {
43
- const pathExpression = assertEx(this.config.jsonPathExpression, "Missing jsonPathExpression in config");
44
- return getJsonPathTransformer(pathExpression);
45
- }
46
- async getPayloadsInWindow(startTimestamp, stopTimestamp) {
47
- const addresses = this.config.witnessAddresses;
48
- const payload_schemas = [assertEx(this.config.witnessSchema, "Missing witnessSchema in config")];
49
- const payloads = [];
50
- const archivist = asArchivistInstance(await this.readArchivist(), "Unable to resolve archivist");
51
- const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
52
- const limit = this.batchLimit;
53
- const witnessSchema = assertEx(this.config.witnessSchema, "Missing witnessSchema in config");
54
- let timestamp = stopTimestamp;
55
- let more = true;
56
- while (more || payloads.length < this.maxTrainingLength) {
57
- const query = { addresses, limit, payload_schemas, schema: BoundWitnessDivinerQuerySchema, timestamp };
58
- const boundWitnesses = (await bwDiviner.divine([query])).filter(
59
- (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp
60
- );
61
- if (boundWitnesses.length === 0)
62
- break;
63
- timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
64
- if (timestamp === Number.MAX_SAFE_INTEGER)
65
- break;
66
- more = boundWitnesses.length === limit;
67
- const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(exists);
68
- if (hashes.length !== 0) {
69
- const batchPayloads = (await archivist.get(hashes)).filter(exists);
70
- payloads.push(...batchPayloads);
71
- }
72
- }
73
- return payloads;
74
- }
75
- };
76
- export {
77
- MemoryForecastingDiviner
78
- };
79
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/MemoryForecastingDiviner.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { exists } from '@xylabs/exists'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { AbstractForecastingDiviner, ForecastingDivinerParams } from '@xyo-network/diviner-forecasting-abstract'\nimport {\n arimaForecastingMethod,\n arimaForecastingName,\n seasonalArimaForecastingMethod,\n seasonalArimaForecastingName,\n} from '@xyo-network/diviner-forecasting-method-arima'\nimport { ForecastingDivinerConfigSchema, ForecastingMethod, PayloadValueTransformer } from '@xyo-network/diviner-forecasting-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport { Payload } from '@xyo-network/payload-model'\nimport jsonpath from 'jsonpath'\n\nexport type SupportedForecastingType = typeof arimaForecastingName | typeof seasonalArimaForecastingName\n\nconst getJsonPathTransformer = (pathExpression: string): PayloadValueTransformer => {\n const transformer = (x: Payload): number => {\n // eslint-disable-next-line import/no-named-as-default-member\n const ret = jsonpath.value(x, pathExpression)\n if (typeof ret === 'number') return ret\n throw new Error('Parsed invalid payload value')\n }\n return transformer\n}\n\nexport class MemoryForecastingDiviner<\n TParams extends ForecastingDivinerParams = ForecastingDivinerParams,\n> extends AbstractForecastingDiviner<TParams> {\n static override configSchemas = [ForecastingDivinerConfigSchema]\n\n protected static readonly forecastingMethodDict: Record<SupportedForecastingType, ForecastingMethod> = {\n arimaForecasting: arimaForecastingMethod,\n seasonalArimaForecasting: seasonalArimaForecastingMethod,\n }\n\n /**\n * The max number of records to search during the batch query\n */\n protected readonly batchLimit = 1_000\n\n // TODO: Inject via config\n protected readonly maxTrainingLength = 10_000\n\n protected override get forecastingMethod(): ForecastingMethod {\n const forecastingMethodName = assertEx(this.config.forecastingMethod, 'Missing forecastingMethod in config') as SupportedForecastingType\n const forecastingMethod = MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName]\n if (forecastingMethod) return forecastingMethod\n throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`)\n }\n\n protected override get transformer(): PayloadValueTransformer {\n const pathExpression = assertEx(this.config.jsonPathExpression, 'Missing jsonPathExpression in config')\n return getJsonPathTransformer(pathExpression)\n }\n\n protected override async getPayloadsInWindow(startTimestamp: number, stopTimestamp: number): Promise<Payload[]> {\n const addresses = this.config.witnessAddresses\n const payload_schemas = [assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')]\n const payloads: Payload[] = []\n const archivist = asArchivistInstance(await this.readArchivist(), 'Unable to resolve archivist')\n const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), 'Unable to resolve boundWitnessDiviner')\n const limit = this.batchLimit\n const witnessSchema = assertEx(this.config.witnessSchema, 'Missing witnessSchema in config')\n let timestamp = stopTimestamp\n let more = true\n\n // TODO: Window size vs sample size\n // Loop until there are no more BWs to process or we've got enough payloads to satisfy the training window\n while (more || payloads.length < this.maxTrainingLength) {\n const query: BoundWitnessDivinerQueryPayload = { addresses, limit, payload_schemas, schema: BoundWitnessDivinerQuerySchema, timestamp }\n const boundWitnesses = ((await bwDiviner.divine([query])) as BoundWitness[]).filter(\n (bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp,\n )\n if (boundWitnesses.length === 0) break\n\n // Update the timestamp value for the next batch\n timestamp = boundWitnesses\n .map((bw) => bw.timestamp)\n .filter(exists)\n .reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER)\n if (timestamp === Number.MAX_SAFE_INTEGER) break\n\n // Set the more flag to false if there are fewer documents returned than the batch size\n more = boundWitnesses.length === limit\n\n // Get the corresponding payload hashes from the BWs\n const hashes = boundWitnesses.map((bw) => bw.payload_hashes[bw.payload_schemas.findIndex((s) => s === witnessSchema)]).filter(exists)\n\n // Get the payloads corresponding to the BW hashes from the archivist\n if (hashes.length !== 0) {\n const batchPayloads = (await archivist.get(hashes)).filter(exists)\n payloads.push(...batchPayloads)\n }\n }\n return payloads\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,cAAc;AACvB,SAAS,2BAA2B;AAEpC,SAA0C,sCAAsC;AAChF,SAAS,kCAA4D;AACrE;AAAA,EACE;AAAA,EAEA;AAAA,OAEK;AACP,SAAS,sCAAkF;AAC3F,SAAS,yBAAyB;AAElC,OAAO,cAAc;AAIrB,IAAM,yBAAyB,CAAC,mBAAoD;A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