<commit_before>class EntryDeleter
  include Sidekiq::Worker
  sidekiq_options queue: :worker_slow

  def perform(feed_id)
    feed = Feed.find(feed_id)

    entry_limit = if ENV["ENTRY_LIMIT"]
      ENV["ENTRY_LIMIT"].to_i
    else
      feed.subscriptions_count == 0 ? 10 : 400
    end

    feed.feed_stats.where("day < ?", 90.days.ago).delete_all
    unless feed.protected? || feed.standalone_request_at.present?
      prune_entries(feed_id, entry_limit)
    end
    UnreadLimiter.new.perform(feed_id)
  end

  def prune_entries(feed_id, entry_limit)
    entry_count = Entry.where(feed_id: feed_id).count
    if entry_count > entry_limit
      entries_to_keep = Entry.where(feed_id: feed_id).order("published DESC").limit(entry_limit).pluck("entries.id")
      entries_to_delete = Entry.where(feed_id: feed_id, starred_entries_count: 0, recently_played_entries_count: 0).where.not(id: entries_to_keep).pluck(:id, :image)
      entries_to_delete_ids = entries_to_delete.map(&:first)
      entries_to_delete_images = entries_to_delete.map { |array| array.last && array.last["processed_url"] }.compact
      delete_entries(feed_id, entries_to_delete_ids, entries_to_delete_images)<commit_msg>Get more accurate subscription count.<commit_after>class EntryDeleter
  include Sidekiq::Worker
  sidekiq_options queue: :worker_slow

  def perform(feed_id)
    feed = Feed.find(feed_id)

    entry_limit = if ENV["ENTRY_LIMIT"]
      ENV["ENTRY_LIMIT"].to_i
    else
      feed.subscriptions.where(active: true).exists? ? 400 : 10
    end

    feed.feed_stats.where("day < ?", 90.days.ago).delete_all
    unless feed.protected? || feed.standalone_request_at.present?
      prune_entries(feed_id, entry_limit)
    end
    UnreadLimiter.new.perform(feed_id)
  end

  def prune_entries(feed_id, entry_limit)
    entry_count = Entry.where(feed_id: feed_id).count
    if entry_count > entry_limit
      entries_to_keep = Entry.where(feed_id: feed_id).order("published DESC").limit(entry_limit).pluck("entries.id")
      entries_to_delete = Entry.where(feed_id: feed_id, starred_entries_count: 0, recently_played_entries_count: 0).where.not(id: entries_to_keep).pluck(:id, :image)
      entries_to_delete_ids = entries_to_delete.map(&:first)
      entries_to_delete_images = entries_to_delete.map { |array| array.last && array.last["processed_url"] }.compact
      delete_entries(feed_id, entries_to_delete_ids, entries_to_delete_images)
{'fraction_non_alphanumeric': 0.10771770532397854, 'fraction_numerical': 0.007841518778373916, 'mean_word_length': 4.676814988290398, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4706471', 'n_tokens_mistral': 855, 'n_tokens_neox': 818, 'n_words': 131}