@xyo-network/diviner-forecasting-memory 2.75.3 → 2.75.4

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.
@@ -1,3 +1,4 @@
1
+ // src/MemoryForecastingDiviner.ts
1
2
  import { assertEx } from "@xylabs/assert";
2
3
  import { exists } from "@xylabs/exists";
3
4
  import { asArchivistInstance } from "@xyo-network/archivist-model";
@@ -10,7 +11,7 @@ import {
10
11
  import { ForecastingDivinerConfigSchema } from "@xyo-network/diviner-forecasting-model";
11
12
  import { asDivinerInstance } from "@xyo-network/diviner-model";
12
13
  import jsonpath from "jsonpath";
13
- const getJsonPathTransformer = (pathExpression) => {
14
+ var getJsonPathTransformer = (pathExpression) => {
14
15
  const transformer = (x) => {
15
16
  const ret = jsonpath.value(x, pathExpression);
16
17
  if (typeof ret === "number")
@@ -19,7 +20,7 @@ const getJsonPathTransformer = (pathExpression) => {
19
20
  };
20
21
  return transformer;
21
22
  };
22
- class MemoryForecastingDiviner extends AbstractForecastingDiviner {
23
+ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractForecastingDiviner {
23
24
  static configSchemas = [ForecastingDivinerConfigSchema];
24
25
  static forecastingMethodDict = {
25
26
  arimaForecasting: arimaForecastingMethod,
@@ -33,7 +34,7 @@ class MemoryForecastingDiviner extends AbstractForecastingDiviner {
33
34
  maxTrainingLength = 1e4;
34
35
  get forecastingMethod() {
35
36
  const forecastingMethodName = assertEx(this.config.forecastingMethod, "Missing forecastingMethod in config");
36
- const forecastingMethod = MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName];
37
+ const forecastingMethod = _MemoryForecastingDiviner.forecastingMethodDict[forecastingMethodName];
37
38
  if (forecastingMethod)
38
39
  return forecastingMethod;
39
40
  throw new Error(`Unsupported forecasting method: ${forecastingMethodName}`);
@@ -71,7 +72,7 @@ class MemoryForecastingDiviner extends AbstractForecastingDiviner {
71
72
  }
72
73
  return payloads;
73
74
  }
74
- }
75
+ };
75
76
  export {
76
77
  MemoryForecastingDiviner
77
78
  };
@@ -1 +1 @@
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,MAAM,yBAAyB,CAAC,mBAAoD;AAClF,QAAM,cAAc,CAAC,MAAuB;AAE1C,UAAM,MAAM,SAAS,MAAM,GAAG,cAAc;AAC5C,QAAI,OAAO,QAAQ;AAAU,aAAO;AACpC,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AACA,SAAO;AACT;AAEO,MAAM,iCAEH,2BAAoC;AAAA,EAC5C,OAAgB,gBAAgB,CAAC,8BAA8B;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,wBAAwB,SAAS,KAAK,OAAO,mBAAmB,qCAAqC;AAC3G,UAAM,oBAAoB,yBAAyB,sBAAsB,qBAAqB;AAC9F,QAAI;AAAmB,aAAO;AAC9B,UAAM,IAAI,MAAM,mCAAmC,qBAAqB,EAAE;AAAA,EAC5E;AAAA,EAEA,IAAuB,cAAuC;AAC5D,UAAM,iBAAiB,SAAS,KAAK,OAAO,oBAAoB,sCAAsC;AACtG,WAAO,uBAAuB,cAAc;AAAA,EAC9C;AAAA,EAEA,MAAyB,oBAAoB,gBAAwB,eAA2C;AAC9G,UAAM,YAAY,KAAK,OAAO;AAC9B,UAAM,kBAAkB,CAAC,SAAS,KAAK,OAAO,eAAe,iCAAiC,CAAC;AAC/F,UAAM,WAAsB,CAAC;AAC7B,UAAM,YAAY,oBAAoB,MAAM,KAAK,cAAc,GAAG,6BAA6B;AAC/F,UAAM,YAAY,mBAAmB,MAAM,KAAK,QAAQ,KAAK,OAAO,mBAAmB,GAAG,IAAI,GAAG,uCAAuC;AACxI,UAAM,QAAQ,KAAK;AACnB,UAAM,gBAAgB,SAAS,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,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":[]}
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":";AAA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AmB,MAAM,KAAK,QAAQ,KAAK,OAAO,mBAAmB,GAAG,IAAI,GAAG,uCAAuC;AACxI,UAAM,QAAQ,KAAK;AACnB,UAAM,gBAAgB,SAAS,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,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":[]}
@@ -1,2 +1,79 @@
1
- export * from "./MemoryForecastingDiviner";
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
+ };
2
79
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './MemoryForecastingDiviner'\n"],"mappings":"AAAA,cAAc;","names":[]}
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;AAClF,QAA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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/dist/docs.json CHANGED
@@ -3848,7 +3848,7 @@
3848
3848
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
3849
3849
  "line": 43,
3850
3850
  "character": 21,
3851
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L43"
3851
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L43"
3852
3852
  }
3853
3853
  ],
3854
3854
  "type": {
@@ -4046,7 +4046,7 @@
4046
4046
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
4047
4047
  "line": 46,
4048
4048
  "character": 21,
4049
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L46"
4049
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L46"
4050
4050
  }
4051
4051
  ],
4052
4052
  "type": {
@@ -4368,7 +4368,7 @@
4368
4368
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
4369
4369
  "line": 33,
4370
4370
  "character": 18,
4371
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L33"
4371
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L33"
4372
4372
  }
4373
4373
  ],
4374
4374
  "type": {
@@ -4456,7 +4456,7 @@
4456
4456
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
4457
4457
  "line": 35,
4458
4458
  "character": 28,
4459
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L35"
4459
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L35"
4460
4460
  }
4461
4461
  ],
4462
4462
  "type": {
@@ -4834,7 +4834,7 @@
4834
4834
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
4835
4835
  "line": 48,
4836
4836
  "character": 25,
4837
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L48"
4837
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L48"
4838
4838
  }
4839
4839
  ],
4840
4840
  "getSignature": {
@@ -4848,7 +4848,7 @@
4848
4848
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
4849
4849
  "line": 48,
4850
4850
  "character": 2,
4851
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L48"
4851
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L48"
4852
4852
  }
4853
4853
  ],
4854
4854
  "type": {
@@ -5194,7 +5194,7 @@
5194
5194
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
5195
5195
  "line": 55,
5196
5196
  "character": 25,
5197
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L55"
5197
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L55"
5198
5198
  }
5199
5199
  ],
5200
5200
  "getSignature": {
@@ -5208,7 +5208,7 @@
5208
5208
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
5209
5209
  "line": 55,
5210
5210
  "character": 2,
5211
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L55"
5211
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L55"
5212
5212
  }
5213
5213
  ],
5214
5214
  "type": {
@@ -8867,7 +8867,7 @@
8867
8867
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
8868
8868
  "line": 60,
8869
8869
  "character": 27,
8870
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L60"
8870
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L60"
8871
8871
  }
8872
8872
  ],
8873
8873
  "signatures": [
@@ -8882,7 +8882,7 @@
8882
8882
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
8883
8883
  "line": 60,
8884
8884
  "character": 2,
8885
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L60"
8885
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L60"
8886
8886
  }
8887
8887
  ],
8888
8888
  "parameters": [
@@ -17689,7 +17689,7 @@
17689
17689
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
17690
17690
  "line": 30,
17691
17691
  "character": 13,
17692
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L30"
17692
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L30"
17693
17693
  }
17694
17694
  ],
17695
17695
  "typeParameters": [
@@ -17750,7 +17750,7 @@
17750
17750
  "fileName": "modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts",
17751
17751
  "line": 18,
17752
17752
  "character": 12,
17753
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/0141374586ea4/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L18"
17753
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/blob/996cd043c458a/packages/modules/packages/diviner/packages/forecasting/packages/memory/src/MemoryForecastingDiviner.ts#L18"
17754
17754
  }
17755
17755
  ],
17756
17756
  "type": {
package/package.json CHANGED
@@ -10,22 +10,22 @@
10
10
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
11
11
  },
12
12
  "dependencies": {
13
- "@xylabs/assert": "^2.12.10",
14
- "@xylabs/exists": "^2.12.10",
15
- "@xyo-network/archivist-model": "~2.75.3",
16
- "@xyo-network/boundwitness-model": "~2.75.3",
17
- "@xyo-network/diviner-boundwitness-model": "~2.75.3",
18
- "@xyo-network/diviner-forecasting-abstract": "~2.75.3",
19
- "@xyo-network/diviner-forecasting-method-arima": "~2.75.3",
20
- "@xyo-network/diviner-forecasting-model": "~2.75.3",
21
- "@xyo-network/diviner-model": "~2.75.3",
22
- "@xyo-network/payload-model": "~2.75.3",
13
+ "@xylabs/assert": "^2.12.14",
14
+ "@xylabs/exists": "^2.12.14",
15
+ "@xyo-network/archivist-model": "~2.75.4",
16
+ "@xyo-network/boundwitness-model": "~2.75.4",
17
+ "@xyo-network/diviner-boundwitness-model": "~2.75.4",
18
+ "@xyo-network/diviner-forecasting-abstract": "~2.75.4",
19
+ "@xyo-network/diviner-forecasting-method-arima": "~2.75.4",
20
+ "@xyo-network/diviner-forecasting-model": "~2.75.4",
21
+ "@xyo-network/diviner-model": "~2.75.4",
22
+ "@xyo-network/payload-model": "~2.75.4",
23
23
  "jsonpath": "^1.1.1"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/jsonpath": "^0.2.1",
27
- "@xylabs/ts-scripts-yarn3": "^3.0.77",
28
- "@xylabs/tsconfig": "^3.0.77",
27
+ "@xylabs/ts-scripts-yarn3": "^3.0.79",
28
+ "@xylabs/tsconfig": "^3.0.79",
29
29
  "typescript": "^5.2.2"
30
30
  },
31
31
  "description": "Primary SDK for using XYO Protocol 2.0",
@@ -71,5 +71,5 @@
71
71
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
72
72
  },
73
73
  "sideEffects": false,
74
- "version": "2.75.3"
74
+ "version": "2.75.4"
75
75
  }