@dnv-plant/typescriptpws 1.0.27 → 1.0.38-alpha.1846913

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.
@@ -0,0 +1,511 @@
1
+ /***********************************************************************
2
+ * This file has been auto-generated by a code generation tool.
3
+ * Version: 1.0.38
4
+ * Date/time: 24 Mar 2025 16:46:20
5
+ * Template: templates/typescriptpws/calculations.razor.
6
+ ***********************************************************************/
7
+
8
+
9
+
10
+ import * as Enums from "../enums";
11
+ import * as Entities from "../entities";
12
+ import * as EntitySchemas from "../entity-schemas";
13
+ import { getAnalyticsApiTarget, getClientAliasId, postRequest } from "../utilities";
14
+
15
+ import Joi from "joi";
16
+ import { AxiosResponse } from "axios";
17
+
18
+
19
+ class CalculationRequestBase {
20
+ /**
21
+ * Calculation request base class.
22
+ */
23
+ constructor() {
24
+ // Base class initialization code
25
+ }
26
+ }
27
+
28
+ class CalculationBase {
29
+ resultCode?: Enums.ResultCode = Enums.ResultCode.SUCCESS;
30
+ messages: string[] = [];
31
+ calculationElapsedTime?: number = 0.0;
32
+ operationId?: string = "";
33
+
34
+ /**
35
+ * Post JSON to URL and time the call
36
+ */
37
+ async postRequest(url: string, data: string): Promise<AxiosResponse> {
38
+ return postRequest(url, data);
39
+ }
40
+
41
+ /**
42
+ * Utility method to print the messages returned by the calculation.
43
+ */
44
+ printMessages(): void {
45
+ if (this.messages && this.messages.length > 0) {
46
+ this.messages.forEach((message) => console.log(message));
47
+ } else {
48
+ console.log("No messages");
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Utility method to handle a failed response.
54
+ */
55
+ handleFailedResponse(response: AxiosResponse): void {
56
+ try {
57
+ const validatedFailedResponse = new CalculationFailedResponseSchema().validate(response.data);
58
+
59
+ this.resultCode = validatedFailedResponse.resultCode;
60
+ this.messages.push(...(validatedFailedResponse.messages ?? []));
61
+ this.calculationElapsedTime = validatedFailedResponse.calculationElapsedTime;
62
+ this.operationId = validatedFailedResponse.operationId;
63
+ } catch (error) {
64
+ if (error instanceof Error) {
65
+ this.messages.push(`Failed to parse response: ${error.message}`);
66
+ } else {
67
+ this.messages.push("An unknown error occurred during response parsing.");
68
+ }
69
+ console.error("Failed to parse response:", error);
70
+ } finally {
71
+ this.messages.push(`${response.statusText} (Status code: ${response.status})`);
72
+ }
73
+ }
74
+ }
75
+
76
+ class CalculationResponseBase {
77
+ resultCode?: Enums.ResultCode;
78
+ messages?: string[];
79
+ calculationElapsedTime?: number;
80
+ operationId?: string;
81
+
82
+ /**
83
+ * Calculation response base class.
84
+ */
85
+ constructor(
86
+ resultCode?: Enums.ResultCode,
87
+ messages?: string[],
88
+ calculationElapsedTime?: number,
89
+ operationId?: string
90
+ ) {
91
+ this.resultCode = resultCode;
92
+ this.messages = messages;
93
+ this.calculationElapsedTime = calculationElapsedTime;
94
+ this.operationId = operationId;
95
+ }
96
+ }
97
+
98
+ class CalculationFailedResponse extends CalculationResponseBase {
99
+ /**
100
+ * Calculation failed response class.
101
+ */
102
+ constructor(
103
+ resultCode?: Enums.ResultCode,
104
+ messages: string[] = [],
105
+ calculationElapsedTime: number = 0,
106
+ operationId: string = ""
107
+ ) {
108
+ super(resultCode, messages, calculationElapsedTime, operationId);
109
+ }
110
+ }
111
+
112
+ class CalculationFailedResponseSchema {
113
+ schema: Joi.ObjectSchema;
114
+
115
+ /**
116
+ * Calculation failed response schema.
117
+ */
118
+ constructor() {
119
+ this.schema = Joi.object({
120
+ resultCode: Joi.string()
121
+ .valid(...Object.values(Enums.ResultCode))
122
+ .required(),
123
+ messages: Joi.array().items(Joi.string()).required(),
124
+ calculationElapsedTime: Joi.number().required(),
125
+ operationId: Joi.string().required(),
126
+ }).unknown(true);
127
+ }
128
+
129
+ validate(data: {
130
+ resultCode: Enums.ResultCode;
131
+ messages: string[];
132
+ calculationElapsedTime: number;
133
+ operationId: string;
134
+ }) {
135
+ const { error, value } = this.schema.validate(data);
136
+ if (error) throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
137
+ return this.makeCalculationFailedResponse(value);
138
+ }
139
+
140
+ makeCalculationFailedResponse(data: {
141
+ resultCode: Enums.ResultCode;
142
+ messages: string[];
143
+ calculationElapsedTime: number;
144
+ operationId: string;
145
+ }) {
146
+ return new CalculationFailedResponse(data.resultCode, data.messages, data.calculationElapsedTime, data.operationId);
147
+ }
148
+ }
149
+
150
+ export interface DispersionCalculationRequestSchemaData {
151
+ material: Entities.Material;
152
+ substrate: Entities.Substrate;
153
+ dischargeResult: Entities.DischargeResult;
154
+ dischargeRecords: Entities.DischargeRecord[];
155
+ dischargeRecordCount: number;
156
+ weather: Entities.Weather;
157
+ dispersionParameters: Entities.DispersionParameters;
158
+ endPointConcentration: number;
159
+ }
160
+
161
+ class DispersionCalculationRequest extends CalculationRequestBase {
162
+ material: Entities.Material;
163
+ substrate: Entities.Substrate;
164
+ dischargeResult: Entities.DischargeResult;
165
+ dischargeRecords: Entities.DischargeRecord[];
166
+ dischargeRecordCount: number;
167
+ weather: Entities.Weather;
168
+ dispersionParameters: Entities.DispersionParameters;
169
+ endPointConcentration: number;
170
+
171
+ /**
172
+ * Dispersion calculation request class.
173
+ *
174
+ * @param {Entities.Material} material - A Material entity with post-discharge composition.
175
+ * @param {Entities.Substrate} substrate - A Substrate entity.
176
+ * @param {Entities.DischargeResult} dischargeResult - Discharge / source term definition.
177
+ * @param {Entities.DischargeRecord[]} dischargeRecords - An array of Discharge Records.
178
+ * @param {number} dischargeRecordCount - Number of Discharge Records.
179
+ * @param {Entities.Weather} weather - A Weather entity.
180
+ * @param {Entities.DispersionParameters} dispersionParameters - A Dispersion Parameters entity.
181
+ * @param {number} endPointConcentration - Concentration at which the dispersion calculations will terminate (v/v fraction).
182
+ */
183
+ constructor(
184
+ material: Entities.Material,
185
+ substrate: Entities.Substrate,
186
+ dischargeResult: Entities.DischargeResult,
187
+ dischargeRecords: Entities.DischargeRecord[],
188
+ dischargeRecordCount: number,
189
+ weather: Entities.Weather,
190
+ dispersionParameters: Entities.DispersionParameters,
191
+ endPointConcentration: number
192
+ ) {
193
+ super();
194
+ this.material = material;
195
+ this.substrate = substrate;
196
+ this.dischargeResult = dischargeResult;
197
+ this.dischargeRecords = dischargeRecords;
198
+ this.dischargeRecordCount = dischargeRecordCount;
199
+ this.weather = weather;
200
+ this.dispersionParameters = dispersionParameters;
201
+ this.endPointConcentration = endPointConcentration;
202
+ }
203
+ }
204
+
205
+ export class DispersionCalculationRequestSchema {
206
+ schema: Joi.ObjectSchema;
207
+ propertyTypes: Record<string, string>;
208
+
209
+ /**
210
+ * Schema for the Dispersion calculation request.
211
+ */
212
+ constructor() {
213
+ this.schema = Joi.object({
214
+ material: new EntitySchemas.MaterialSchema().schema,
215
+ substrate: new EntitySchemas.SubstrateSchema().schema,
216
+ dischargeResult: new EntitySchemas.DischargeResultSchema().schema,
217
+ dischargeRecords: Joi.array().items(new EntitySchemas.DischargeRecordSchema().schema).allow(null),
218
+ dischargeRecordCount: Joi.number().integer(),
219
+ weather: new EntitySchemas.WeatherSchema().schema,
220
+ dispersionParameters: new EntitySchemas.DispersionParametersSchema().schema,
221
+ endPointConcentration: Joi.number().unsafe(),
222
+ }).unknown(true);
223
+
224
+ this.propertyTypes = {
225
+ material: "Entities.Material",
226
+ substrate: "Entities.Substrate",
227
+ dischargeResult: "Entities.DischargeResult",
228
+ dischargeRecords: "Entities.DischargeRecord[]",
229
+ dischargeRecordCount: "number",
230
+ weather: "Entities.Weather",
231
+ dispersionParameters: "Entities.DispersionParameters",
232
+ endPointConcentration: "number",
233
+ };
234
+ }
235
+
236
+ validate(data: DispersionCalculationRequestSchemaData): DispersionCalculationRequest {
237
+ const { error, value } = this.schema.validate(data, { abortEarly: false });
238
+ if (error) {
239
+ throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
240
+ }
241
+ return this.makeCalculationRequest(value);
242
+ }
243
+
244
+ makeCalculationRequest(data: DispersionCalculationRequestSchemaData): DispersionCalculationRequest {
245
+ return new DispersionCalculationRequest(
246
+ data.material,
247
+ data.substrate,
248
+ data.dischargeResult,
249
+ data.dischargeRecords,
250
+ data.dischargeRecordCount,
251
+ data.weather,
252
+ data.dispersionParameters,
253
+ data.endPointConcentration
254
+ );
255
+ }
256
+ }
257
+
258
+ export class DispersionCalculation extends CalculationBase {
259
+ material: Entities.Material;
260
+ substrate: Entities.Substrate;
261
+ dischargeResult: Entities.DischargeResult;
262
+ dischargeRecords: Entities.DischargeRecord[];
263
+ dischargeRecordCount: number;
264
+ weather: Entities.Weather;
265
+ dispersionParameters: Entities.DispersionParameters;
266
+ endPointConcentration: number;
267
+ scalarUdmOutputs?: Entities.ScalarUdmOutputs;
268
+ dispersionRecords?: Entities.DispersionRecord[];
269
+ poolRecords?: Entities.PoolRecord[];
270
+
271
+ /**
272
+ * Calculates atmospheric dispersion of a source term (either instantaneous, fixed duration, or time-varying). Simulates the dispersion of one or more "observers", which are returned as outputs and can be used to obtain post-processed concentrations and other views of the dispersing cloud. Each observer is comprised of a number
273
+ of DispersionRecords. The modelling includes rainout and re-evaporation for 2-phase releases.
274
+ Time-varying scenarios with mixtures are not currently handled and will give an error. It is recommended therefore to use a continuous or instantaneous source for mixtures.
275
+ *
276
+ * @param {Entities.Material} material - A Material entity with post-discharge composition.
277
+ * @param {Entities.Substrate} substrate - A Substrate entity.
278
+ * @param {Entities.DischargeResult} dischargeResult - Discharge / source term definition.
279
+ * @param {Entities.DischargeRecord[]} dischargeRecords - An array of Discharge Records.
280
+ * @param {number} dischargeRecordCount - Number of Discharge Records.
281
+ * @param {Entities.Weather} weather - A Weather entity.
282
+ * @param {Entities.DispersionParameters} dispersionParameters - A Dispersion Parameters entity.
283
+ * @param {number} endPointConcentration - Concentration at which the dispersion calculations will terminate (v/v fraction).
284
+ */
285
+ constructor(
286
+ material: Entities.Material,
287
+ substrate: Entities.Substrate,
288
+ dischargeResult: Entities.DischargeResult,
289
+ dischargeRecords: Entities.DischargeRecord[],
290
+ dischargeRecordCount: number,
291
+ weather: Entities.Weather,
292
+ dispersionParameters: Entities.DispersionParameters,
293
+ endPointConcentration: number
294
+ ) {
295
+ super();
296
+ this.material = material;
297
+ this.substrate = substrate;
298
+ this.dischargeResult = dischargeResult;
299
+ this.dischargeRecords = dischargeRecords;
300
+ this.dischargeRecordCount = dischargeRecordCount;
301
+ this.weather = weather;
302
+ this.dispersionParameters = dispersionParameters;
303
+ this.endPointConcentration = endPointConcentration;
304
+ }
305
+
306
+ async run() {
307
+ try {
308
+ const request = new DispersionCalculationRequest(
309
+ this.material,
310
+ this.substrate,
311
+ this.dischargeResult,
312
+ this.dischargeRecords,
313
+ this.dischargeRecordCount,
314
+ this.weather,
315
+ this.dispersionParameters,
316
+ this.endPointConcentration
317
+ );
318
+
319
+ const schema = new DispersionCalculationRequestSchema();
320
+ const validatedRequest = schema.validate(request);
321
+
322
+ const requestJson = JSON.stringify(validatedRequest);
323
+ const url = `${getAnalyticsApiTarget()}calculatedispersion?clientId=${getClientAliasId()}`;
324
+
325
+ this.resultCode = Enums.ResultCode.UNEXPECTED_APPLICATION_ERROR;
326
+
327
+ const response = await this.postRequest(url, requestJson);
328
+
329
+ if (response.status >= 200 && response.status < 300) {
330
+ const schema = new DispersionCalculationResponseSchema();
331
+ const validatedResponse = schema.validate(response.data);
332
+
333
+ this.resultCode = validatedResponse.resultCode;
334
+ if (this.resultCode === Enums.ResultCode.SUCCESS) {
335
+ this.scalarUdmOutputs = validatedResponse.scalarUdmOutputs;
336
+ this.dispersionRecords = validatedResponse.dispersionRecords;
337
+ this.poolRecords = validatedResponse.poolRecords;
338
+ this.resultCode = validatedResponse.resultCode;
339
+ this.messages = validatedResponse.messages ?? [];
340
+ this.calculationElapsedTime = validatedResponse.calculationElapsedTime;
341
+ this.operationId = validatedResponse.operationId;
342
+ } else {
343
+ this.messages.push(...(validatedResponse.messages ?? []));
344
+ }
345
+ } else {
346
+ this.handleFailedResponse(response);
347
+ }
348
+ } catch (error) {
349
+ if (error instanceof Error) {
350
+ this.messages.push(`Error: ${error.message}`);
351
+ } else {
352
+ this.messages.push(`Unexpected error: ${JSON.stringify(error)}`);
353
+ }
354
+ console.error(error);
355
+ this.resultCode = Enums.ResultCode.UNEXPECTED_APPLICATION_ERROR;
356
+ }
357
+
358
+ return this.resultCode;
359
+ }
360
+
361
+ toString() {
362
+ const parts = ["* Dispersion"];
363
+
364
+ parts.push(`scalarUdmOutputs: ${String(this.scalarUdmOutputs)}`);
365
+ parts.push("*** dispersionRecords:");
366
+ parts.push(
367
+ this.dispersionRecords && this.dispersionRecords.length > 0
368
+ ? this.dispersionRecords.map((point) => `dispersionRecordsElement: ${point}`).join("\n")
369
+ : "dispersionRecords does not contain any elements"
370
+ );
371
+ parts.push("*** poolRecords:");
372
+ parts.push(
373
+ this.poolRecords && this.poolRecords.length > 0
374
+ ? this.poolRecords.map((point) => `poolRecordsElement: ${point}`).join("\n")
375
+ : "poolRecords does not contain any elements"
376
+ );
377
+ parts.push(`resultCode: ${String(this.resultCode)}`);
378
+ parts.push("*** messages:");
379
+ parts.push(`messages: ${this.messages !== undefined ? this.messages : "(None)"}`);
380
+ parts.push(`calculationElapsedTime: ${this.calculationElapsedTime !== undefined ? this.calculationElapsedTime : "(None)"}`);
381
+ parts.push(`operationId: ${this.operationId !== undefined ? this.operationId : "(None)"}`);
382
+
383
+ return parts.join("\n");
384
+ }
385
+ }
386
+
387
+ export class DispersionCalculationResponse extends CalculationResponseBase {
388
+ scalarUdmOutputs: Entities.ScalarUdmOutputs;
389
+ dispersionRecords: Entities.DispersionRecord[];
390
+ poolRecords: Entities.PoolRecord[];
391
+
392
+ /**
393
+ * Dispersion calculation response class.
394
+ *
395
+ * @param {Entities.ScalarUdmOutputs} scalarUdmOutputs - Scalar dispersion results.
396
+ * @param {Entities.DispersionRecord[]} dispersionRecords - An array of Dispersion Records.
397
+ * @param {Entities.PoolRecord[]} poolRecords - An array of Pool Records.
398
+ */
399
+ constructor(
400
+ scalarUdmOutputs: Entities.ScalarUdmOutputs,
401
+ dispersionRecords: Entities.DispersionRecord[],
402
+ poolRecords: Entities.PoolRecord[],
403
+ resultCode: Enums.ResultCode,
404
+ messages: string[],
405
+ calculationElapsedTime: number,
406
+ operationId: string
407
+ ) {
408
+ super();
409
+ this.scalarUdmOutputs = scalarUdmOutputs;
410
+ this.dispersionRecords = dispersionRecords;
411
+ this.poolRecords = poolRecords;
412
+ this.resultCode = resultCode;
413
+ this.messages = messages;
414
+ this.calculationElapsedTime = calculationElapsedTime;
415
+ this.operationId = operationId;
416
+ }
417
+
418
+ initialiseFromDictionary(data: { [key: string]: unknown }) {
419
+ if (data.scalarUdmOutputs) {
420
+ this.scalarUdmOutputs = new Entities.ScalarUdmOutputs();
421
+ this.scalarUdmOutputs.initialiseFromDictionary(data.scalarUdmOutputs as { [key: string]: unknown });
422
+ }
423
+ if (data.dispersionRecords && Array.isArray(data.dispersionRecords)) {
424
+ this.dispersionRecords = data.dispersionRecords.map(
425
+ (item) => {
426
+ const record = new Entities.DispersionRecord();
427
+ record.initialiseFromDictionary(item);
428
+ return record;
429
+ }
430
+ );
431
+ }
432
+ if (data.poolRecords && Array.isArray(data.poolRecords)) {
433
+ this.poolRecords = data.poolRecords.map(
434
+ (item) => {
435
+ const record = new Entities.PoolRecord();
436
+ record.initialiseFromDictionary(item);
437
+ return record;
438
+ }
439
+ );
440
+ }
441
+ if (data.resultCode !== undefined && (typeof data.resultCode === "string" || typeof data.resultCode === "number")) {
442
+ this.resultCode = data.resultCode as Enums.ResultCode;
443
+ }
444
+ this.messages = this.messages ?? [];
445
+ if (data.messages && Array.isArray(data.messages)) {
446
+ this.messages.push(...data.messages);
447
+ }
448
+ if (data.calculationElapsedTime !== undefined && typeof data.calculationElapsedTime === "number") {
449
+ this.calculationElapsedTime = data.calculationElapsedTime as number;
450
+ }
451
+ if (data.operationId !== undefined && typeof data.operationId === "string") {
452
+ this.operationId = data.operationId as string;
453
+ }
454
+ }
455
+ }
456
+
457
+ export interface DispersionCalculationResponseSchemaData {
458
+ scalarUdmOutputs: Entities.ScalarUdmOutputs;
459
+ dispersionRecords: Entities.DispersionRecord[];
460
+ poolRecords: Entities.PoolRecord[];
461
+ resultCode: Enums.ResultCode;
462
+ messages: string[];
463
+ calculationElapsedTime: number;
464
+ operationId: string;
465
+ }
466
+
467
+ export class DispersionCalculationResponseSchema {
468
+ schema: Joi.ObjectSchema;
469
+ propertyTypes: Record<string, string>;
470
+
471
+ /**
472
+ * Schema for the Dispersion calculation response.
473
+ */
474
+ constructor() {
475
+ this.schema = Joi.object({
476
+ scalarUdmOutputs: new EntitySchemas.ScalarUdmOutputsSchema().schema,
477
+ dispersionRecords: Joi.array().items(new EntitySchemas.DispersionRecordSchema().schema).allow(null),
478
+ poolRecords: Joi.array().items(new EntitySchemas.PoolRecordSchema().schema).allow(null),
479
+ resultCode: Joi.string().valid(...Object.values(Enums.ResultCode)),
480
+ messages: Joi.array().items(Joi.string()),
481
+ calculationElapsedTime: Joi.number().unsafe(),
482
+ operationId: Joi.string().uuid().allow(null),
483
+ }).unknown(true);
484
+
485
+ this.propertyTypes = {
486
+ scalarUdmOutputs: "Entities.ScalarUdmOutputs",
487
+ dispersionRecords: "Entities.DispersionRecord[]",
488
+ poolRecords: "Entities.PoolRecord[]",
489
+ };
490
+ }
491
+
492
+ validate(data: DispersionCalculationResponseSchemaData): DispersionCalculationResponse {
493
+ const { error, value } = this.schema.validate(data, { abortEarly: false });
494
+ if (error) {
495
+ throw new Error(`Validation error: ${error.details.map((x) => x.message).join(", ")}`);
496
+ }
497
+ return this.makeCalculationResponse(value);
498
+ }
499
+
500
+ makeCalculationResponse(data: DispersionCalculationResponseSchemaData): DispersionCalculationResponse {
501
+ return new DispersionCalculationResponse(
502
+ data.scalarUdmOutputs,
503
+ data.dispersionRecords,
504
+ data.poolRecords,
505
+ data.resultCode,
506
+ data.messages,
507
+ data.calculationElapsedTime,
508
+ data.operationId
509
+ );
510
+ }
511
+ }