@xyo-network/diviner-forecasting-memory 2.84.19 → 2.85.0
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/index.cjs +26 -12
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +29 -16
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +35 -17
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +38 -21
- package/dist/node/index.js.map +1 -1
- package/package.json +9 -9
package/dist/browser/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
9
|
var __export = (target, all) => {
|
|
9
10
|
for (var name in all)
|
|
10
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -44,24 +45,29 @@ var import_diviner_forecasting_method_arima = require("@xyo-network/diviner-fore
|
|
|
44
45
|
var import_diviner_forecasting_model = require("@xyo-network/diviner-forecasting-model");
|
|
45
46
|
var import_diviner_model = require("@xyo-network/diviner-model");
|
|
46
47
|
var import_jsonpath = __toESM(require("jsonpath"), 1);
|
|
47
|
-
var getJsonPathTransformer = (pathExpression) => {
|
|
48
|
-
const transformer = (x) => {
|
|
48
|
+
var getJsonPathTransformer = /* @__PURE__ */ __name((pathExpression) => {
|
|
49
|
+
const transformer = /* @__PURE__ */ __name((x) => {
|
|
49
50
|
const ret = import_jsonpath.default.value(x, pathExpression);
|
|
50
51
|
if (typeof ret === "number")
|
|
51
52
|
return ret;
|
|
52
53
|
throw new Error("Parsed invalid payload value");
|
|
53
|
-
};
|
|
54
|
+
}, "transformer");
|
|
54
55
|
return transformer;
|
|
55
|
-
};
|
|
56
|
+
}, "getJsonPathTransformer");
|
|
56
57
|
var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_diviner_forecasting_abstract.AbstractForecastingDiviner {
|
|
57
|
-
static
|
|
58
|
+
static {
|
|
59
|
+
__name(this, "MemoryForecastingDiviner");
|
|
60
|
+
}
|
|
61
|
+
static configSchemas = [
|
|
62
|
+
import_diviner_forecasting_model.ForecastingDivinerConfigSchema
|
|
63
|
+
];
|
|
58
64
|
static forecastingMethodDict = {
|
|
59
65
|
arimaForecasting: import_diviner_forecasting_method_arima.arimaForecastingMethod,
|
|
60
66
|
seasonalArimaForecasting: import_diviner_forecasting_method_arima.seasonalArimaForecastingMethod
|
|
61
67
|
};
|
|
62
68
|
/**
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
* The max number of records to search during the batch query
|
|
70
|
+
*/
|
|
65
71
|
batchLimit = 1e3;
|
|
66
72
|
// TODO: Inject via config
|
|
67
73
|
maxTrainingLength = 1e4;
|
|
@@ -78,7 +84,9 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_di
|
|
|
78
84
|
}
|
|
79
85
|
async getPayloadsInWindow(startTimestamp, stopTimestamp) {
|
|
80
86
|
const addresses = this.config.witnessAddresses;
|
|
81
|
-
const payload_schemas = [
|
|
87
|
+
const payload_schemas = [
|
|
88
|
+
(0, import_assert.assertEx)(this.config.witnessSchema, "Missing witnessSchema in config")
|
|
89
|
+
];
|
|
82
90
|
const payloads = [];
|
|
83
91
|
const archivist = (0, import_archivist_model.asArchivistInstance)(await this.getArchivist(), "Unable to resolve archivist");
|
|
84
92
|
const bwDiviner = (0, import_diviner_model.asDivinerInstance)((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
|
|
@@ -87,10 +95,16 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_di
|
|
|
87
95
|
let timestamp = stopTimestamp;
|
|
88
96
|
let more = true;
|
|
89
97
|
while (more || payloads.length < this.maxTrainingLength) {
|
|
90
|
-
const query = {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
const query = {
|
|
99
|
+
addresses,
|
|
100
|
+
limit,
|
|
101
|
+
payload_schemas,
|
|
102
|
+
schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema,
|
|
103
|
+
timestamp
|
|
104
|
+
};
|
|
105
|
+
const boundWitnesses = (await bwDiviner.divine([
|
|
106
|
+
query
|
|
107
|
+
])).filter((bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp);
|
|
94
108
|
if (boundWitnesses.length === 0)
|
|
95
109
|
break;
|
|
96
110
|
timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(import_exists.exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
|
|
@@ -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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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":"
|
|
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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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;;;;;;;ACAA,oBAAyB;AACzB,oBAAuB;AACvB,6BAAoC;AAEpC,wCAAgF;AAChF,0CAAqE;AACrE,8CAKO;AACP,uCAA2F;AAC3F,2BAAkC;AAElC,sBAAqB;AAIrB,IAAMA,yBAAyB,wBAACC,mBAAAA;AAC9B,QAAMC,cAAc,wBAACC,MAAAA;AAEnB,UAAMC,MAAMC,gBAAAA,QAASC,MAAMH,GAAGF,cAAAA;AAC9B,QAAI,OAAOG,QAAQ;AAAU,aAAOA;AACpC,UAAM,IAAIG,MAAM,8BAAA;EAClB,G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,CAACC,OAAOA,GAAGT,aAAaS,GAAGT,aAAahB,kBAAkByB,GAAGT,aAAaf,aAAAA;AAE5E,UAAIqB,eAAeJ,WAAW;AAAG;AAGjCF,kBAAYM,eACTI,IAAI,CAACD,OAAOA,GAAGT,SAAS,EACxBQ,OAAOG,oBAAAA,EAEPC,OAAO,CAACC,GAAGC,MAAMC,KAAKC,IAAIH,GAAGC,CAAAA,GAAIG,OAAOC,gBAAgB;AAC3D,UAAIlB,cAAciB,OAAOC;AAAkB;AAG3CjB,aAAOK,eAAeJ,WAAWH;AAGjC,YAAMoB,SAASb,eAAeI,IAAI,CAACD,OAAOA,GAAGW,eAAeX,GAAGrB,gBAAgBiC,QAAQhC,aAAAA,CAAAA,CAAe,EAAEmB,OAAOG,oBAAAA;AAG/G,UAAIQ,OAAOjB,SAAS,GAAG;AACrB,cAAMoB,iBAAiB,MAAM/B,UAAUgC,IAAIJ,MAAAA,GAASX,OAAOG,oBAAAA;AAC3DrB,iBAASkC,KAAI,GAAIF,aAAAA;MACnB;IACF;AACA,WAAOhC;EACT;AACF;","names":["getJsonPathTransformer","pathExpression","transformer","x","ret","jsonpath","value","Error","MemoryForecastingDiviner","AbstractForecastingDiviner","configSchemas","ForecastingDivinerConfigSchema","forecastingMethodDict","arimaForecasting","arimaForecastingMethod","seasonalArimaForecasting","seasonalArimaForecastingMethod","batchLimit","maxTrainingLength","forecastingMethod","forecastingMethodName","assertEx","config","jsonPathExpression","getPayloadsInWindow","startTimestamp","stopTimestamp","addresses","witnessAddresses","payload_schemas","witnessSchema","payloads","archivist","asArchivistInstance","getArchivist","bwDiviner","asDivinerInstance","resolve","boundWitnessDiviner","pop","limit","timestamp","more","length","query","schema","BoundWitnessDivinerQuerySchema","boundWitnesses","divine","filter","bw","map","exists","reduce","a","b","Math","min","Number","MAX_SAFE_INTEGER","hashes","payload_hashes","indexOf","batchPayloads","get","push"]}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
1
4
|
// src/MemoryForecastingDiviner.ts
|
|
2
5
|
import { assertEx } from "@xylabs/assert";
|
|
3
6
|
import { exists } from "@xylabs/exists";
|
|
4
7
|
import { asArchivistInstance } from "@xyo-network/archivist-model";
|
|
5
8
|
import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
|
|
6
9
|
import { AbstractForecastingDiviner } from "@xyo-network/diviner-forecasting-abstract";
|
|
7
|
-
import {
|
|
8
|
-
arimaForecastingMethod,
|
|
9
|
-
seasonalArimaForecastingMethod
|
|
10
|
-
} from "@xyo-network/diviner-forecasting-method-arima";
|
|
10
|
+
import { arimaForecastingMethod, seasonalArimaForecastingMethod } from "@xyo-network/diviner-forecasting-method-arima";
|
|
11
11
|
import { ForecastingDivinerConfigSchema } from "@xyo-network/diviner-forecasting-model";
|
|
12
12
|
import { asDivinerInstance } from "@xyo-network/diviner-model";
|
|
13
13
|
import jsonpath from "jsonpath";
|
|
14
|
-
var getJsonPathTransformer = (pathExpression) => {
|
|
15
|
-
const transformer = (x) => {
|
|
14
|
+
var getJsonPathTransformer = /* @__PURE__ */ __name((pathExpression) => {
|
|
15
|
+
const transformer = /* @__PURE__ */ __name((x) => {
|
|
16
16
|
const ret = jsonpath.value(x, pathExpression);
|
|
17
17
|
if (typeof ret === "number")
|
|
18
18
|
return ret;
|
|
19
19
|
throw new Error("Parsed invalid payload value");
|
|
20
|
-
};
|
|
20
|
+
}, "transformer");
|
|
21
21
|
return transformer;
|
|
22
|
-
};
|
|
22
|
+
}, "getJsonPathTransformer");
|
|
23
23
|
var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractForecastingDiviner {
|
|
24
|
-
static
|
|
24
|
+
static {
|
|
25
|
+
__name(this, "MemoryForecastingDiviner");
|
|
26
|
+
}
|
|
27
|
+
static configSchemas = [
|
|
28
|
+
ForecastingDivinerConfigSchema
|
|
29
|
+
];
|
|
25
30
|
static forecastingMethodDict = {
|
|
26
31
|
arimaForecasting: arimaForecastingMethod,
|
|
27
32
|
seasonalArimaForecasting: seasonalArimaForecastingMethod
|
|
28
33
|
};
|
|
29
34
|
/**
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
* The max number of records to search during the batch query
|
|
36
|
+
*/
|
|
32
37
|
batchLimit = 1e3;
|
|
33
38
|
// TODO: Inject via config
|
|
34
39
|
maxTrainingLength = 1e4;
|
|
@@ -45,7 +50,9 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractF
|
|
|
45
50
|
}
|
|
46
51
|
async getPayloadsInWindow(startTimestamp, stopTimestamp) {
|
|
47
52
|
const addresses = this.config.witnessAddresses;
|
|
48
|
-
const payload_schemas = [
|
|
53
|
+
const payload_schemas = [
|
|
54
|
+
assertEx(this.config.witnessSchema, "Missing witnessSchema in config")
|
|
55
|
+
];
|
|
49
56
|
const payloads = [];
|
|
50
57
|
const archivist = asArchivistInstance(await this.getArchivist(), "Unable to resolve archivist");
|
|
51
58
|
const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
|
|
@@ -54,10 +61,16 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractF
|
|
|
54
61
|
let timestamp = stopTimestamp;
|
|
55
62
|
let more = true;
|
|
56
63
|
while (more || payloads.length < this.maxTrainingLength) {
|
|
57
|
-
const query = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
const query = {
|
|
65
|
+
addresses,
|
|
66
|
+
limit,
|
|
67
|
+
payload_schemas,
|
|
68
|
+
schema: BoundWitnessDivinerQuerySchema,
|
|
69
|
+
timestamp
|
|
70
|
+
};
|
|
71
|
+
const boundWitnesses = (await bwDiviner.divine([
|
|
72
|
+
query
|
|
73
|
+
])).filter((bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp);
|
|
61
74
|
if (boundWitnesses.length === 0)
|
|
62
75
|
break;
|
|
63
76
|
timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
|
|
@@ -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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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":"
|
|
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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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,SAASA,gBAAgB;AACzB,SAASC,cAAc;AACvB,SAASC,2BAA2B;AAEpC,SAA0CC,sCAAsC;AAChF,SAASC,kCAA4D;AACrE,SACEC,wBAEAC,sCAEK;AACP,SAASC,sCAAkF;AAC3F,SAASC,yBAAyB;AAElC,OAAOC,cAAc;AAIrB,IAAMC,yBAAyB,wBAACC,mBAAAA;AAC9B,QAAMC,cAAc,wBAACC,MAAAA;AAEnB,UAAMC,MAAMC,SAASC,MAAMH,GAAGF,cAAAA;AAC9B,QAAI,OAAOG,QAAQ;AAAU,aAAOA;AACpC,UAAM,IAAIG,MAAM,8BAAA;EAClB,GALoB;AAMpB,SAAOL;AACT,GAR+B;AAUxB,IAAMM,2BAAN,MAAMA,kCAEHC,2BAAAA;EA/BV,OA+BUA;;;EACR,OAAgBC,gBAAgB;IAACC;;EAEjC,OAA0BC,wBAA6E;IACrGC,kBAAkBC;IAClBC,0BAA0BC;EAC5B;;;;EAKmBC,aAAa;;EAGbC,oBAAoB;EAEvC,IAAuBC,oBAAuC;AAC5D,UAAMC,wBAAwBC,SAAS,KAAKC,OAAOH,mBAAmB,qCAAA;AACtE,UAAMA,oBAAoBX,0BAAyBI,sBAAsBQ,qBAAAA;AACzE,QAAID;AAAmB,aAAOA;AAC9B,UAAM,IAAIZ,M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BAAgBiC,QAAQhC,aAAAA,CAAAA,CAAe,EAAEmB,OAAOG,MAAAA;AAG/G,UAAIQ,OAAOjB,SAAS,GAAG;AACrB,cAAMoB,iBAAiB,MAAM/B,UAAUgC,IAAIJ,MAAAA,GAASX,OAAOG,MAAAA;AAC3DrB,iBAASkC,KAAI,GAAIF,aAAAA;MACnB;IACF;AACA,WAAOhC;EACT;AACF;","names":["assertEx","exists","asArchivistInstance","BoundWitnessDivinerQuerySchema","AbstractForecastingDiviner","arimaForecastingMethod","seasonalArimaForecastingMethod","ForecastingDivinerConfigSchema","asDivinerInstance","jsonpath","getJsonPathTransformer","pathExpression","transformer","x","ret","jsonpath","value","Error","MemoryForecastingDiviner","AbstractForecastingDiviner","configSchemas","ForecastingDivinerConfigSchema","forecastingMethodDict","arimaForecasting","arimaForecastingMethod","seasonalArimaForecasting","seasonalArimaForecastingMethod","batchLimit","maxTrainingLength","forecastingMethod","forecastingMethodName","assertEx","config","jsonPathExpression","getPayloadsInWindow","startTimestamp","stopTimestamp","addresses","witnessAddresses","payload_schemas","witnessSchema","payloads","archivist","asArchivistInstance","getArchivist","bwDiviner","asDivinerInstance","resolve","boundWitnessDiviner","pop","limit","timestamp","more","length","query","schema","BoundWitnessDivinerQuerySchema","boundWitnesses","divine","filter","bw","map","exists","reduce","a","b","Math","min","Number","MAX_SAFE_INTEGER","hashes","payload_hashes","indexOf","batchPayloads","get","push"]}
|
package/dist/node/index.cjs
CHANGED
|
@@ -5,6 +5,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
10
|
var __export = (target, all) => {
|
|
9
11
|
for (var name in all)
|
|
10
12
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -26,6 +28,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
28
|
mod
|
|
27
29
|
));
|
|
28
30
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
var __publicField = (obj, key, value) => {
|
|
32
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
29
35
|
|
|
30
36
|
// src/index.ts
|
|
31
37
|
var src_exports = {};
|
|
@@ -44,24 +50,19 @@ var import_diviner_forecasting_method_arima = require("@xyo-network/diviner-fore
|
|
|
44
50
|
var import_diviner_forecasting_model = require("@xyo-network/diviner-forecasting-model");
|
|
45
51
|
var import_diviner_model = require("@xyo-network/diviner-model");
|
|
46
52
|
var import_jsonpath = __toESM(require("jsonpath"), 1);
|
|
47
|
-
var getJsonPathTransformer = (pathExpression) => {
|
|
48
|
-
const transformer = (x) => {
|
|
53
|
+
var getJsonPathTransformer = /* @__PURE__ */ __name((pathExpression) => {
|
|
54
|
+
const transformer = /* @__PURE__ */ __name((x) => {
|
|
49
55
|
const ret = import_jsonpath.default.value(x, pathExpression);
|
|
50
56
|
if (typeof ret === "number")
|
|
51
57
|
return ret;
|
|
52
58
|
throw new Error("Parsed invalid payload value");
|
|
53
|
-
};
|
|
59
|
+
}, "transformer");
|
|
54
60
|
return transformer;
|
|
55
|
-
};
|
|
56
|
-
var
|
|
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
|
-
};
|
|
61
|
+
}, "getJsonPathTransformer");
|
|
62
|
+
var _MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_diviner_forecasting_abstract.AbstractForecastingDiviner {
|
|
62
63
|
/**
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
* The max number of records to search during the batch query
|
|
65
|
+
*/
|
|
65
66
|
batchLimit = 1e3;
|
|
66
67
|
// TODO: Inject via config
|
|
67
68
|
maxTrainingLength = 1e4;
|
|
@@ -78,7 +79,9 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_di
|
|
|
78
79
|
}
|
|
79
80
|
async getPayloadsInWindow(startTimestamp, stopTimestamp) {
|
|
80
81
|
const addresses = this.config.witnessAddresses;
|
|
81
|
-
const payload_schemas = [
|
|
82
|
+
const payload_schemas = [
|
|
83
|
+
(0, import_assert.assertEx)(this.config.witnessSchema, "Missing witnessSchema in config")
|
|
84
|
+
];
|
|
82
85
|
const payloads = [];
|
|
83
86
|
const archivist = (0, import_archivist_model.asArchivistInstance)(await this.getArchivist(), "Unable to resolve archivist");
|
|
84
87
|
const bwDiviner = (0, import_diviner_model.asDivinerInstance)((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
|
|
@@ -87,10 +90,16 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_di
|
|
|
87
90
|
let timestamp = stopTimestamp;
|
|
88
91
|
let more = true;
|
|
89
92
|
while (more || payloads.length < this.maxTrainingLength) {
|
|
90
|
-
const query = {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
const query = {
|
|
94
|
+
addresses,
|
|
95
|
+
limit,
|
|
96
|
+
payload_schemas,
|
|
97
|
+
schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema,
|
|
98
|
+
timestamp
|
|
99
|
+
};
|
|
100
|
+
const boundWitnesses = (await bwDiviner.divine([
|
|
101
|
+
query
|
|
102
|
+
])).filter((bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp);
|
|
94
103
|
if (boundWitnesses.length === 0)
|
|
95
104
|
break;
|
|
96
105
|
timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(import_exists.exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
|
|
@@ -106,6 +115,15 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends import_di
|
|
|
106
115
|
return payloads;
|
|
107
116
|
}
|
|
108
117
|
};
|
|
118
|
+
__name(_MemoryForecastingDiviner, "MemoryForecastingDiviner");
|
|
119
|
+
__publicField(_MemoryForecastingDiviner, "configSchemas", [
|
|
120
|
+
import_diviner_forecasting_model.ForecastingDivinerConfigSchema
|
|
121
|
+
]);
|
|
122
|
+
__publicField(_MemoryForecastingDiviner, "forecastingMethodDict", {
|
|
123
|
+
arimaForecasting: import_diviner_forecasting_method_arima.arimaForecastingMethod,
|
|
124
|
+
seasonalArimaForecasting: import_diviner_forecasting_method_arima.seasonalArimaForecastingMethod
|
|
125
|
+
});
|
|
126
|
+
var MemoryForecastingDiviner = _MemoryForecastingDiviner;
|
|
109
127
|
// Annotate the CommonJS export names for ESM import in node:
|
|
110
128
|
0 && (module.exports = {
|
|
111
129
|
MemoryForecastingDiviner
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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":"
|
|
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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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;;;;;;;ACAA,oBAAyB;AACzB,oBAAuB;AACvB,6BAAoC;AAEpC,wCAA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SAAS,KAAK1B,mBAAmB;AACvD,YAAM2B,QAAyC;QAAEjB;QAAWa;QAAOX;QAAiBgB,QAAQC;QAAgCL;MAAU;AACtI,YAAMM,kBAAmB,MAAMZ,UAAUa,OAAO;QAACJ;OAAM,GAAsBK,OAC3E,CAACC,OAAOA,GAAGT,aAAaS,GAAGT,aAAahB,kBAAkByB,GAAGT,aAAaf,aAAAA;AAE5E,UAAIqB,eAAeJ,WAAW;AAAG;AAGjCF,kBAAYM,eACTI,IAAI,CAACD,OAAOA,GAAGT,SAAS,EACxBQ,OAAOG,oBAAAA,EAEPC,OAAO,CAACC,GAAGC,MAAMC,KAAKC,IAAIH,GAAGC,CAAAA,GAAIG,OAAOC,gBAAgB;AAC3D,UAAIlB,cAAciB,OAAOC;AAAkB;AAG3CjB,aAAOK,eAAeJ,WAAWH;AAGjC,YAAMoB,SAASb,eAAeI,IAAI,CAACD,OAAOA,GAAGW,eAAeX,GAAGrB,gBAAgBiC,QAAQhC,aAAAA,CAAAA,CAAe,EAAEmB,OAAOG,oBAAAA;AAG/G,UAAIQ,OAAOjB,SAAS,GAAG;AACrB,cAAMoB,iBAAiB,MAAM/B,UAAUgC,IAAIJ,MAAAA,GAASX,OAAOG,oBAAAA;AAC3DrB,iBAASkC,KAAI,GAAIF,aAAAA;MACnB;IACF;AACA,WAAOhC;EACT;AACF;AAtEUhB;AACR,cAHWD,2BAGKoD,iBAAgB;EAACC;;AAEjC,cALWrD,2BAKeQ,yBAA6E;EACrG8C,kBAAkBC;EAClBC,0BAA0BC;AAC5B;AARK,IAAMzD,2BAAN;","names":["getJsonPathTransformer","pathExpression","transformer","x","ret","jsonpath","value","Error","MemoryForecastingDiviner","AbstractForecastingDiviner","batchLimit","maxTrainingLength","forecastingMethod","forecastingMethodName","assertEx","config","forecastingMethodDict","jsonPathExpression","getPayloadsInWindow","startTimestamp","stopTimestamp","addresses","witnessAddresses","payload_schemas","witnessSchema","payloads","archivist","asArchivistInstance","getArchivist","bwDiviner","asDivinerInstance","resolve","boundWitnessDiviner","pop","limit","timestamp","more","length","query","schema","BoundWitnessDivinerQuerySchema","boundWitnesses","divine","filter","bw","map","exists","reduce","a","b","Math","min","Number","MAX_SAFE_INTEGER","hashes","payload_hashes","indexOf","batchPayloads","get","push","configSchemas","ForecastingDivinerConfigSchema","arimaForecasting","arimaForecastingMethod","seasonalArimaForecasting","seasonalArimaForecastingMethod"]}
|
package/dist/node/index.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
|
|
1
9
|
// src/MemoryForecastingDiviner.ts
|
|
2
10
|
import { assertEx } from "@xylabs/assert";
|
|
3
11
|
import { exists } from "@xylabs/exists";
|
|
4
12
|
import { asArchivistInstance } from "@xyo-network/archivist-model";
|
|
5
13
|
import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
|
|
6
14
|
import { AbstractForecastingDiviner } from "@xyo-network/diviner-forecasting-abstract";
|
|
7
|
-
import {
|
|
8
|
-
arimaForecastingMethod,
|
|
9
|
-
seasonalArimaForecastingMethod
|
|
10
|
-
} from "@xyo-network/diviner-forecasting-method-arima";
|
|
15
|
+
import { arimaForecastingMethod, seasonalArimaForecastingMethod } from "@xyo-network/diviner-forecasting-method-arima";
|
|
11
16
|
import { ForecastingDivinerConfigSchema } from "@xyo-network/diviner-forecasting-model";
|
|
12
17
|
import { asDivinerInstance } from "@xyo-network/diviner-model";
|
|
13
18
|
import jsonpath from "jsonpath";
|
|
14
|
-
var getJsonPathTransformer = (pathExpression) => {
|
|
15
|
-
const transformer = (x) => {
|
|
19
|
+
var getJsonPathTransformer = /* @__PURE__ */ __name((pathExpression) => {
|
|
20
|
+
const transformer = /* @__PURE__ */ __name((x) => {
|
|
16
21
|
const ret = jsonpath.value(x, pathExpression);
|
|
17
22
|
if (typeof ret === "number")
|
|
18
23
|
return ret;
|
|
19
24
|
throw new Error("Parsed invalid payload value");
|
|
20
|
-
};
|
|
25
|
+
}, "transformer");
|
|
21
26
|
return transformer;
|
|
22
|
-
};
|
|
23
|
-
var
|
|
24
|
-
static configSchemas = [ForecastingDivinerConfigSchema];
|
|
25
|
-
static forecastingMethodDict = {
|
|
26
|
-
arimaForecasting: arimaForecastingMethod,
|
|
27
|
-
seasonalArimaForecasting: seasonalArimaForecastingMethod
|
|
28
|
-
};
|
|
27
|
+
}, "getJsonPathTransformer");
|
|
28
|
+
var _MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractForecastingDiviner {
|
|
29
29
|
/**
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
* The max number of records to search during the batch query
|
|
31
|
+
*/
|
|
32
32
|
batchLimit = 1e3;
|
|
33
33
|
// TODO: Inject via config
|
|
34
34
|
maxTrainingLength = 1e4;
|
|
@@ -45,7 +45,9 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractF
|
|
|
45
45
|
}
|
|
46
46
|
async getPayloadsInWindow(startTimestamp, stopTimestamp) {
|
|
47
47
|
const addresses = this.config.witnessAddresses;
|
|
48
|
-
const payload_schemas = [
|
|
48
|
+
const payload_schemas = [
|
|
49
|
+
assertEx(this.config.witnessSchema, "Missing witnessSchema in config")
|
|
50
|
+
];
|
|
49
51
|
const payloads = [];
|
|
50
52
|
const archivist = asArchivistInstance(await this.getArchivist(), "Unable to resolve archivist");
|
|
51
53
|
const bwDiviner = asDivinerInstance((await this.resolve(this.config.boundWitnessDiviner)).pop(), "Unable to resolve boundWitnessDiviner");
|
|
@@ -54,10 +56,16 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractF
|
|
|
54
56
|
let timestamp = stopTimestamp;
|
|
55
57
|
let more = true;
|
|
56
58
|
while (more || payloads.length < this.maxTrainingLength) {
|
|
57
|
-
const query = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const query = {
|
|
60
|
+
addresses,
|
|
61
|
+
limit,
|
|
62
|
+
payload_schemas,
|
|
63
|
+
schema: BoundWitnessDivinerQuerySchema,
|
|
64
|
+
timestamp
|
|
65
|
+
};
|
|
66
|
+
const boundWitnesses = (await bwDiviner.divine([
|
|
67
|
+
query
|
|
68
|
+
])).filter((bw) => bw.timestamp && bw.timestamp >= startTimestamp && bw.timestamp <= stopTimestamp);
|
|
61
69
|
if (boundWitnesses.length === 0)
|
|
62
70
|
break;
|
|
63
71
|
timestamp = boundWitnesses.map((bw) => bw.timestamp).filter(exists).reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER);
|
|
@@ -73,6 +81,15 @@ var MemoryForecastingDiviner = class _MemoryForecastingDiviner extends AbstractF
|
|
|
73
81
|
return payloads;
|
|
74
82
|
}
|
|
75
83
|
};
|
|
84
|
+
__name(_MemoryForecastingDiviner, "MemoryForecastingDiviner");
|
|
85
|
+
__publicField(_MemoryForecastingDiviner, "configSchemas", [
|
|
86
|
+
ForecastingDivinerConfigSchema
|
|
87
|
+
]);
|
|
88
|
+
__publicField(_MemoryForecastingDiviner, "forecastingMethodDict", {
|
|
89
|
+
arimaForecasting: arimaForecastingMethod,
|
|
90
|
+
seasonalArimaForecasting: seasonalArimaForecastingMethod
|
|
91
|
+
});
|
|
92
|
+
var MemoryForecastingDiviner = _MemoryForecastingDiviner;
|
|
76
93
|
export {
|
|
77
94
|
MemoryForecastingDiviner
|
|
78
95
|
};
|
package/dist/node/index.js.map
CHANGED
|
@@ -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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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":"
|
|
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 = 1000\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.getArchivist(), '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 // eslint-disable-next-line unicorn/no-array-reduce\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.indexOf(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,SAASA,gBAAgB;AACzB,SAASC,cAAc;AACvB,SAASC,2BAA2B;AAEpC,SAA0CC,sCAAsC;AAChF,SAASC,kCAA4D;AACrE,SACEC,wBAEAC,sCAEK;AACP,SAASC,sCAAkF;AAC3F,SAASC,yBAAyB;AAElC,OAAOC,cAAc;AAIrB,IAAMC,yBAAyB,wBAACC,mBAAAA;AAC9B,QAAMC,cAAc,wBAACC,MAAAA;AAEnB,UAAMC,MAAMC,SAASC,MAAMH,GAAGF,cAAAA;AAC9B,QAAI,OAAOG,QAAQ;AAAU,aAAOA;AACpC,UAAM,IAAIG,MAAM,8BAAA;EAClB,GALoB;AAMpB,SAAOL;AACT,GAR+B;AAUxB,IAAMM,4BAAN,MAAMA,kCAEHC,2BAAAA;;;;EAWWC,aAAa;;EAGbC,oBAAoB;EAEvC,IAAuBC,oBAAuC;AAC5D,UAAMC,wBAAwBC,SAAS,KAAKC,OAAOH,mBAAmB,qCAAA;AACtE,UAAMA,oBAAoBJ,0BAAyBQ,sBAAsBH,qBAAAA;AACzE,QAAID;AAAmB,aAAOA;AAC9B,UAAM,IAAIL,MAAM,mCAAmCM,qBAAAA,EAAuB;EAC5E;EAEA,IAAuBX,cAAuC;AAC5D,UAAMD,iBAAiBa,SAAS,KAAKC,OAAOE,oBAAoB,sCAAA;AAChE,WAAOjB,uBAAuBC,cAAAA;EAChC;EAEA,MAAyBiB,oBAAoBC,gBAAwBC,eAA2C;AAC9G,UAAMC,YAAY,KAAKN,OAAOO;AAC9B,UAAMC,kBAAkB;MAACT,SAAS,KAAKC,OAAOS,eAAe,iCAAA;;AAC7D,UAAMC,WAAsB,CAAA;AAC5B,UAAMC,YAAYC,oBAAoB,MAAM,KAAKC,aAAY,GAA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BAAN;","names":["assertEx","exists","asArchivistInstance","BoundWitnessDivinerQuerySchema","AbstractForecastingDiviner","arimaForecastingMethod","seasonalArimaForecastingMethod","ForecastingDivinerConfigSchema","asDivinerInstance","jsonpath","getJsonPathTransformer","pathExpression","transformer","x","ret","jsonpath","value","Error","MemoryForecastingDiviner","AbstractForecastingDiviner","batchLimit","maxTrainingLength","forecastingMethod","forecastingMethodName","assertEx","config","forecastingMethodDict","jsonPathExpression","getPayloadsInWindow","startTimestamp","stopTimestamp","addresses","witnessAddresses","payload_schemas","witnessSchema","payloads","archivist","asArchivistInstance","getArchivist","bwDiviner","asDivinerInstance","resolve","boundWitnessDiviner","pop","limit","timestamp","more","length","query","schema","BoundWitnessDivinerQuerySchema","boundWitnesses","divine","filter","bw","map","exists","reduce","a","b","Math","min","Number","MAX_SAFE_INTEGER","hashes","payload_hashes","indexOf","batchPayloads","get","push","configSchemas","ForecastingDivinerConfigSchema","arimaForecasting","arimaForecastingMethod","seasonalArimaForecasting","seasonalArimaForecastingMethod"]}
|
package/package.json
CHANGED
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@xylabs/assert": "^2.13.20",
|
|
14
14
|
"@xylabs/exists": "^2.13.20",
|
|
15
|
-
"@xyo-network/archivist-model": "~2.
|
|
16
|
-
"@xyo-network/boundwitness-model": "~2.
|
|
17
|
-
"@xyo-network/diviner-boundwitness-model": "~2.
|
|
18
|
-
"@xyo-network/diviner-forecasting-abstract": "~2.
|
|
19
|
-
"@xyo-network/diviner-forecasting-method-arima": "~2.
|
|
20
|
-
"@xyo-network/diviner-forecasting-model": "~2.
|
|
21
|
-
"@xyo-network/diviner-model": "~2.
|
|
22
|
-
"@xyo-network/payload-model": "~2.
|
|
15
|
+
"@xyo-network/archivist-model": "~2.85.0",
|
|
16
|
+
"@xyo-network/boundwitness-model": "~2.85.0",
|
|
17
|
+
"@xyo-network/diviner-boundwitness-model": "~2.85.0",
|
|
18
|
+
"@xyo-network/diviner-forecasting-abstract": "~2.85.0",
|
|
19
|
+
"@xyo-network/diviner-forecasting-method-arima": "~2.85.0",
|
|
20
|
+
"@xyo-network/diviner-forecasting-model": "~2.85.0",
|
|
21
|
+
"@xyo-network/diviner-model": "~2.85.0",
|
|
22
|
+
"@xyo-network/payload-model": "~2.85.0",
|
|
23
23
|
"jsonpath": "^1.1.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
@@ -67,6 +67,6 @@
|
|
|
67
67
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
|
|
68
68
|
},
|
|
69
69
|
"sideEffects": false,
|
|
70
|
-
"version": "2.
|
|
70
|
+
"version": "2.85.0",
|
|
71
71
|
"type": "module"
|
|
72
72
|
}
|