<commit_before>class Admin::ExperimentsController < ApplicationController
  newrelic_ignore
  before_filter :require_admin
  
  # @@FIX:
  # This should all really be in whiplash, but I can't figure out how to call it properly.
  def all_tests
    tests = {}
    REDIS.keys('whiplash/goals/*').each do |g|
      REDIS.smembers(g).each do |t|
        tests[t] = {goal: g[15..-1], options: []}
      end
    end
    
    tests.keys.each do |t|
      prefix = "whiplash/#{t}/"
      suffix = "/spins"
      REDIS.keys(prefix + "*" + suffix).each do |o|
        tests[t][:options].append o[prefix.length..-suffix.length-1]
      end
    end
    tests
  end
  
  def stats
    mystats = []
    all_tests.each do |test_name, test_info|
      test_stats = {
        name: test_name,
        trials: 0,
        arms: [],
        goal: test_info[:goal]
      }
      test_info[:options].each do |opt_name|
        spins = REDIS.get("whiplash/#{test_name}/#{opt_name}/spins").to_i
        test_stats[:arms].append({
          name: opt_name,
          spins: spins,
          wins: REDIS.get("whiplash/#{test_name}/#{opt_name}/wins").to_i,
        })
        test_stats[:trials] += spins
      end
      mystats.append test_stats
    end
    mystats
  end
  
  def sentemails_by_hour
    spins = Hash.new(0)
    wins = Hash.new(0)
    SentEmail.all.each do |e|
      # "#{e.created_at.wday} #{e.created_at.hour}"
      q = e.created_at.hour
      spins[q] += 1
      wins[q] += 1 if e.signature_id
    end
    [spins, wins]
  end
  
  def index
    @stats = stats
    @hourlydata = sentemails_by_hour
  end
end<commit_msg>using _group by_ for getting sent_emails by hour<commit_after>class Admin::ExperimentsController < ApplicationController
  newrelic_ignore
  before_filter :require_admin
  
  # @@FIX:
  # This should all really be in whiplash, but I can't figure out how to call it properly.
  def all_tests
    tests = {}
    REDIS.keys('whiplash/goals/*').each do |g|
      REDIS.smembers(g).each do |t|
        tests[t] = {goal: g[15..-1], options: []}
      end
    end
    
    tests.keys.each do |t|
      prefix = "whiplash/#{t}/"
      suffix = "/spins"
      REDIS.keys(prefix + "*" + suffix).each do |o|
        tests[t][:options].append o[prefix.length..-suffix.length-1]
      end
    end
    tests
  end
  
  def stats
    mystats = []
    all_tests.each do |test_name, test_info|
      test_stats = {
        name: test_name,
        trials: 0,
        arms: [],
        goal: test_info[:goal]
      }
      test_info[:options].each do |opt_name|
        spins = REDIS.get("whiplash/#{test_name}/#{opt_name}/spins").to_i
        test_stats[:arms].append({
          name: opt_name,
          spins: spins,
          wins: REDIS.get("whiplash/#{test_name}/#{opt_name}/wins").to_i,
        })
        test_stats[:trials] += spins
      end
      mystats.append test_stats
    end
    mystats
  end
  
  def sent_emails_by_hour
    sent_emails_by_hour = ActiveRecord::Base.connection.execute("select count(id) as sent, date_part('hour', created_at) as hour from sent_emails group by hour")
    spins = sent_emails_by_hour.inject({}) {|h, r| h[r["hour"].to_i] = r["sent"].to_i; h}
    signed_emails_by_hour = ActiveRecord::Base.connection.execute("select count(id) as signed, date_part('hour', created_at) as hour from sent_emails where signature_id is not null group by hour")
    wins = signed_emails_by_hour.inject({}) {|h, r| h[r["hour"].to_i] = r["signed"].to_i; h}
    [spins, wins]
  end

  def index
    @stats = stats
    @hourlydata = sent_emails_by_hour
  end
end
{'fraction_non_alphanumeric': 0.13271863649063984, 'fraction_numerical': 0.003911707180776753, 'mean_word_length': 2.4892787524366473, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1452489', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1280, 'n_words': 279}