-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
212 lines (185 loc) · 7.06 KB
/
Copy pathindex.ts
File metadata and controls
212 lines (185 loc) · 7.06 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
import { engineParser } from './Engine';
import { thenable } from '../utils/promise/thenable';
import { EXCEPTION, NO_CONDITION_MATCH, DEFINITION_NOT_FOUND } from '../utils/labels';
import { CONTROL } from '../utils/constants';
import { IDefinition, MaybeThenable } from '../dtos/types';
import { IStorageAsync, IStorageSync } from '../storages/types';
import { IEvaluationResult } from './types';
import SplitIO from '../../types/splitio';
import { ILogger } from '../logger/types';
import { returnSetsUnion, setToArray } from '../utils/lang/sets';
import { WARN_FLAGSET_WITHOUT_FLAGS } from '../logger/constants';
const EVALUATION_EXCEPTION = {
treatment: CONTROL,
label: EXCEPTION,
config: null
};
const EVALUATION_DEFINITION_NOT_FOUND = {
treatment: CONTROL,
label: DEFINITION_NOT_FOUND,
config: null
};
function treatmentsException(definitionNames: string[]) {
const evaluations: Record<string, IEvaluationResult> = {};
definitionNames.forEach(definitionName => {
evaluations[definitionName] = EVALUATION_EXCEPTION;
});
return evaluations;
}
export function evaluateFeature(
log: ILogger,
key: SplitIO.SplitKey,
definitionName: string,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions
): MaybeThenable<IEvaluationResult> {
let definition;
try {
definition = storage.definitions.get(definitionName);
} catch (e) {
// Exception on sync storage. Not possible ATM with InMemory and InLocal storages.
return EVALUATION_EXCEPTION;
}
return thenable(definition) ?
definition.then((definition) => getEvaluation(log, key, definition, attributes, storage, options))
.catch(
// Exception on async storage. For example, when the storage is redis or
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
() => EVALUATION_EXCEPTION
) :
getEvaluation(log, key, definition, attributes, storage, options);
}
export function evaluateFeatures(
log: ILogger,
key: SplitIO.SplitKey,
definitionNames: string[],
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
let definitions;
try {
definitions = storage.definitions.getMany(definitionNames);
} catch (e) {
// Exception on sync storage. Not possible ATM with InMemory and InLocal storages.
return treatmentsException(definitionNames);
}
return thenable(definitions) ?
definitions.then(definitions => getEvaluations(log, key, definitionNames, definitions, attributes, storage, options))
.catch(
// Exception on async storage. For example, when the storage is redis or
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
() => treatmentsException(definitionNames)
) :
getEvaluations(log, key, definitionNames, definitions, attributes, storage, options);
}
export function evaluateFeaturesByFlagSets(
log: ILogger,
key: SplitIO.SplitKey,
flagSets: string[],
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
method: string,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
let storedFlagNames: MaybeThenable<Set<string>[]>;
function evaluate(featureFlagsByFlagSets: Set<string>[]) {
let featureFlags = new Set<string>();
for (let i = 0; i < flagSets.length; i++) {
const featureFlagByFlagSet = featureFlagsByFlagSets[i];
if (featureFlagByFlagSet.size) {
featureFlags = returnSetsUnion(featureFlags, featureFlagByFlagSet);
} else {
log.warn(WARN_FLAGSET_WITHOUT_FLAGS, [method, flagSets[i]]);
}
}
return featureFlags.size ?
evaluateFeatures(log, key, setToArray(featureFlags), attributes, storage, options) :
{};
}
// get features by flag sets
try {
storedFlagNames = storage.definitions.getNamesBySets(flagSets);
} catch (e) {
// return empty evaluations
return {};
}
// evaluate related features
return thenable(storedFlagNames) ?
storedFlagNames.then((storedFlagNames) => evaluate(storedFlagNames))
.catch(() => ({})) :
evaluate(storedFlagNames);
}
function setEvaluationDataFromDefinition(evaluation: IEvaluationResult, definition: IDefinition, options?: SplitIO.EvaluationOptions): IEvaluationResult {
evaluation.changeNumber = definition.changeNumber;
evaluation.config = definition.configurations && definition.configurations[evaluation.treatment] || null;
evaluation.type = definition.type;
evaluation.subtype = definition.subtype;
// @ts-expect-error impressionsDisabled is not exposed in the public typings yet.
evaluation.impressionsDisabled = options?.impressionsDisabled || definition.impressionsDisabled;
return evaluation;
}
function getEvaluation(
log: ILogger,
key: SplitIO.SplitKey,
definition: IDefinition | null,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<IEvaluationResult> {
if (definition) {
const split = engineParser(log, definition, storage);
const evaluation = split.getTreatment(key, attributes, evaluateFeature);
return thenable(evaluation) ?
evaluation.then(result => setEvaluationDataFromDefinition(result, definition, options)) :
setEvaluationDataFromDefinition(evaluation, definition, options);
}
return EVALUATION_DEFINITION_NOT_FOUND;
}
function getEvaluations(
log: ILogger,
key: SplitIO.SplitKey,
definitionNames: string[],
definitions: Record<string, IDefinition | null>,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
const result: Record<string, IEvaluationResult> = {};
const thenables: Promise<void>[] = [];
definitionNames.forEach(definitionName => {
const evaluation = getEvaluation(log, key, definitions[definitionName], attributes, storage, options);
thenable(evaluation) ?
thenables.push(evaluation.then(res => {
result[definitionName] = res;
})) :
result[definitionName] = evaluation;
});
return thenables.length > 0 ? Promise.all(thenables).then(() => result) : result;
}
export function evaluateDefaultTreatment(
definitionName: string,
storage: IStorageSync | IStorageAsync,
): MaybeThenable<IEvaluationResult> {
let definition;
try {
definition = storage.definitions.get(definitionName);
} catch (e) {
return EVALUATION_EXCEPTION;
}
return thenable(definition) ?
definition.then(getDefaultTreatment).catch(() => EVALUATION_EXCEPTION) :
getDefaultTreatment(definition);
}
function getDefaultTreatment(
definition: IDefinition | null,
): MaybeThenable<IEvaluationResult> {
if (definition) {
return setEvaluationDataFromDefinition({
treatment: definition.defaultTreatment,
label: NO_CONDITION_MATCH // "default rule"
}, definition);
}
return EVALUATION_DEFINITION_NOT_FOUND;
}