Skip to content

Basic Bot Analytics System#1425

Open
firasrg wants to merge 5 commits intodevelopfrom
firasrg/analytics
Open

Basic Bot Analytics System#1425
firasrg wants to merge 5 commits intodevelopfrom
firasrg/analytics

Conversation

@firasrg
Copy link
Contributor

@firasrg firasrg commented Mar 2, 2026

Early implementation of basic analytics for TJ Bot

What’s done so far:

  • NEW: AnalyticsService for analytics Logic and persistence in Database (V16 DB Migration included);
  • Using onSlashCommandInteraction API to record slash commands;

/ping slash command use saved

image

UPDATE 06/03/2025

Following @Zabuzard CR, the analytics feature is no more specified for slash commands. Now, it's open to track any other sort of events!

Demo: the new table structure
image

@@ -0,0 +1,13 @@
CREATE TABLE command_usage
Copy link
Member

Choose a reason for hiding this comment

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

rename table to metrics or metric_events

Copy link
Member

Choose a reason for hiding this comment

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

since u called it Metrics please also call the table metrics or metric_events, not analytics, thanks.

@firasrg firasrg force-pushed the firasrg/analytics branch from f56996b to 25a4407 Compare March 6, 2026 20:59
error_message TEXT
id INTEGER PRIMARY KEY AUTOINCREMENT,
event TEXT NOT NULL,
happened_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Copy link
Member

Choose a reason for hiding this comment

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

please remove the default, happened_at is mandatory to set, it shouldnt be defaulted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see why not use DEFAULT CURRENT_TIMESTAMP here; this can be helpful sometimes, like to avoid repetitive code and bypass the necessity to instantiate Instant object from the Java code

Copy link
Member

Choose a reason for hiding this comment

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

Because the event never happened at the moment of insertion into the DB but at the moment the caller of the count method says.
If thr default timestamp would be used it would always be incorrect. Remember that this process is async. Saving the event into the DB could happen ten minutes after count was called.

* @param event the event to save
* @param moment the moment when the event is dispatched
*/
private void persist(String event, Instant moment) {
Copy link
Member

Choose a reason for hiding this comment

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

the name isnt ideal since this method might do more than just persisting. thats also an implementation-detail that might change.
rename it to processEvent instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in fact, I found it not ideal too to put processEvent as a name, since the event is being processed in count(), and the role of this method is only about save/persist in db

Copy link
Member

Choose a reason for hiding this comment

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

Your understanding of the methods role is incorrect. the count method doesnt do any processing. it merely forwards the call immediately to the service where it will be processed async to not block the caller.

Instant moment = Instant.now();
service.submit(() -> persist(event, moment));

logger.debug("Event {} new record saved successfully", event);
Copy link
Member

Choose a reason for hiding this comment

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

that log is misleading. the call is async so at this point nothing might have happened yet. remove that log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh that's right, mb

* @param event the event to save
* @param moment the moment when the event is dispatched
*/
private void persist(String event, Instant moment) {
Copy link
Member

Choose a reason for hiding this comment

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

rename moment to happenedAt

* @param moment the moment when the event is dispatched
*/
private void persist(String event, Instant moment) {
logger.debug("Persisting event: {}, at {}", event, moment);
Copy link
Member

Choose a reason for hiding this comment

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

not sure why ur adding all these debug logs everywhere, we already have debug-level logs on the database side if needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's for debugging in java code

.setEvent(event)
.setHappenedAt(moment)
.insert());
logger.debug("Event {} persisted successfully", event);
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as I said

@firasrg firasrg marked this pull request as ready for review March 10, 2026 18:27
@firasrg firasrg requested a review from a team as a code owner March 10, 2026 18:27
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants