Repository navigation
Expand file tree
/
Copy pathDataModel.java
More file actions
551 lines (439 loc) · 16.3 KB
/
Copy pathDataModel.java
File metadata and controls
551 lines (439 loc) · 16.3 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package co.featbit.server;
import co.featbit.commons.json.JsonHelper;
import co.featbit.server.exterior.DataStorageTypes;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public abstract class DataModel {
private DataModel() {
}
/**
* the object is an implementation of{@link DataStorageTypes.Item}, to represent the archived data
*/
public final static class ArchivedItem implements DataStorageTypes.Item {
private final String id;
private final Long timestamp;
private final Boolean isArchived = Boolean.TRUE;
public ArchivedItem(String id, Long timestamp) {
this.id = id;
this.timestamp = timestamp;
}
@Override
public String getId() {
return id;
}
@Override
public boolean isArchived() {
return isArchived;
}
@Override
public Long getTimestamp() {
return timestamp;
}
@Override
public Integer getType() {
return FB_ARCHIVED_ITEM;
}
@Override
public int compareTo(@NotNull DataStorageTypes.Item o) {
return timestamp.compareTo(o.getTimestamp());
}
}
static class StreamingMessage {
static final String DATA_SYNC = "data-sync";
static final String PING = "ping";
protected final String messageType;
StreamingMessage(String messageType) {
this.messageType = messageType;
}
public String getMessageType() {
return messageType;
}
}
static class DataSyncMessage extends StreamingMessage {
final InternalData data;
DataSyncMessage(Long timestamp) {
super(timestamp == null ? PING : DATA_SYNC);
this.data = timestamp == null ? null : new InternalData(timestamp);
}
static class InternalData {
Long timestamp;
InternalData(Long timestamp) {
this.timestamp = timestamp;
}
}
}
static class All extends StreamingMessage {
private final Data data;
All(String messageType, Data data) {
super(messageType);
this.data = data;
}
public Data data() {
return data;
}
boolean isProcessData() {
return DATA_SYNC.equalsIgnoreCase(messageType) && data != null && ("full".equalsIgnoreCase(data.eventType) || "patch".equalsIgnoreCase(data.eventType));
}
}
/**
* versioned data of feature flags and related data from feature flag center
*/
@JsonAdapter(JsonHelper.AfterJsonParseDeserializableTypeAdapterFactory.class)
static class Data implements JsonHelper.AfterJsonParseDeserializable {
@VisibleForTesting
/*private*/ String eventType;
private final List<FeatureFlag> featureFlags;
private final List<Segment> segments;
private Long timestamp;
Data(String eventType, List<FeatureFlag> featureFlags, List<Segment> segments) {
this.eventType = eventType;
this.featureFlags = featureFlags;
this.segments = segments;
}
@Override
public void afterDeserialization() {
long v1 = (featureFlags != null) ? featureFlags.stream().map(flag -> flag.timestamp).max(Long::compare).orElse(0L) : 0L;
long v2 = (segments != null) ? segments.stream().map(segment -> segment.timestamp).max(Long::compare).orElse(0L) : 0L;
timestamp = Math.max(v1, v2);
}
public List<FeatureFlag> getFeatureFlags() {
return featureFlags == null ? Collections.emptyList() : featureFlags;
}
public List<Segment> getSegments() {
return segments == null ? Collections.emptyList() : segments;
}
public String getEventType() {
return eventType;
}
public Long getTimestamp() {
return timestamp;
}
Map<DataStorageTypes.Category, Map<String, DataStorageTypes.Item>> toStorageType() {
ImmutableMap.Builder<String, DataStorageTypes.Item> flags = ImmutableMap.builder();
for (FeatureFlag flag : getFeatureFlags()) {
DataStorageTypes.Item item = flag.isArchived ? flag.toArchivedItem() : flag;
flags.put(item.getId(), item);
}
ImmutableMap.Builder<String, DataStorageTypes.Item> segments = ImmutableMap.builder();
for (Segment segment : getSegments()) {
DataStorageTypes.Item item = segment.isArchived() ? segment.toArchivedItem() : segment;
segments.put(item.getId(), item);
}
return ImmutableMap.of(DataStorageTypes.FEATURES, flags.build(), DataStorageTypes.SEGMENTS, segments.build());
}
}
@JsonAdapter(JsonHelper.AfterJsonParseDeserializableTypeAdapterFactory.class)
static class Segment implements DataStorageTypes.Item, JsonHelper.AfterJsonParseDeserializable {
private final String id;
private final Boolean isArchived;
final Date updatedAt;
private Long timestamp;
private final List<String> included;
private final List<String> excluded;
private final List<TargetRule> rules;
Segment(String id, Boolean isArchived, Date updatedAt, List<String> included, List<String> excluded, List<TargetRule> rules) {
this.id = id;
this.isArchived = isArchived;
this.updatedAt = updatedAt;
this.included = included;
this.excluded = excluded;
this.rules = rules;
}
@Override
public String getId() {
return id;
}
@Override
public boolean isArchived() {
return isArchived != null && isArchived;
}
@Override
public Long getTimestamp() {
return timestamp;
}
@Override
public Integer getType() {
return FB_SEGMENT;
}
public List<String> getIncluded() {
return included == null ? Collections.emptyList() : included;
}
public List<String> getExcluded() {
return excluded == null ? Collections.emptyList() : excluded;
}
public List<TargetRule> getRules() {
return rules == null ? Collections.emptyList() : rules;
}
public Boolean isMatchUser(String userKeyId) {
if (getExcluded().contains(userKeyId)) {
return Boolean.FALSE;
}
if (getIncluded().contains(userKeyId)) {
return Boolean.TRUE;
}
// if no included or excluded, then it's to match rules
return null;
}
public DataStorageTypes.Item toArchivedItem() {
return new ArchivedItem(this.id, this.timestamp);
}
@Override
public void afterDeserialization() {
this.timestamp = updatedAt.getTime();
}
@Override
public int compareTo(@NotNull DataStorageTypes.Item o) {
return timestamp.compareTo(o.getTimestamp());
}
}
@JsonAdapter(JsonHelper.AfterJsonParseDeserializableTypeAdapterFactory.class)
static class FeatureFlag implements DataStorageTypes.Item, JsonHelper.AfterJsonParseDeserializable {
final String id;
final Date updatedAt;
private Long timestamp;
private final boolean isArchived;
private final boolean exptIncludeAllTargets;
private final boolean isEnabled;
private final String name;
private final String key;
private final String variationType;
private final List<Variation> variations;
public final List<TargetUser> targetUsers;
private final List<TargetRule> rules;
private final Fallthrough fallthrough;
private final String disabledVariationId;
private transient Map<String, Variation> variationMap;
FeatureFlag(String id, Date updatedAt, boolean isArchived, boolean exptIncludeAllTargets, boolean isEnabled, String name, String key, String variationType, List<Variation> variations, List<TargetUser> targetUsers, List<TargetRule> rules, Fallthrough fallthrough, String disabledVariationId) {
this.id = id;
this.updatedAt = updatedAt;
this.isArchived = isArchived;
this.exptIncludeAllTargets = exptIncludeAllTargets;
this.isEnabled = isEnabled;
this.name = name;
this.key = key;
this.variationType = variationType;
this.variations = variations;
this.targetUsers = targetUsers;
this.rules = rules;
this.fallthrough = fallthrough;
this.disabledVariationId = disabledVariationId;
}
public DataStorageTypes.Item toArchivedItem() {
return new ArchivedItem(this.key, this.timestamp);
}
@Override
public String getId() {
return key;
}
@Override
public boolean isArchived() {
return isArchived;
}
@Override
public Long getTimestamp() {
return timestamp;
}
@Override
public Integer getType() {
return FB_FEATURE_FLAG;
}
public boolean exptIncludeAllTargets() {
return exptIncludeAllTargets;
}
public boolean isEnabled() {
return isEnabled;
}
public List<Variation> getVariations() {
return variations == null ? Collections.emptyList() : variations;
}
public Variation getVariation(String id) {
return variationMap.get(id);
}
public List<TargetUser> getTargetUsers() {
return targetUsers == null ? Collections.emptyList() : targetUsers;
}
public List<TargetRule> getRules() {
return rules == null ? Collections.emptyList() : rules;
}
public Fallthrough getFallthrough() {
return fallthrough;
}
public String getDisabledVariationId() {
return disabledVariationId;
}
public String getName() {
return name;
}
public String getKey() {
return key;
}
public String getVariationType() {
return variationType;
}
Boolean containsSegment(String segmentId) {
return getRules().stream()
.flatMap(rule -> rule.getConditions().stream())
.filter(cond -> StringUtils.isBlank(cond.getOp()))
.flatMap(cond -> {
List<String> segments = JsonHelper
.deserialize(cond.getValue(), new TypeToken<List<String>>() {
}.getType());
return segments.stream();
}).collect(Collectors.toList()).contains(segmentId);
}
@Override
public void afterDeserialization() {
this.timestamp = updatedAt.getTime();
if (!isArchived) {
ImmutableMap.Builder<String, Variation> builder = ImmutableMap.builder();
for (Variation variation : getVariations()) {
builder.put(variation.id, variation);
}
this.variationMap = builder.build();
}
}
@Override
public int compareTo(@NotNull DataStorageTypes.Item o) {
return timestamp.compareTo(o.getTimestamp());
}
}
static final class Variation {
private final String id;
private final String value;
Variation(String id, String value) {
this.id = id;
this.value = value;
}
public String getId() {
return id;
}
public String getValue() {
return value;
}
}
static final class TargetUser {
private final List<String> keyIds;
private final String variationId;
public TargetUser(List<String> keyIds, String variationId) {
this.keyIds = keyIds;
this.variationId = variationId;
}
public List<String> getKeyIds() {
return keyIds == null ? Collections.emptyList() : keyIds;
}
public String getVariationId() {
return variationId;
}
public boolean isTargeted(String user) {
return getKeyIds().stream().anyMatch(key -> key.equals(user));
}
}
static final class TargetRule {
private final boolean includedInExpt;
private final String dispatchKey;
private final List<Condition> conditions;
private final List<RolloutVariation> variations;
TargetRule(boolean includedInExpt, String dispatchKey, List<Condition> conditions, List<RolloutVariation> variations) {
this.includedInExpt = includedInExpt;
this.dispatchKey = dispatchKey;
this.conditions = conditions;
this.variations = variations;
}
public boolean includedInExpt() {
return includedInExpt;
}
public List<Condition> getConditions() {
return conditions == null ? Collections.emptyList() : conditions;
}
public List<RolloutVariation> getVariations() {
return variations == null ? Collections.emptyList() : variations;
}
public String getDispatchKey() {
return dispatchKey;
}
}
static final class Condition {
private final String property;
private final String op;
private final String value;
Condition(String property, String op, String value) {
this.property = property;
this.op = op;
this.value = value;
}
public String getProperty() {
return property;
}
public String getOp() {
return op;
}
public String getValue() {
return value;
}
}
static final class RolloutVariation {
private final String id;
private final double[] rollout;
private final double exptRollout;
RolloutVariation(String id, double[] rollout, double exptRollout) {
this.id = id;
this.rollout = rollout;
this.exptRollout = exptRollout;
}
public String getId() {
return id;
}
public double[] getRollout() {
return rollout;
}
public double getExptRollout() {
return exptRollout;
}
public double splittingPercentage() {
if (rollout != null) return rollout[1] - rollout[0];
return 0D;
}
}
static final class Fallthrough {
private final boolean includedInExpt;
private final String dispatchKey;
private final List<RolloutVariation> variations;
Fallthrough(boolean includedInExpt, String dispatchKey, List<RolloutVariation> variations) {
this.includedInExpt = includedInExpt;
this.dispatchKey = dispatchKey;
this.variations = variations;
}
public boolean includedInExpt() {
return includedInExpt;
}
public List<RolloutVariation> getVariations() {
return variations == null ? Collections.emptyList() : variations;
}
public String getDispatchKey() {
return dispatchKey;
}
}
static class DataExposer {
private final List<FeatureFlag> featureFlags;
private final List<Segment> segments;
public DataExposer(List<FeatureFlag> featureFlags, List<Segment> segments) {
this.featureFlags = featureFlags;
this.segments = segments;
}
public List<FeatureFlag> getFeatureFlags() {
return featureFlags;
}
public List<Segment> getSegments() {
return segments;
}
}
}