Conversation
application/src/main/java/org/togetherjava/tjbot/features/system/BotCore.java
Show resolved
Hide resolved
application/src/main/java/org/togetherjava/tjbot/features/system/BotCore.java
Show resolved
Hide resolved
| private static final ExecutorService COMMAND_SERVICE = Executors.newCachedThreadPool(); | ||
| private static final ScheduledExecutorService ROUTINE_SERVICE = | ||
| Executors.newScheduledThreadPool(5); | ||
| private final Config config; |
There was a problem hiding this comment.
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; |
| // Initialize analytics service | ||
| analyticsService = new AnalyticsService(database); |
There was a problem hiding this comment.
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 { |
| import java.time.Instant; | ||
|
|
||
| /** | ||
| * Service for tracking and recording command usage analytics. |
There was a problem hiding this comment.
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 |
| 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; | ||
| } |
There was a problem hiding this comment.
- 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 | |||
There was a problem hiding this comment.
rename table to metrics or metric_events
| 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 |
There was a problem hiding this comment.
remove, all we want is:
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
event TEXT NOT NULL,
happened_at TIMESTAMP NOT NULL| CREATE INDEX idx_command_usage_channel ON command_usage(channel_id); | ||
| CREATE INDEX idx_command_usage_command_name ON command_usage(command_name); |
f56996b to
25a4407
Compare
|



Early implementation of basic analytics for TJ Bot
What’s done so far:
AnalyticsServicefor analytics Logic and persistence in Database (V16 DB Migration included);/pingslash command use savedUPDATE 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
