Skip to content

Commit 2188142

Browse files
e7macgg4race
authored andcommitted
add counting ability on client (#218)
* add counting ability on client * change param name * add sdkCounterUiEditor usage * rebase * change counter logic * send counts * updated counting to match iOS design * renamed LPCountAggregator -> CountAggregator * remove extra lines, update test inputs * add newline at end of Log.java * remove 4 spaces in Log.java
1 parent 50f3fcd commit 2188142

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.leanplum.internal.Constants;
3737
import com.leanplum.internal.FileManager;
3838
import com.leanplum.internal.JsonConverter;
39+
import com.leanplum.internal.CountAggregator;
3940
import com.leanplum.internal.LeanplumEventDataManager;
4041
import com.leanplum.internal.LeanplumInternal;
4142
import com.leanplum.internal.LeanplumMessageMatchFilter;
@@ -55,10 +56,13 @@
5556
import org.json.JSONObject;
5657

5758
import java.util.ArrayList;
59+
import java.util.Arrays;
5860
import java.util.Date;
5961
import java.util.HashMap;
62+
import java.util.HashSet;
6063
import java.util.List;
6164
import java.util.Map;
65+
import java.util.Set;
6266
import java.util.TimeZone;
6367
import java.util.concurrent.Executors;
6468
import java.util.concurrent.ScheduledExecutorService;
@@ -814,6 +818,7 @@ protected Void doInBackground(Void... params) {
814818
Constants.loggingEnabled = true;
815819
}
816820

821+
parseSdkCounters(response);
817822
parseVariantDebugInfo(response);
818823

819824
// Allow bidirectional realtime variable updates.
@@ -2158,4 +2163,13 @@ private static void parseVariantDebugInfo(JSONObject response) {
21582163
public static void clearUserContent() {
21592164
VarCache.clearUserContent();
21602165
}
2166+
2167+
private static void parseSdkCounters(JSONObject response) {
2168+
JSONArray enabledCounters = response.optJSONArray(
2169+
Constants.Keys.ENABLED_COUNTERS);
2170+
if (enabledCounters != null) {
2171+
HashSet counterSet = new HashSet<>(Arrays.asList(enabledCounters));
2172+
CountAggregator.INSTANCE.setEnabledCounters(counterSet);
2173+
}
2174+
}
21612175
}

AndroidSDKCore/src/main/java/com/leanplum/internal/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public static class Params {
151151
public static final String KINDS = "kinds";
152152
public static final String LIMIT_TRACKING = "limitTracking";
153153
public static final String MESSAGE = "message";
154+
public static final String COUNT = "count";
154155
public static final String MESSAGE_ID = "messageId";
155156
public static final String NEW_USER_ID = "newUserId";
156157
public static final String INBOX_MESSAGE_ID = "newsfeedMessageId";
@@ -206,6 +207,7 @@ public static class Keys {
206207
public static final String SUBTITLE = "Subtitle";
207208
public static final String SYNC_INBOX = "syncNewsfeed";
208209
public static final String LOGGING_ENABLED = "loggingEnabled";
210+
public static final String ENABLED_COUNTERS = "enabledSdkCounters";
209211
public static final String TIMEZONE = "timezone";
210212
public static final String TIMEZONE_OFFSET_SECONDS = "timezoneOffsetSeconds";
211213
public static final String TITLE = "Title";
@@ -250,6 +252,7 @@ public static class Values {
250252
public static final String DEFAULT_PUSH_MESSAGE = "Push message goes here.";
251253
public static final String SDK_LOG = "sdkLog";
252254
public static final String SDK_ERROR = "sdkError";
255+
public static final String SDK_COUNT = "sdkCount";
253256
public static final String FILE_PREFIX = "__file__";
254257
}
255258

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.leanplum.internal;
2+
3+
import java.util.HashSet;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class CountAggregator {
8+
public static final CountAggregator INSTANCE = new CountAggregator();
9+
10+
private HashSet<String> enabledCounters = new HashSet<>();
11+
private final HashMap<String, Integer> counts = new HashMap<>();
12+
13+
public void setEnabledCounters(HashSet<String> enabledCounters) {
14+
this.enabledCounters = enabledCounters;
15+
}
16+
17+
public void incrementCount(String name) {
18+
incrementCount(name, 1);
19+
}
20+
21+
public void incrementCount(String name, int incrementCount) {
22+
if (enabledCounters.contains(name)) {
23+
Integer count = 0;
24+
if (counts.containsKey(name)) {
25+
count = counts.get(name);
26+
}
27+
count = count + incrementCount;
28+
counts.put(name, count);
29+
}
30+
}
31+
32+
private HashMap<String, Integer> getAndClearCounts() {
33+
HashMap<String, Integer> previousCounts = new HashMap<>();
34+
previousCounts.putAll(counts);
35+
counts.clear();
36+
return previousCounts;
37+
}
38+
39+
public void sendAllCounts() {
40+
HashMap<String, Integer> counts = getAndClearCounts();
41+
42+
for(Map.Entry<String, Integer> entry : counts.entrySet()) {
43+
String name = entry.getKey();
44+
Integer count = entry.getValue();
45+
try {
46+
HashMap<String, Object> params = new HashMap<>();
47+
params.put(Constants.Params.TYPE, Constants.Values.SDK_COUNT);
48+
params.put(Constants.Params.MESSAGE, name);
49+
params.put(Constants.Params.COUNT, count);
50+
Request.post(Constants.Methods.LOG, params).sendEventually();
51+
} catch (Throwable t) {
52+
android.util.Log.e("Leanplum", "Unable to send count.", t);
53+
}
54+
}
55+
}
56+
57+
public HashMap<String, Integer> getCounts() {
58+
return counts;
59+
}
60+
}

AndroidSDKCore/src/main/java/com/leanplum/internal/LeanplumUIEditorWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected LeanplumUIEditorWrapper() {
6363
interfaceEditorSingleton = (LeanplumUIEditor) method.invoke(null);
6464
if (interfaceEditorSingleton != null) {
6565
interfaceEditorSingleton.allowInterfaceEditing(Constants.isDevelopmentModeEnabled);
66+
CountAggregator.INSTANCE.incrementCount("sdkCountUiEditor");
6667
}
6768
} catch (IllegalAccessException e) {
6869
Util.handleException(e);

AndroidSDKCore/src/main/java/com/leanplum/internal/Request.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
581581
}
582582

583583
private void sendRequests() {
584+
CountAggregator.INSTANCE.sendAllCounts();
584585
requestSequenceRecorder.beforeRead();
585586

586587
RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2018, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
package com.leanplum.internal;
22+
23+
import com.leanplum.__setup.AbstractTest;
24+
25+
import org.junit.Test;
26+
27+
import java.util.HashMap;
28+
import java.util.HashSet;
29+
import java.util.Arrays;
30+
31+
import static org.junit.Assert.assertEquals;
32+
33+
/**
34+
* @author Grace Gu
35+
*/
36+
public class CountAggregatorTest extends AbstractTest {
37+
@Test
38+
public void testIncrementDisabledCount() {
39+
CountAggregator countAggregator = new CountAggregator();
40+
String testString = "test";
41+
42+
countAggregator.incrementCount(testString);
43+
HashMap<String, Integer> counts = countAggregator.getCounts();
44+
assertEquals(null, counts.get(testString));
45+
46+
countAggregator.incrementCount(testString);
47+
counts = countAggregator.getCounts();
48+
assertEquals(null, counts.get(testString));
49+
}
50+
51+
@Test
52+
public void testIncrementCount() {
53+
CountAggregator countAggregator = new CountAggregator();
54+
String testString = "test";
55+
HashSet<String> testSet = new HashSet<String>(Arrays.asList(testString));
56+
countAggregator.setEnabledCounters(testSet);
57+
58+
countAggregator.incrementCount(testString);
59+
HashMap<String, Integer> counts = countAggregator.getCounts();
60+
assertEquals(1, counts.get(testString).intValue());
61+
62+
countAggregator.incrementCount(testString);
63+
counts = countAggregator.getCounts();
64+
assertEquals(2, counts.get(testString).intValue());
65+
}
66+
67+
@Test
68+
public void testIncrementDisabledCountMultiple() {
69+
CountAggregator countAggregator = new CountAggregator();
70+
String testString = "test";
71+
72+
countAggregator.incrementCount(testString, 2);
73+
HashMap<String, Integer> counts = countAggregator.getCounts();
74+
assertEquals(null, counts.get(testString));
75+
76+
countAggregator.incrementCount(testString, 15);
77+
counts = countAggregator.getCounts();
78+
assertEquals(null, counts.get(testString));
79+
}
80+
81+
@Test
82+
public void testIncrementCountMultiple() {
83+
CountAggregator countAggregator = new CountAggregator();
84+
String testString = "test";
85+
HashSet<String> testSet = new HashSet<String>(Arrays.asList(testString));
86+
countAggregator.setEnabledCounters(testSet);
87+
88+
countAggregator.incrementCount(testString, 2);
89+
HashMap<String, Integer> counts = countAggregator.getCounts();
90+
assertEquals(2, counts.get(testString).intValue());
91+
92+
countAggregator.incrementCount(testString, 15);
93+
counts = countAggregator.getCounts();
94+
assertEquals(17, counts.get(testString).intValue());
95+
}
96+
}

0 commit comments

Comments
 (0)