<commit_before>class Topic < ActiveRecord::Base
  include Likeable
  include Trashable
  include Subscribable
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  TOP_HOT_VALUE = 100000000000000000000

  belongs_to :user, counter_cache: true
  belongs_to :category, counter_cache: true
  has_many :comments, as: 'commentable'
  is_impressionable :counter_cache => true

  validates :title, :body, presence: true

  after_create :update_hot, :owner_subscribe
  after_touch :update_hot

  after_trash :decrement_counter_cache
  after_restore :increment_counter_cache
  # Fix double desc counter
  after_destroy :increment_counter_cache, if: :trashed?

  def visit_count
    impressionist_count(:filter=>:all)
  end

  def increment_counter_cache
    if category
      Category.update_counters category.id, topics_count: 1
    end
    User.update_counters user.id, topics_count: 1
  end

  def decrement_counter_cache
    if category
      Category.update_counters category.id, topics_count: -1
    end
    User.update_counters user.id, topics_count: -1
  end

  def calculate_hot
    order = Math.log10([comments_count, 1].max)
    order + created_at.to_f / 45000 * 2 * 30
  end

  def update_hot
    # reload because comments_count has been cache in associations
    reload
    update_attribute :hot, calculate_hot if hot != TOP_HOT_VALUE    
  end

  def owner_subscribe
    subscribe_by user
  end

  def total_pages
    (comments_count.to_f / Comment.default_per_page).ceil
  end

  def top_it
    self.hot = TOP_HOT_VALUE
    self.save
  end
  def cancel_top_it
    self.update_hot
  end

  def more_like_this(num = 5)
    Topic.search(
      query: {
        more_like_this: {
          fields: ['title', 'body'],
          like_text: title + '\n' + body
        }
      },
      filter: {
        and: [
          { term: { trashed: false } },
          { not: { term: { id: id } } }
        ]
      }
    ).limit(num).records.to_a rescue []
  end
end
<commit_msg>change order of topics<commit_after>class Topic < ActiveRecord::Base
  include Likeable
  include Trashable
  include Subscribable
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  TOP_HOT_VALUE = 100000000000000000000

  belongs_to :user, counter_cache: true
  belongs_to :category, counter_cache: true
  has_many :comments, as: 'commentable'
  is_impressionable :counter_cache => true

  validates :title, :body, presence: true

  after_create :update_hot, :owner_subscribe
  after_touch :update_hot

  after_trash :decrement_counter_cache
  after_restore :increment_counter_cache
  # Fix double desc counter
  after_destroy :increment_counter_cache, if: :trashed?

  def visit_count
    impressionist_count(:filter=>:all)
  end

  def increment_counter_cache
    if category
      Category.update_counters category.id, topics_count: 1
    end
    User.update_counters user.id, topics_count: 1
  end

  def decrement_counter_cache
    if category
      Category.update_counters category.id, topics_count: -1
    end
    User.update_counters user.id, topics_count: -1
  end

  def calculate_hot
    # order = Math.log10([comments_count, 1].max)
    # order + created_at.to_f / 45000 * 2 * 30

    updated_at.to_f / 45000
  end

  def update_hot
    # reload because comments_count has been cache in associations
    reload
    update_attribute :hot, calculate_hot if hot != TOP_HOT_VALUE
  end

  def owner_subscribe
    subscribe_by user
  end

  def total_pages
    (comments_count.to_f / Comment.default_per_page).ceil
  end

  def top_it
    self.hot = TOP_HOT_VALUE
    self.save
  end
  def cancel_top_it
    self.update_hot
  end

  def more_like_this(num = 5)
    Topic.search(
      query: {
        more_like_this: {
          fields: ['title', 'body'],
          like_text: title + '\n' + body
        }
      },
      filter: {
        and: [
          { term: { trashed: false } },
          { not: { term: { id: id } } }
        ]
      }
    ).limit(num).records.to_a rescue []
  end
end

{'fraction_non_alphanumeric': 0.07707608155146693, 'fraction_numerical': 0.01964196916956738, 'mean_word_length': 3.006972111553785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5777170', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1292, 'n_words': 323}