<commit_before>class Stats
  def self.debug(message)
    puts(message) if ENV["DEBUG"]
  end

  def self.since(last_date)
    message = "\n"
    new_members = Membership.active.since(last_date)
    groups = new_members.group_by(&:kind)
    plans = MembershipPlan.all.values.sort_by(&:amount)

    plans.select{|plan| groups[plan.id] }.
      map{|plan| [plan, groups[plan.id]] }.
      each do |plan, group|
        message << "#{group.size} new #{plan.shortname} #{"member".pluralize(group.size)}"
        names = group.map(&:name).compact
        message << " including\n - #{names.join("\n - ")}" if names.any?
        message << "\n"
      end

    companies, people = new_members.partition(&:corporate?)
    message << "\n"
    message << "#{people.count} new #{"developer".pluralize(people.count)}\n"
    message << "#{companies.count} new #{"company".pluralize(companies.count)}\n"
    message << "#{new_members.size} new #{"member".pluralize(new_members.size)} total\n"
    message << "\n"

    counts = Membership.active.group(:kind).count.map do |id,c|
      [MembershipPlan.all[MembershipPlan.ids[id]], c]
    end.to_h
    corp, dev = counts.group_by{|s,c| s.id.start_with?("corporate") }.values
    message << "#{corp.map(&:last).inject(:+)} #{"company".pluralize(companies.count)} ("
    message << corp.sort_by{|s,c| -s.amount}.map{|s,c| "#{c} #{s.shortname}" }.join(", ")
    message << ")\n"

    message << "#{dev.map(&:last).inject(:+)} #{"developer".pluralize(people.count)} ("
    message << dev.sort_by{|s,c| -s.amount}.map{|s,c| "#{c} #{s.shortname}" }.join(", ")
    message << ")\n"
  end

  def self.expiring_annual_memberships
    # no need to look at memberships that are already expired.
    expiring_memberships = Membership.on_trial.where(
      "expires_at BETWEEN ? AND ?", Time.now, 1.month.from_now
    ).order("expires_at ASC")

    if expiring_memberships.any?
      message = "It appears that the following subscriptions are going to expire soon: \n"

      expiring_memberships.each do |membership|
        message << "*#{membership.name}* "
        message << "https://dashboard.stripe.com/customers/#{membership.user.stripe_id}\n"
        message << "Expires at: #{membership.expires_at.strftime("%A, %d %B %Y")}\n\n"
      end

      message<commit_msg>partition has reliable ordering, unlike group_by<commit_after>class Stats
  def self.debug(message)
    puts(message) if ENV["DEBUG"]
  end

  def self.since(last_date)
    message = "\n"
    new_members = Membership.active.since(last_date)
    groups = new_members.group_by(&:kind)
    plans = MembershipPlan.all.values.sort_by(&:amount)

    plans.select{|plan| groups[plan.id] }.
      map{|plan| [plan, groups[plan.id]] }.
      each do |plan, group|
        message << "#{group.size} new #{plan.shortname} #{"member".pluralize(group.size)}"
        names = group.map(&:name).compact
        message << " including\n - #{names.join("\n - ")}" if names.any?
        message << "\n"
      end

    companies, people = new_members.partition(&:corporate?)
    message << "\n"
    message << "#{people.count} new #{"developer".pluralize(people.count)}\n"
    message << "#{companies.count} new #{"company".pluralize(companies.count)}\n"
    message << "#{new_members.size} new #{"member".pluralize(new_members.size)} total\n"
    message << "\n"

    counts = Membership.active.group(:kind).count.map do |id,c|
      [MembershipPlan.all[MembershipPlan.ids[id]], c]
    end.to_h
    corp, dev = counts.partition{|s,c| s.id.start_with?("corporate") }
    message << "#{corp.map(&:last).inject(:+)} #{"company".pluralize(companies.count)} ("
    message << corp.sort_by{|s,c| -s.amount}.map{|s,c| "#{c} #{s.shortname}" }.join(", ")
    message << ")\n"

    message << "#{dev.map(&:last).inject(:+)} #{"developer".pluralize(people.count)} ("
    message << dev.sort_by{|s,c| -s.amount}.map{|s,c| "#{c} #{s.shortname}" }.join(", ")
    message << ")\n"
  end

  def self.expiring_annual_memberships
    # no need to look at memberships that are already expired.
    expiring_memberships = Membership.on_trial.where(
      "expires_at BETWEEN ? AND ?", Time.now, 1.month.from_now
    ).order("expires_at ASC")

    if expiring_memberships.any?
      message = "It appears that the following subscriptions are going to expire soon: \n"

      expiring_memberships.each do |membership|
        message << "*#{membership.name}* "
        message << "https://dashboard.stripe.com/customers/#{membership.user.stripe_id}\n"
        message << "Expires at: #{membership.expires_at.strftime("%A, %d %B %Y")}\n\n"
      end

      message
{'fraction_non_alphanumeric': 0.18583495776478232, 'fraction_numerical': 0.00043318171973142733, 'mean_word_length': 3.43611911623439, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '857129', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1590, 'n_words': 301}