<commit_before>module BountyStatsHelper

  require 'date'

  def avg_bounty_lifespan(startd=nil, endd=nil)
    query = "SELECT avg(closed_at - created_at) AS avg_life FROM wips WHERE wips.type IN ('Task') AND wips.state = 'resolved'"
    if !startd.nil? && !endd.nil?
      startd = startd.to_time
      endd = endd.to_time
      query += " AND (wips.created_at BETWEEN '#{startd}' AND '#{endd}')"
    end
    Wip.connection.execute(query).first
  end

  def created(user_type = nil, week_range = 1, history = 4)
    stat_helper('created', week_range, history, user_type)
  end

  def awarded(user_type = nil, week_range = 1, history = 4)
    stat_helper('awarded', week_range, history, user_type)
  end

  def closed(user_type = nil, week_range = 1, history = 4)
    stat_helper('closed', week_range, history, user_type)
  end

  # TODO: this is not DRY
  def date_helper(week_range = 1, history = 4)
    dates = []
    weeks = history.downto(-1).to_a
    ceiling = weeks.max
    weeks.each do |d|
      silence_stream(STDOUT) do
        if d == -1
          # the current week
          startt = week_range == 1 ? Date.today.beginning_of_week : week_range.week.ago.beginning_of_week
        else
          startt = (d+week_range).week.ago.beginning_of_week
        end
        dates << startt
      end
    end
    dates
  end

  private

  def filtered_query(query, user_type = nil)
    query = query.select{|t| t.user.username != 'kernel' &&
                     t.product.slug != 'meta'}
    if user_type == 'core'
      query = query.select{|t| t.product.core_team?(t.user)}
    elsif user_type == 'staff'
      query = query.select{|t| t.user.staff?}
    end
    query
  end

  # TODO: clean up; this code is hella gross
  # need to cache...only the last row will be new
  def stat_helper(status, week_range = 1, history = 4, user_type = nil)
    results = []
    dates = []
    weeks = history.downto(-1).to_a
    ceiling = weeks.max

    weeks.each do |d|
      silence_stream(STDOUT) do
        if d == -1
          # the current week
          startt = week_range == 1 ? Date.today.beginning_of_week : week_range.week.ago.beginning_of_week
          endt = Time.now
        else
          startt = (d+week_range).week.ago.beginning_of_week
          endt = d.week.ago.beginning_of_week
        end

        query = Task.where(created_at: startt..endt)
                    
        if status == 'awarded'
          query = Task.where(state: :resolved).where(closed_at: startt..endt).select{|x| !x.awards.empty?}
        elsif status == 'closed'
          query = Task.where(state: :resolved).where(closed_at: startt..endt)
        end
        dates << startt
        results << filtered_query(query, user_type).count.to_f
      end
    end
    results
  end

end<commit_msg>cache the stats<commit_after>module BountyStatsHelper

  require 'date'

  def avg_bounty_lifespan(startd=nil, endd=nil)
    query = "SELECT avg(closed_at - created_at) AS avg_life FROM wips WHERE wips.type IN ('Task') AND wips.state = 'resolved'"
    if !startd.nil? && !endd.nil?
      startd = startd.to_time
      endd = endd.to_time
      query += " AND (wips.created_at BETWEEN '#{startd}' AND '#{endd}')"
    end
    Wip.connection.execute(query).first
  end

  def created(user_type = nil, week_range = 1, history = 4)
    stat_helper('created', week_range, history, user_type)
  end

  def awarded(user_type = nil, week_range = 1, history = 4)
    stat_helper('awarded', week_range, history, user_type)
  end

  def closed(user_type = nil, week_range = 1, history = 4)
    stat_helper('closed', week_range, history, user_type)
  end

  # TODO: this is not DRY
  def date_helper(week_range = 1, history = 4)
    dates = []
    weeks = history.downto(-1).to_a
    ceiling = weeks.max
    weeks.each do |d|
      silence_stream(STDOUT) do
        if d == -1
          # the current week
          startt = week_range == 1 ? Date.today.beginning_of_week : week_range.week.ago.beginning_of_week
        else
          startt = (d+week_range).week.ago.beginning_of_week
        end
        dates << startt
      end
    end
    dates
  end

  private

  def filtered_query(query, user_type = nil)
    query = query.select{|t| t.user.username != 'kernel' &&
                     t.product.slug != 'meta'}
    if user_type == 'core'
      query = query.select{|t| t.product.core_team?(t.user)}
    elsif user_type == 'staff'
      query = query.select{|t| t.user.staff?}
    end
    query
  end

  # TODO: clean up; this code is hella gross
  # need to cache...only the last row will be new
  def stat_helper(status, week_range = 1, history = 4, user_type = nil)
    results = []
    dates = []
    weeks = history.downto(-1).to_a
    ceiling = weeks.max

    weeks.each do |d|
      silence_stream(STDOUT) do
        if d == -1
          # the current week
          startt = week_range == 1 ? Date.today.beginning_of_week : week_range.week.ago.beginning_of_week
          endt = Time.now
        else
          startt = (d+week_range).week.ago.beginning_of_week
          endt = d.week.ago.beginning_of_week
        end

        query = Task.where(created_at: startt..endt)
                    
        if status == 'awarded'
          query = Task.where(state: :resolved).where(closed_at: startt..endt).select{|x| !x.awards.empty?}
        elsif status == 'closed'
          query = Task.where(state: :resolved).where(closed_at: startt..endt)
        end
        dates << startt
        results << Rails.cache.fetch("#{status}_#{user_type}_#{endt.to_i}") do; filtered_query(query, user_type).count.to_f; end
      end
    end
    results
  end

end
{'fraction_non_alphanumeric': 0.11603974449964514, 'fraction_numerical': 0.0056777856635912, 'mean_word_length': 2.430919050517346, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5774708', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1909, 'n_words': 456}