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