@fintekkers/ledger-models 1.1.52 → 1.1.54
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/README.md +37 -0
- package/catalog/measures.json +1 -1
- package/index.js +19 -0
- package/index.js.map +1 -1
- package/index.ts +27 -1
- package/node/wrappers/models/security/security.d.ts +29 -0
- package/node/wrappers/models/security/security.js +59 -0
- package/node/wrappers/models/security/security.js.map +1 -1
- package/node/wrappers/models/security/security.ts +63 -0
- package/node/wrappers/models/security/security_metric.d.ts +163 -0
- package/node/wrappers/models/security/security_metric.js +431 -0
- package/node/wrappers/models/security/security_metric.js.map +1 -0
- package/node/wrappers/models/security/security_metric.test.d.ts +1 -0
- package/node/wrappers/models/security/security_metric.test.js +215 -0
- package/node/wrappers/models/security/security_metric.test.js.map +1 -0
- package/node/wrappers/models/security/security_metric.test.ts +223 -0
- package/node/wrappers/models/security/security_metric.ts +514 -0
- package/node/wrappers/services/security-metric-service/SecurityMetricService.d.ts +94 -0
- package/node/wrappers/services/security-metric-service/SecurityMetricService.js +257 -0
- package/node/wrappers/services/security-metric-service/SecurityMetricService.js.map +1 -0
- package/node/wrappers/services/security-metric-service/SecurityMetricService.ts +298 -0
- package/node/wrappers/services/security_metric_fake_server.d.ts +35 -0
- package/node/wrappers/services/security_metric_fake_server.js +203 -0
- package/node/wrappers/services/security_metric_fake_server.js.map +1 -0
- package/node/wrappers/services/security_metric_fake_server.ts +194 -0
- package/node/wrappers/services/security_metric_readme.test.d.ts +1 -0
- package/node/wrappers/services/security_metric_readme.test.js +132 -0
- package/node/wrappers/services/security_metric_readme.test.js.map +1 -0
- package/node/wrappers/services/security_metric_readme.test.ts +115 -0
- package/node/wrappers/services/security_metric_service.test.d.ts +1 -0
- package/node/wrappers/services/security_metric_service.test.js +307 -0
- package/node/wrappers/services/security_metric_service.test.js.map +1 -0
- package/node/wrappers/services/security_metric_service.test.ts +321 -0
- package/node/wrappers/util/object-types.test.js +34 -1
- package/node/wrappers/util/object-types.test.js.map +1 -1
- package/node/wrappers/util/object-types.test.ts +43 -0
- package/package.json +1 -1
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.resetSecurityMetricServiceForTests = exports.setSecurityMetricService = exports.getSecurityMetricService = exports.setSecurityMetricFetcher = exports.SecurityMetricService = exports.GrpcSecurityMetricFetcher = exports.DEFAULT_DEADLINE_MS = void 0;
|
|
36
|
+
/**
|
|
37
|
+
* SecurityMetricService (LM-485): read and record security metrics (shares
|
|
38
|
+
* outstanding, NAV per share, total net assets) through the SecurityMetric
|
|
39
|
+
* gRPC service, which is served at the PRICE service address.
|
|
40
|
+
*
|
|
41
|
+
* const service = getSecurityMetricService();
|
|
42
|
+
* const latest = await service.get('SHARES_OUTSTANDING', [spyUuid]);
|
|
43
|
+
*
|
|
44
|
+
* Every call goes through a `SecurityMetricFetcher`. The default one is a
|
|
45
|
+
* gRPC client for `resolve(Service.PRICE)` (BROKER_HOST first, then
|
|
46
|
+
* PRICE_SERVICE_HOST/_PORT, ...; plaintext for loopback, TLS otherwise) with
|
|
47
|
+
* a per-call deadline. `setSecurityMetricFetcher` installs another fetcher
|
|
48
|
+
* for every service instance in the process (tests, alternate transports).
|
|
49
|
+
*/
|
|
50
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
51
|
+
const security_metric_service_grpc_pb_1 = require("../../../fintekkers/services/security-metric-service/security_metric_service_grpc_pb");
|
|
52
|
+
const date_1 = require("../../models/utils/date");
|
|
53
|
+
const datetime_1 = require("../../models/utils/datetime");
|
|
54
|
+
const security_metric_1 = require("../../models/security/security_metric");
|
|
55
|
+
const serviceaddress_1 = require("../../util/serviceaddress");
|
|
56
|
+
/** Default per-call deadline of the gRPC fetcher. */
|
|
57
|
+
exports.DEFAULT_DEADLINE_MS = 30000;
|
|
58
|
+
/**
|
|
59
|
+
* The default fetcher: a gRPC client for one endpoint. Errors become
|
|
60
|
+
* `ServiceUnavailable` (gRPC UNAVAILABLE) or `SecurityMetricServiceError`,
|
|
61
|
+
* both naming `host:port` and the status.
|
|
62
|
+
*/
|
|
63
|
+
class GrpcSecurityMetricFetcher {
|
|
64
|
+
constructor(endpoint, options = {}) {
|
|
65
|
+
var _a;
|
|
66
|
+
this.address = (0, serviceaddress_1.target)(endpoint);
|
|
67
|
+
this.deadlineMs = (_a = options.deadlineMs) !== null && _a !== void 0 ? _a : exports.DEFAULT_DEADLINE_MS;
|
|
68
|
+
const credentials = endpoint.plaintext ? grpc.credentials.createInsecure() : grpc.credentials.createSsl();
|
|
69
|
+
this.client = new security_metric_service_grpc_pb_1.SecurityMetricClient(this.address, credentials);
|
|
70
|
+
}
|
|
71
|
+
close() {
|
|
72
|
+
this.client.close();
|
|
73
|
+
}
|
|
74
|
+
callOptions() {
|
|
75
|
+
return { deadline: new Date(Date.now() + this.deadlineMs) };
|
|
76
|
+
}
|
|
77
|
+
/** The service error for a failed call to `rpc`. */
|
|
78
|
+
toServiceError(rpc, err) {
|
|
79
|
+
var _a;
|
|
80
|
+
const e = err;
|
|
81
|
+
const code = typeof (e === null || e === void 0 ? void 0 : e.code) === 'number' ? e.code : undefined;
|
|
82
|
+
const codeName = code === undefined ? 'UNKNOWN' : ((_a = grpc.status[code]) !== null && _a !== void 0 ? _a : String(code));
|
|
83
|
+
const details = (e === null || e === void 0 ? void 0 : e.details) || (err instanceof Error ? err.message : String(err));
|
|
84
|
+
if (code === grpc.status.UNAVAILABLE) {
|
|
85
|
+
return new security_metric_1.ServiceUnavailable(`SecurityMetric service unreachable at ${this.address} (${details})`, { address: this.address, code, codeName });
|
|
86
|
+
}
|
|
87
|
+
return new security_metric_1.SecurityMetricServiceError(`SecurityMetric ${rpc} failed at ${this.address}: ${codeName} (${details})`, { address: this.address, code, codeName });
|
|
88
|
+
}
|
|
89
|
+
getLatest(request) {
|
|
90
|
+
return new Promise((resolvePromise, reject) => {
|
|
91
|
+
this.client.getLatest(request, new grpc.Metadata(), this.callOptions(), (err, response) => {
|
|
92
|
+
if (err)
|
|
93
|
+
reject(this.toServiceError('GetLatest', err));
|
|
94
|
+
else
|
|
95
|
+
resolvePromise(response);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
search(request) {
|
|
100
|
+
return new Promise((resolvePromise, reject) => {
|
|
101
|
+
const chunks = [];
|
|
102
|
+
const stream = this.client.search(request, this.callOptions());
|
|
103
|
+
stream.on('data', (chunk) => chunks.push(chunk));
|
|
104
|
+
stream.on('end', () => resolvePromise(chunks));
|
|
105
|
+
stream.on('error', (err) => reject(this.toServiceError('Search', err)));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
createOrUpdate(request) {
|
|
109
|
+
return new Promise((resolvePromise, reject) => {
|
|
110
|
+
this.client.createOrUpdate(request, new grpc.Metadata(), this.callOptions(), (err, response) => {
|
|
111
|
+
if (err)
|
|
112
|
+
reject(this.toServiceError('CreateOrUpdate', err));
|
|
113
|
+
else
|
|
114
|
+
resolvePromise(response);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.GrpcSecurityMetricFetcher = GrpcSecurityMetricFetcher;
|
|
120
|
+
let installedFetcher = null;
|
|
121
|
+
let defaultService = null;
|
|
122
|
+
class SecurityMetricService {
|
|
123
|
+
constructor(options = {}) {
|
|
124
|
+
this.ownFetcher = options.fetcher;
|
|
125
|
+
this.env = options.env;
|
|
126
|
+
this.deadlineMs = options.deadlineMs;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* A service whose default gRPC fetcher resolves the PRICE address from
|
|
130
|
+
* `env` instead of `process.env`.
|
|
131
|
+
*/
|
|
132
|
+
static fromEnv(env, options = {}) {
|
|
133
|
+
return new SecurityMetricService({ env, deadlineMs: options.deadlineMs });
|
|
134
|
+
}
|
|
135
|
+
/** This instance's fetcher, else the installed one, else the default gRPC fetcher (built on first use). */
|
|
136
|
+
fetcher() {
|
|
137
|
+
var _a;
|
|
138
|
+
if (this.ownFetcher)
|
|
139
|
+
return this.ownFetcher;
|
|
140
|
+
if (installedFetcher)
|
|
141
|
+
return installedFetcher;
|
|
142
|
+
if (!this.grpcFetcher) {
|
|
143
|
+
this.grpcFetcher = new GrpcSecurityMetricFetcher((0, serviceaddress_1.resolve)(serviceaddress_1.Service.PRICE, (_a = this.env) !== null && _a !== void 0 ? _a : process.env), { deadlineMs: this.deadlineMs });
|
|
144
|
+
}
|
|
145
|
+
return this.grpcFetcher;
|
|
146
|
+
}
|
|
147
|
+
/** Closes the default gRPC client, if one was built. */
|
|
148
|
+
close() {
|
|
149
|
+
var _a;
|
|
150
|
+
(_a = this.grpcFetcher) === null || _a === void 0 ? void 0 : _a.close();
|
|
151
|
+
this.grpcFetcher = undefined;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The latest value of `metric` on or before `on` (default today) for each
|
|
155
|
+
* security, in one GetLatest call. Every requested UUID string is a key;
|
|
156
|
+
* its value is undefined when the security has no point on or before `on`.
|
|
157
|
+
* No call for an empty list.
|
|
158
|
+
*/
|
|
159
|
+
get(metric, uuids, on) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
const m = (0, security_metric_1.checkMetric)(metric);
|
|
162
|
+
const onDate = on === undefined ? date_1.LocalDate.today() : (0, security_metric_1.toLocalDate)(on);
|
|
163
|
+
const ids = uuids.map(security_metric_1.toSecurityId);
|
|
164
|
+
if (ids.length === 0)
|
|
165
|
+
return new Map();
|
|
166
|
+
const response = yield this.fetcher().getLatest((0, security_metric_1.buildGetLatest)(m, ids, onDate));
|
|
167
|
+
return (0, security_metric_1.mapLatest)(response, m, ids, onDate);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/** Every point of `metric` for one security from `from` to `to` (default today), both inclusive. */
|
|
171
|
+
getHistory(metric, uuid, from, to) {
|
|
172
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
173
|
+
const m = (0, security_metric_1.checkMetric)(metric);
|
|
174
|
+
const securityId = (0, security_metric_1.toSecurityId)(uuid);
|
|
175
|
+
const request = (0, security_metric_1.buildSearch)(m, securityId, from, to === undefined ? date_1.LocalDate.today() : to);
|
|
176
|
+
const chunks = yield this.fetcher().search(request);
|
|
177
|
+
return (0, security_metric_1.mergeChunks)(chunks, securityId, m);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Stores points of `metric` for one security. Points are checked first
|
|
182
|
+
* (`InvalidMetricPoints`, nothing sent), then sent in chunks of
|
|
183
|
+
* `RECORD_CHUNK_SIZE`, one CreateOrUpdate each. Returns the as_of the
|
|
184
|
+
* service stamped on the last chunk. If a later chunk fails, the error
|
|
185
|
+
* says how many points were already stored.
|
|
186
|
+
*/
|
|
187
|
+
record(metric, uuid, points) {
|
|
188
|
+
var _a;
|
|
189
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
190
|
+
const m = (0, security_metric_1.checkMetric)(metric);
|
|
191
|
+
const securityId = (0, security_metric_1.toSecurityId)(uuid);
|
|
192
|
+
const chunks = (0, security_metric_1.chunkRecord)((0, security_metric_1.validateRecord)(m, points), security_metric_1.RECORD_CHUNK_SIZE);
|
|
193
|
+
const fetcher = this.fetcher();
|
|
194
|
+
let stored = 0;
|
|
195
|
+
let response;
|
|
196
|
+
for (const chunk of chunks) {
|
|
197
|
+
try {
|
|
198
|
+
response = yield fetcher.createOrUpdate((0, security_metric_1.buildCreate)(m, securityId, chunk));
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
if (stored === 0)
|
|
202
|
+
throw e;
|
|
203
|
+
throw alreadyStored(e, stored);
|
|
204
|
+
}
|
|
205
|
+
stored += chunk.length;
|
|
206
|
+
}
|
|
207
|
+
const asOf = (_a = response === null || response === void 0 ? void 0 : response.getSeries()) === null || _a === void 0 ? void 0 : _a.getAsOf();
|
|
208
|
+
if (!asOf)
|
|
209
|
+
throw new security_metric_1.SecurityMetricServiceError('SecurityMetric CreateOrUpdate response has no series.as_of');
|
|
210
|
+
return new datetime_1.ZonedDateTime(asOf);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.SecurityMetricService = SecurityMetricService;
|
|
215
|
+
/** `e` with "N points already stored" added, keeping its class. */
|
|
216
|
+
function alreadyStored(e, stored) {
|
|
217
|
+
const suffix = `; ${stored} points already stored`;
|
|
218
|
+
if (e instanceof security_metric_1.SecurityMetricServiceError) {
|
|
219
|
+
const details = { address: e.address, code: e.code, codeName: e.codeName };
|
|
220
|
+
return e instanceof security_metric_1.ServiceUnavailable
|
|
221
|
+
? new security_metric_1.ServiceUnavailable(e.message + suffix, details)
|
|
222
|
+
: new security_metric_1.SecurityMetricServiceError(e.message + suffix, details);
|
|
223
|
+
}
|
|
224
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
225
|
+
return new security_metric_1.SecurityMetricServiceError(`SecurityMetric CreateOrUpdate failed: ${message}${suffix}`);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Installs `fetcher` for every SecurityMetricService in the process (except
|
|
229
|
+
* instances built with their own fetcher). `null` restores the default.
|
|
230
|
+
*/
|
|
231
|
+
function setSecurityMetricFetcher(fetcher) {
|
|
232
|
+
installedFetcher = fetcher;
|
|
233
|
+
}
|
|
234
|
+
exports.setSecurityMetricFetcher = setSecurityMetricFetcher;
|
|
235
|
+
/** The process-wide service that `Security.getMetric` and friends use, built on first use. */
|
|
236
|
+
function getSecurityMetricService() {
|
|
237
|
+
if (!defaultService)
|
|
238
|
+
defaultService = new SecurityMetricService();
|
|
239
|
+
return defaultService;
|
|
240
|
+
}
|
|
241
|
+
exports.getSecurityMetricService = getSecurityMetricService;
|
|
242
|
+
/**
|
|
243
|
+
* Replaces the process-wide service, e.g. with `SecurityMetricService.fromEnv(env)`
|
|
244
|
+
* in a test. `null` clears it (the next `getSecurityMetricService()` builds a new one).
|
|
245
|
+
*/
|
|
246
|
+
function setSecurityMetricService(service) {
|
|
247
|
+
defaultService = service;
|
|
248
|
+
}
|
|
249
|
+
exports.setSecurityMetricService = setSecurityMetricService;
|
|
250
|
+
/** Test teardown: removes the installed fetcher and closes and clears the process-wide service. */
|
|
251
|
+
function resetSecurityMetricServiceForTests() {
|
|
252
|
+
installedFetcher = null;
|
|
253
|
+
defaultService === null || defaultService === void 0 ? void 0 : defaultService.close();
|
|
254
|
+
defaultService = null;
|
|
255
|
+
}
|
|
256
|
+
exports.resetSecurityMetricServiceForTests = resetSecurityMetricServiceForTests;
|
|
257
|
+
//# sourceMappingURL=SecurityMetricService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SecurityMetricService.js","sourceRoot":"","sources":["SecurityMetricService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;GAaG;AACH,oDAAsC;AAEtC,0IAA4H;AAc5H,kDAAoD;AACpD,0DAA4D;AAC5D,2EAoB+C;AAC/C,8DAA8F;AAE9F,qDAAqD;AACxC,QAAA,mBAAmB,GAAG,KAAM,CAAC;AAc1C;;;;GAIG;AACH,MAAa,yBAAyB;IAKpC,YAAY,QAAkB,EAAE,UAA8B,EAAE;;QAC9D,IAAI,CAAC,OAAO,GAAG,IAAA,uBAAM,EAAC,QAAQ,CAAC,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,CAAC,UAAU,mCAAI,2BAAmB,CAAC;QAC5D,MAAM,WAAW,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE,CAAC;QAC1G,IAAI,CAAC,MAAM,GAAG,IAAI,sDAAoB,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACpE,CAAC;IAED,KAAK;QACH,IAAI,CAAC,MAAM,CA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c,GAAG,IAAI,CAAC;AACxB,CAAC;AAOC,gFAAkC"}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SecurityMetricService (LM-485): read and record security metrics (shares
|
|
3
|
+
* outstanding, NAV per share, total net assets) through the SecurityMetric
|
|
4
|
+
* gRPC service, which is served at the PRICE service address.
|
|
5
|
+
*
|
|
6
|
+
* const service = getSecurityMetricService();
|
|
7
|
+
* const latest = await service.get('SHARES_OUTSTANDING', [spyUuid]);
|
|
8
|
+
*
|
|
9
|
+
* Every call goes through a `SecurityMetricFetcher`. The default one is a
|
|
10
|
+
* gRPC client for `resolve(Service.PRICE)` (BROKER_HOST first, then
|
|
11
|
+
* PRICE_SERVICE_HOST/_PORT, ...; plaintext for loopback, TLS otherwise) with
|
|
12
|
+
* a per-call deadline. `setSecurityMetricFetcher` installs another fetcher
|
|
13
|
+
* for every service instance in the process (tests, alternate transports).
|
|
14
|
+
*/
|
|
15
|
+
import * as grpc from '@grpc/grpc-js';
|
|
16
|
+
|
|
17
|
+
import { SecurityMetricClient } from '../../../fintekkers/services/security-metric-service/security_metric_service_grpc_pb';
|
|
18
|
+
import type {
|
|
19
|
+
CreateSecurityMetricRequestProto,
|
|
20
|
+
CreateSecurityMetricResponseProto,
|
|
21
|
+
} from '../../../fintekkers/requests/security_metric/create_security_metric_request_pb';
|
|
22
|
+
import type {
|
|
23
|
+
GetLatestSecurityMetricRequestProto,
|
|
24
|
+
GetLatestSecurityMetricResponseProto,
|
|
25
|
+
} from '../../../fintekkers/requests/security_metric/get_latest_security_metric_request_pb';
|
|
26
|
+
import type {
|
|
27
|
+
SearchSecurityMetricRequestProto,
|
|
28
|
+
SearchSecurityMetricResponseProto,
|
|
29
|
+
} from '../../../fintekkers/requests/security_metric/search_security_metric_request_pb';
|
|
30
|
+
|
|
31
|
+
import { LocalDate } from '../../models/utils/date';
|
|
32
|
+
import { ZonedDateTime } from '../../models/utils/datetime';
|
|
33
|
+
import {
|
|
34
|
+
RECORD_CHUNK_SIZE,
|
|
35
|
+
SecurityMetricServiceError,
|
|
36
|
+
ServiceUnavailable,
|
|
37
|
+
buildCreate,
|
|
38
|
+
buildGetLatest,
|
|
39
|
+
buildSearch,
|
|
40
|
+
checkMetric,
|
|
41
|
+
chunkRecord,
|
|
42
|
+
mapLatest,
|
|
43
|
+
mergeChunks,
|
|
44
|
+
toLocalDate,
|
|
45
|
+
toSecurityId,
|
|
46
|
+
validateRecord,
|
|
47
|
+
type DateInput,
|
|
48
|
+
type MetricResult,
|
|
49
|
+
type MetricSeries,
|
|
50
|
+
type RecordPointsInput,
|
|
51
|
+
type SecurityIdInput,
|
|
52
|
+
type SecurityMetricInput,
|
|
53
|
+
} from '../../models/security/security_metric';
|
|
54
|
+
import { Service, resolve, target, type Endpoint, type Env } from '../../util/serviceaddress';
|
|
55
|
+
|
|
56
|
+
/** Default per-call deadline of the gRPC fetcher. */
|
|
57
|
+
export const DEFAULT_DEADLINE_MS = 30_000;
|
|
58
|
+
|
|
59
|
+
/** The three SecurityMetric RPCs. `search` returns every streamed message, in order. */
|
|
60
|
+
export interface SecurityMetricFetcher {
|
|
61
|
+
getLatest(request: GetLatestSecurityMetricRequestProto): Promise<GetLatestSecurityMetricResponseProto>;
|
|
62
|
+
search(request: SearchSecurityMetricRequestProto): Promise<SearchSecurityMetricResponseProto[]>;
|
|
63
|
+
createOrUpdate(request: CreateSecurityMetricRequestProto): Promise<CreateSecurityMetricResponseProto>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface GrpcFetcherOptions {
|
|
67
|
+
/** Per-call deadline in milliseconds (default 30000). */
|
|
68
|
+
deadlineMs?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The default fetcher: a gRPC client for one endpoint. Errors become
|
|
73
|
+
* `ServiceUnavailable` (gRPC UNAVAILABLE) or `SecurityMetricServiceError`,
|
|
74
|
+
* both naming `host:port` and the status.
|
|
75
|
+
*/
|
|
76
|
+
export class GrpcSecurityMetricFetcher implements SecurityMetricFetcher {
|
|
77
|
+
readonly address: string;
|
|
78
|
+
readonly deadlineMs: number;
|
|
79
|
+
private readonly client: SecurityMetricClient;
|
|
80
|
+
|
|
81
|
+
constructor(endpoint: Endpoint, options: GrpcFetcherOptions = {}) {
|
|
82
|
+
this.address = target(endpoint);
|
|
83
|
+
this.deadlineMs = options.deadlineMs ?? DEFAULT_DEADLINE_MS;
|
|
84
|
+
const credentials = endpoint.plaintext ? grpc.credentials.createInsecure() : grpc.credentials.createSsl();
|
|
85
|
+
this.client = new SecurityMetricClient(this.address, credentials);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
close(): void {
|
|
89
|
+
this.client.close();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private callOptions(): Partial<grpc.CallOptions> {
|
|
93
|
+
return { deadline: new Date(Date.now() + this.deadlineMs) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The service error for a failed call to `rpc`. */
|
|
97
|
+
toServiceError(rpc: string, err: unknown): SecurityMetricServiceError {
|
|
98
|
+
const e = err as Partial<grpc.ServiceError>;
|
|
99
|
+
const code = typeof e?.code === 'number' ? e.code : undefined;
|
|
100
|
+
const codeName = code === undefined ? 'UNKNOWN' : (grpc.status[code] ?? String(code));
|
|
101
|
+
const details = e?.details || (err instanceof Error ? err.message : String(err));
|
|
102
|
+
if (code === grpc.status.UNAVAILABLE) {
|
|
103
|
+
return new ServiceUnavailable(`SecurityMetric service unreachable at ${this.address} (${details})`,
|
|
104
|
+
{ address: this.address, code, codeName });
|
|
105
|
+
}
|
|
106
|
+
return new SecurityMetricServiceError(`SecurityMetric ${rpc} failed at ${this.address}: ${codeName} (${details})`,
|
|
107
|
+
{ address: this.address, code, codeName });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
getLatest(request: GetLatestSecurityMetricRequestProto): Promise<GetLatestSecurityMetricResponseProto> {
|
|
111
|
+
return new Promise((resolvePromise, reject) => {
|
|
112
|
+
this.client.getLatest(request, new grpc.Metadata(), this.callOptions(), (err, response) => {
|
|
113
|
+
if (err) reject(this.toServiceError('GetLatest', err));
|
|
114
|
+
else resolvePromise(response);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
search(request: SearchSecurityMetricRequestProto): Promise<SearchSecurityMetricResponseProto[]> {
|
|
120
|
+
return new Promise((resolvePromise, reject) => {
|
|
121
|
+
const chunks: SearchSecurityMetricResponseProto[] = [];
|
|
122
|
+
const stream = this.client.search(request, this.callOptions());
|
|
123
|
+
stream.on('data', (chunk: SearchSecurityMetricResponseProto) => chunks.push(chunk));
|
|
124
|
+
stream.on('end', () => resolvePromise(chunks));
|
|
125
|
+
stream.on('error', (err: unknown) => reject(this.toServiceError('Search', err)));
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
createOrUpdate(request: CreateSecurityMetricRequestProto): Promise<CreateSecurityMetricResponseProto> {
|
|
130
|
+
return new Promise((resolvePromise, reject) => {
|
|
131
|
+
this.client.createOrUpdate(request, new grpc.Metadata(), this.callOptions(), (err, response) => {
|
|
132
|
+
if (err) reject(this.toServiceError('CreateOrUpdate', err));
|
|
133
|
+
else resolvePromise(response);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface SecurityMetricServiceOptions {
|
|
140
|
+
/** Fetcher for this instance only. Default: gRPC to `resolve(Service.PRICE, env)`. */
|
|
141
|
+
fetcher?: SecurityMetricFetcher;
|
|
142
|
+
/** Environment the default address is resolved from (default `process.env`). */
|
|
143
|
+
env?: Env;
|
|
144
|
+
/** Per-call deadline of the default gRPC fetcher in milliseconds (default 30000). */
|
|
145
|
+
deadlineMs?: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let installedFetcher: SecurityMetricFetcher | null = null;
|
|
149
|
+
let defaultService: SecurityMetricService | null = null;
|
|
150
|
+
|
|
151
|
+
class SecurityMetricService {
|
|
152
|
+
private readonly ownFetcher?: SecurityMetricFetcher;
|
|
153
|
+
private readonly env?: Env;
|
|
154
|
+
private readonly deadlineMs?: number;
|
|
155
|
+
private grpcFetcher?: GrpcSecurityMetricFetcher;
|
|
156
|
+
|
|
157
|
+
constructor(options: SecurityMetricServiceOptions = {}) {
|
|
158
|
+
this.ownFetcher = options.fetcher;
|
|
159
|
+
this.env = options.env;
|
|
160
|
+
this.deadlineMs = options.deadlineMs;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* A service whose default gRPC fetcher resolves the PRICE address from
|
|
165
|
+
* `env` instead of `process.env`.
|
|
166
|
+
*/
|
|
167
|
+
static fromEnv(env: Env, options: { deadlineMs?: number } = {}): SecurityMetricService {
|
|
168
|
+
return new SecurityMetricService({ env, deadlineMs: options.deadlineMs });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** This instance's fetcher, else the installed one, else the default gRPC fetcher (built on first use). */
|
|
172
|
+
private fetcher(): SecurityMetricFetcher {
|
|
173
|
+
if (this.ownFetcher) return this.ownFetcher;
|
|
174
|
+
if (installedFetcher) return installedFetcher;
|
|
175
|
+
if (!this.grpcFetcher) {
|
|
176
|
+
this.grpcFetcher = new GrpcSecurityMetricFetcher(
|
|
177
|
+
resolve(Service.PRICE, this.env ?? process.env), { deadlineMs: this.deadlineMs });
|
|
178
|
+
}
|
|
179
|
+
return this.grpcFetcher;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Closes the default gRPC client, if one was built. */
|
|
183
|
+
close(): void {
|
|
184
|
+
this.grpcFetcher?.close();
|
|
185
|
+
this.grpcFetcher = undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The latest value of `metric` on or before `on` (default today) for each
|
|
190
|
+
* security, in one GetLatest call. Every requested UUID string is a key;
|
|
191
|
+
* its value is undefined when the security has no point on or before `on`.
|
|
192
|
+
* No call for an empty list.
|
|
193
|
+
*/
|
|
194
|
+
async get(
|
|
195
|
+
metric: SecurityMetricInput,
|
|
196
|
+
uuids: ReadonlyArray<SecurityIdInput>,
|
|
197
|
+
on?: DateInput,
|
|
198
|
+
): Promise<Map<string, MetricResult | undefined>> {
|
|
199
|
+
const m = checkMetric(metric);
|
|
200
|
+
const onDate = on === undefined ? LocalDate.today() : toLocalDate(on);
|
|
201
|
+
const ids = uuids.map(toSecurityId);
|
|
202
|
+
if (ids.length === 0) return new Map();
|
|
203
|
+
const response = await this.fetcher().getLatest(buildGetLatest(m, ids, onDate));
|
|
204
|
+
return mapLatest(response, m, ids, onDate);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Every point of `metric` for one security from `from` to `to` (default today), both inclusive. */
|
|
208
|
+
async getHistory(
|
|
209
|
+
metric: SecurityMetricInput,
|
|
210
|
+
uuid: SecurityIdInput,
|
|
211
|
+
from: DateInput,
|
|
212
|
+
to?: DateInput,
|
|
213
|
+
): Promise<MetricSeries> {
|
|
214
|
+
const m = checkMetric(metric);
|
|
215
|
+
const securityId = toSecurityId(uuid);
|
|
216
|
+
const request = buildSearch(m, securityId, from, to === undefined ? LocalDate.today() : to);
|
|
217
|
+
const chunks = await this.fetcher().search(request);
|
|
218
|
+
return mergeChunks(chunks, securityId, m);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Stores points of `metric` for one security. Points are checked first
|
|
223
|
+
* (`InvalidMetricPoints`, nothing sent), then sent in chunks of
|
|
224
|
+
* `RECORD_CHUNK_SIZE`, one CreateOrUpdate each. Returns the as_of the
|
|
225
|
+
* service stamped on the last chunk. If a later chunk fails, the error
|
|
226
|
+
* says how many points were already stored.
|
|
227
|
+
*/
|
|
228
|
+
async record(metric: SecurityMetricInput, uuid: SecurityIdInput, points: RecordPointsInput): Promise<ZonedDateTime> {
|
|
229
|
+
const m = checkMetric(metric);
|
|
230
|
+
const securityId = toSecurityId(uuid);
|
|
231
|
+
const chunks = chunkRecord(validateRecord(m, points), RECORD_CHUNK_SIZE);
|
|
232
|
+
const fetcher = this.fetcher();
|
|
233
|
+
let stored = 0;
|
|
234
|
+
let response: CreateSecurityMetricResponseProto | undefined;
|
|
235
|
+
for (const chunk of chunks) {
|
|
236
|
+
try {
|
|
237
|
+
response = await fetcher.createOrUpdate(buildCreate(m, securityId, chunk));
|
|
238
|
+
} catch (e) {
|
|
239
|
+
if (stored === 0) throw e;
|
|
240
|
+
throw alreadyStored(e, stored);
|
|
241
|
+
}
|
|
242
|
+
stored += chunk.length;
|
|
243
|
+
}
|
|
244
|
+
const asOf = response?.getSeries()?.getAsOf();
|
|
245
|
+
if (!asOf) throw new SecurityMetricServiceError('SecurityMetric CreateOrUpdate response has no series.as_of');
|
|
246
|
+
return new ZonedDateTime(asOf);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** `e` with "N points already stored" added, keeping its class. */
|
|
251
|
+
function alreadyStored(e: unknown, stored: number): Error {
|
|
252
|
+
const suffix = `; ${stored} points already stored`;
|
|
253
|
+
if (e instanceof SecurityMetricServiceError) {
|
|
254
|
+
const details = { address: e.address, code: e.code, codeName: e.codeName };
|
|
255
|
+
return e instanceof ServiceUnavailable
|
|
256
|
+
? new ServiceUnavailable(e.message + suffix, details)
|
|
257
|
+
: new SecurityMetricServiceError(e.message + suffix, details);
|
|
258
|
+
}
|
|
259
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
260
|
+
return new SecurityMetricServiceError(`SecurityMetric CreateOrUpdate failed: ${message}${suffix}`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Installs `fetcher` for every SecurityMetricService in the process (except
|
|
265
|
+
* instances built with their own fetcher). `null` restores the default.
|
|
266
|
+
*/
|
|
267
|
+
function setSecurityMetricFetcher(fetcher: SecurityMetricFetcher | null): void {
|
|
268
|
+
installedFetcher = fetcher;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** The process-wide service that `Security.getMetric` and friends use, built on first use. */
|
|
272
|
+
function getSecurityMetricService(): SecurityMetricService {
|
|
273
|
+
if (!defaultService) defaultService = new SecurityMetricService();
|
|
274
|
+
return defaultService;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Replaces the process-wide service, e.g. with `SecurityMetricService.fromEnv(env)`
|
|
279
|
+
* in a test. `null` clears it (the next `getSecurityMetricService()` builds a new one).
|
|
280
|
+
*/
|
|
281
|
+
function setSecurityMetricService(service: SecurityMetricService | null): void {
|
|
282
|
+
defaultService = service;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Test teardown: removes the installed fetcher and closes and clears the process-wide service. */
|
|
286
|
+
function resetSecurityMetricServiceForTests(): void {
|
|
287
|
+
installedFetcher = null;
|
|
288
|
+
defaultService?.close();
|
|
289
|
+
defaultService = null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export {
|
|
293
|
+
SecurityMetricService,
|
|
294
|
+
setSecurityMetricFetcher,
|
|
295
|
+
getSecurityMetricService,
|
|
296
|
+
setSecurityMetricService,
|
|
297
|
+
resetSecurityMetricServiceForTests,
|
|
298
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SecurityMetricTypeProto } from '../../fintekkers/models/security/security_metric_type_pb';
|
|
2
|
+
import { type CreateSecurityMetricRequestProto } from '../../fintekkers/requests/security_metric/create_security_metric_request_pb';
|
|
3
|
+
import { type GetLatestSecurityMetricRequestProto } from '../../fintekkers/requests/security_metric/get_latest_security_metric_request_pb';
|
|
4
|
+
import { type SearchSecurityMetricRequestProto } from '../../fintekkers/requests/security_metric/search_security_metric_request_pb';
|
|
5
|
+
/** The seeded SPY security: SHARES_OUTSTANDING 2026-11-19 → 15204137000, 2026-11-20 → 15210000000. */
|
|
6
|
+
export declare const SPY_UUID = "5f0c2a1e-7b3d-4c8e-9a10-2b3c4d5e6f70";
|
|
7
|
+
export declare class FakeSecurityMetricServer {
|
|
8
|
+
readonly port: number;
|
|
9
|
+
readonly calls: {
|
|
10
|
+
getLatest: number;
|
|
11
|
+
search: number;
|
|
12
|
+
createOrUpdate: number;
|
|
13
|
+
};
|
|
14
|
+
readonly getLatestRequests: GetLatestSecurityMetricRequestProto[];
|
|
15
|
+
readonly searchRequests: SearchSecurityMetricRequestProto[];
|
|
16
|
+
readonly createRequests: CreateSecurityMetricRequestProto[];
|
|
17
|
+
/** `${uuid}|${metric}` → ISO date → canonical decimal string. */
|
|
18
|
+
private readonly store;
|
|
19
|
+
private readonly server;
|
|
20
|
+
private constructor();
|
|
21
|
+
/** Starts a server on 127.0.0.1 with a free port, seeded with SPY. */
|
|
22
|
+
static start(): Promise<FakeSecurityMetricServer>;
|
|
23
|
+
/** `host:port` of the server. */
|
|
24
|
+
get address(): string;
|
|
25
|
+
/** Adds points for (uuid, metric). */
|
|
26
|
+
seed(uuid: string, metric: SecurityMetricTypeProto, points: Array<[string, string]>): void;
|
|
27
|
+
/** Stops the server and drops open calls. */
|
|
28
|
+
stop(): Promise<void>;
|
|
29
|
+
private knows;
|
|
30
|
+
private sorted;
|
|
31
|
+
private series;
|
|
32
|
+
private getLatest;
|
|
33
|
+
private search;
|
|
34
|
+
private createOrUpdate;
|
|
35
|
+
}
|