Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] rework event module #63

Merged
merged 95 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
3ffecf2
chore: ignore vscode path
arifBurakDemiray Sep 18, 2023
4397867
feat: module events
arifBurakDemiray Sep 18, 2023
abd616c
feat: new constructor for EventImpl
arifBurakDemiray Sep 18, 2023
d42571c
feat: new way of events module call
arifBurakDemiray Sep 18, 2023
6e1b263
feat: module events
arifBurakDemiray Sep 18, 2023
906d346
fix: update version to 23.8.0
arifBurakDemiray Sep 19, 2023
a867c1b
Merge pull request #61 from Countly/staging
arifBurakDemiray Sep 19, 2023
2b2ae27
Merge pull request #62 from Countly/update_version
arifBurakDemiray Sep 19, 2023
44dabe0
Merge branch 'remake_events_module' of https://github.com/Countly/cou…
arifBurakDemiray Sep 19, 2023
92c6041
feat: events module to sdk core
arifBurakDemiray Sep 19, 2023
18782df
feat: end event deatiled
arifBurakDemiray Sep 19, 2023
8bd5a24
feat: init check to events call
arifBurakDemiray Sep 19, 2023
a094d0b
feat: event impl queue to store it in the future
arifBurakDemiray Sep 19, 2023
70b2519
refactor: format and add java 8 way refactorings
arifBurakDemiray Sep 19, 2023
51255a3
refactor: deprecate session events and add new way of event addind
arifBurakDemiray Sep 19, 2023
998e08b
feat: add deprecated changelog
arifBurakDemiray Sep 19, 2023
1046cbd
feat: add deprecated call for event
arifBurakDemiray Sep 19, 2023
5f85c58
feat: map in a beauty way,
arifBurakDemiray Sep 19, 2023
661e8c5
feat: info logs
arifBurakDemiray Sep 19, 2023
07b2551
feat: timed events additionb
arifBurakDemiray Sep 19, 2023
cdf08d0
Merge pull request #64 from Countly/staging
arifBurakDemiray Sep 19, 2023
ffb893c
fix: remove unnecesaary check
arifBurakDemiray Sep 19, 2023
aa16aea
Merge branch 'remake_events_module' of https://github.com/Countly/cou…
arifBurakDemiray Sep 19, 2023
5d94375
feat: remove unnecessary check
arifBurakDemiray Sep 19, 2023
10b113d
fix: add correct param
arifBurakDemiray Sep 19, 2023
558b350
feat: migarete cly to County logs
arifBurakDemiray Sep 19, 2023
95ace1b
revert: Countly to Cly
arifBurakDemiray Sep 19, 2023
d147821
feat: log infos
arifBurakDemiray Sep 19, 2023
db613c5
revert: remove logs to anoother pr
arifBurakDemiray Sep 19, 2023
3e27845
feat: add deprecation
arifBurakDemiray Sep 19, 2023
d03d186
feat: push util func to utils class
arifBurakDemiray Sep 19, 2023
14c3ec8
feat: tests for new util
arifBurakDemiray Sep 19, 2023
ca4e7af
feat: convert string to object
arifBurakDemiray Sep 20, 2023
5bcebf9
feat: util funcs
arifBurakDemiray Sep 20, 2023
cd9b8e9
feat: tests for util and serialization
arifBurakDemiray Sep 20, 2023
a77a9a6
chore: reevert
arifBurakDemiray Sep 20, 2023
66b7f27
Merge pull request #67 from Countly/change_segmentation_type
arifBurakDemiray Sep 20, 2023
f687994
feat: usage of util send obj segm
arifBurakDemiray Sep 20, 2023
ea83105
fix: merge issue
arifBurakDemiray Sep 20, 2023
ecfcf1a
Merge pull request #68 from Countly/migrate_old_logs
arifBurakDemiray Sep 20, 2023
3e93ed6
fix: undo merge conflict changes
arifBurakDemiray Sep 20, 2023
5e406bf
feat: new util string joiner
arifBurakDemiray Sep 20, 2023
bdcdf63
feat: new sdk storage way of events
arifBurakDemiray Sep 20, 2023
d9ad1fc
feat: new way of event add
arifBurakDemiray Sep 20, 2023
a724792
feat: expose target folder to used by other inner classes
arifBurakDemiray Sep 20, 2023
3136e45
feat: default param for sum and dur
arifBurakDemiray Sep 20, 2023
5f01f54
feat: delete unnecesary mapping
arifBurakDemiray Sep 20, 2023
1ee02ae
feat: string joiner and test
arifBurakDemiray Sep 20, 2023
dbb8736
fix: remove unnecessary call
arifBurakDemiray Sep 20, 2023
cc957ef
feat: reorder events to usage in scheduled
arifBurakDemiray Sep 20, 2023
cc25f6e
chore: remove unnecessary log
arifBurakDemiray Sep 20, 2023
62a041f
fix: reorder recordEvent param
arifBurakDemiray Sep 20, 2023
807326e
feat: remove unnecessary funcs and add comment tests
arifBurakDemiray Sep 21, 2023
9cbcf8f
doc: add utils comments
arifBurakDemiray Sep 21, 2023
4645b7b
feat: serialize deserialize test
arifBurakDemiray Sep 21, 2023
e19c909
Merge pull request #71 from Countly/change_segmentation_type
arifBurakDemiray Sep 21, 2023
d2b4074
fix: remove unnecessary util func
arifBurakDemiray Sep 21, 2023
74ca0c8
fix: ctx t config
arifBurakDemiray Sep 21, 2023
9b0c554
Merge branch 'staging' into remake_events_module
ArtursKadikis Sep 21, 2023
fe18de5
fix: revert default params
arifBurakDemiray Sep 21, 2023
3a498d0
Merge branch 'staging' into remake_events_module
arifBurakDemiray Sep 21, 2023
ce02883
fix: semicolon
arifBurakDemiray Sep 21, 2023
65d21bf
fix: use mutable version of instances
arifBurakDemiray Sep 21, 2023
d05dda9
fix: change to lf
arifBurakDemiray Sep 21, 2023
fcd0080
feat: read file content util and test
arifBurakDemiray Sep 21, 2023
da65f86
feat: get current event queue test util
arifBurakDemiray Sep 21, 2023
77df326
feat: test utils to get last item and return event impl1
arifBurakDemiray Sep 21, 2023
cb4aac4
feat: restore event queue when starting event module1
arifBurakDemiray Sep 21, 2023
444c5a0
feat: use restoore
arifBurakDemiray Sep 21, 2023
5a50040
feat: storage init
arifBurakDemiray Sep 21, 2023
8749d07
feat: module event tests for recording event
arifBurakDemiray Sep 21, 2023
ca83ea0
fix: rename read event queue func
arifBurakDemiray Sep 22, 2023
9dc9fe3
feat: reearrange test order and add new tests
arifBurakDemiray Sep 22, 2023
2624930
Merge branch 'staging' into remake_events_module
ArtursKadikis Sep 22, 2023
dcb6ce4
refactor: to in memory cache
arifBurakDemiray Sep 22, 2023
3132b8e
feat: standolaness for every test util
arifBurakDemiray Sep 22, 2023
3b5aa05
feat: conveint usages nd fix
arifBurakDemiray Sep 25, 2023
58fd752
refactor: recordEvent function
arifBurakDemiray Sep 25, 2023
f7502b6
refactor: reformat imports
arifBurakDemiray Sep 25, 2023
ae84c77
refactor: module events test
arifBurakDemiray Sep 26, 2023
da0657e
feat: add halt to sdk
arifBurakDemiray Sep 26, 2023
17887f7
Added a validation function for events in tests
ArtursKadikis Sep 27, 2023
b78edda
Merge branch 'staging' into remake_events_module
arifBurakDemiray Sep 27, 2023
75b687a
fix: test utils
arifBurakDemiray Sep 27, 2023
a96e839
feat: rename event impl queeu to event queue
arifBurakDemiray Sep 27, 2023
71bc4c2
feat: add force sending
arifBurakDemiray Sep 27, 2023
89d955a
refactor: use reduced way
arifBurakDemiray Sep 27, 2023
f5a4fca
fix: os specific test
arifBurakDemiray Sep 28, 2023
fcae388
refactor: remove unused param
arifBurakDemiray Sep 28, 2023
b26fb1c
feat: event queue test
arifBurakDemiray Sep 28, 2023
0857eb3
feat: event queue size over test
arifBurakDemiray Sep 28, 2023
45aee6c
feat: cancel event tests
arifBurakDemiray Sep 28, 2023
605e6c7
Refactoring test and variable names
ArtursKadikis Sep 28, 2023
a8314c1
Refactoring tests
ArtursKadikis Sep 28, 2023
7d80e2f
Merge branch 'staging' into remake_events_module
ArtursKadikis Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
* "removeFromCohort(key)"

* In Countly class, the old "init(directory,config)" method is deprecated, use "init(config)" instead via "instance()" call.
* Deprecated "Countly::event" call, deprecated builder pattern. Use "Countly::events" instead.
* Deprecated "Usage::event" call, deprecated builder pattern. Use "Countly::events" instead.
* Deprecated "Countly::stop(boolean)" call, use "Countly::halt" instead via "instance()" call.

* The following methods are deprecated from the "Event" interface:
* "record"
* "endAndRecord"
* "addSegment"
* "addSegments"
* "setSegmentation"
* "setSum"
* "setCount"
* "setDuration"
* "isInvalid"

22.09.2
* Fixed internal log calls that did not respect the configured log level and did not work with the log listener.
Expand Down
74 changes: 37 additions & 37 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.google.gms:google-services:4.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
ext.CLY_VERSION = "23.8.0"
ext.POWERMOCK_VERSION = "1.7.4"
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
repositories {
google()
jcenter()
//mavenLocal()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
google()
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.google.gms:google-services:4.3.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
ext.CLY_VERSION = "23.8.0"
ext.POWERMOCK_VERSION = "1.7.4"

tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
repositories {
google()
jcenter()
//mavenLocal()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
39 changes: 37 additions & 2 deletions sdk-java/src/main/java/ly/count/sdk/java/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import java.io.File;
import java.util.Map;

import ly.count.sdk.java.internal.*;
import ly.count.sdk.java.internal.CtxCore;
import ly.count.sdk.java.internal.Device;
import ly.count.sdk.java.internal.InternalConfig;
import ly.count.sdk.java.internal.Log;
import ly.count.sdk.java.internal.ModuleBackendMode;
import ly.count.sdk.java.internal.ModuleEvents;
import ly.count.sdk.java.internal.SDKCore;

/**
* Main Countly SDK API class.
Expand Down Expand Up @@ -128,6 +133,7 @@ public static void init(final File sdkStorageRootDirectory, final Config config)
* Also clears all the data if called with {@code clearData = true}.
*
* @param clearData whether to clear all Countly data or not
* @deprecated use {@link #halt()} instead via instance() call
*/
public static void stop(boolean clearData) {
if (isInitialized()) {
Expand All @@ -144,6 +150,13 @@ public static void stop(boolean clearData) {
}
}

/**
* Stop Countly SDK. Stops all tasks and releases resources.
*/
public void halt() {
stop(true);
}

/**
* Returns whether Countly SDK has been already initialized or not.
*
Expand Down Expand Up @@ -311,12 +324,34 @@ public static void onConsentRemoval(Config.Feature... features) {
}
}

/**
* Record event with provided key.
*
* @param key key for this event, cannot be null or empty
* @return Builder object for this event
* @deprecated use {@link #events()} instead via instance() call
*/
@Override
public Event event(String key) {
L.d("[Countly] event: key = " + key);
return ((Session) sdk.session(ctx, null)).event(key);
}

/**
* Event module calls
*
* @return event module otherwise null if SDK is not initialized
*/
public ModuleEvents.Events events() {
if (!isInitialized()) {
if (L != null) {
L.e("[Countly] SDK is not initialized yet.");
}
return null;
}
return sdk.events();
}

@Override
public Event timedEvent(String key) {
L.d("[Countly] timedEvent: key = " + key);
Expand Down
10 changes: 10 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ly.count.sdk.java;

import ly.count.sdk.java.internal.ModuleEvents;
import javax.annotation.Nonnull;
import java.util.Map;

Expand All @@ -11,6 +12,7 @@ public interface Event {
/**
* Add event to the buffer, send it to the server in case number of events in the session
* is equal or bigger than {@link Config#eventsBufferSize} or wait until next {@link Session#update()}.
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
void record();

Expand All @@ -19,6 +21,7 @@ public interface Event {
* and current time in seconds. Then add the event to its session (if they're enabled),
* send it to the server in case number of events in the session is equal or bigger
* than {@link Config#eventsBufferSize} or wait until next {@link Session#update()}.
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#endEvent(String, Map, int, double)} instead
*/
void endAndRecord();

Expand All @@ -28,6 +31,7 @@ public interface Event {
* @param key key of segment, must not be null or empty
* @param value value of segment, must not be null or empty
* @return this instance for method chaining
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
Event addSegment(@Nonnull String key, @Nonnull String value);

Expand All @@ -38,6 +42,7 @@ public interface Event {
* segmentation from; cannot contain nulls or empty strings; must have
* even length
* @return this instance for method chaining
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
Event addSegments(@Nonnull String... segmentation);

Expand All @@ -46,6 +51,7 @@ public interface Event {
*
* @param segmentation map of segment pairs ({key1: value1, key2: value2}
* @return this instance for method chaining
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
Event setSegmentation(@Nonnull Map<String, String> segmentation);

Expand All @@ -54,6 +60,7 @@ public interface Event {
*
* @param count event count, cannot be 0
* @return this instance for method chaining
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
Event setCount(int count);

Expand All @@ -62,6 +69,7 @@ public interface Event {
*
* @param sum event sum
* @return this instance for method chaining
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
Event setSum(double sum);

Expand All @@ -70,6 +78,7 @@ public interface Event {
*
* @param duration event duration
* @return this instance for method chaining
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent(String, int, double, Map, double)} instead
*/
Event setDuration(double duration);

Expand All @@ -81,6 +90,7 @@ public interface Event {
* <li>Invalid data supplied (count < 0, NaN as sum, duration < 0, etc.) while in production mode</li>
* <li>Event has been already recorded in session, thus should be discarded</li>
* </ul>
* @deprecated this function is deprecated
*/
boolean isInvalid();
}
2 changes: 2 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/Usage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ly.count.sdk.java;

import ly.count.sdk.java.internal.ModuleEvents;
import java.util.Map;

/**
Expand All @@ -20,6 +21,7 @@ public interface Usage {
* @param key key for this event, cannot be null or empty
* @return Event instance.
* @see Event#record()
* @deprecated this function is deprecated, use {@link ModuleEvents.Events#recordEvent} instead
*/
Event event(String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public enum CoreFeature {
Sessions(1 << 1, ModuleSessions::new),
Events(1 << 2),
Events(1 << 2, ModuleEvents::new),
Views(1 << 3, ModuleViews::new),
CrashReporting(1 << 4, ModuleCrash::new),
Location(1 << 5),
Expand Down
16 changes: 16 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public interface EventRecorder {
*/
private boolean invalid = false;

EventImpl(@Nonnull String key, int count, Double sum, Double duration, @Nonnull Map<String, Object> segmentation, @Nonnull Log givenL) {
L = givenL;

this.recorder = null;
this.key = key;
this.count = count;
this.sum = sum;
this.duration = duration;
this.segmentation = segmentation;
this.timestamp = Device.dev.uniqueTimestamp();
this.hour = Device.dev.currentHour();
this.dow = Device.dev.currentDayOfWeek();
}



EventImpl(@Nonnull EventRecorder recorder, @Nonnull String key, @Nonnull Log givenL) {
L = givenL;
if (recorder == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ly.count.sdk.java.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class EventImplQueue {

static final String DELIMITER = ":::";
private final Log L;
private final InternalConfig config;
final List<EventImpl> eventQueueMemoryCache;

protected EventImplQueue(Log logger, InternalConfig config) {
L = logger;
this.config = config;
eventQueueMemoryCache = new ArrayList<>(config.getEventsBufferSize());
}

/**
* Returns the number of events currently stored in the queue.
*/
protected int eqSize() {
return eventQueueMemoryCache.size();
}

void addEvent(final EventImpl event) {
L.d("[EventImplQueue] Adding event: " + event.key);
if (eventQueueMemoryCache.size() < config.getEventsBufferSize()) {
arifBurakDemiray marked this conversation as resolved.
Show resolved Hide resolved
eventQueueMemoryCache.add(event);
writeEventQueueToStorage();
}
}

/**
* set the new value in event data storage
*/
void writeEventQueueToStorage() {
final String eventQueue = joinEvents(eventQueueMemoryCache);

L.d("[EventImplQueue] Setting event data: " + eventQueue);
SDKCore.instance.sdkStorage.storeEventQueue(eventQueue);
}

/**
* Restores events from disk
*/
void restore() {
arifBurakDemiray marked this conversation as resolved.
Show resolved Hide resolved
L.d("[EventImplQueue] Restoring events from disk");
eventQueueMemoryCache.clear();

final String[] array = getEvents();
for (String s : array) {

final EventImpl event = EventImpl.fromJSON(s, (ev) -> {
}, L);
if (event != null) {
eventQueueMemoryCache.add(event);
}
}
// order the events from least to most recent
eventQueueMemoryCache.sort((e1, e2) -> (int) (e1.timestamp - e2.timestamp));
}

private String joinEvents(final Collection<EventImpl> collection) {
final List<String> strings = new ArrayList<>();
for (EventImpl e : collection) {
strings.add(e.toJSON(L));
}
return Utils.join(strings, EventImplQueue.DELIMITER);
}

/**
* Returns an unsorted array of the current stored event JSON strings.
*/
private synchronized String[] getEvents() {
L.d("[EventImplQueue] Getting events from disk");
final String joinedEventsStr = SDKCore.instance.sdkStorage.readEventQueue();
return joinedEventsStr.isEmpty() ? new String[0] : joinedEventsStr.split(DELIMITER);
}

public void clear() {
SDKCore.instance.sdkStorage.storeEventQueue("");
eventQueueMemoryCache.clear();
}
}
Loading