<commit_before>require "date"
require_relative "../database"

class FeaturedScoreUpdater
  def self.execute(date)
    batch_trace("FeaturedScoreUpdater", "execute", [date]) {
      clear_featured_score(date)
      featured_score = generate_featured_score(date)
      update_featured_score(featured_score)
    }
  end

  def self.clear_featured_score(date)
    Value.where(:type => Value::Type::FEATURED_SCORE, :date => date).delete
  end

  def self.generate_featured_score(date)
    featured_score = []
    Ranking.where(:type => Ranking::Type::DAILY_RANKING,
                  :date => date)
           .order(:ranking)
           .limit(1000)
           .each { |daily_ranking|
      total_ranking = Ranking.where(:type => Ranking::Type::TOTAL_RANKING,
                                    :gem_id => daily_ranking[:gem_id],
                                    :date => daily_ranking[:date]).first
      raise "Database inconsistency." unless total_ranking

      rank_diff = total_ranking[:ranking] - daily_ranking[:ranking]

      record = {:type => Value::Type::FEATURED_SCORE,
                :gem_id => daily_ranking[:gem_id],
                :date => daily_ranking[:date],
                :value => rank_diff}
      featured_score << record
    }
    featured_score
  end

  def self.update_featured_score(featured_score)
    Value.multi_insert(featured_score)
  end
end

if $0 == __FILE__
  date = ARGV[0] || Date.today - 1
  FeaturedScoreUpdater.execute(date)
end<commit_msg>Fix featured ranking generating<commit_after>require "date"
require_relative "../database"

class FeaturedScoreUpdater
  def self.execute(date)
    batch_trace("FeaturedScoreUpdater", "execute", [date]) {
      clear_featured_score(date)
      featured_score = generate_featured_score(date)
      update_featured_score(featured_score)
    }
  end

  def self.clear_featured_score(date)
    Value.where(:type => Value::Type::FEATURED_SCORE, :date => date).delete
  end

  def self.generate_featured_score(date)
    featured_score = []
    Ranking.where(:type => Ranking::Type::DAILY_RANKING,
                  :date => date)
           .order(:ranking)
           .limit(1000)
           .each { |daily_ranking|
      total_ranking = Ranking.where(:type => Ranking::Type::TOTAL_RANKING,
                                    :gem_id => daily_ranking[:gem_id],
                                    :date => daily_ranking[:date]).first
      raise "Database inconsistency." unless total_ranking

      rank_diff = total_ranking[:ranking] - daily_ranking[:ranking]

      next unless rank_diff > 0

      record = {:type => Value::Type::FEATURED_SCORE,
                :gem_id => daily_ranking[:gem_id],
                :date => daily_ranking[:date],
                :value => rank_diff}

      featured_score << record
    }
    featured_score
  end

  def self.update_featured_score(featured_score)
    Value.multi_insert(featured_score)
  end
end

if $0 == __FILE__
  date = ARGV[0] || Date.today - 1
  FeaturedScoreUpdater.execute(date)
end
{'fraction_non_alphanumeric': 0.11151555261416281, 'fraction_numerical': 0.004963600264725348, 'mean_word_length': 2.6910866910866913, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5917557', 'n_tokens_mistral': 970, 'n_tokens_neox': 918, 'n_words': 158}