Skip to content

Commit

Permalink
Added Metric override (#37)
Browse files Browse the repository at this point in the history
* Initial testing endpoints

* Moving time generators to separate files.

* Increasing SDK version

* Removing SDK interface.

* Deprecated calls. Adding metric override.
  • Loading branch information
ArtursKadikis authored Mar 10, 2023
1 parent c6d0061 commit 03ffd64
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 265 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
22.09.1
* Adding a way to override metrics sent by "begin session" requests.
* Fixed bug where "setApplicationVersion" would not set the application version in metrics
* ! Minor breaking change ! The following methods and their functionality are deprecated from the "Config" class and will not function anymore:
- "getApplicationName"
- "setApplicationName"

22.09.0
* The "resetDeviceId", "login", and "logout" have been deprecated.
* ! Minor breaking change ! The following methods and their functionality are deprecated from the "Config" class and will not function anymore:
Expand Down
8 changes: 7 additions & 1 deletion app-java/src/main/java/ly/count/java/demo/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public static void main(String[] args) throws Exception {
String COUNTLY_SERVER_URL = "https://try.count.ly/";
String COUNTLY_APP_KEY = "YOUR_APP_KEY";

Map<String, String> metricOverride = new HashMap<>();
metricOverride.put("aa", "11");
metricOverride.put("bb", "222");

Config config = new Config(COUNTLY_SERVER_URL, COUNTLY_APP_KEY)
.setLoggingLevel(Config.LoggingLevel.DEBUG)
.setDeviceIdStrategy(Config.DeviceIdStrategy.UUID)
Expand All @@ -115,7 +119,9 @@ public static void main(String[] args) throws Exception {
public void LogHappened(String logMessage, Config.LoggingLevel logLevel) {
System.out.println("[" + logLevel + "] " + logMessage);
}
});
})
.setMetricOverride(metricOverride)
.setApplicationVersion("123.56.h");

// Countly needs persistent storage for requests, configuration storage, user profiles and other temporary data,
// therefore requires a separate data folder to run
Expand Down
78 changes: 39 additions & 39 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# RELEASE FIELD SECTION
VERSION_NAME=22.09.0
GROUP=ly.count.sdk

POM_URL=https://github.com/Countly/countly-sdk-java
POM_SCM_URL=https://github.com/Countly/countly-sdk-java

POM_LICENCE_NAME=MIT License
POM_LICENCE_URL=http://www.opensource.org/licenses/mit-license.php
POM_LICENCE_DIST=repo

POM_DEVELOPER_NAME=Countly

#SIGNING SECTION
signing.keyId=xx
signing.password=xx
signing.secretKeyRingFile=xx

mavenCentralUsername=xx
mavenCentralPassword=xx
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# RELEASE FIELD SECTION
VERSION_NAME=22.09.1
GROUP=ly.count.sdk

POM_URL=https://github.com/Countly/countly-sdk-java
POM_SCM_URL=https://github.com/Countly/countly-sdk-java

POM_LICENCE_NAME=MIT License
POM_LICENCE_URL=http://www.opensource.org/licenses/mit-license.php
POM_LICENCE_DIST=repo

POM_DEVELOPER_NAME=Countly

#SIGNING SECTION
signing.keyId=xx
signing.password=xx
signing.secretKeyRingFile=xx

mavenCentralUsername=xx
mavenCentralPassword=xx
29 changes: 16 additions & 13 deletions sdk-java/src/main/java/ly/count/sdk/java/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,7 @@ public boolean restore(byte[] data) {
/**
* Countly SDK version to be sent in HTTP requests
*/
protected String sdkVersion = "22.09.0";

/**
* Countly Application name to be sent in HTTP requests
*/
protected String applicationName;
protected String sdkVersion = "22.09.1";

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

protected Map<String, String> metricOverride = new HashMap<>();

/**
* Salt string for parameter tampering protection
*/
Expand Down Expand Up @@ -892,13 +889,9 @@ public Config setSdkVersion(String sdkVersion) {
*
* @param name new name
* @return {@code this} instance for method chaining
* @deprecated this will do nothing
*/
public Config setApplicationName(String name) {
if (Utils.isEmptyOrNull(name)) {
System.out.print("[ConfigCore] name cannot be empty");
} else {
this.applicationName = name;
}
return this;
}

Expand Down Expand Up @@ -1254,11 +1247,11 @@ public boolean isFeatureEnabled(int feature) {

/**
* Getter for {@link #applicationName}
*
* @deprecated will return empty string
* @return {@link #applicationName} value
*/
public String getApplicationName() {
return applicationName;
return "";
}

/**
Expand Down Expand Up @@ -1414,5 +1407,15 @@ public Class<? extends Module> getModuleOverride(int feature) {
public boolean requiresConsent() {
return requiresConsent;
}

/**
* Mechanism for overriding metrics that are sent together with "begin session" requests and remote config
* @param metricOverride map of values to be used for override
* @return {@code this} instance for method chaining
*/
public Config setMetricOverride(Map<String, String> metricOverride) {
this.metricOverride.putAll(metricOverride);
return this;
}
}

6 changes: 6 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public static void init(final File directory, final Config config) {

InternalConfig internalConfig = new InternalConfig(config);
Log L = new Log(internalConfig.loggingLevel, internalConfig.logListener);

device.setMetricOverride(internalConfig.getMetricOverride());
if (internalConfig.getApplicationVersion() != null) {
device.setAppVersion(internalConfig.getApplicationVersion());
}

SDKCore sdk = new SDKCore();
sdk.init(new CtxCore(sdk, internalConfig, L, directory), L);

Expand Down
97 changes: 35 additions & 62 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -31,6 +33,8 @@ public class Device {
private Boolean online;
private Boolean muted;

private Map<String, String> metricOverride = new HashMap<>();

protected Device() {
dev = this;
}
Expand All @@ -50,62 +54,6 @@ public interface TimeGenerator {
long timestamp();
}

/**
* Always increasing timer.
*/
static class UniformTimeGenerator implements TimeGenerator {
private Long last;

@Override
public synchronized long timestamp() {
long ms = System.currentTimeMillis();
if (last == null) {
last = ms;
} else if (last >= ms) {
last = last + 1;
return last;
} else {
last = ms;
}
return ms;
}
}

/**
* Unique timer, keeps last 10 returned values in memory.
*/
static class UniqueTimeGenerator implements TimeGenerator {
List<Long> lastTsMs = new ArrayList<>(10);
long addition = 0;

long currentTimeMillis() {
return System.currentTimeMillis() + addition;
}

public synchronized long timestamp() {
long ms = currentTimeMillis();

// change time back case
if (lastTsMs.size() > 2) {
long min = Collections.min(lastTsMs);
if (ms < min) {
lastTsMs.clear();
lastTsMs.add(ms);
return ms;
}
}
// usual case
while (lastTsMs.contains(ms)) {
ms += 1;
}
while (lastTsMs.size() >= 10) {
lastTsMs.remove(0);
}
lastTsMs.add(ms);
return ms;
}
}

protected TimeGenerator uniqueTimer = new UniqueTimeGenerator();
protected TimeGenerator uniformTimer = new UniformTimeGenerator();

Expand Down Expand Up @@ -149,19 +97,39 @@ public String getLocale() {

/**
* Build metrics {@link Params} object as required by Countly server
*
* @param ctx Ctx in which to request metrics
*/
public Params buildMetrics(final CtxCore ctx) {
public Params buildMetrics() {
Params params = new Params();
params.obj("metrics")
Params.Obj metricObj = params.obj("metrics")
.put("_device", getDevice())
.put("_os", getOS())
.put("_os_version", getOSVersion())
.put("_resolution", getResolution())
.put("_locale", getLocale())
.put("_app_version", getAppVersion())
.add();
.put("_app_version", getAppVersion());


//override metric values
if (metricOverride != null) {
for (String k : metricOverride.keySet()) {
if (k == null || k.length() == 0) {
//L.w("Provided metric override key can't be null or empty");//todo add log
continue;
}

String overrideValue = metricOverride.get(k);

if (overrideValue == null) {
//L.w("Provided metric override value can't be null, key:[" + k + "]");//todo add log
continue;
}

metricObj.put(k, overrideValue);
}
}

//add the object after adding the overrides
metricObj.add();

return params;
}
Expand Down Expand Up @@ -527,4 +495,9 @@ public Device setMuted(Boolean muted) {
this.muted = muted;
return this;
}

public Device setMetricOverride(Map<String, String> givenMetricOverride) {
metricOverride.putAll(givenMetricOverride);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public interface DeviceIdGenerator {
boolean isAvailable();

String generate(CtxCore context, int realm);
String generate(CtxCore context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ly.count.sdk.java.Config;
Expand Down Expand Up @@ -120,7 +121,7 @@ public byte[] store() {
stream.writeInt(loggingLevel.getLevel());
stream.writeUTF(sdkName);
stream.writeUTF(sdkVersion);
stream.writeObject(applicationName);
stream.writeObject("name");
stream.writeObject(applicationVersion);
stream.writeBoolean(usePOST);
stream.writeObject(salt);
Expand Down Expand Up @@ -211,7 +212,7 @@ public boolean restore(byte[] data) {

sdkName = stream.readUTF();
sdkVersion = stream.readUTF();
applicationName = (String) stream.readObject();
String throwawayApplicationName = (String) stream.readObject();//we are only reading this for backwards compatibility. Throw away in the future
applicationVersion = (String) stream.readObject();
usePOST = stream.readBoolean();
salt = (String) stream.readObject();
Expand Down Expand Up @@ -375,4 +376,8 @@ public Long getRemoteConfigUpdateTimeoutLength() {
return remoteConfigUpdateRequestTimeout;
}
//endregion

public Map<String, String> getMetricOverride() {
return metricOverride;
}
}
Loading

0 comments on commit 03ffd64

Please sign in to comment.