Skip to content

Commit 8fa559c

Browse files
authored
feat: add track function (#33)
1 parent c83e444 commit 8fa559c

File tree

9 files changed

+181
-64
lines changed

9 files changed

+181
-64
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.featureprobe</groupId>
88
<artifactId>server-sdk-java</artifactId>
9-
<version>1.4.1-SNAPSHOT</version>
9+
<version>2.0.1</version>
1010
<name>server-sdk-java</name>
1111
<url>https://github.com/FeatureProbe/server-sdk-java</url>
1212
<description>FeatureProbe Server Side SDK for Java</description>

src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.featureprobe.sdk.server.FPUser;
66
import com.featureprobe.sdk.server.FeatureProbe;
77
import java.io.IOException;
8+
import java.util.Random;
89
import java.util.concurrent.TimeUnit;
910

1011
import com.featureprobe.sdk.server.Loggers;
@@ -23,7 +24,7 @@ public class FeatureProbeDemo {
2324
// Server Side SDK Key for your project and environment
2425
public static final String FEATURE_PROBE_SERVER_SDK_KEY = "server-8ed48815ef044428826787e9a238b9c6a479f98c";
2526

26-
public static void main(String[] args) throws IOException {
27+
public static void main(String[] args) throws IOException, InterruptedException {
2728

2829
Logger root = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
2930
root.setLevel(Level.WARN);
@@ -55,6 +56,28 @@ public static void main(String[] args) throws IOException {
5556
System.out.println("detail:" + isOpenDetail.getReason());
5657
System.out.println("rule index:" + isOpenDetail.getRuleIndex());
5758

59+
60+
// Simulate conversion rate of 1000 users for a new feature
61+
final String YOUR_CUSTOM_EVENT_NAME = "new_feature_conversion";
62+
for (int i = 0; i < 1000; i++) {
63+
FPUser eventUser = new FPUser().stableRollout(String.valueOf(System.nanoTime()));
64+
boolean newFeature = fpClient.boolValue(YOUR_TOGGLE_KEY, eventUser, false);
65+
Random random = new Random();
66+
int randomRang = random.nextInt(100);
67+
if (newFeature) {
68+
if (randomRang <= 55) {
69+
System.out.println("New feature conversion.");
70+
fpClient.track(YOUR_CUSTOM_EVENT_NAME, eventUser);
71+
}
72+
} else {
73+
if (randomRang > 55) {
74+
System.out.println("Old feature conversion.");
75+
fpClient.track(YOUR_CUSTOM_EVENT_NAME, eventUser);
76+
}
77+
}
78+
Thread.sleep(200);
79+
}
80+
5881
fpClient.close();
5982

6083
}
Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,61 @@
11
package com.featureprobe.sdk.server;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
35
public class AccessEvent extends Event {
46

57
private final String key;
6-
7-
private final String value;
8+
private final Object value;
89

910
private final Long version;
1011

11-
private final Integer index;
12+
private final Integer variationIndex;
1213

13-
public AccessEvent(long timestamp, FPUser user, String key, String value, Long version, Integer index) {
14-
super(timestamp, user);
15-
this.key = key;
14+
private final Integer ruleIndex;
15+
16+
private final String reason;
17+
18+
@JsonIgnore
19+
private boolean trackAccessEvents;
20+
21+
public AccessEvent(String user, String key, Object value, Long version, Integer variationIndex,
22+
Integer ruleIndex, String reason, boolean trackAccessEvents) {
23+
super("access", System.currentTimeMillis(), user);
1624
this.value = value;
1725
this.version = version;
18-
this.index = index;
26+
this.variationIndex = variationIndex;
27+
this.key = key;
28+
this.ruleIndex = ruleIndex;
29+
this.reason = reason;
30+
this.trackAccessEvents = trackAccessEvents;
1931
}
2032

33+
2134
public String getKey() {
2235
return key;
2336
}
2437

25-
public String getValue() {
38+
public Object getValue() {
2639
return value;
2740
}
2841

2942
public Long getVersion() {
3043
return version;
3144
}
3245

33-
public Integer getIndex() {
34-
return index;
46+
public Integer getVariationIndex() {
47+
return variationIndex;
48+
}
49+
50+
public Integer getRuleIndex() {
51+
return ruleIndex;
52+
}
53+
54+
public String getReason() {
55+
return reason;
56+
}
57+
58+
public boolean isTrackAccessEvents() {
59+
return trackAccessEvents;
3560
}
3661
}

src/main/java/com/featureprobe/sdk/server/AccessRecorder.java renamed to src/main/java/com/featureprobe/sdk/server/AccessSummaryRecorder.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@
55
import java.util.List;
66
import java.util.Map;
77

8-
public class AccessRecorder {
8+
public class AccessSummaryRecorder {
99

1010
Map<String, List<Counter>> counters;
1111

1212
long startTime;
1313

1414
long endTime;
1515

16-
AccessRecorder() {
16+
AccessSummaryRecorder() {
1717
counters = new HashMap<>();
1818
}
1919

20-
private AccessRecorder(AccessRecorder accessRecorder) {
21-
counters = new HashMap<>(accessRecorder.counters);
22-
startTime = accessRecorder.startTime;
20+
private AccessSummaryRecorder(AccessSummaryRecorder accessSummaryRecorder) {
21+
counters = new HashMap<>(accessSummaryRecorder.counters);
22+
startTime = accessSummaryRecorder.startTime;
2323
endTime = System.currentTimeMillis();
2424
}
2525

2626
static final class Counter {
2727

2828
long count;
2929

30-
final String value;
30+
final Object value;
3131

3232
final Long version;
3333

3434
final Integer index;
3535

36-
public Counter(String value, Long version, Integer index) {
36+
public Counter(Object value, Long version, Integer index) {
3737
this.value = value;
3838
this.version = version;
3939
this.index = index;
@@ -44,15 +44,15 @@ public void increment() {
4444
++count;
4545
}
4646

47-
public boolean isGroup(String value, Long version, Integer index) {
48-
return this.value.equals(value) && this.version.equals(version) && this.index.equals(index);
47+
public boolean isGroup(Long version, Integer index) {
48+
return this.version.equals(version) && this.index.equals(index);
4949
}
5050

5151
public long getCount() {
5252
return count;
5353
}
5454

55-
public String getValue() {
55+
public Object getValue() {
5656
return value;
5757
}
5858

@@ -73,15 +73,15 @@ public void add(Event event) {
7373
if (counters.containsKey(accessEvent.getKey())) {
7474
List<Counter> counters = this.counters.get(accessEvent.getKey());
7575
for (Counter counter : counters) {
76-
if (counter.isGroup(accessEvent.getValue(), accessEvent.getVersion(), accessEvent.getIndex())) {
76+
if (counter.isGroup(accessEvent.getVersion(), accessEvent.getVariationIndex())) {
7777
counter.increment();
7878
return;
7979
}
8080
}
81-
counters.add(new Counter(accessEvent.getValue(), accessEvent.getVersion(), accessEvent.getIndex()));
81+
counters.add(new Counter(accessEvent.getValue(), accessEvent.getVersion(), accessEvent.getVariationIndex()));
8282
} else {
8383
List<Counter> groups = new ArrayList<>(1);
84-
groups.add(new Counter(accessEvent.getValue(), accessEvent.getVersion(), accessEvent.getIndex()));
84+
groups.add(new Counter(accessEvent.getValue(), accessEvent.getVersion(), accessEvent.getVariationIndex()));
8585
counters.put(accessEvent.getKey(), groups);
8686
}
8787
}
@@ -90,8 +90,8 @@ public void clear() {
9090
counters = new HashMap<>();
9191
}
9292

93-
public AccessRecorder snapshot() {
94-
return new AccessRecorder(this);
93+
public AccessSummaryRecorder snapshot() {
94+
return new AccessSummaryRecorder(this);
9595
}
9696

9797
public Map<String, List<Counter>> getCounters() {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.featureprobe.sdk.server;
2+
3+
public class CustomEvent extends Event {
4+
5+
private final String name;
6+
7+
private final Double value;
8+
9+
public CustomEvent(String name, String user, Double value) {
10+
super("custom", System.currentTimeMillis(), user);
11+
this.name = name;
12+
this.value = value;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public Double getValue() {
20+
return value;
21+
}
22+
}

src/main/java/com/featureprobe/sdk/server/DefaultEventProcessor.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ private static final class EventRepository {
212212

213213
List<Event> events = new ArrayList<>();
214214

215-
AccessRecorder access = new AccessRecorder();
215+
AccessSummaryRecorder access = new AccessSummaryRecorder();
216216

217217
public EventRepository() {
218218
}
219219

220-
private EventRepository(EventRepository eventRepository) {
221-
this.events = eventRepository.events;
222-
this.access = eventRepository.access.snapshot();
220+
private EventRepository(EventRepository repository) {
221+
this.events = new ArrayList<>(repository.events);
222+
this.access = repository.access.snapshot();
223223
}
224224

225225
boolean isEmpty() {
@@ -229,6 +229,12 @@ boolean isEmpty() {
229229
void add(Event event) {
230230
if (event instanceof AccessEvent) {
231231
access.add(event);
232+
// if (((AccessEvent) event).isTrackAccessEvents()) {
233+
if (true) {
234+
events.add(event);
235+
}
236+
} else if (event instanceof CustomEvent) {
237+
events.add(event);
232238
}
233239
}
234240

@@ -245,7 +251,7 @@ public List<Event> getEvents() {
245251
return events;
246252
}
247253

248-
public AccessRecorder getAccess() {
254+
public AccessSummaryRecorder getAccess() {
249255
return access;
250256
}
251257
}

src/main/java/com/featureprobe/sdk/server/Event.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@
22

33
public class Event {
44

5-
private final long createdTime;
5+
private final String kind;
66

7-
private final FPUser user;
7+
private final long time;
88

9-
public Event(long createdTime, FPUser user) {
10-
this.createdTime = createdTime;
9+
private final String user;
10+
11+
public Event(String kind, long time, String user) {
12+
this.kind = kind;
13+
this.time = time;
1114
this.user = user;
1215
}
1316

14-
public long getCreatedTime() {
15-
return createdTime;
17+
public String getKind() {
18+
return kind;
19+
}
20+
21+
public long getTime() {
22+
return time;
1623
}
1724

18-
public FPUser getUser() {
25+
public String getUser() {
1926
return user;
2027
}
2128
}

0 commit comments

Comments
 (0)