diff --git a/app/components/admin/dashboard/health_card_component.html.erb b/app/components/admin/dashboard/health_card_component.html.erb index 66c5a7329..a20ea1283 100644 --- a/app/components/admin/dashboard/health_card_component.html.erb +++ b/app/components/admin/dashboard/health_card_component.html.erb @@ -1,6 +1,6 @@
-

Chapter Health

+

Chapter Health

<% tiles.each do |tile| %>
@@ -19,6 +19,34 @@
<%= content %>
+ <% if new_sign_ups.any? %> +
+ New sign-ups (last 3 months) + + + + + + + + + + <% new_sign_ups.each do |row| %> + + + + + + <% end %> + + + + + + +
MonthStudentsCoaches
<%= sign_up_month_label(row[:month]) %><%= number_value(row[:students]) %><%= number_value(row[:coaches]) %>
Total<%= number_value(sign_up_totals[:students]) %><%= number_value(sign_up_totals[:coaches]) %>
+
+ <% end %>
diff --git a/app/components/admin/dashboard/health_card_component.rb b/app/components/admin/dashboard/health_card_component.rb index c6875ca7a..7d99daf52 100644 --- a/app/components/admin/dashboard/health_card_component.rb +++ b/app/components/admin/dashboard/health_card_component.rb @@ -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) @@ -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 diff --git a/app/controllers/admin/chapters_controller.rb b/app/controllers/admin/chapters_controller.rb index 9ccf4ac19..af10cc5ee 100644 --- a/app/controllers/admin/chapters_controller.rb +++ b/app/controllers/admin/chapters_controller.rb @@ -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 diff --git a/app/models/subscription.rb b/app/models/subscription.rb index d1914d696..c0bd155e2 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -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 diff --git a/app/presenters/chapter_sign_ups_presenter.rb b/app/presenters/chapter_sign_ups_presenter.rb new file mode 100644 index 000000000..7b78250d6 --- /dev/null +++ b/app/presenters/chapter_sign_ups_presenter.rb @@ -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 diff --git a/app/views/admin/chapters/show.html.haml b/app/views/admin/chapters/show.html.haml index 44607031a..c2d5e28dc 100644 --- a/app/views/admin/chapters/show.html.haml +++ b/app/views/admin/chapters/show.html.haml @@ -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 diff --git a/spec/components/admin/dashboard/health_card_component_spec.rb b/spec/components/admin/dashboard/health_card_component_spec.rb index 050205754..c53600b6d 100644 --- a/spec/components/admin/dashboard/health_card_component_spec.rb +++ b/spec/components/admin/dashboard/health_card_component_spec.rb @@ -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:) diff --git a/spec/controllers/admin/chapters_controller_spec.rb b/spec/controllers/admin/chapters_controller_spec.rb index 780d20b21..6b9045234 100644 --- a/spec/controllers/admin/chapters_controller_spec.rb +++ b/spec/controllers/admin/chapters_controller_spec.rb @@ -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 diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb index c66025310..d9e860ff4 100644 --- a/spec/models/subscription_spec.rb +++ b/spec/models/subscription_spec.rb @@ -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), diff --git a/spec/presenters/chapter_sign_ups_presenter_spec.rb b/spec/presenters/chapter_sign_ups_presenter_spec.rb new file mode 100644 index 000000000..2ee6eda51 --- /dev/null +++ b/spec/presenters/chapter_sign_ups_presenter_spec.rb @@ -0,0 +1,169 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ChapterSignUpsPresenter do + subject(:by_month) { described_class.new(chapter).by_month } + + around do |example| + travel_to(Time.zone.local(2026, 11, 6, 12, 0)) { example.run } + end + + let(:chapter) { Fabricate(:chapter) } + let(:students) { Fabricate(:group, name: 'Students', chapter:) } + let(:coaches) { Fabricate(:group, name: 'Coaches', chapter:) } + + def students_in(month) + by_month.find { |row| row[:month] == month }.fetch(:students) + end + + def coaches_in(month) + by_month.find { |row| row[:month] == month }.fetch(:coaches) + end + + it 'returns the last three months in ascending order' do + expect(by_month.map { |row| row[:month] }) + .to eq([Date.new(2026, 9, 1), Date.new(2026, 10, 1), Date.new(2026, 11, 1)]) + end + + it 'splits sign-ups into students and coaches' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 9, 10)) + Fabricate(:subscription, group: coaches, created_at: Time.zone.local(2026, 9, 11)) + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 11, 2)) + + expect(by_month).to eq([ + { month: Date.new(2026, 9, 1), students: 1, coaches: 1 }, + { month: Date.new(2026, 10, 1), students: 0, coaches: 0 }, + { month: Date.new(2026, 11, 1), students: 1, coaches: 0 } + ]) + end + + it 'ignores sign-ups from other chapters' do + other_chapter = Fabricate(:chapter_with_groups) + other_chapter.groups.each do |group| + Fabricate(:subscription, group:, created_at: Time.zone.local(2026, 10, 5)) + end + + expect(by_month.sum { |row| row[:students] + row[:coaches] }).to eq(0) + end + + it 'ignores sign-ups older than the window' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 8, 31, 23, 30)) + + expect(by_month.sum { |row| row[:students] }).to eq(0) + end + + it 'ignores sign-ups after the current month' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 12, 1, 0, 30)) + + expect(by_month.sum { |row| row[:students] }).to eq(0) + end + + it 'counts a sign-up on the first instant of the oldest month' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 9, 1, 0, 0)) + + expect(students_in(Date.new(2026, 9, 1))).to eq(1) + end + + it 'ignores rows dated after now' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 11, 6, 11, 59)) + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 11, 6, 12, 1)) + + expect(students_in(Date.new(2026, 11, 1))).to eq(1) + end + + it 'counts a member who left and rejoined in the same month once' do + member = Fabricate(:member) + Fabricate(:subscription, member:, group: students, created_at: Time.zone.local(2026, 9, 3), + discarded_at: Time.zone.local(2026, 9, 20)) + Fabricate(:subscription, member:, group: students, created_at: Time.zone.local(2026, 9, 25)) + + expect(students_in(Date.new(2026, 9, 1))).to eq(1) + end + + it 'counts a member who rejoined in a later month in both months' do + member = Fabricate(:member) + Fabricate(:subscription, member:, group: students, created_at: Time.zone.local(2026, 9, 3), + discarded_at: Time.zone.local(2026, 9, 20)) + Fabricate(:subscription, member:, group: students, created_at: Time.zone.local(2026, 10, 2)) + + expect(students_in(Date.new(2026, 9, 1))).to eq(1) + expect(students_in(Date.new(2026, 10, 1))).to eq(1) + end + + it 'matches group names case-insensitively' do + group = Fabricate(:group, name: 'Students', chapter:) + group.update_column(:name, 'students') + Fabricate(:subscription, group:, created_at: Time.zone.local(2026, 10, 5)) + + expect(students_in(Date.new(2026, 10, 1))).to eq(1) + end + + it 'ignores sign-ups from groups that are neither students nor coaches' do + mentors = Fabricate(:group, name: 'Students', chapter:) + mentors.update_column(:name, 'Mentors') + Fabricate(:subscription, group: mentors, created_at: Time.zone.local(2026, 10, 5)) + + expect(by_month.sum { |row| row[:students] + row[:coaches] }).to eq(0) + end + + it 'ignores a group without a name instead of raising' do + unnamed = Fabricate(:group, name: 'Students', chapter:) + unnamed.update_column(:name, nil) + Fabricate(:subscription, group: unnamed, created_at: Time.zone.local(2026, 10, 5)) + + expect(by_month.sum { |row| row[:students] + row[:coaches] }).to eq(0) + end + + it 'starts the window at the first moment of the oldest month in the chapter zone' do + berlin = Fabricate(:chapter, time_zone: 'Berlin') + group = Fabricate(:group, name: 'Students', chapter: berlin) + # 23:30 in London on 31 August is 00:30 in Berlin on 1 September. + Fabricate(:subscription, group:, created_at: Time.zone.local(2026, 8, 31, 23, 30)) + + rows = described_class.new(berlin).by_month.to_h { |row| [row[:month], row[:students]] } + + expect(rows[Date.new(2026, 9, 1)]).to eq(1) + end + + it 'falls back to the app time zone when the chapter time zone no longer resolves' do + chapter.update_column(:time_zone, 'Invalid/Zone') + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 10, 5)) + + expect(students_in(Date.new(2026, 10, 1))).to eq(1) + end + + it 'counts a sign-up in the first hour of a BST month in that month' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 10, 1, 0, 30)) + + expect(students_in(Date.new(2026, 10, 1))).to eq(1) + expect(students_in(Date.new(2026, 9, 1))).to eq(0) + end + + it 'counts a churned member in the month they signed up' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 9, 10), + discarded_at: Time.zone.local(2026, 10, 3)) + + expect(students_in(Date.new(2026, 9, 1))).to eq(1) + expect(students_in(Date.new(2026, 10, 1))).to eq(0) + end + + it 'ignores backfilled tombstones, which carry the unsubscribe time in created_at' do + Fabricate(:subscription, group: students, created_at: Time.zone.local(2026, 10, 3), + discarded_at: Time.zone.local(2026, 10, 3)) + + expect(by_month.sum { |row| row[:students] }).to eq(0) + end + + it 'buckets by the chapter time zone rather than the app time zone' do + berlin = Fabricate(:chapter, time_zone: 'Berlin') + group = Fabricate(:group, name: 'Students', chapter: berlin) + # 23:30 in London on 30 September is 00:30 in Berlin on 1 October. + Fabricate(:subscription, group:, created_at: Time.zone.local(2026, 9, 30, 23, 30)) + + rows = described_class.new(berlin).by_month.to_h { |row| [row[:month], row[:students]] } + + expect(rows[Date.new(2026, 10, 1)]).to eq(1) + expect(rows[Date.new(2026, 9, 1)]).to eq(0) + end +end