-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemaphore.cfc
188 lines (164 loc) · 6.21 KB
/
semaphore.cfc
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
component {
variables.flags = {};
public struct function getAllFlags(){
return variables.flags;
}
public void function setAllFlags( required struct flags ){
variables.flags = arguments.flags;
}
public struct function getFlag( required string flagId ){
return variables.flags[ arguments.flagId ];
}
public void function setFlag( required string flagId, required struct flag ){
variables.flags[ arguments.flagId ] = arguments.flag;
}
public boolean function checkForUser( required string flagId, required struct userAttributes ){
var flags = getAllFlags();
if ( !structKeyExists(flags, arguments.flagId) ){
return false;
}
var flag = flags[ flagId ];
return checkFlagForUser( flag, arguments.userAttributes );
}
public struct function getAllFlagsForUser( required struct userAttributes ){
var applicableFlagsForUser = {};
var flagIds = variables.flags.keyArray();
for ( var flagId in flagIds ){
param name="variables.flags['#flagId#'].baseState" default="false";
if ( checkForUser( flagId, arguments.userAttributes ) ) {
applicableFlagsForUser[flagId] = !variables.flags[flagId].baseState;
} else {
applicableFlagsForUser[flagId] = variables.flags[flagId].baseState;
}
}
return applicableFlagsForUser;
}
// ================== PRIVATES ==================
private boolean function checkFlagForUser( required struct flag, required struct userAttributes ){
//todo: caching will speed this up
if ( arguments.flag.active == false ){
return false;
}
for ( var ruleGroup in arguments.flag.rules ){
//all rules in a ruleGroup must match for the group to be true
var groupAllTrue = true;
for ( var rule in ruleGroup ){
if ( evaluateRule( rule, arguments.userAttributes ) == false ){
groupAllTrue = false;
break;
}
}
//if any ruleGroup is all true, then the flag is enabled
if ( groupAllTrue ){
return true;
}
}
return false;
}
private boolean function evaluateRule( required struct rule, required struct userAttributes ){
switch ( arguments.rule.type ){
case 'everybody':
return true;
case 'nobody':
return false;
case '%':
var crc = getUserRuleCRC( arguments.userAttributes, arguments.rule );
return evalRuleOperator( crc, '<=', arguments.rule.percentage/100 );
case 'filter':
if ( !arguments.userAttributes.keyExists(arguments.rule.attribute) ){
return false;
}
var userAttributeVal = arguments.userAttributes[ arguments.rule.attribute ];
return evalRuleOperator( userAttributeVal, arguments.rule.operator, arguments.rule.comparator );
default:
return false;
}
}
private numeric function getUserRuleCRC( required struct userAttributes, required struct rule ){
/*
This is what I'm doing.
Is it safe? I don't see why not.
Is it "right"? Probably not.
Does it work? Seems like it to me!
GOAL:
Given a struct with some data, convert that to a numeric value
between 0-1 in a deterministic manner.
APPROACH:
- We want deterministic JSON of userAttributes but can't count on CFML engines to give it to us.
- To make it deterministic, create an array in sorted-struct-key-order of each k/v pair
- serialize that array to JSON
- prepend the deterministic-json of the rule (so that the same users don't get selected for every % rule)
- MD5 hash the resulting string to create a value with widely distributed numeric range
- then strip out all letters, leaving some numeric value
- then prepend "0." to get it to be somewhere between 0 and 1
- run a left(6) on it to guarantee no more than 4 digits after the decimal place
This appears to generate a sufficiently random, widely distributed range
of numbers between 0-1. Collisions are possible, but that should be true
of any solution, and two users having the same CRC is not actually a
problem for our purposes. We don't need a unique number for all users, we
just need to be able to segment them consistently.
*/
var userAttrsString = structToDeterministicString( arguments.userAttributes );
var ruleString = structToDeterministicString( arguments.rule );
var mashup = ruleString & userAttrsString;
var hashed = hash( mashup );
var digits = hashed.reReplaceNoCase('[a-z]', '', 'ALL');
if ( digits == '' ){
/*
it's theoretically possible for the MD5 to have no digits
so let's handle that eventuality by subbing in zero's.
*/
digits = '0000';
}
var crc = '0.' & digits;
return left(crc, 6);
}
private string function structToDeterministicString( required struct input ){
var keys = structKeyArray( arguments.input );
var data = [];
arraySort( keys, 'text' );
for ( var propName in keys ){
data.append({ '#propName#': arguments.input[propName] });
}
return serializeJson( data );
}
private boolean function evalRuleOperator(required any userAttributeValue, required string operator, required any ruleValue){
switch (arguments.operator){
case '=':
case '==':
return arguments.userAttributeValue == arguments.ruleValue;
case '!=':
return arguments.userAttributeValue != arguments.ruleValue;
case '<':
return arguments.userAttributeValue < arguments.ruleValue;
case '<=':
return arguments.userAttributeValue <= arguments.ruleValue;
case '>':
return arguments.userAttributeValue > arguments.ruleValue;
case '>=':
return arguments.userAttributeValue >= arguments.ruleValue;
case 'in':
var ruleValueArray = ruleValue;
try {
if (!isArray(ruleValueArray)){
ruleValueArray = listToArray(arguments.ruleValue);
}
} catch (any e){
throw(type: "semaphore.invalidRuleValueInput", message: "Expected rule value to be an array or a string (list)");
}
return arrayFindNoCase(ruleValueArray, arguments.userAttributeValue) != 0;
case 'has':
var userAttributeValueArray = arguments.userAttributeValue;
try {
if (!isArray(userAttributeValueArray)){
userAttributeValueArray = listToArray(userAttributeValueArray);
}
} catch (any e){
throw(type: "semaphore.invalidUserAttributeValue", message: "Expected user attribute value to be an array or a string (list)");
}
return arrayFindNoCase(userAttributeValueArray, arguments.ruleValue) != 0;
default:
return false;
}
}
}