@twin.org/telemetry-service 0.0.2-next.3 → 0.0.3-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,582 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@twin.org/core');
4
- var telemetryModels = require('@twin.org/telemetry-models');
5
- var web = require('@twin.org/web');
6
-
7
- /**
8
- * The source used when communicating about these routes.
9
- */
10
- const ROUTES_SOURCE = "telemetryRoutes";
11
- /**
12
- * The tag to associate with the routes.
13
- */
14
- const tagsTelemetry = [
15
- {
16
- name: "Telemetry",
17
- description: "Endpoints which are modelled to access a telemetry contract."
18
- }
19
- ];
20
- /**
21
- * The REST routes for telemetry.
22
- * @param baseRouteName Prefix to prepend to the paths.
23
- * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
24
- * @returns The generated routes.
25
- */
26
- function generateRestRoutesTelemetry(baseRouteName, componentName) {
27
- const createMetricRoute = {
28
- operationId: "telemetryCreateMetric",
29
- summary: "Create a telemetry metric",
30
- tag: tagsTelemetry[0].name,
31
- method: "POST",
32
- path: `${baseRouteName}/metric`,
33
- handler: async (httpRequestContext, request) => telemetryCreateMetric(httpRequestContext, componentName, request),
34
- requestType: {
35
- type: "ITelemetryCreateMetricRequest",
36
- examples: [
37
- {
38
- id: "telemetryCreateMetricRequestExample",
39
- request: {
40
- body: {
41
- id: "my-counter",
42
- label: "My Counter",
43
- description: "This is my counter",
44
- type: telemetryModels.MetricType.Counter,
45
- unit: "KG"
46
- }
47
- }
48
- }
49
- ]
50
- },
51
- responseType: [
52
- {
53
- type: "ICreatedResponse",
54
- examples: [
55
- {
56
- id: "telemetryCreateMetricResponseExample",
57
- response: {
58
- statusCode: web.HttpStatusCode.created,
59
- headers: {
60
- [web.HeaderTypes.Location]: "my-counter"
61
- }
62
- }
63
- }
64
- ]
65
- }
66
- ]
67
- };
68
- const getMetricRoute = {
69
- operationId: "telemetryGetMetric",
70
- summary: "Get a telemetry metric and it's most recent value",
71
- tag: tagsTelemetry[0].name,
72
- method: "GET",
73
- path: `${baseRouteName}/metric/:id`,
74
- handler: async (httpRequestContext, request) => telemetryGetMetric(httpRequestContext, componentName, request),
75
- requestType: {
76
- type: "ITelemetryGetMetricRequest",
77
- examples: [
78
- {
79
- id: "telemetryGetMetricRequestExample",
80
- request: {
81
- pathParams: {
82
- id: "my-counter"
83
- }
84
- }
85
- }
86
- ]
87
- },
88
- responseType: [
89
- {
90
- type: "ITelemetryGetMetricResponse",
91
- examples: [
92
- {
93
- id: "telemetryGetMetricResponseExample",
94
- response: {
95
- body: {
96
- metric: {
97
- id: "my-counter",
98
- label: "My Counter",
99
- description: "This is my counter",
100
- type: telemetryModels.MetricType.Counter,
101
- unit: "KG"
102
- },
103
- value: {
104
- id: "aabbccdd11223445566",
105
- ts: 1715252922273,
106
- value: 10
107
- }
108
- }
109
- }
110
- }
111
- ]
112
- }
113
- ]
114
- };
115
- const updateMetricRoute = {
116
- operationId: "telemetryUpdateMetric",
117
- summary: "Update a telemetry metric",
118
- tag: tagsTelemetry[0].name,
119
- method: "PUT",
120
- path: `${baseRouteName}/metric/:id`,
121
- handler: async (httpRequestContext, request) => telemetryUpdateMetric(httpRequestContext, componentName, request),
122
- requestType: {
123
- type: "ITelemetryUpdateMetricRequest",
124
- examples: [
125
- {
126
- id: "telemetryUpdateMetricRequestExample",
127
- request: {
128
- pathParams: {
129
- id: "my-counter"
130
- },
131
- body: {
132
- label: "My New Label"
133
- }
134
- }
135
- }
136
- ]
137
- },
138
- responseType: [
139
- {
140
- type: "INoContentResponse"
141
- }
142
- ]
143
- };
144
- const addMetricValueRoute = {
145
- operationId: "telemetryAddMetricValue",
146
- summary: "Add a telemetry metric value",
147
- tag: tagsTelemetry[0].name,
148
- method: "POST",
149
- path: `${baseRouteName}/metric/:id/value`,
150
- handler: async (httpRequestContext, request) => telemetryAddMetricValue(httpRequestContext, componentName, request),
151
- requestType: {
152
- type: "ITelemetryAddMetricValueRequest",
153
- examples: [
154
- {
155
- id: "telemetryAddMetricValueRequestExample",
156
- request: {
157
- pathParams: {
158
- id: "my-counter"
159
- },
160
- body: {
161
- value: 10
162
- }
163
- }
164
- },
165
- {
166
- id: "telemetryAddMetricValueIncRequestExample",
167
- request: {
168
- pathParams: {
169
- id: "my-counter"
170
- },
171
- body: {
172
- value: "inc"
173
- }
174
- }
175
- }
176
- ]
177
- },
178
- responseType: [
179
- {
180
- type: "ICreatedResponse",
181
- examples: [
182
- {
183
- id: "telemetryAddMetricValueResponseExample",
184
- response: {
185
- statusCode: web.HttpStatusCode.created,
186
- headers: {
187
- [web.HeaderTypes.Location]: "aabbccdd11223445566"
188
- }
189
- }
190
- }
191
- ]
192
- }
193
- ]
194
- };
195
- const removeMetricRoute = {
196
- operationId: "telemetryRemoveMetric",
197
- summary: "Remove a telemetry metric and it's values.",
198
- tag: tagsTelemetry[0].name,
199
- method: "DELETE",
200
- path: `${baseRouteName}/metric/:id`,
201
- handler: async (httpRequestContext, request) => telemetryRemoveMetric(httpRequestContext, componentName, request),
202
- requestType: {
203
- type: "ITelemetryRemoveMetricRequest",
204
- examples: [
205
- {
206
- id: "telemetryRemoveMetricRequestExample",
207
- request: {
208
- pathParams: {
209
- id: "my-counter"
210
- }
211
- }
212
- }
213
- ]
214
- },
215
- responseType: [
216
- {
217
- type: "INoContentResponse"
218
- }
219
- ]
220
- };
221
- const listMetricsRoute = {
222
- operationId: "telemetryListEntries",
223
- summary: "Get a list of the telemetry metrics",
224
- tag: tagsTelemetry[0].name,
225
- method: "GET",
226
- path: `${baseRouteName}/metric`,
227
- handler: async (httpRequestContext, request) => telemetryMetricList(httpRequestContext, componentName, request),
228
- requestType: {
229
- type: "ITelemetryListRequest",
230
- examples: [
231
- {
232
- id: "telemetryListRequestExample",
233
- request: {
234
- query: {
235
- type: telemetryModels.MetricType.Counter
236
- }
237
- }
238
- }
239
- ]
240
- },
241
- responseType: [
242
- {
243
- type: "ITelemetryListResponse",
244
- examples: [
245
- {
246
- id: "telemetryListResponseExample",
247
- response: {
248
- body: {
249
- entities: [
250
- {
251
- id: "my-counter",
252
- label: "My Counter",
253
- type: telemetryModels.MetricType.Counter,
254
- unit: "KG"
255
- },
256
- {
257
- id: "my-counter-2",
258
- label: "My Counter 2",
259
- type: telemetryModels.MetricType.IncDecCounter,
260
- unit: "M"
261
- }
262
- ],
263
- cursor: "1"
264
- }
265
- }
266
- }
267
- ]
268
- }
269
- ]
270
- };
271
- const listMetricsValuesRoute = {
272
- operationId: "telemetryValuesListEntries",
273
- summary: "Get a list of the values for a telemetry metric",
274
- tag: tagsTelemetry[0].name,
275
- method: "GET",
276
- path: `${baseRouteName}/metric/:id/value`,
277
- handler: async (httpRequestContext, request) => telemetryMetricValueList(httpRequestContext, componentName, request),
278
- requestType: {
279
- type: "ITelemetryValuesListRequest",
280
- examples: [
281
- {
282
- id: "telemetryValuesListRequestExample",
283
- request: {
284
- pathParams: {
285
- id: "my-counter"
286
- }
287
- }
288
- }
289
- ]
290
- },
291
- responseType: [
292
- {
293
- type: "ITelemetryValuesListResponse",
294
- examples: [
295
- {
296
- id: "telemetryValuesListResponseExample",
297
- response: {
298
- body: {
299
- metric: {
300
- id: "my-counter",
301
- label: "My Counter",
302
- type: telemetryModels.MetricType.Counter,
303
- unit: "KG"
304
- },
305
- entities: [
306
- {
307
- id: "aabbccdd11223445566",
308
- ts: 1715252922273,
309
- value: 10
310
- },
311
- {
312
- id: "aabbccdd11223445566",
313
- ts: 1715252922274,
314
- value: 11
315
- }
316
- ],
317
- cursor: "1"
318
- }
319
- }
320
- }
321
- ]
322
- }
323
- ]
324
- };
325
- return [
326
- createMetricRoute,
327
- getMetricRoute,
328
- updateMetricRoute,
329
- addMetricValueRoute,
330
- removeMetricRoute,
331
- listMetricsRoute,
332
- listMetricsValuesRoute
333
- ];
334
- }
335
- /**
336
- * Create a new telemetry metric.
337
- * @param httpRequestContext The request context for the API.
338
- * @param componentName The name of the component to use in the routes.
339
- * @param request The request.
340
- * @returns The response object with additional http response properties.
341
- */
342
- async function telemetryCreateMetric(httpRequestContext, componentName, request) {
343
- core.Guards.object(ROUTES_SOURCE, "request", request);
344
- core.Guards.object(ROUTES_SOURCE, "request.body", request.body);
345
- const component = core.ComponentFactory.get(componentName);
346
- await component.createMetric({
347
- id: request.body.id,
348
- label: request.body.label,
349
- description: request.body.description,
350
- type: request.body.type,
351
- unit: request.body.unit
352
- });
353
- return {
354
- statusCode: web.HttpStatusCode.created,
355
- headers: {
356
- [web.HeaderTypes.Location]: request.body.id
357
- }
358
- };
359
- }
360
- /**
361
- * Gets a telemetry metric.
362
- * @param httpRequestContext The request context for the API.
363
- * @param componentName The name of the component to use in the routes.
364
- * @param request The request.
365
- * @returns The response object with additional http response properties.
366
- */
367
- async function telemetryGetMetric(httpRequestContext, componentName, request) {
368
- core.Guards.object(ROUTES_SOURCE, "request", request);
369
- core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
370
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
371
- const component = core.ComponentFactory.get(componentName);
372
- const result = await component.getMetric(request.pathParams.id);
373
- return { body: result };
374
- }
375
- /**
376
- * Updates a telemetry metric.
377
- * @param httpRequestContext The request context for the API.
378
- * @param componentName The name of the component to use in the routes.
379
- * @param request The request.
380
- * @returns The response object with additional http response properties.
381
- */
382
- async function telemetryUpdateMetric(httpRequestContext, componentName, request) {
383
- core.Guards.object(ROUTES_SOURCE, "request", request);
384
- core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
385
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
386
- core.Guards.object(ROUTES_SOURCE, "request.body", request.body);
387
- const component = core.ComponentFactory.get(componentName);
388
- await component.updateMetric({
389
- id: request.pathParams.id,
390
- label: request.body.label,
391
- description: request.body.description,
392
- unit: request.body.unit
393
- });
394
- return { statusCode: web.HttpStatusCode.noContent };
395
- }
396
- /**
397
- * Add a telemetry metric value.
398
- * @param httpRequestContext The request context for the API.
399
- * @param componentName The name of the component to use in the routes.
400
- * @param request The request.
401
- * @returns The response object with additional http response properties.
402
- */
403
- async function telemetryAddMetricValue(httpRequestContext, componentName, request) {
404
- core.Guards.object(ROUTES_SOURCE, "request", request);
405
- core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
406
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
407
- core.Guards.object(ROUTES_SOURCE, "request.body", request.body);
408
- const component = core.ComponentFactory.get(componentName);
409
- const id = await component.addMetricValue(request.pathParams.id, request.body.value, request.body.customData);
410
- return { statusCode: web.HttpStatusCode.created, headers: { [web.HeaderTypes.Location]: id } };
411
- }
412
- /**
413
- * Removes a telemetry metric.
414
- * @param httpRequestContext The request context for the API.
415
- * @param componentName The name of the component to use in the routes.
416
- * @param request The request.
417
- * @returns The response object with additional http response properties.
418
- */
419
- async function telemetryRemoveMetric(httpRequestContext, componentName, request) {
420
- core.Guards.object(ROUTES_SOURCE, "request", request);
421
- core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
422
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
423
- const component = core.ComponentFactory.get(componentName);
424
- await component.removeMetric(request.pathParams.id);
425
- return {
426
- statusCode: web.HttpStatusCode.noContent
427
- };
428
- }
429
- /**
430
- * Get a list of the telemetry metrics.
431
- * @param httpRequestContext The request context for the API.
432
- * @param componentName The name of the component to use in the routes.
433
- * @param request The request.
434
- * @returns The response object with additional http response properties.
435
- */
436
- async function telemetryMetricList(httpRequestContext, componentName, request) {
437
- core.Guards.object(ROUTES_SOURCE, "request", request);
438
- core.Guards.object(ROUTES_SOURCE, "request.query", request.query);
439
- const component = core.ComponentFactory.get(componentName);
440
- const itemsAndCursor = await component.query(core.Coerce.number(request?.query?.type), request?.query?.cursor, core.Coerce.number(request?.query?.limit));
441
- return {
442
- body: itemsAndCursor
443
- };
444
- }
445
- /**
446
- * Get a list of the telemetry metric values.
447
- * @param httpRequestContext The request context for the API.
448
- * @param componentName The name of the component to use in the routes.
449
- * @param request The request.
450
- * @returns The response object with additional http response properties.
451
- */
452
- async function telemetryMetricValueList(httpRequestContext, componentName, request) {
453
- core.Guards.object(ROUTES_SOURCE, "request", request);
454
- core.Guards.object(ROUTES_SOURCE, "request.query", request.query);
455
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
456
- const component = core.ComponentFactory.get(componentName);
457
- const itemsAndCursor = await component.queryValues(request?.pathParams.id, core.Coerce.number(request?.query?.timeStart), core.Coerce.number(request?.query?.timeEnd), request?.query?.cursor, core.Coerce.number(request?.query?.limit));
458
- return {
459
- body: itemsAndCursor
460
- };
461
- }
462
-
463
- // Copyright 2024 IOTA Stiftung.
464
- // SPDX-License-Identifier: Apache-2.0.
465
- /**
466
- * Service for performing telemetry operations to a connector.
467
- */
468
- class TelemetryService {
469
- /**
470
- * Runtime name for the class.
471
- */
472
- static CLASS_NAME = "TelemetryService";
473
- /**
474
- * Telemetry connector used by the service.
475
- * @internal
476
- */
477
- _telemetryConnector;
478
- /**
479
- * Create a new instance of TelemetryService.
480
- * @param options The options for the connector.
481
- */
482
- constructor(options) {
483
- this._telemetryConnector = telemetryModels.TelemetryConnectorFactory.get(options?.telemetryConnectorType ?? "telemetry");
484
- }
485
- /**
486
- * Create a new metric.
487
- * @param metric The metric details.
488
- * @returns Nothing.
489
- */
490
- async createMetric(metric) {
491
- core.Guards.object(TelemetryService.CLASS_NAME, "metric", metric);
492
- await this._telemetryConnector.createMetric(metric);
493
- }
494
- /**
495
- * Get the metric details and it's most recent value.
496
- * @param id The metric id.
497
- * @returns The metric details and it's most recent value.
498
- */
499
- async getMetric(id) {
500
- core.Guards.stringValue(TelemetryService.CLASS_NAME, "id", id);
501
- return this._telemetryConnector.getMetric(id);
502
- }
503
- /**
504
- * Update metric.
505
- * @param metric The metric details.
506
- * @returns Nothing.
507
- */
508
- async updateMetric(metric) {
509
- core.Guards.object(TelemetryService.CLASS_NAME, "metric", metric);
510
- core.Guards.stringValue(TelemetryService.CLASS_NAME, "metric.id", metric.id);
511
- return this._telemetryConnector.updateMetric(metric);
512
- }
513
- /**
514
- * Add a metric value.
515
- * @param id The id of the metric.
516
- * @param value The value for the add operation.
517
- * @param customData The custom data for the add operation.
518
- * @returns The created metric value id.
519
- */
520
- async addMetricValue(id, value, customData) {
521
- core.Guards.stringValue(TelemetryService.CLASS_NAME, "id", id);
522
- core.Guards.defined(TelemetryService.CLASS_NAME, "value", value);
523
- return this._telemetryConnector.addMetricValue(id, value, customData);
524
- }
525
- /**
526
- * Remove metric.
527
- * @param id The id of the metric.
528
- * @returns Nothing.
529
- */
530
- async removeMetric(id) {
531
- core.Guards.stringValue(TelemetryService.CLASS_NAME, "id", id);
532
- return this._telemetryConnector.removeMetric(id);
533
- }
534
- /**
535
- * Query the metrics.
536
- * @param type The type of the metric.
537
- * @param cursor The cursor to request the next chunk of entities.
538
- * @param limit Limit the number of entities to return.
539
- * @returns All the entities for the storage matching the conditions,
540
- * and a cursor which can be used to request more entities.
541
- * @throws NotImplementedError if the implementation does not support retrieval.
542
- */
543
- async query(type, cursor, limit) {
544
- return this._telemetryConnector.query(type, cursor, limit);
545
- }
546
- /**
547
- * Query the metric values.
548
- * @param id The id of the metric.
549
- * @param timeStart The inclusive time as the start of the metric entries.
550
- * @param timeEnd The inclusive time as the end of the metric entries.
551
- * @param cursor The cursor to request the next chunk of entities.
552
- * @param limit Limit the number of entities to return.
553
- * @returns All the entities for the storage matching the conditions,
554
- * and a cursor which can be used to request more entities.
555
- * @throws NotImplementedError if the implementation does not support retrieval.
556
- */
557
- async queryValues(id, timeStart, timeEnd, cursor, limit) {
558
- core.Guards.stringValue(TelemetryService.CLASS_NAME, "id", id);
559
- return this._telemetryConnector.queryValues(id, timeStart, timeEnd, cursor, limit);
560
- }
561
- }
562
-
563
- const restEntryPoints = [
564
- {
565
- name: "telemetry",
566
- defaultBaseRoute: "telemetry",
567
- tags: tagsTelemetry,
568
- generateRoutes: generateRestRoutesTelemetry
569
- }
570
- ];
571
-
572
- exports.TelemetryService = TelemetryService;
573
- exports.generateRestRoutesTelemetry = generateRestRoutesTelemetry;
574
- exports.restEntryPoints = restEntryPoints;
575
- exports.tagsTelemetry = tagsTelemetry;
576
- exports.telemetryAddMetricValue = telemetryAddMetricValue;
577
- exports.telemetryCreateMetric = telemetryCreateMetric;
578
- exports.telemetryGetMetric = telemetryGetMetric;
579
- exports.telemetryMetricList = telemetryMetricList;
580
- exports.telemetryMetricValueList = telemetryMetricValueList;
581
- exports.telemetryRemoveMetric = telemetryRemoveMetric;
582
- exports.telemetryUpdateMetric = telemetryUpdateMetric;