Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion app/components/admin/dashboard/health_card_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="card border-info mb-4">
<div class="card-body">
<h3>Chapter Health</h3>
<h3 class="mb-4">Chapter Health</h3>
<div class="row">
<% tiles.each do |tile| %>
<div class="col-6 col-md-3 text-center mb-2">
Expand All @@ -19,6 +19,34 @@
<div class="col-12">
<%= content %>
</div>
<% if new_sign_ups.any? %>
<div class="col-12 mt-4">
<strong class="small mb-2 d-block">New sign-ups (last 3 months)</strong>
<table id="chapter-new-sign-ups" class="table table-sm align-middle mb-0">
<thead>
<tr>
<th scope="col" class="small fw-normal text-muted">Month</th>
<th scope="col" class="small fw-normal text-muted text-end">Students</th>
<th scope="col" class="small fw-normal text-muted text-end">Coaches</th>
</tr>
</thead>
<tbody>
<% new_sign_ups.each do |row| %>
<tr>
<th scope="row" class="fw-normal"><%= sign_up_month_label(row[:month]) %></th>
<td class="text-end"><%= number_value(row[:students]) %></td>
<td class="text-end"><%= number_value(row[:coaches]) %></td>
</tr>
<% end %>
<tr class="fw-bold">
<th scope="row">Total</th>
<td class="text-end"><%= number_value(sign_up_totals[:students]) %></td>
<td class="text-end"><%= number_value(sign_up_totals[:coaches]) %></td>
</tr>
</tbody>
</table>
</div>
<% end %>
</div>
</div>
</div>
14 changes: 12 additions & 2 deletions app/components/admin/dashboard/health_card_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class HealthCardComponent < ViewComponent::Base
BUCKET_CLASSES = { inactive: 'bg-secondary', dormant: 'bg-warning text-dark',
active: 'bg-success' }.freeze

def initialize(health:) # rubocop:disable Lint/MissingSuper
def initialize(health:, new_sign_ups: []) # rubocop:disable Lint/MissingSuper
@health = health
@new_sign_ups = new_sign_ups
end

private

attr_reader :health
attr_reader :health, :new_sign_ups

def bucket_badge_class = BUCKET_CLASSES.fetch(health.bucket)

Expand Down Expand Up @@ -54,6 +55,15 @@ def eligible_members_value
number_value(health.eligible_students + health.eligible_coaches)
end

def sign_up_month_label(month)
month.strftime('%b %Y')
end

def sign_up_totals
{ students: new_sign_ups.sum { |row| row[:students] },
coaches: new_sign_ups.sum { |row| row[:coaches] } }
end

def number_value(value)
number_with_delimiter(value)
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/admin/chapters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def show
@groups = @chapter.groups
@subscribers = @chapter.subscriptions.kept.last(20).reverse
@how_you_found_us = HowYouFoundUsPresenter.new(@chapter)
@new_sign_ups = ChapterSignUpsPresenter.new(@chapter).by_month
end

def edit
Expand Down
6 changes: 6 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Subscription < ApplicationRecord
# (index_subscriptions_on_member_id_group_id_active) enforces this at the DB level.
validates :group, uniqueness: { scope: :member_id, conditions: -> { kept } }

# Tombstone rows created by SubscriptionTombstoneBackfill carry the
# unsubscribe time in created_at, so they are not sign-ups for that month.
# Genuine churned subscriptions keep their own created_at and do count.
scope :excluding_backfilled_tombstones,
-> { where('subscriptions.discarded_at IS NULL OR subscriptions.discarded_at <> subscriptions.created_at') }

def student?
group.name.casecmp('students').zero?
end
Expand Down
73 changes: 73 additions & 0 deletions app/presenters/chapter_sign_ups_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

# Monthly new sign-ups for a chapter, split into its Students and Coaches groups.
#
# Months are bucketed in the chapter's own time zone, and the timestamps are
# converted in Ruby: the database session time zone is not pinned, so doing the
# arithmetic in SQL shifts sign-ups made in the first hour of a BST month into
# the previous month. A member counts once per group per month, so leaving and
# rejoining within a month does not count twice.
class ChapterSignUpsPresenter
MONTHS = 3
GROUPS = { 'students' => :students, 'coaches' => :coaches }.freeze

def initialize(chapter)
@chapter = chapter
end

def by_month
counts = counts_by_month_and_group
month_starts.map do |month|
{ month:,
students: counts.fetch([month, :students], 0),
coaches: counts.fetch([month, :coaches], 0) }
end
end

private

attr_reader :chapter

# Legacy rows can carry a zone string that no longer resolves.
def zone
ActiveSupport::TimeZone[chapter.time_zone] || Time.zone
end

def now
@now ||= Time.current.in_time_zone(zone)
end

def month_starts
@month_starts ||= (0...MONTHS).map { |n| (now - n.months).to_date.beginning_of_month }.reverse
end

# Ends at now, not at the end of the month: a row with a future created_at
# is not a sign-up yet.
def window
month_starts.first.in_time_zone(zone)...now
end

def counts_by_month_and_group
members = Hash.new { |hash, key| hash[key] = Set.new }
subscriptions_in_window.each do |member_id, signed_up_at, group_name|
column = GROUPS[group_name.to_s.downcase]
next unless column

members[[month_of(signed_up_at), column]] << member_id
end
members.transform_values(&:size)
end

def month_of(time)
time.in_time_zone(zone).to_date.beginning_of_month
end

def subscriptions_in_window
Subscription
.excluding_backfilled_tombstones
.where(created_at: window)
.joins(:group)
.where(groups: { chapter_id: chapter.id })
.pluck(:member_id, :created_at, 'groups.name')
end
end
2 changes: 1 addition & 1 deletion app/views/admin/chapters/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
%div.text-muted.small= "Based on #{pluralize(@how_you_found_us.total_responses, 'response')}"

.col-12.col-lg-8
= render Admin::Dashboard::HealthCardComponent.new(health: @chapter_health) do
= render Admin::Dashboard::HealthCardComponent.new(health: @chapter_health, new_sign_ups: @new_sign_ups) do
= render Admin::Dashboard::WorkshopTimelineComponent.new(past: @past_workshop_dates, future: @planned_workshop_dates, event_past: @past_event_dates, event_future: @planned_event_dates)
.mb-4.mt-md-4.mt-lg-0
.d-md-flex.justify-content-between.align-items-center
Expand Down
26 changes: 26 additions & 0 deletions spec/components/admin/dashboard/health_card_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@
expect(page).to have_css('.badge', text: 'inactive')
end

it 'renders new sign-ups split across the last 3 months' do
health = Admin::Dashboard::ChapterHealth.row(chapter:)
sign_ups = [
{ month: Date.new(2026, 9, 1), students: 5, coaches: 3 },
{ month: Date.new(2026, 10, 1), students: 8, coaches: 4 },
{ month: Date.new(2026, 11, 1), students: 2, coaches: 1 }
]

render_inline(described_class.new(health:, new_sign_ups: sign_ups))

expect(page).to have_text('New sign-ups (last 3 months)')
expect(page).to have_css('th', text: 'Sep 2026')
expect(page).to have_css('th', text: 'Oct 2026')
expect(page).to have_css('th', text: 'Nov 2026')
expect(page).to have_css('tr', text: /Sep 2026\s+5\s+3/)
expect(page).to have_css('tr.fw-bold', text: /Total\s+15\s+8/)
end

it 'hides the sign-up table when given no rows' do
health = Admin::Dashboard::ChapterHealth.row(chapter:)

render_inline(described_class.new(health:))

expect(page).to have_text('New sign-ups (last 3 months)', count: 0)
end

it 'renders the yielded content inside the card' do
health = Admin::Dashboard::ChapterHealth.row(chapter:)

Expand Down
53 changes: 53 additions & 0 deletions spec/controllers/admin/chapters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,58 @@
expect(response).to be_successful
expect(response.body).to include('Chapter Health')
end

it 'shows new sign-ups split across the last 3 months' do
travel_to(Time.zone.local(2026, 11, 6, 12, 0)) do
login_as_admin(Fabricate(:member))
chapter = Fabricate(:chapter)
students = Fabricate(:group, name: 'Students', chapter:)
coaches = Fabricate(:group, name: 'Coaches', chapter:)
Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 9, 10))
Fabricate(:subscription, group: coaches, created_at: Time.zone.local(2026, 10, 10))
Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 11, 2))
Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 8, 20))

get :show, params: { id: chapter.id }

expect(response.body).to include('New sign-ups (last 3 months)')
expected = [
{ month: Date.new(2026, 9, 1), students: 1, coaches: 0 },
{ month: Date.new(2026, 10, 1), students: 0, coaches: 1 },
{ month: Date.new(2026, 11, 1), students: 1, coaches: 0 }
]
expect(controller.view_assigns['new_sign_ups']).to eq(expected)

table = Capybara.string(response.body).find('table#chapter-new-sign-ups')
expect(table.text.squish).to include('Sep 2026', 'Oct 2026', 'Nov 2026')
expect(table.find('tr.fw-bold').text.squish).to eq('Total 2 1')
end
end

it 'renders zero rows for a chapter without sign-ups' do
travel_to(Time.zone.local(2026, 11, 6, 12, 0)) do
login_as_admin(Fabricate(:member))
chapter = Fabricate(:chapter)

get :show, params: { id: chapter.id }

table = Capybara.string(response.body).find('table#chapter-new-sign-ups')
expect(table.find('tr.fw-bold').text.squish).to eq('Total 0 0')
end
end

it 'does not count backfilled tombstones as sign-ups' do
travel_to(Time.zone.local(2026, 11, 6, 12, 0)) do
login_as_admin(Fabricate(:member))
chapter = Fabricate(:chapter)
students = Fabricate(:group, name: 'Students', chapter:)
removed_at = Time.zone.local(2026, 10, 3)
Fabricate(:subscription, group: students, created_at: removed_at, discarded_at: removed_at)

get :show, params: { id: chapter.id }

expect(controller.view_assigns['new_sign_ups'].sum { |row| row[:students] }).to eq(0)
end
end
end
end
12 changes: 12 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
end

describe 'history scopes' do
it 'excluding_backfilled_tombstones drops rows dated at their unsubscribe time' do
removed_at = 3.days.ago
backfilled = Fabricate(:discarded_subscription, created_at: removed_at, discarded_at: removed_at)
churned = Fabricate(:discarded_subscription, created_at: 2.months.ago, discarded_at: 3.days.ago)
active = Fabricate(:subscription)

scoped = described_class.excluding_backfilled_tombstones

expect(scoped).to include(active, churned)
expect(scoped).not_to include(backfilled)
end

it 'kept excludes tombstones while all includes them' do
subscription = Fabricate(:subscription)
tombstone = Fabricate(:discarded_subscription, member: Fabricate(:member),
Expand Down
Loading
Loading