-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmodels.ts
More file actions
295 lines (260 loc) · 10.2 KB
/
models.ts
File metadata and controls
295 lines (260 loc) · 10.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import * as semver from 'semver';
import {
FeatureModel,
FeatureStateModel,
MultivariateFeatureOptionModel,
MultivariateFeatureStateValueModel
} from '../features/models.js';
import { getCastingFunction as getCastingFunction } from '../utils/index.js';
import {
ALL_RULE,
ANY_RULE,
NONE_RULE,
NOT_CONTAINS,
REGEX,
MODULO,
IN,
CONDITION_OPERATORS
} from './constants.js';
import { isSemver } from './util.js';
import {
EvaluationContext,
Overrides
} from '../evaluation/evaluationContext/evaluationContext.types.js';
import { CONSTANTS } from '../features/constants.js';
import { EvaluationResultSegments, SegmentSource } from '../evaluation/models.js';
export const all = (iterable: Array<any>) => iterable.filter(e => !!e).length === iterable.length;
export const any = (iterable: Array<any>) => iterable.filter(e => !!e).length > 0;
export const matchingFunctions = {
[CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) => thisValue == otherValue,
[CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) => otherValue > thisValue,
[CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
otherValue >= thisValue,
[CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) => thisValue > otherValue,
[CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
thisValue >= otherValue,
[CONDITION_OPERATORS.NOT_EQUAL]: (thisValue: any, otherValue: any) => thisValue != otherValue,
[CONDITION_OPERATORS.CONTAINS]: (thisValue: any, otherValue: any) => {
try {
return !!otherValue && otherValue.includes(thisValue);
} catch {
return false;
}
}
};
// Semver library throws an error if the version is invalid, in this case, we want to catch and return false
const safeSemverCompare = (
semverMatchingFunction: (conditionValue: any, traitValue: any) => boolean
) => {
return (conditionValue: any, traitValue: any) => {
try {
return semverMatchingFunction(conditionValue, traitValue);
} catch {
return false;
}
};
};
export const semverMatchingFunction = {
...matchingFunctions,
[CONDITION_OPERATORS.EQUAL]: safeSemverCompare((conditionValue, traitValue) =>
semver.eq(traitValue, conditionValue)
),
[CONDITION_OPERATORS.GREATER_THAN]: safeSemverCompare((conditionValue, traitValue) =>
semver.gt(traitValue, conditionValue)
),
[CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: safeSemverCompare((conditionValue, traitValue) =>
semver.gte(traitValue, conditionValue)
),
[CONDITION_OPERATORS.LESS_THAN]: safeSemverCompare((conditionValue, traitValue) =>
semver.lt(traitValue, conditionValue)
),
[CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: safeSemverCompare((conditionValue, traitValue) =>
semver.lte(traitValue, conditionValue)
)
};
export const getMatchingFunctions = (semver: boolean) =>
semver ? semverMatchingFunction : matchingFunctions;
export class SegmentConditionModel {
EXCEPTION_OPERATOR_METHODS: { [key: string]: string } = {
[NOT_CONTAINS]: 'evaluateNotContains',
[REGEX]: 'evaluateRegex',
[MODULO]: 'evaluateModulo',
[IN]: 'evaluateIn'
};
operator: string;
value: string | null | undefined | string[];
property: string | null | undefined;
constructor(
operator: string,
value?: string | null | undefined | string[],
property?: string | null | undefined
) {
this.operator = operator;
this.value = value;
this.property = property;
}
matchesTraitValue(traitValue: any) {
const evaluators: { [key: string]: CallableFunction } = {
evaluateNotContains: (traitValue: any) => {
return (
typeof traitValue == 'string' &&
!!this.value &&
!traitValue.includes(this.value?.toString())
);
},
evaluateRegex: (traitValue: any) => {
try {
if (!this.value) {
return false;
}
const regex = new RegExp(this.value?.toString());
return !!traitValue?.toString().match(regex);
} catch {
return false;
}
},
evaluateModulo: (traitValue: any) => {
const parsedTraitValue = parseFloat(traitValue);
if (isNaN(parsedTraitValue) || !this.value) {
return false;
}
const parts = this.value.toString().split('|');
if (parts.length !== 2) {
return false;
}
const divisor = parseFloat(parts[0]);
const remainder = parseFloat(parts[1]);
if (isNaN(divisor) || isNaN(remainder) || divisor === 0) {
return false;
}
return parsedTraitValue % divisor === remainder;
},
evaluateIn: (traitValue: string[] | string) => {
if (!traitValue || typeof traitValue === 'boolean') {
return false;
}
if (Array.isArray(this.value)) {
return this.value.includes(traitValue.toString());
}
if (typeof this.value === 'string') {
try {
const parsed = JSON.parse(this.value);
if (Array.isArray(parsed)) {
return parsed.includes(traitValue.toString());
}
} catch {}
}
return this.value?.split(',').includes(traitValue.toString());
}
};
// TODO: move this logic to the evaluator module
if (this.EXCEPTION_OPERATOR_METHODS[this.operator]) {
const evaluatorFunction = evaluators[this.EXCEPTION_OPERATOR_METHODS[this.operator]];
return evaluatorFunction(traitValue);
}
const defaultFunction = (x: any, y: any) => false;
const matchingFunctionSet = getMatchingFunctions(isSemver(this.value));
const matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;
const traitType = isSemver(this.value) ? 'semver' : typeof traitValue;
const castToTypeOfTraitValue = getCastingFunction(traitType);
return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
}
}
export class SegmentRuleModel {
type: string;
rules: SegmentRuleModel[] = [];
conditions: SegmentConditionModel[] = [];
constructor(type: string) {
this.type = type;
}
static none(iterable: Array<any>) {
return iterable.filter(e => !!e).length === 0;
}
matchingFunction(): CallableFunction {
return {
[ANY_RULE]: any,
[ALL_RULE]: all,
[NONE_RULE]: SegmentRuleModel.none
}[this.type] as CallableFunction;
}
}
export class SegmentModel {
id: number;
name: string;
rules: SegmentRuleModel[] = [];
featureStates: FeatureStateModel[] = [];
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
static fromSegmentResult(
segmentResults: EvaluationResultSegments,
evaluationContext: EvaluationContext
): SegmentModel[] {
const segmentModels: SegmentModel[] = [];
if (!evaluationContext.segments) {
return [];
}
for (const segmentResult of segmentResults) {
if (segmentResult.metadata?.source === SegmentSource.IDENTITY_OVERRIDE) {
continue;
}
const segmentMetadataId = segmentResult.metadata?.id;
if (!segmentMetadataId) {
continue;
}
const segmentContext = evaluationContext.segments[segmentMetadataId.toString()];
if (segmentContext) {
const segment = new SegmentModel(segmentMetadataId, segmentContext.name);
segment.rules = segmentContext.rules.map(rule => new SegmentRuleModel(rule.type));
segment.featureStates = SegmentModel.createFeatureStatesFromOverrides(
segmentContext.overrides || []
);
segmentModels.push(segment);
}
}
return segmentModels;
}
private static createFeatureStatesFromOverrides(overrides: Overrides): FeatureStateModel[] {
if (!overrides) return [];
return overrides
.filter(override => {
const overrideMetadataId = override?.metadata?.id;
return typeof overrideMetadataId === 'number';
})
.map(override => {
const overrideMetadataId = override.metadata!.id as number;
const feature = new FeatureModel(
overrideMetadataId,
override.name,
override.variants?.length && override.variants.length > 0
? CONSTANTS.MULTIVARIATE
: CONSTANTS.STANDARD
);
const featureState = new FeatureStateModel(
feature,
override.enabled,
override.priority || 0
);
if (override.value !== undefined) {
featureState.setValue(override.value);
}
if (override.variants && override.variants.length > 0) {
featureState.multivariateFeatureStateValues = this.createMultivariateValues(
override.variants
);
}
return featureState;
});
}
private static createMultivariateValues(variants: any[]): MultivariateFeatureStateValueModel[] {
return variants.map(
variant =>
new MultivariateFeatureStateValueModel(
new MultivariateFeatureOptionModel(variant.value, variant.id as number),
variant.weight as number,
variant.id as number
)
);
}
}