Skip to content

Basic Bot Analytics System#1425

Draft
firasrg wants to merge 4 commits intodevelopfrom
firasrg/analytics
Draft

Basic Bot Analytics System#1425
firasrg wants to merge 4 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

private static final ExecutorService COMMAND_SERVICE = Executors.newCachedThreadPool();
private static final ScheduledExecutorService ROUTINE_SERVICE =
Executors.newScheduledThreadPool(5);
private final Config config;
Copy link
Member

Choose a reason for hiding this comment

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

dont remove that

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 not being used ?

* @param config the configuration to use for this system
*/
public BotCore(JDA jda, Database database, Config config) {
this.config = config;
Copy link
Member

Choose a reason for hiding this comment

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

dont remove that

Comment on lines +102 to +103
// Initialize analytics service
analyticsService = new AnalyticsService(database);
Copy link
Member

Choose a reason for hiding this comment

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

BotCore shouldnt own AnalyticsService. especially bc its also needed in Features.

Create it in the class one above both and then distribute it there properly. I.e. create it in Application.java next to the Config and then give it to BotCore through constructor and also to Features the same way as Config is passed around

* Commands should call {@link #recordCommandExecution(long, String, long, boolean, String)} after
* execution to track their usage.
*/
public final class AnalyticsService {
Copy link
Member

Choose a reason for hiding this comment

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

rename to Metrics

import java.time.Instant;

/**
* Service for tracking and recording command usage analytics.
Copy link
Member

Choose a reason for hiding this comment

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

its not only command usage but general analytics. so Service for tracking and recording events for analytics purposes

this.config = config;
Collection<Feature> features = Features.createFeatures(jda, database, config);

// Initialize analytics service
Copy link
Member

Choose a reason for hiding this comment

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

remove useless comment

Comment on lines +386 to +401
try {
requireUserInteractor(UserInteractionType.SLASH_COMMAND.getPrefixedName(name),
SlashCommand.class)
.onSlashCommand(event));
.onSlashCommand(event);


analyticsService.recordCommandSuccess(event.getChannel().getIdLong(), name,
event.getUser().getIdLong());
} catch (Exception ex) {

analyticsService.recordCommandFailure(event.getChannel().getIdLong(), name,
event.getUser().getIdLong(),
ex.getMessage() != null ? ex.getMessage() : ex.getClass().getSimpleName());

throw ex;
}
Copy link
Member

Choose a reason for hiding this comment

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

  • remove that try-catch stuff.
  • log before the slash command is called, not after, i.e. write sth like:
COMMAND_SERVICE.execute(() -> {
  UserInteractor interactor = requireUserInteractor(...);
  metrics.count("slash-" + name);
  interactor.onSlashCommand(event);
});

@@ -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

Comment on lines +3 to +9
id INTEGER PRIMARY KEY AUTOINCREMENT,
channel_id BIGINT NOT NULL,
command_name TEXT NOT NULL,
user_id BIGINT NOT NULL,
executed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
success BOOLEAN NOT NULL DEFAULT TRUE,
error_message TEXT
Copy link
Member

Choose a reason for hiding this comment

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

remove, all we want is:

id          INTEGER   NOT NULL PRIMARY KEY AUTOINCREMENT,
event       TEXT      NOT NULL,
happened_at TIMESTAMP NOT NULL

Comment on lines +12 to +13
CREATE INDEX idx_command_usage_channel ON command_usage(channel_id);
CREATE INDEX idx_command_usage_command_name ON command_usage(command_name);
Copy link
Member

Choose a reason for hiding this comment

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

remove

@firasrg firasrg force-pushed the firasrg/analytics branch from f56996b to 25a4407 Compare March 6, 2026 20:59
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 6, 2026

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