-
-
Notifications
You must be signed in to change notification settings - Fork 109
Basic Bot Analytics System #1425
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8774945
Analytics service setup with first use in Ping command
firasrg 5002f75
Analytics: remove AnalyticsService injection in PingCommand and using…
firasrg 071c1dc
feat: update command usage analytics to use generated record methods
firasrg 25a4407
Analytics: applies changes for zabuzard 1st CR;
firasrg a27f89e
Analytics - Metrics > persist(): rename argument moment to happenedAt
firasrg 75618d5
Analytics update
firasrg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
application/src/main/java/org/togetherjava/tjbot/features/analytics/AnalyticsService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package org.togetherjava.tjbot.features.analytics; | ||
|
|
||
| import org.jetbrains.annotations.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.togetherjava.tjbot.db.Database; | ||
| import org.togetherjava.tjbot.db.generated.tables.CommandUsage; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| /** | ||
| * Service for tracking and recording command usage analytics. | ||
| * <p> | ||
| * This service records every command execution along with its success/failure status, allowing for | ||
| * analysis of command usage patterns, popular commands, error rates, and activity over time. | ||
| * <p> | ||
| * Commands should call {@link #recordCommandExecution(long, String, long, boolean, String)} after | ||
| * execution to track their usage. | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| */ | ||
| public final class AnalyticsService { | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| private static final Logger logger = LoggerFactory.getLogger(AnalyticsService.class); | ||
|
|
||
| private final Database database; | ||
|
|
||
| /** | ||
| * Creates a new instance. | ||
| * | ||
| * @param database the database to use for storing and retrieving analytics data | ||
| */ | ||
| public AnalyticsService(Database database) { | ||
| this.database = database; | ||
| } | ||
|
|
||
| /** | ||
| * Records a command execution with success/failure status. | ||
| * <p> | ||
| * This method should be called by commands after they complete execution to track usage | ||
| * patterns and error rates. | ||
| * | ||
| * @param channelId the channel ID where the command was executed | ||
| * @param commandName the name of the command that was executed | ||
| * @param userId the ID of the user who executed the command | ||
| * @param success whether the command executed successfully | ||
| * @param errorMessage optional error message if the command failed (null if successful) | ||
| */ | ||
| public void recordCommandExecution(long channelId, String commandName, long userId, | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| boolean success, @Nullable String errorMessage) { | ||
|
|
||
| database.write(context -> context.newRecord(CommandUsage.COMMAND_USAGE) | ||
| .setChannelId(channelId) | ||
| .setCommandName(commandName) | ||
| .setUserId(userId) | ||
| .setExecutedAt(Instant.now()) | ||
| .setSuccess(success) | ||
| .setErrorMessage(errorMessage) | ||
| .insert()); | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (!success && errorMessage != null) { | ||
| logger.warn("Command '{}' failed on channel {} with error: {}", commandName, channelId, | ||
| errorMessage); | ||
| } | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Records a successful command execution. | ||
| * | ||
| * @param channelId the channel ID where the command was executed | ||
| * @param commandName the name of the command that was executed | ||
| * @param userId the ID of the user who executed the command | ||
| */ | ||
| public void recordCommandSuccess(long channelId, String commandName, long userId) { | ||
| recordCommandExecution(channelId, commandName, userId, true, null); | ||
| } | ||
|
|
||
| /** | ||
| * Records a failed command execution. | ||
| * | ||
| * @param channelId the channel ID where the command was executed | ||
| * @param commandName the name of the command that was executed | ||
| * @param userId the ID of the user who executed the command | ||
| * @param errorMessage a description of what went wrong | ||
| */ | ||
| public void recordCommandFailure(long channelId, String commandName, long userId, | ||
| String errorMessage) { | ||
| recordCommandExecution(channelId, commandName, userId, false, errorMessage); | ||
| } | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| } | ||
13 changes: 13 additions & 0 deletions
13
application/src/main/java/org/togetherjava/tjbot/features/analytics/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * Analytics system for collecting and persisting bot activity metrics. | ||
| * <p> | ||
| * This package provides services and components that record events for later analysis and reporting | ||
| * across multiple feature areas, not limited to commands. | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| */ | ||
| @MethodsReturnNonnullByDefault | ||
| @ParametersAreNonnullByDefault | ||
| package org.togetherjava.tjbot.features.analytics; | ||
|
|
||
| import org.togetherjava.tjbot.annotations.MethodsReturnNonnullByDefault; | ||
|
|
||
| import javax.annotation.ParametersAreNonnullByDefault; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
application/src/main/resources/db/V16__Add_Analytics_System.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| CREATE TABLE command_usage | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| ( | ||
| 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 | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
| ); | ||
|
|
||
| CREATE INDEX idx_command_usage_channel ON command_usage(channel_id); | ||
| CREATE INDEX idx_command_usage_command_name ON command_usage(command_name); | ||
|
Zabuzard marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.