-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.ts
More file actions
81 lines (65 loc) · 2.98 KB
/
Copy pathutils.ts
File metadata and controls
81 lines (65 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Shared utils for Redis and Pluggable storage
import { IMetadata } from '../dtos/types';
import { Method, StoredImpressionWithMetadata } from '../sync/submitters/types';
import { ISettings } from '../types';
import SplitIO from '../../types/splitio';
import { UNKNOWN } from '../utils/constants';
import { MAX_LATENCY_BUCKET_COUNT } from './inMemory/TelemetryCacheInMemory';
import { METHOD_NAMES } from './KeyBuilderSS';
export function metadataBuilder(settings: Pick<ISettings, 'version' | 'runtime'>): IMetadata {
return {
s: settings.version,
i: settings.runtime.ip || UNKNOWN,
n: settings.runtime.hostname || UNKNOWN,
};
}
// Converts impressions to be stored in Redis or pluggable storage.
export function impressionsToJSON(impressions: SplitIO.ImpressionDTO[], metadata: IMetadata): string[] {
return impressions.map(impression => {
const impressionWithMetadata: StoredImpressionWithMetadata = {
m: metadata,
i: {
k: impression.keyName,
b: impression.bucketingKey,
f: impression.feature,
t: impression.treatment,
r: impression.label,
c: impression.changeNumber,
m: impression.time,
pt: impression.pt,
properties: impression.properties
// @TODO set entityType
}
};
return JSON.stringify(impressionWithMetadata);
});
}
// Utilities used by TelemetryCacheInRedis and TelemetryCachePluggable
const REVERSE_METHOD_NAMES = Object.keys(METHOD_NAMES).reduce((acc, key) => {
acc[METHOD_NAMES[key as Method]] = key as Method;
return acc;
}, {} as Record<string, Method>);
export function parseMetadata(field: string): [metadata: string] | string {
const parts = field.split('/');
if (parts.length !== 3) return `invalid subsection count. Expected 3, got: ${parts.length}`;
const [s /* metadata.s */, n /* metadata.n */, i /* metadata.i */] = parts;
return [JSON.stringify({ s, n, i })];
}
export function parseExceptionField(field: string): [metadata: string, method: Method] | string {
const parts = field.split('/');
if (parts.length !== 4) return `invalid subsection count. Expected 4, got: ${parts.length}`;
const [s /* metadata.s */, n /* metadata.n */, i /* metadata.i */, m] = parts;
const method = REVERSE_METHOD_NAMES[m];
if (!method) return `unknown method '${m}'`;
return [JSON.stringify({ s, n, i }), method];
}
export function parseLatencyField(field: string): [metadata: string, method: Method, bucket: number] | string {
const parts = field.split('/');
if (parts.length !== 5) return `invalid subsection count. Expected 5, got: ${parts.length}`;
const [s /* metadata.s */, n /* metadata.n */, i /* metadata.i */, m, b] = parts;
const method = REVERSE_METHOD_NAMES[m];
if (!method) return `unknown method '${m}'`;
const bucket = parseInt(b);
if (isNaN(bucket) || bucket >= MAX_LATENCY_BUCKET_COUNT) return `invalid bucket. Expected a number between 0 and ${MAX_LATENCY_BUCKET_COUNT - 1}, got: ${b}`;
return [JSON.stringify({ s, n, i }), method, bucket];
}