@lwmacct/260529-promclient 0.16.260628 → 0.17.260629
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 +150 -147
- package/dist/transform/instant.d.ts +0 -1
- package/dist/transform/instant.d.ts.map +1 -1
- package/dist/transform/instant.js +0 -19
- package/dist/transform/instant.js.map +1 -1
- package/dist/transform/records.d.ts +39 -36
- package/dist/transform/records.d.ts.map +1 -1
- package/dist/transform/records.js +100 -124
- package/dist/transform/records.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -177,90 +177,50 @@ const time = serializeTime(new Date());
|
|
|
177
177
|
|
|
178
178
|
Prometheus 的 sample value 是字符串。转换工具会在需要时解析为 number,并过滤 `NaN`。
|
|
179
179
|
|
|
180
|
-
###
|
|
180
|
+
### Record Schema Transform
|
|
181
181
|
|
|
182
|
-
有些指标会把表格结构编码到 labels 中,例如用某些 labels 表示
|
|
182
|
+
有些指标会把表格结构编码到 labels 中,例如用某些 labels 表示 record key、field name,并用 sample value 或某个 label 表示字段值。`mapVectorToRecords` 和 `mapVectorToRecordMap` 使用 schema 直接把 vector 转成对象,不再暴露中间 field rows。
|
|
183
183
|
|
|
184
184
|
示例一:sample value 作为字段值。
|
|
185
185
|
|
|
186
|
-
假设查询返回这些 vector samples:
|
|
187
|
-
|
|
188
186
|
```promql
|
|
189
187
|
demo_service_quota{tenant="acme", service="api", resource="cpu"} 4
|
|
190
188
|
demo_service_quota{tenant="acme", service="api", resource="memory_gb"} 16
|
|
191
189
|
demo_service_quota{tenant="acme", service="worker", resource="cpu"} 2
|
|
192
190
|
```
|
|
193
191
|
|
|
194
|
-
可以把它理解为:
|
|
195
|
-
|
|
196
|
-
| index | key | field | value |
|
|
197
|
-
| --- | --- | --- | --- |
|
|
198
|
-
| `acme` | `api` | `cpu` | `4` |
|
|
199
|
-
| `acme` | `api` | `memory_gb` | `16` |
|
|
200
|
-
| `acme` | `worker` | `cpu` | `2` |
|
|
201
|
-
|
|
202
192
|
```ts
|
|
203
193
|
import {
|
|
204
|
-
|
|
205
|
-
|
|
194
|
+
label,
|
|
195
|
+
labels,
|
|
196
|
+
mapVectorToRecords,
|
|
197
|
+
numberSample,
|
|
206
198
|
} from "@lwmacct/260529-promclient";
|
|
207
199
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// }
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
如果字段由多个 label 共同决定,可以把多个 label 组合成字段名:
|
|
232
|
-
|
|
233
|
-
```ts
|
|
234
|
-
const rows = mapVectorToFieldRows(response, {
|
|
235
|
-
indexLabels: ["tenant"],
|
|
236
|
-
keyLabels: ["service"],
|
|
237
|
-
fieldLabels: ["method", "status"],
|
|
200
|
+
type ServiceQuota = {
|
|
201
|
+
key: string;
|
|
202
|
+
tenant: string;
|
|
203
|
+
service: string;
|
|
204
|
+
cpu?: number;
|
|
205
|
+
memoryGb?: number;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const rows = mapVectorToRecords<ServiceQuota>(response, {
|
|
209
|
+
key: labels(["tenant", "service"]),
|
|
210
|
+
field: label("resource"),
|
|
211
|
+
base: {
|
|
212
|
+
key: labels(["tenant", "service"]),
|
|
213
|
+
tenant: label("tenant"),
|
|
214
|
+
service: label("service"),
|
|
215
|
+
},
|
|
216
|
+
fields: {
|
|
217
|
+
cpu: numberSample("cpu"),
|
|
218
|
+
memory_gb: numberSample("memoryGb"),
|
|
219
|
+
},
|
|
238
220
|
});
|
|
239
|
-
// method="GET", status="200" -> field: "GET.200"
|
|
240
221
|
```
|
|
241
222
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
```ts
|
|
245
|
-
const rows = [
|
|
246
|
-
...mapVectorToFieldRows(inResponse, {
|
|
247
|
-
keyLabels: ["switch", "ifIndex"],
|
|
248
|
-
field: "inBps",
|
|
249
|
-
valueSource: "sample",
|
|
250
|
-
}),
|
|
251
|
-
...mapVectorToFieldRows(outResponse, {
|
|
252
|
-
keyLabels: ["switch", "ifIndex"],
|
|
253
|
-
field: "outBps",
|
|
254
|
-
valueSource: "sample",
|
|
255
|
-
}),
|
|
256
|
-
];
|
|
257
|
-
|
|
258
|
-
const table = mapFieldRowsByKey(rows);
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
示例二:`value` label 作为字段值。
|
|
262
|
-
|
|
263
|
-
这类指标通常用 sample value `1` 表示“这条信息存在”,真正的字段值在 label 中:
|
|
223
|
+
示例二:字段值来自 label。
|
|
264
224
|
|
|
265
225
|
```promql
|
|
266
226
|
demo_asset_info{asset="srv-01", field="region", value="us-east"} 1
|
|
@@ -268,93 +228,152 @@ demo_asset_info{asset="srv-01", field="owner", value="platform"} 1
|
|
|
268
228
|
demo_asset_info{asset="srv-02", field="region", value="eu-west"} 1
|
|
269
229
|
```
|
|
270
230
|
|
|
271
|
-
可以把它理解为:
|
|
272
|
-
|
|
273
|
-
| key | field | value |
|
|
274
|
-
| --- | --- | --- |
|
|
275
|
-
| `srv-01` | `region` | `us-east` |
|
|
276
|
-
| `srv-01` | `owner` | `platform` |
|
|
277
|
-
| `srv-02` | `region` | `eu-west` |
|
|
278
|
-
|
|
279
231
|
```ts
|
|
280
232
|
import {
|
|
281
|
-
|
|
282
|
-
|
|
233
|
+
label,
|
|
234
|
+
labelValue,
|
|
235
|
+
mapVectorToRecordMap,
|
|
283
236
|
} from "@lwmacct/260529-promclient";
|
|
284
237
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
238
|
+
type AssetInfo = {
|
|
239
|
+
asset: string;
|
|
240
|
+
region?: string;
|
|
241
|
+
owner?: string;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const table = mapVectorToRecordMap<AssetInfo>(response, {
|
|
245
|
+
key: label("asset"),
|
|
246
|
+
field: label("field"),
|
|
247
|
+
base: {
|
|
248
|
+
asset: label("asset"),
|
|
249
|
+
},
|
|
250
|
+
fields: {
|
|
251
|
+
region: labelValue("region", "value"),
|
|
252
|
+
owner: labelValue("owner", "value"),
|
|
253
|
+
},
|
|
292
254
|
});
|
|
293
|
-
|
|
294
|
-
const table = mapFieldRowsByKey(rows);
|
|
295
|
-
// {
|
|
296
|
-
// "srv-01": {
|
|
297
|
-
// region: "us-east",
|
|
298
|
-
// owner: "platform"
|
|
299
|
-
// },
|
|
300
|
-
// "srv-02": {
|
|
301
|
-
// region: "eu-west"
|
|
302
|
-
// }
|
|
303
|
-
// }
|
|
304
255
|
```
|
|
305
256
|
|
|
306
|
-
|
|
257
|
+
示例三:部分字段来自 label,部分字段来自 sample。
|
|
307
258
|
|
|
308
259
|
```ts
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
260
|
+
import {
|
|
261
|
+
autoValue,
|
|
262
|
+
label,
|
|
263
|
+
labelValue,
|
|
264
|
+
labels,
|
|
265
|
+
mapVectorToRecords,
|
|
266
|
+
numberSample,
|
|
267
|
+
} from "@lwmacct/260529-promclient";
|
|
268
|
+
|
|
269
|
+
type DiskInfo = {
|
|
270
|
+
key: string;
|
|
271
|
+
host: string;
|
|
272
|
+
serialNumber: string;
|
|
273
|
+
modelNumber?: string;
|
|
274
|
+
namespace?: string;
|
|
275
|
+
totalBytes?: number;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const disks = mapVectorToRecords<DiskInfo>(response, {
|
|
279
|
+
key: labels(["host", "serial"]),
|
|
280
|
+
field: label("field"),
|
|
281
|
+
base: {
|
|
282
|
+
key: labels(["host", "serial"]),
|
|
283
|
+
host: label("host"),
|
|
284
|
+
serialNumber: label("serial"),
|
|
285
|
+
},
|
|
286
|
+
fields: {
|
|
287
|
+
model_number: labelValue("modelNumber", "value"),
|
|
288
|
+
namespace: autoValue("namespace", "value"),
|
|
289
|
+
total_bytes: numberSample("totalBytes"),
|
|
319
290
|
},
|
|
320
291
|
});
|
|
321
292
|
```
|
|
322
293
|
|
|
323
|
-
|
|
294
|
+
示例四:字段来自 metric name。
|
|
324
295
|
|
|
325
296
|
```ts
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
297
|
+
import {
|
|
298
|
+
label,
|
|
299
|
+
mapVectorToRecordMap,
|
|
300
|
+
metricName,
|
|
301
|
+
numberSample,
|
|
302
|
+
} from "@lwmacct/260529-promclient";
|
|
303
|
+
|
|
304
|
+
type MemoryStats = {
|
|
305
|
+
total?: number;
|
|
306
|
+
available?: number;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const stats = mapVectorToRecordMap<MemoryStats>(response, {
|
|
310
|
+
key: label("instance"),
|
|
311
|
+
field: metricName(),
|
|
312
|
+
fields: {
|
|
313
|
+
node_memory_MemTotal_bytes: numberSample("total"),
|
|
314
|
+
node_memory_MemAvailable_bytes: numberSample("available"),
|
|
340
315
|
},
|
|
341
316
|
});
|
|
342
317
|
```
|
|
343
318
|
|
|
344
|
-
`
|
|
319
|
+
多个查询结果需要合并成同一批 records 时,可以使用 `mergeVectorRecords`。每个输入可以有独立 schema,只要 key 相同就会合并到同一个对象。
|
|
345
320
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
321
|
+
```ts
|
|
322
|
+
import {
|
|
323
|
+
constant,
|
|
324
|
+
label,
|
|
325
|
+
labels,
|
|
326
|
+
mergeVectorRecords,
|
|
327
|
+
numberSample,
|
|
328
|
+
} from "@lwmacct/260529-promclient";
|
|
351
329
|
|
|
352
|
-
|
|
330
|
+
type PortTraffic = {
|
|
331
|
+
key: string;
|
|
332
|
+
switchId: string;
|
|
333
|
+
ifIndex: string;
|
|
334
|
+
inBps?: number;
|
|
335
|
+
outBps?: number;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const base = {
|
|
339
|
+
key: labels(["switch", "ifIndex"]),
|
|
340
|
+
switchId: label("switch"),
|
|
341
|
+
ifIndex: label("ifIndex"),
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const rows = mergeVectorRecords<PortTraffic>([
|
|
345
|
+
{
|
|
346
|
+
response: inResponse,
|
|
347
|
+
schema: {
|
|
348
|
+
key: labels(["switch", "ifIndex"]),
|
|
349
|
+
field: constant("inBps"),
|
|
350
|
+
base,
|
|
351
|
+
fields: { inBps: numberSample("inBps") },
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
response: outResponse,
|
|
356
|
+
schema: {
|
|
357
|
+
key: labels(["switch", "ifIndex"]),
|
|
358
|
+
field: constant("outBps"),
|
|
359
|
+
base,
|
|
360
|
+
fields: { outBps: numberSample("outBps") },
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
]);
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
重复字段默认采用后出现的值。可以在 schema 或字段规则中配置:
|
|
353
367
|
|
|
354
368
|
```ts
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
369
|
+
const rows = mapVectorToRecords(response, {
|
|
370
|
+
key: label("asset"),
|
|
371
|
+
field: label("field"),
|
|
372
|
+
duplicate: "first",
|
|
373
|
+
fields: {
|
|
374
|
+
owner: labelValue("owner", "value", { duplicate: "error" }),
|
|
375
|
+
},
|
|
376
|
+
});
|
|
358
377
|
```
|
|
359
378
|
|
|
360
379
|
### Instant Response
|
|
@@ -366,7 +385,6 @@ import {
|
|
|
366
385
|
getScalarNumber,
|
|
367
386
|
mapVector,
|
|
368
387
|
mapVectorByLabel,
|
|
369
|
-
mapVectorByLabelAndName,
|
|
370
388
|
} from "@lwmacct/260529-promclient";
|
|
371
389
|
```
|
|
372
390
|
|
|
@@ -378,23 +396,8 @@ import {
|
|
|
378
396
|
| `getScalarValue(response)` | 从 scalar/string response 中取出原始字符串值 |
|
|
379
397
|
| `getScalarNumber(response, defaultValue?)` | 从 scalar response 中解析数字 |
|
|
380
398
|
| `mapVectorByLabel(response, labelName, parser?)` | 按指定 label 聚合 vector 数值 |
|
|
381
|
-
| `mapVectorByLabelAndName(response, labelName, mapper, parser?)` | 按指定 label 聚合,并用 `__name__` 区分字段 |
|
|
382
399
|
| `mapVector(response, mapper)` | 自定义映射 vector items |
|
|
383
400
|
|
|
384
|
-
`mapVectorByLabelAndName` 适合多个 metric name 共同组成一张对象表的场景:
|
|
385
|
-
|
|
386
|
-
```ts
|
|
387
|
-
const stats = mapVectorByLabelAndName(response, "instance", (name, value, prev) => {
|
|
388
|
-
if (name === "node_memory_MemTotal_bytes") {
|
|
389
|
-
return { ...prev, total: value };
|
|
390
|
-
}
|
|
391
|
-
if (name === "node_memory_MemAvailable_bytes") {
|
|
392
|
-
return { ...prev, available: value };
|
|
393
|
-
}
|
|
394
|
-
return prev;
|
|
395
|
-
});
|
|
396
|
-
```
|
|
397
|
-
|
|
398
401
|
### Range Response
|
|
399
402
|
|
|
400
403
|
```ts
|
|
@@ -3,6 +3,5 @@ export declare const getVectorItems: (response: PromSuccessResponse<PromInstantD
|
|
|
3
3
|
export declare const getScalarValue: (response: PromSuccessResponse<PromScalarData | PromStringData>) => string | undefined;
|
|
4
4
|
export declare const getScalarNumber: (response: PromSuccessResponse<PromScalarData>, defaultValue?: number) => number;
|
|
5
5
|
export declare const mapVectorByLabel: (response: PromSuccessResponse<PromVectorData>, labelName: string, parser?: (value: string) => number) => Record<string, number>;
|
|
6
|
-
export declare const mapVectorByLabelAndName: <T extends Record<string, unknown>>(response: PromSuccessResponse<PromInstantData>, labelName: string, mapper: (metricName: string, value: number, previous: T) => T, parser?: (value: string) => number) => Record<string, T>;
|
|
7
6
|
export declare const mapVector: <T>(response: PromSuccessResponse<PromVectorData>, mapper: (item: PromVectorItem) => T | undefined) => T[];
|
|
8
7
|
//# sourceMappingURL=instant.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"instant.d.ts","sourceRoot":"","sources":["../../src/transform/instant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,cAAc,EACf,MAAM,UAAU,CAAC;AAQlB,eAAO,MAAM,cAAc,GACzB,UAAU,mBAAmB,CAAC,eAAe,CAAC,KAC7C,cAAc,EAKhB,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,UAAU,mBAAmB,CAAC,cAAc,GAAG,cAAc,CAAC,KAC7D,MAAM,GAAG,SAKX,CAAC;AAEF,eAAO,MAAM,eAAe,GAC1B,UAAU,mBAAmB,CAAC,cAAc,CAAC,EAC7C,qBAAyB,KACxB,MAKF,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAC3B,UAAU,mBAAmB,CAAC,cAAc,CAAC,EAC7C,WAAW,MAAM,EACjB,SAAQ,CAAC,KAAK,EAAE,MAAM,KAAK,MAAyC,KACnE,MAAM,CAAC,MAAM,EAAE,MAAM,CAYvB,CAAC;AAEF,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"instant.d.ts","sourceRoot":"","sources":["../../src/transform/instant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,cAAc,EACf,MAAM,UAAU,CAAC;AAQlB,eAAO,MAAM,cAAc,GACzB,UAAU,mBAAmB,CAAC,eAAe,CAAC,KAC7C,cAAc,EAKhB,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,UAAU,mBAAmB,CAAC,cAAc,GAAG,cAAc,CAAC,KAC7D,MAAM,GAAG,SAKX,CAAC;AAEF,eAAO,MAAM,eAAe,GAC1B,UAAU,mBAAmB,CAAC,cAAc,CAAC,EAC7C,qBAAyB,KACxB,MAKF,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAC3B,UAAU,mBAAmB,CAAC,cAAc,CAAC,EAC7C,WAAW,MAAM,EACjB,SAAQ,CAAC,KAAK,EAAE,MAAM,KAAK,MAAyC,KACnE,MAAM,CAAC,MAAM,EAAE,MAAM,CAYvB,CAAC;AAEF,eAAO,MAAM,SAAS,GAAI,CAAC,EACzB,UAAU,mBAAmB,CAAC,cAAc,CAAC,EAC7C,QAAQ,CAAC,IAAI,EAAE,cAAc,KAAK,CAAC,GAAG,SAAS,KAC9C,CAAC,EAOI,CAAC"}
|
|
@@ -30,25 +30,6 @@ export const mapVectorByLabel = (response, labelName, parser = (value) => safePa
|
|
|
30
30
|
return result;
|
|
31
31
|
}, {});
|
|
32
32
|
};
|
|
33
|
-
export const mapVectorByLabelAndName = (response, labelName, mapper, parser = (value) => safeParseFloat(value)) => {
|
|
34
|
-
if (!isVectorData(response.data)) {
|
|
35
|
-
return {};
|
|
36
|
-
}
|
|
37
|
-
return response.data.result.reduce((result, item) => {
|
|
38
|
-
const labelValue = item.metric[labelName];
|
|
39
|
-
const metricName = item.metric.__name__;
|
|
40
|
-
if (!labelValue || !metricName) {
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
const value = parser(item.value[1]);
|
|
44
|
-
if (Number.isNaN(value)) {
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
const previous = result[labelValue] ?? {};
|
|
48
|
-
result[labelValue] = mapper(metricName, value, previous);
|
|
49
|
-
return result;
|
|
50
|
-
}, {});
|
|
51
|
-
};
|
|
52
33
|
export const mapVector = (response, mapper) => response.data.result.reduce((result, item) => {
|
|
53
34
|
const mapped = mapper(item);
|
|
54
35
|
if (mapped !== undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"instant.js","sourceRoot":"","sources":["../../src/transform/instant.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,cAAc,GACf,MAAM,QAAQ,CAAC;AAEhB,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,QAA8C,EAC5B,EAAE;IACpB,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACjC,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;AAC9B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,QAA8D,EAC1C,EAAE;IACtB,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACjE,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,QAA6C,EAC7C,YAAY,GAAG,MAAM,CAAC,GAAG,EACjB,EAAE;IACV,MAAM,KAAK,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;IACvC,OAAO,KAAK,KAAK,SAAS;QACxB,CAAC,CAAC,YAAY;QACd,CAAC,CAAC,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AAC1C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,QAA6C,EAC7C,SAAiB,EACjB,SAAoC,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc,CAAC,KAAK,CAAC,EAC5C,EAAE;IAC1B,OAAO,QAAQ,CAAC,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,IAAI,CAAC,MAAM,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,EAAE,EAAE,CAAC,CAAC"}
|
|
@@ -1,42 +1,45 @@
|
|
|
1
|
-
import type { PromInstantData,
|
|
2
|
-
export type
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { PromInstantData, PromVectorItem, PromSuccessResponse } from "../types";
|
|
2
|
+
export type PromRecordValue = string | number | boolean | null;
|
|
3
|
+
export type PromRecordValueResolver<TValue = PromRecordValue> = (item: PromVectorItem) => TValue | undefined;
|
|
4
|
+
export type DuplicateRecordFieldStrategy = "last" | "first" | "array" | "error";
|
|
5
|
+
export type PromRecordFieldRule = {
|
|
6
6
|
field: string;
|
|
7
|
-
value:
|
|
8
|
-
|
|
9
|
-
sampleValue: string;
|
|
10
|
-
timestampMs: number;
|
|
7
|
+
value: PromRecordValueResolver;
|
|
8
|
+
duplicate?: DuplicateRecordFieldStrategy;
|
|
11
9
|
};
|
|
12
|
-
export type
|
|
13
|
-
|
|
14
|
-
as?: string;
|
|
15
|
-
valueLabel?: string;
|
|
16
|
-
valueSource?: PromFieldValueSource;
|
|
17
|
-
sampleParser?: (value: string, item: PromVectorItem) => PromFieldValue | undefined;
|
|
10
|
+
export type PromRecordBaseSchema<TRecord extends Record<string, unknown>> = {
|
|
11
|
+
[K in keyof TRecord]?: PromRecordValueResolver<TRecord[K]>;
|
|
18
12
|
};
|
|
19
|
-
export type
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
labelSeparator?: string;
|
|
27
|
-
sampleParser?: (value: string, item: PromVectorItem) => PromFieldValue | undefined;
|
|
28
|
-
fieldMappings?: Record<string, PromFieldMapping>;
|
|
29
|
-
unknownField?: "keep" | "drop";
|
|
13
|
+
export type PromRecordSchema<TRecord extends Record<string, unknown>> = {
|
|
14
|
+
key: PromRecordValueResolver<string>;
|
|
15
|
+
field: PromRecordValueResolver<string>;
|
|
16
|
+
base?: PromRecordBaseSchema<TRecord>;
|
|
17
|
+
fields: Record<string, PromRecordFieldRule | readonly PromRecordFieldRule[]>;
|
|
18
|
+
duplicate?: DuplicateRecordFieldStrategy;
|
|
19
|
+
unknownField?: "drop" | "error";
|
|
30
20
|
};
|
|
31
|
-
export type
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
export type MergeVectorRecordsInput<TRecord extends Record<string, unknown>> = {
|
|
22
|
+
response: PromSuccessResponse<PromInstantData>;
|
|
23
|
+
schema: PromRecordSchema<TRecord>;
|
|
34
24
|
};
|
|
35
|
-
export
|
|
36
|
-
export
|
|
37
|
-
export
|
|
38
|
-
export declare const
|
|
39
|
-
export declare const
|
|
40
|
-
export declare const
|
|
41
|
-
export declare const
|
|
25
|
+
export declare const mapVectorToRecordMap: <TRecord extends Record<string, unknown>>(response: PromSuccessResponse<PromInstantData>, schema: PromRecordSchema<TRecord>) => Record<string, TRecord>;
|
|
26
|
+
export declare const mapVectorToRecords: <TRecord extends Record<string, unknown>>(response: PromSuccessResponse<PromInstantData>, schema: PromRecordSchema<TRecord>) => TRecord[];
|
|
27
|
+
export declare const mergeVectorRecords: <TRecord extends Record<string, unknown>>(inputs: readonly MergeVectorRecordsInput<TRecord>[]) => TRecord[];
|
|
28
|
+
export declare const label: (name: string) => PromRecordValueResolver<string>;
|
|
29
|
+
export declare const labels: (names: readonly string[], separator?: string) => PromRecordValueResolver<string>;
|
|
30
|
+
export declare const metricName: () => PromRecordValueResolver<string>;
|
|
31
|
+
export declare const constant: <TValue extends PromRecordValue>(value: TValue) => PromRecordValueResolver<TValue>;
|
|
32
|
+
export declare const sample: <TRecord extends Record<string, unknown>>(field: keyof TRecord | string, options?: {
|
|
33
|
+
duplicate?: DuplicateRecordFieldStrategy;
|
|
34
|
+
}) => PromRecordFieldRule;
|
|
35
|
+
export declare const numberSample: <TRecord extends Record<string, unknown>>(field: keyof TRecord | string, options?: {
|
|
36
|
+
duplicate?: DuplicateRecordFieldStrategy;
|
|
37
|
+
defaultValue?: number;
|
|
38
|
+
}) => PromRecordFieldRule;
|
|
39
|
+
export declare const labelValue: <TRecord extends Record<string, unknown>>(field: keyof TRecord | string, labelName: string, options?: {
|
|
40
|
+
duplicate?: DuplicateRecordFieldStrategy;
|
|
41
|
+
}) => PromRecordFieldRule;
|
|
42
|
+
export declare const autoValue: <TRecord extends Record<string, unknown>>(field: keyof TRecord | string, labelName: string, options?: {
|
|
43
|
+
duplicate?: DuplicateRecordFieldStrategy;
|
|
44
|
+
}) => PromRecordFieldRule;
|
|
42
45
|
//# sourceMappingURL=records.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"records.d.ts","sourceRoot":"","sources":["../../src/transform/records.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,
|
|
1
|
+
{"version":3,"file":"records.d.ts","sourceRoot":"","sources":["../../src/transform/records.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,mBAAmB,EACpB,MAAM,UAAU,CAAC;AAGlB,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAE/D,MAAM,MAAM,uBAAuB,CAAC,MAAM,GAAG,eAAe,IAAI,CAC9D,IAAI,EAAE,cAAc,KACjB,MAAM,GAAG,SAAS,CAAC;AAExB,MAAM,MAAM,4BAA4B,GACpC,MAAM,GACN,OAAO,GACP,OAAO,GACP,OAAO,CAAC;AAEZ,MAAM,MAAM,mBAAmB,GAAG;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,uBAAuB,CAAC;IAC/B,SAAS,CAAC,EAAE,4BAA4B,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,oBAAoB,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KACzE,CAAC,IAAI,MAAM,OAAO,CAA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|
@@ -1,103 +1,5 @@
|
|
|
1
|
-
import { isVectorData, safeParseFloat
|
|
2
|
-
const
|
|
3
|
-
const defaultValueLabel = "value";
|
|
4
|
-
const joinLabelValues = (labels, labelNames, separator) => {
|
|
5
|
-
const values = [];
|
|
6
|
-
for (const labelName of labelNames) {
|
|
7
|
-
const value = labels[labelName];
|
|
8
|
-
if (value === undefined) {
|
|
9
|
-
return undefined;
|
|
10
|
-
}
|
|
11
|
-
values.push(value);
|
|
12
|
-
}
|
|
13
|
-
return values.join(separator);
|
|
14
|
-
};
|
|
15
|
-
const resolveFieldValue = (item, options) => {
|
|
16
|
-
const labelValue = item.metric[options.valueLabel];
|
|
17
|
-
if (options.valueSource === "label") {
|
|
18
|
-
return labelValue;
|
|
19
|
-
}
|
|
20
|
-
if (options.valueSource === "auto" && labelValue !== undefined) {
|
|
21
|
-
return labelValue;
|
|
22
|
-
}
|
|
23
|
-
return options.sampleParser(item.value[1], item);
|
|
24
|
-
};
|
|
25
|
-
const resolveFieldMapping = (field, options) => {
|
|
26
|
-
const mapping = options.fieldMappings?.[field];
|
|
27
|
-
if (mapping === undefined) {
|
|
28
|
-
return options.unknownField === "drop" ? undefined : { field };
|
|
29
|
-
}
|
|
30
|
-
if (mapping === false) {
|
|
31
|
-
return undefined;
|
|
32
|
-
}
|
|
33
|
-
if (typeof mapping === "string") {
|
|
34
|
-
return { field: mapping };
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
...mapping,
|
|
38
|
-
field: mapping.as ?? field,
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
export const mapVectorItemToFieldRow = (item, options) => {
|
|
42
|
-
const labelSeparator = options.labelSeparator ?? defaultLabelSeparator;
|
|
43
|
-
const indexLabels = options.indexLabels ?? [];
|
|
44
|
-
const valueOptions = {
|
|
45
|
-
sampleParser: options.sampleParser ??
|
|
46
|
-
((value) => safeParseFloat(value)),
|
|
47
|
-
valueLabel: options.valueLabel ?? defaultValueLabel,
|
|
48
|
-
valueSource: options.valueSource ?? "auto",
|
|
49
|
-
};
|
|
50
|
-
const index = indexLabels.length > 0
|
|
51
|
-
? joinLabelValues(item.metric, indexLabels, labelSeparator)
|
|
52
|
-
: undefined;
|
|
53
|
-
if (indexLabels.length > 0 && index === undefined) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
const key = joinLabelValues(item.metric, options.keyLabels, labelSeparator);
|
|
57
|
-
const rawField = options.field ??
|
|
58
|
-
(options.fieldLabels
|
|
59
|
-
? joinLabelValues(item.metric, options.fieldLabels, labelSeparator)
|
|
60
|
-
: undefined);
|
|
61
|
-
if (key === undefined || rawField === undefined) {
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
const fieldMapping = resolveFieldMapping(rawField, options);
|
|
65
|
-
if (fieldMapping === undefined) {
|
|
66
|
-
return undefined;
|
|
67
|
-
}
|
|
68
|
-
const fieldValueOptions = {
|
|
69
|
-
...valueOptions,
|
|
70
|
-
valueLabel: fieldMapping.valueLabel ?? valueOptions.valueLabel,
|
|
71
|
-
valueSource: fieldMapping.valueSource ?? valueOptions.valueSource,
|
|
72
|
-
sampleParser: fieldMapping.sampleParser ?? valueOptions.sampleParser,
|
|
73
|
-
};
|
|
74
|
-
const value = resolveFieldValue(item, fieldValueOptions);
|
|
75
|
-
if (value === undefined) {
|
|
76
|
-
return undefined;
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
index,
|
|
80
|
-
key,
|
|
81
|
-
field: fieldMapping.field,
|
|
82
|
-
value,
|
|
83
|
-
labels: { ...item.metric },
|
|
84
|
-
sampleValue: item.value[1],
|
|
85
|
-
timestampMs: toMilliseconds(item.value[0]),
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
export const mapVectorToFieldRows = (response, options) => {
|
|
89
|
-
if (!isVectorData(response.data)) {
|
|
90
|
-
return [];
|
|
91
|
-
}
|
|
92
|
-
return response.data.result.reduce((rows, item) => {
|
|
93
|
-
const row = mapVectorItemToFieldRow(item, options);
|
|
94
|
-
if (row !== undefined) {
|
|
95
|
-
rows.push(row);
|
|
96
|
-
}
|
|
97
|
-
return rows;
|
|
98
|
-
}, []);
|
|
99
|
-
};
|
|
100
|
-
const setFieldValue = (record, field, value, duplicate) => {
|
|
1
|
+
import { isVectorData, safeParseFloat } from "./core";
|
|
2
|
+
const setRecordField = (record, field, value, duplicate) => {
|
|
101
3
|
const current = record[field];
|
|
102
4
|
if (current === undefined) {
|
|
103
5
|
record[field] = value;
|
|
@@ -111,35 +13,109 @@ const setFieldValue = (record, field, value, duplicate) => {
|
|
|
111
13
|
return;
|
|
112
14
|
}
|
|
113
15
|
if (duplicate === "array") {
|
|
114
|
-
record[field] = Array.isArray(current)
|
|
115
|
-
? [...current, value]
|
|
116
|
-
: [current, value];
|
|
16
|
+
record[field] = (Array.isArray(current) ? [...current, value] : [current, value]);
|
|
117
17
|
return;
|
|
118
18
|
}
|
|
119
|
-
throw new Error(`Duplicate
|
|
19
|
+
throw new Error(`Duplicate record field "${field}".`);
|
|
120
20
|
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
21
|
+
const applyBase = (record, item, schema) => {
|
|
22
|
+
if (!schema) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
for (const [field, resolver] of Object.entries(schema)) {
|
|
26
|
+
if (record[field] !== undefined || !resolver) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const value = resolver(item);
|
|
30
|
+
if (value !== undefined) {
|
|
31
|
+
record[field] = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
129
34
|
};
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
35
|
+
const getFieldRules = (rawField, schema) => {
|
|
36
|
+
const rules = schema.fields[rawField];
|
|
37
|
+
if (rules === undefined) {
|
|
38
|
+
if (schema.unknownField === "error") {
|
|
39
|
+
throw new Error(`Unknown record field "${rawField}".`);
|
|
40
|
+
}
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
return Array.isArray(rules) ? rules : [rules];
|
|
44
|
+
};
|
|
45
|
+
export const mapVectorToRecordMap = (response, schema) => {
|
|
46
|
+
if (!isVectorData(response.data)) {
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
return response.data.result.reduce((records, item) => {
|
|
50
|
+
const key = schema.key(item);
|
|
51
|
+
const rawField = schema.field(item);
|
|
52
|
+
if (key === undefined || rawField === undefined) {
|
|
53
|
+
return records;
|
|
54
|
+
}
|
|
55
|
+
const rules = getFieldRules(rawField, schema);
|
|
56
|
+
if (rules.length === 0) {
|
|
57
|
+
return records;
|
|
136
58
|
}
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
59
|
+
const record = records[key] ?? {};
|
|
60
|
+
applyBase(record, item, schema.base);
|
|
61
|
+
for (const rule of rules) {
|
|
62
|
+
const value = rule.value(item);
|
|
63
|
+
if (value !== undefined) {
|
|
64
|
+
setRecordField(record, rule.field, value, rule.duplicate ?? schema.duplicate ?? "last");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
records[key] = record;
|
|
68
|
+
return records;
|
|
143
69
|
}, {});
|
|
144
70
|
};
|
|
71
|
+
export const mapVectorToRecords = (response, schema) => Object.values(mapVectorToRecordMap(response, schema));
|
|
72
|
+
export const mergeVectorRecords = (inputs) => {
|
|
73
|
+
const records = {};
|
|
74
|
+
for (const input of inputs) {
|
|
75
|
+
Object.entries(mapVectorToRecordMap(input.response, input.schema)).forEach(([key, record]) => {
|
|
76
|
+
records[key] = { ...(records[key] ?? {}), ...record };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return Object.values(records);
|
|
80
|
+
};
|
|
81
|
+
export const label = (name) => (item) => item.metric[name];
|
|
82
|
+
export const labels = (names, separator = "\u0000") => (item) => {
|
|
83
|
+
const values = [];
|
|
84
|
+
for (const name of names) {
|
|
85
|
+
const value = item.metric[name];
|
|
86
|
+
if (value === undefined) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
values.push(value);
|
|
90
|
+
}
|
|
91
|
+
return values.join(separator);
|
|
92
|
+
};
|
|
93
|
+
export const metricName = () => (item) => item.metric.__name__;
|
|
94
|
+
export const constant = (value) => () => value;
|
|
95
|
+
export const sample = (field, options = {}) => ({
|
|
96
|
+
field: String(field),
|
|
97
|
+
value: (item) => item.value[1],
|
|
98
|
+
duplicate: options.duplicate,
|
|
99
|
+
});
|
|
100
|
+
export const numberSample = (field, options = {}) => ({
|
|
101
|
+
field: String(field),
|
|
102
|
+
value: (item) => {
|
|
103
|
+
const value = safeParseFloat(item.value[1], Number.NaN);
|
|
104
|
+
if (Number.isNaN(value)) {
|
|
105
|
+
return options.defaultValue;
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
},
|
|
109
|
+
duplicate: options.duplicate,
|
|
110
|
+
});
|
|
111
|
+
export const labelValue = (field, labelName, options = {}) => ({
|
|
112
|
+
field: String(field),
|
|
113
|
+
value: (item) => item.metric[labelName],
|
|
114
|
+
duplicate: options.duplicate,
|
|
115
|
+
});
|
|
116
|
+
export const autoValue = (field, labelName, options = {}) => ({
|
|
117
|
+
field: String(field),
|
|
118
|
+
value: (item) => item.metric[labelName] ?? item.value[1],
|
|
119
|
+
duplicate: options.duplicate,
|
|
120
|
+
});
|
|
145
121
|
//# sourceMappingURL=records.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"records.js","sourceRoot":"","sources":["../../src/transform/records.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"records.js","sourceRoot":"","sources":["../../src/transform/records.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAsCtD,MAAM,c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