FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
# AI usage statistics from discourse-ai plugin
|
|
|
|
|
# Shows total usage, favorite features, token consumption, etc.
|
2025-11-26 12:24:07 -03:00
|
|
|
# Uses AiApiRequestStat for efficient aggregation queries
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
module DiscourseRewind
|
|
|
|
|
module Action
|
|
|
|
|
class AiUsage < BaseReport
|
|
|
|
|
def call
|
|
|
|
|
return if !enabled?
|
|
|
|
|
|
2025-11-26 12:24:07 -03:00
|
|
|
base_query = AiApiRequestStat.where(user_id: user.id).where(bucket_date: date)
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
|
|
|
|
|
# Get aggregated stats in a single query
|
|
|
|
|
stats =
|
|
|
|
|
base_query.select(
|
2025-11-26 12:24:07 -03:00
|
|
|
"COALESCE(SUM(usage_count), 0) as total_requests",
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
"COALESCE(SUM(request_tokens), 0) as total_request_tokens",
|
|
|
|
|
"COALESCE(SUM(response_tokens), 0) as total_response_tokens",
|
2025-11-26 12:24:07 -03:00
|
|
|
"COALESCE(SUM(CASE WHEN response_tokens > 0 THEN usage_count ELSE 0 END), 0) as successful_requests",
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
).take
|
|
|
|
|
|
|
|
|
|
return if stats.total_requests == 0
|
|
|
|
|
|
|
|
|
|
total_tokens = stats.total_request_tokens + stats.total_response_tokens
|
|
|
|
|
success_rate =
|
|
|
|
|
(
|
|
|
|
|
if stats.total_requests > 0
|
|
|
|
|
(stats.successful_requests.to_f / stats.total_requests * 100).round(1)
|
|
|
|
|
else
|
|
|
|
|
0
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Most used features (top 5)
|
|
|
|
|
feature_usage =
|
|
|
|
|
base_query
|
|
|
|
|
.group(:feature_name)
|
2025-11-26 12:24:07 -03:00
|
|
|
.order("SUM(usage_count) DESC")
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
.limit(5)
|
2025-11-26 12:24:07 -03:00
|
|
|
.pluck(:feature_name, Arel.sql("SUM(usage_count)"))
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
.to_h
|
|
|
|
|
|
|
|
|
|
# Most used AI model (top 5)
|
|
|
|
|
model_usage =
|
|
|
|
|
base_query
|
|
|
|
|
.where.not(language_model: nil)
|
|
|
|
|
.group(:language_model)
|
2025-11-26 12:24:07 -03:00
|
|
|
.order("SUM(usage_count) DESC")
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
.limit(5)
|
2025-11-26 12:24:07 -03:00
|
|
|
.pluck(:language_model, Arel.sql("SUM(usage_count)"))
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
.to_h
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
data: {
|
|
|
|
|
total_requests: stats.total_requests,
|
|
|
|
|
total_tokens: total_tokens,
|
|
|
|
|
request_tokens: stats.total_request_tokens,
|
|
|
|
|
response_tokens: stats.total_response_tokens,
|
|
|
|
|
feature_usage: feature_usage,
|
|
|
|
|
model_usage: model_usage,
|
|
|
|
|
success_rate: success_rate,
|
|
|
|
|
},
|
|
|
|
|
identifier: "ai-usage",
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def enabled?
|
2025-11-26 12:24:07 -03:00
|
|
|
defined?(AiApiRequestStat) && SiteSetting.discourse_ai_enabled
|
FEATURE: Add seven new metrics to Discourse Rewind reports (#26)
## Summary
Adds seven new metrics to the Discourse Rewind plugin, significantly expanding the types of insights users can get about their annual
activity. These new reports cover temporal patterns, community engagement, plugin integrations, and content analysis.
## New Reports
### 1. Time of Day Activity (`time-of-day-activity`)
- Analyzes user activity by hour in their timezone
- Determines personality type: "early bird" (6-9am), "night owl" (10pm-2am), or "balanced"
- Aggregates posts, chat messages, and page views
### 2. New User Interactions (`new-user-interactions`)
- Tracks veteran mentorship and community building behavior
- Measures likes, replies, and mentions to users who joined this year
- Shows unique new users interacted with and total engagement
### 3. Chat Usage (`chat-usage`)
- Total messages and average message length
- Top 5 favorite channels with message counts
- DM statistics (message count, unique conversations)
- Reactions received on messages
### 4. AI Usage (`ai-usage`)
- Integrates with `discourse-ai` plugin
- Total requests, tokens consumed (request/response breakdown)
- Top 5 most used features and AI models
- Success rate calculation
### 5. Favorite GIFs (`favorite-gifs`)
- Extracts GIFs from posts and chat messages
- Ranks by engagement score (usage × 10 + likes + reactions)
- Supports Giphy, Tenor, and direct GIF URLs
- Shows top 5 GIFs with usage statistics
### 6. Assignments (`assignments`)
- Integrates with `discourse-assign` plugin
- Tracks assignments received and given
- Shows completion rate and pending assignments
### 7. Invites (`invites`)
- Total invites sent and redemption rate
- Impact metrics: invitee posts, topics, and likes created
- Most active invitee identification
- Average trust level of invitees
## Technical Details
- All reports extend `BaseReport` class
- Include proper enablement checks for plugin dependencies
- Use efficient database queries with proper joins and aggregations
- Return `nil` when no relevant data exists
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
2025-10-30 11:18:35 -03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|