Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public void count(String event) {
count(event, Map.of());
}

/**
* Track an event execution with dimensions provided.
*
* @param event the event to save
* @param dimensions the dimensions to save
Comment on lines +48 to +51
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please dont change the javadoc, it was this:

     * Track an event execution with additional contextual data.
     *
     * @param event the name of the event to record (e.g. "user_signup", "purchase")
     * @param dimensions optional key-value pairs providing extra context about the event. These are
     *        often referred to as "metadata" and can include things like: userId: "12345", name:
     *        "John Smith", channel_name: "chit-chat" etc. This data helps with filtering, grouping,
     *        and analyzing events later. Note: A value for a metric should be a Java primitive
     *        (String, int, double, long float).
     *

Copy link
Copy Markdown
Contributor Author

@firasrg firasrg Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was absolutely no javadoc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was. The method that was previously public facing is now not public facing anymore and the method you made public facing has a different, less detailled, javadoc:
grafik

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright then, all i have to do is to make the new one has better javadoc and done

*/
public void count(String event, Map<String, Object> dimensions) {
count(event, dimensions, true);
}

/**
* Track an event execution with additional contextual data.
*
Expand All @@ -53,15 +63,21 @@ public void count(String event) {
* "John Smith", channel_name: "chit-chat" etc. This data helps with filtering, grouping,
* and analyzing events later. Note: A value for a metric should be a Java primitive
* (String, int, double, long float).
* @param doAsync A flag to enable/disable async event process.
*/
public void count(String event, Map<String, Object> dimensions) {
void count(String event, Map<String, Object> dimensions, boolean doAsync) {
logger.debug("Counting new record for event: {}", event);

Instant happenedAt = Instant.now();
String serializedDimensions = serializeDimensions(dimensions);
String serializedDimensions = dimensions.isEmpty() ? null : serializeDimensions(dimensions);

Runnable task = () -> processEvent(event, happenedAt, serializedDimensions);

service.submit(() -> processEvent(event, happenedAt,
dimensions.isEmpty() ? null : serializedDimensions));
if (doAsync) {
service.submit(task);
} else {
task.run();
}
}

private static String serializeDimensions(Map<String, Object> dimensions) {
Expand All @@ -73,6 +89,7 @@ private static String serializeDimensions(Map<String, Object> dimensions) {
}

/**
* Process event persistence.
*
* @param event the event to save
* @param happenedAt the moment when the event is dispatched
Expand All @@ -85,5 +102,4 @@ private void processEvent(String event, Instant happenedAt, @Nullable String dim
.setDimensions(dimensionsJson)
.insert());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.togetherjava.tjbot.features.analytics;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.togetherjava.tjbot.db.Database;
import org.togetherjava.tjbot.db.generated.tables.MetricEvents;
import org.togetherjava.tjbot.db.generated.tables.records.MetricEventsRecord;

import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

final class MetricsTests {
private Database database;
private Metrics metrics;

@BeforeEach
void setUp() {
database = Database.createMemoryDatabase(MetricEvents.METRIC_EVENTS);
metrics = new Metrics(database);
}

@Test
void countWithDoAsyncFalsePersists() {

String expectedEvent = "test-event";

Instant expectedHappenedAt = Instant.now();

metrics.count(expectedEvent, Map.of(), false);

MetricEventsRecord eventRecord = Objects.requireNonNull(
database.read(context -> context.selectFrom(MetricEvents.METRIC_EVENTS).fetchOne()),
"event not found");

Instant actualHappenedAt = eventRecord.getHappenedAt();

assertEquals(expectedEvent, eventRecord.getEvent());
assertCloseEnough(expectedHappenedAt, actualHappenedAt);
}

private void assertCloseEnough(Instant expectedHappenedAt, Instant actualHappenedAt) {

Duration thresholdDuration = Duration.ofMinutes(1);

Duration actualDuration = Duration.between(expectedHappenedAt, actualHappenedAt);

boolean isBelowThreshold = actualDuration.compareTo(thresholdDuration) < 0;

assertTrue(isBelowThreshold);
}
}
Loading