discourse-rewind/spec/services/fetch_reports_spec.rb
Rafael dos Santos Silva 93b37e4069
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

60 lines
1.7 KiB
Ruby

# frozen_string_literal: true
RSpec.describe(DiscourseRewind::FetchReports) do
describe ".call" do
subject(:result) { described_class.call(**dependencies) }
fab!(:current_user, :user)
let(:guardian) { Guardian.new(current_user) }
let(:dependencies) { { guardian: } }
context "when in january" do
before { freeze_time DateTime.parse("2021-01-22") }
it "computes the correct previous year" do
expect(result.year).to eq(2020)
end
end
context "when in december" do
before { freeze_time DateTime.parse("2021-12-22") }
it "computes the correct previous year" do
expect(result.year).to eq(2021)
end
end
context "when out of valid months december" do
before { freeze_time DateTime.parse("2021-02-22") }
it { is_expected.to fail_to_find_a_model(:year) }
end
context "when reports is cached" do
before { freeze_time DateTime.parse("2021-12-22") }
it "returns the cached reports" do
expect(result.reports.length).to eq(16)
allow(DiscourseRewind::Action::TopWords).to receive(:call)
expect(result.reports.length).to eq(16)
expect(DiscourseRewind::Action::TopWords).to_not have_received(:call)
end
end
context "when reports is not cached" do
before do
freeze_time DateTime.parse("2021-01-22")
Discourse.redis.del("rewind:#{current_user.username}:2020")
end
it "returns the reports" do
allow(DiscourseRewind::Action::TopWords).to receive(:call)
expect(result.reports.length).to eq(16)
expect(DiscourseRewind::Action::TopWords).to have_received(:call)
end
end
end
end