-
Notifications
You must be signed in to change notification settings - Fork 358
/
condition-evaluator-internal.ts
178 lines (152 loc) · 5.45 KB
/
condition-evaluator-internal.ts
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
/*!
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import {
AndCondition,
OneOfCondition,
EvaluationContext,
NamedCondition,
OrCondition,
PercentCondition,
PercentConditionOperator
} from './remote-config-api';
import * as farmhash from 'farmhash';
import long = require('long');
/**
* Encapsulates condition evaluation logic to simplify organization and
* facilitate testing.
*
* @internal
*/
export class ConditionEvaluator {
private static MAX_CONDITION_RECURSION_DEPTH = 10;
public evaluateConditions(
namedConditions: NamedCondition[],
context: EvaluationContext): Map<string, boolean> {
// The order of the conditions is significant.
// A JS Map preserves the order of insertion ("Iteration happens in insertion order"
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#description).
const evaluatedConditions = new Map();
for (const namedCondition of namedConditions) {
evaluatedConditions.set(
namedCondition.name,
this.evaluateCondition(namedCondition.condition, context));
}
return evaluatedConditions;
}
private evaluateCondition(
condition: OneOfCondition,
context: EvaluationContext,
nestingLevel = 0): boolean {
if (nestingLevel >= ConditionEvaluator.MAX_CONDITION_RECURSION_DEPTH) {
// TODO: add logging once we have a wrapped logger.
return false;
}
if (condition.orCondition) {
return this.evaluateOrCondition(condition.orCondition, context, nestingLevel + 1)
}
if (condition.andCondition) {
return this.evaluateAndCondition(condition.andCondition, context, nestingLevel + 1)
}
if (condition.true) {
return true;
}
if (condition.false) {
return false;
}
if (condition.percent) {
return this.evaluatePercentCondition(condition.percent, context);
}
// TODO: add logging once we have a wrapped logger.
return false;
}
private evaluateOrCondition(
orCondition: OrCondition,
context: EvaluationContext,
nestingLevel: number): boolean {
const subConditions = orCondition.conditions || [];
for (const subCondition of subConditions) {
// Recursive call.
const result = this.evaluateCondition(
subCondition, context, nestingLevel + 1);
// Short-circuit the evaluation result for true.
if (result) {
return result;
}
}
return false;
}
private evaluateAndCondition(
andCondition: AndCondition,
context: EvaluationContext,
nestingLevel: number): boolean {
const subConditions = andCondition.conditions || [];
for (const subCondition of subConditions) {
// Recursive call.
const result = this.evaluateCondition(
subCondition, context, nestingLevel + 1);
// Short-circuit the evaluation result for false.
if (!result) {
return result;
}
}
return true;
}
private evaluatePercentCondition(
percentCondition: PercentCondition,
context: EvaluationContext
): boolean {
if (!context.randomizationId) {
// TODO: add logging once we have a wrapped logger.
return false;
}
// This is the entry point for processing percent condition data from the response.
// We're not using a proto library, so we can't assume undefined fields have
// default values.
const { seed, percentOperator, microPercent, microPercentRange } = percentCondition;
if (!percentOperator) {
// TODO: add logging once we have a wrapped logger.
return false;
}
const normalizedMicroPercent = microPercent || 0;
const normalizedMicroPercentUpperBound = microPercentRange?.microPercentUpperBound || 0;
const normalizedMicroPercentLowerBound = microPercentRange?.microPercentLowerBound || 0;
const seedPrefix = seed && seed.length > 0 ? `${seed}.` : '';
const stringToHash = `${seedPrefix}${context.randomizationId}`;
// Using a 64-bit long for consistency with the Remote Config fetch endpoint.
let hash64 = long.fromString(farmhash.fingerprint64(stringToHash));
// Negate the hash if its value is less than 0. We handle this manually because the
// Long library doesn't provided an absolute value method.
if (hash64.lt(0)) {
hash64 = hash64.negate();
}
const instanceMicroPercentile = hash64.mod(100 * 1_000_000);
switch (percentOperator) {
case PercentConditionOperator.LESS_OR_EQUAL:
return instanceMicroPercentile.lte(normalizedMicroPercent);
case PercentConditionOperator.GREATER_THAN:
return instanceMicroPercentile.gt(normalizedMicroPercent);
case PercentConditionOperator.BETWEEN:
return instanceMicroPercentile.gt(normalizedMicroPercentLowerBound)
&& instanceMicroPercentile.lte(normalizedMicroPercentUpperBound);
case PercentConditionOperator.UNKNOWN:
default:
break;
}
// TODO: add logging once we have a wrapped logger.
return false;
}
}