Skip to content

Commit 03ffd64

Browse files
Added Metric override (#37)
* Initial testing endpoints * Moving time generators to separate files. * Increasing SDK version * Removing SDK interface. * Deprecated calls. Adding metric override.
1 parent c6d0061 commit 03ffd64

File tree

18 files changed

+328
-265
lines changed

18 files changed

+328
-265
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
22.09.1
2+
* Adding a way to override metrics sent by "begin session" requests.
3+
* Fixed bug where "setApplicationVersion" would not set the application version in metrics
4+
* ! Minor breaking change ! The following methods and their functionality are deprecated from the "Config" class and will not function anymore:
5+
- "getApplicationName"
6+
- "setApplicationName"
7+
18
22.09.0
29
* The "resetDeviceId", "login", and "logout" have been deprecated.
310
* ! Minor breaking change ! The following methods and their functionality are deprecated from the "Config" class and will not function anymore:

app-java/src/main/java/ly/count/java/demo/Sample.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public static void main(String[] args) throws Exception {
104104
String COUNTLY_SERVER_URL = "https://try.count.ly/";
105105
String COUNTLY_APP_KEY = "YOUR_APP_KEY";
106106

107+
Map<String, String> metricOverride = new HashMap<>();
108+
metricOverride.put("aa", "11");
109+
metricOverride.put("bb", "222");
110+
107111
Config config = new Config(COUNTLY_SERVER_URL, COUNTLY_APP_KEY)
108112
.setLoggingLevel(Config.LoggingLevel.DEBUG)
109113
.setDeviceIdStrategy(Config.DeviceIdStrategy.UUID)
@@ -115,7 +119,9 @@ public static void main(String[] args) throws Exception {
115119
public void LogHappened(String logMessage, Config.LoggingLevel logLevel) {
116120
System.out.println("[" + logLevel + "] " + logMessage);
117121
}
118-
});
122+
})
123+
.setMetricOverride(metricOverride)
124+
.setApplicationVersion("123.56.h");
119125

120126
// Countly needs persistent storage for requests, configuration storage, user profiles and other temporary data,
121127
// therefore requires a separate data folder to run

gradle.properties

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# Project-wide Gradle settings.
2-
3-
# IDE (e.g. Android Studio) users:
4-
# Gradle settings configured through the IDE *will override*
5-
# any settings specified in this file.
6-
7-
# For more details on how to configure your build environment visit
8-
# http://www.gradle.org/docs/current/userguide/build_environment.html
9-
10-
# Specifies the JVM arguments used for the daemon process.
11-
# The setting is particularly useful for tweaking memory settings.
12-
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13-
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14-
15-
# When configured, Gradle will run in incubating parallel mode.
16-
# This option should only be used with decoupled projects. More details, visit
17-
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18-
# org.gradle.parallel=true
19-
20-
# RELEASE FIELD SECTION
21-
VERSION_NAME=22.09.0
22-
GROUP=ly.count.sdk
23-
24-
POM_URL=https://github.com/Countly/countly-sdk-java
25-
POM_SCM_URL=https://github.com/Countly/countly-sdk-java
26-
27-
POM_LICENCE_NAME=MIT License
28-
POM_LICENCE_URL=http://www.opensource.org/licenses/mit-license.php
29-
POM_LICENCE_DIST=repo
30-
31-
POM_DEVELOPER_NAME=Countly
32-
33-
#SIGNING SECTION
34-
signing.keyId=xx
35-
signing.password=xx
36-
signing.secretKeyRingFile=xx
37-
38-
mavenCentralUsername=xx
39-
mavenCentralPassword=xx
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true
19+
20+
# RELEASE FIELD SECTION
21+
VERSION_NAME=22.09.1
22+
GROUP=ly.count.sdk
23+
24+
POM_URL=https://github.com/Countly/countly-sdk-java
25+
POM_SCM_URL=https://github.com/Countly/countly-sdk-java
26+
27+
POM_LICENCE_NAME=MIT License
28+
POM_LICENCE_URL=http://www.opensource.org/licenses/mit-license.php
29+
POM_LICENCE_DIST=repo
30+
31+
POM_DEVELOPER_NAME=Countly
32+
33+
#SIGNING SECTION
34+
signing.keyId=xx
35+
signing.password=xx
36+
signing.secretKeyRingFile=xx
37+
38+
mavenCentralUsername=xx
39+
mavenCentralPassword=xx

sdk-java/src/main/java/ly/count/sdk/java/Config.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,7 @@ public boolean restore(byte[] data) {
269269
/**
270270
* Countly SDK version to be sent in HTTP requests
271271
*/
272-
protected String sdkVersion = "22.09.0";
273-
274-
/**
275-
* Countly Application name to be sent in HTTP requests
276-
*/
277-
protected String applicationName;
272+
protected String sdkVersion = "22.09.1";
278273

279274
/**
280275
* Countly SDK version to be sent in HTTP requests
@@ -291,6 +286,8 @@ public boolean restore(byte[] data) {
291286
*/
292287
protected boolean enableBackendMode = false;
293288

289+
protected Map<String, String> metricOverride = new HashMap<>();
290+
294291
/**
295292
* Salt string for parameter tampering protection
296293
*/
@@ -892,13 +889,9 @@ public Config setSdkVersion(String sdkVersion) {
892889
*
893890
* @param name new name
894891
* @return {@code this} instance for method chaining
892+
* @deprecated this will do nothing
895893
*/
896894
public Config setApplicationName(String name) {
897-
if (Utils.isEmptyOrNull(name)) {
898-
System.out.print("[ConfigCore] name cannot be empty");
899-
} else {
900-
this.applicationName = name;
901-
}
902895
return this;
903896
}
904897

@@ -1254,11 +1247,11 @@ public boolean isFeatureEnabled(int feature) {
12541247

12551248
/**
12561249
* Getter for {@link #applicationName}
1257-
*
1250+
* @deprecated will return empty string
12581251
* @return {@link #applicationName} value
12591252
*/
12601253
public String getApplicationName() {
1261-
return applicationName;
1254+
return "";
12621255
}
12631256

12641257
/**
@@ -1414,5 +1407,15 @@ public Class<? extends Module> getModuleOverride(int feature) {
14141407
public boolean requiresConsent() {
14151408
return requiresConsent;
14161409
}
1410+
1411+
/**
1412+
* Mechanism for overriding metrics that are sent together with "begin session" requests and remote config
1413+
* @param metricOverride map of values to be used for override
1414+
* @return {@code this} instance for method chaining
1415+
*/
1416+
public Config setMetricOverride(Map<String, String> metricOverride) {
1417+
this.metricOverride.putAll(metricOverride);
1418+
return this;
1419+
}
14171420
}
14181421

sdk-java/src/main/java/ly/count/sdk/java/Countly.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public static void init(final File directory, final Config config) {
7979

8080
InternalConfig internalConfig = new InternalConfig(config);
8181
Log L = new Log(internalConfig.loggingLevel, internalConfig.logListener);
82+
83+
device.setMetricOverride(internalConfig.getMetricOverride());
84+
if (internalConfig.getApplicationVersion() != null) {
85+
device.setAppVersion(internalConfig.getApplicationVersion());
86+
}
87+
8288
SDKCore sdk = new SDKCore();
8389
sdk.init(new CtxCore(sdk, internalConfig, L, directory), L);
8490

sdk-java/src/main/java/ly/count/sdk/java/internal/Device.java

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import java.util.Calendar;
88
import java.util.Collections;
99
import java.util.Date;
10+
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Locale;
13+
import java.util.Map;
1214
import java.util.TimeZone;
1315
import java.util.regex.Matcher;
1416
import java.util.regex.Pattern;
@@ -31,6 +33,8 @@ public class Device {
3133
private Boolean online;
3234
private Boolean muted;
3335

36+
private Map<String, String> metricOverride = new HashMap<>();
37+
3438
protected Device() {
3539
dev = this;
3640
}
@@ -50,62 +54,6 @@ public interface TimeGenerator {
5054
long timestamp();
5155
}
5256

53-
/**
54-
* Always increasing timer.
55-
*/
56-
static class UniformTimeGenerator implements TimeGenerator {
57-
private Long last;
58-
59-
@Override
60-
public synchronized long timestamp() {
61-
long ms = System.currentTimeMillis();
62-
if (last == null) {
63-
last = ms;
64-
} else if (last >= ms) {
65-
last = last + 1;
66-
return last;
67-
} else {
68-
last = ms;
69-
}
70-
return ms;
71-
}
72-
}
73-
74-
/**
75-
* Unique timer, keeps last 10 returned values in memory.
76-
*/
77-
static class UniqueTimeGenerator implements TimeGenerator {
78-
List<Long> lastTsMs = new ArrayList<>(10);
79-
long addition = 0;
80-
81-
long currentTimeMillis() {
82-
return System.currentTimeMillis() + addition;
83-
}
84-
85-
public synchronized long timestamp() {
86-
long ms = currentTimeMillis();
87-
88-
// change time back case
89-
if (lastTsMs.size() > 2) {
90-
long min = Collections.min(lastTsMs);
91-
if (ms < min) {
92-
lastTsMs.clear();
93-
lastTsMs.add(ms);
94-
return ms;
95-
}
96-
}
97-
// usual case
98-
while (lastTsMs.contains(ms)) {
99-
ms += 1;
100-
}
101-
while (lastTsMs.size() >= 10) {
102-
lastTsMs.remove(0);
103-
}
104-
lastTsMs.add(ms);
105-
return ms;
106-
}
107-
}
108-
10957
protected TimeGenerator uniqueTimer = new UniqueTimeGenerator();
11058
protected TimeGenerator uniformTimer = new UniformTimeGenerator();
11159

@@ -149,19 +97,39 @@ public String getLocale() {
14997

15098
/**
15199
* Build metrics {@link Params} object as required by Countly server
152-
*
153-
* @param ctx Ctx in which to request metrics
154100
*/
155-
public Params buildMetrics(final CtxCore ctx) {
101+
public Params buildMetrics() {
156102
Params params = new Params();
157-
params.obj("metrics")
103+
Params.Obj metricObj = params.obj("metrics")
158104
.put("_device", getDevice())
159105
.put("_os", getOS())
160106
.put("_os_version", getOSVersion())
161107
.put("_resolution", getResolution())
162108
.put("_locale", getLocale())
163-
.put("_app_version", getAppVersion())
164-
.add();
109+
.put("_app_version", getAppVersion());
110+
111+
112+
//override metric values
113+
if (metricOverride != null) {
114+
for (String k : metricOverride.keySet()) {
115+
if (k == null || k.length() == 0) {
116+
//L.w("Provided metric override key can't be null or empty");//todo add log
117+
continue;
118+
}
119+
120+
String overrideValue = metricOverride.get(k);
121+
122+
if (overrideValue == null) {
123+
//L.w("Provided metric override value can't be null, key:[" + k + "]");//todo add log
124+
continue;
125+
}
126+
127+
metricObj.put(k, overrideValue);
128+
}
129+
}
130+
131+
//add the object after adding the overrides
132+
metricObj.add();
165133

166134
return params;
167135
}
@@ -527,4 +495,9 @@ public Device setMuted(Boolean muted) {
527495
this.muted = muted;
528496
return this;
529497
}
498+
499+
public Device setMetricOverride(Map<String, String> givenMetricOverride) {
500+
metricOverride.putAll(givenMetricOverride);
501+
return this;
502+
}
530503
}

sdk-java/src/main/java/ly/count/sdk/java/internal/DeviceIdGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
public interface DeviceIdGenerator {
44
boolean isAvailable();
55

6-
String generate(CtxCore context, int realm);
6+
String generate(CtxCore context);
77
}

sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.HashMap;
1212
import java.util.HashSet;
1313
import java.util.List;
14+
import java.util.Map;
1415
import java.util.Set;
1516

1617
import ly.count.sdk.java.Config;
@@ -120,7 +121,7 @@ public byte[] store() {
120121
stream.writeInt(loggingLevel.getLevel());
121122
stream.writeUTF(sdkName);
122123
stream.writeUTF(sdkVersion);
123-
stream.writeObject(applicationName);
124+
stream.writeObject("name");
124125
stream.writeObject(applicationVersion);
125126
stream.writeBoolean(usePOST);
126127
stream.writeObject(salt);
@@ -211,7 +212,7 @@ public boolean restore(byte[] data) {
211212

212213
sdkName = stream.readUTF();
213214
sdkVersion = stream.readUTF();
214-
applicationName = (String) stream.readObject();
215+
String throwawayApplicationName = (String) stream.readObject();//we are only reading this for backwards compatibility. Throw away in the future
215216
applicationVersion = (String) stream.readObject();
216217
usePOST = stream.readBoolean();
217218
salt = (String) stream.readObject();
@@ -375,4 +376,8 @@ public Long getRemoteConfigUpdateTimeoutLength() {
375376
return remoteConfigUpdateRequestTimeout;
376377
}
377378
//endregion
379+
380+
public Map<String, String> getMetricOverride() {
381+
return metricOverride;
382+
}
378383
}

0 commit comments

Comments
 (0)