<commit_before>class ArchiverJob < ApplicationJob
  queue_as :cron

  def archive_max_messages_per_thread(forum)
    max_messages = conf('max_messages_per_thread', forum).to_i # max messages per thread

    # first: max messages per thread (to avoid monster threads like „Test, bitte ignorieren“)
    CfThread.transaction do
      threads = CfThread.select('threads.thread_id, COUNT(*) AS cnt, threads.flags')
                  .joins(:messages)
                  .where(archived: false, forum_id: forum.forum_id)
                  .group('threads.thread_id')

      threads.each do |t|
        next unless t.cnt.to_i > max_messages
        if t.flags['no-archive'] == 'yes'
          Rails.logger.info('ArchiveRunnerTask: archiving (deleting!) thread ' +
                            t.thread_id.to_s + ' because of to many messages')

          audit(t, 'destroy', nil)
          SearchDocument.where('reference_id IN (?)', t.messages.map(&:message_id)).delete_all
          t.destroy

        else
          Rails.logger.info('ArchiveRunnerTask: archiving thread ' +
                            t.thread_id.to_s + ' because of to many messages')

          CfThread.connection.execute 'UPDATE threads SET archived = true WHERE thread_id = ' + t.thread_id.to_s
          Message.connection.execute 'UPDATE messages SET ip = NULL where thread_id = ' + t.thread_id.to_s
          CfThread.connection.execute 'DELETE FROM invisible_threads WHERE thread_id = ' + t.thread_id.to_s
          audit(t, 'archive', nil)
        end
      end
    end
  end

  def archive_max_threads_per_forum(forum)
    max_threads = conf('max_threads', forum).to_i # max threads per forum

    # second: max threads per forum
    CfThread.transaction do
      while CfThread.where(forum_id: forum.forum_id, archived: false).count > max_threads
        rslt = CfThread.connection.execute 'SELECT threads.thread_id, MAX(messages.created_at) AS created_at' \
                                           '  FROM threads INNER JOIN messages USING(thread_id)' \
                                           '  WHERE threads.forum_id = ' + forum.forum_id.to_s +
                                           '        AND archived = false' \
                                           '  GROUP BY threads.thread_id' \
                                           '  ORDER BY MAX(messages.created_at) ASC LIMIT 1'
        tid = rslt[0]['thread_id']
        t = CfThread.find tid
        message_ids = t.messages.map(&:message_id)

        if t.flags['no-archive'] == 'yes'
          Rails.logger.info 'ArchiveRunnerTask: archiving (deleting!) thread ' + tid.to_s +
                            ' because oldest while to many threads'
          audit(t, 'destroy', nil)
          SearchDocument.where('reference_id IN (?)', message_ids).delete_all
          t.destroy
        else
          Rails.logger.info 'ArchiveRunnerTask: archiving thread ' + tid.to_s + ' because oldest while to many threads'
          audit(t, 'archive', nil)

          CfThread.connection.execute 'UPDATE threads SET archived = true WHERE thread_id = ' + tid.to_s<commit_msg>fix: don't archive sticky threads<commit_after>class ArchiverJob < ApplicationJob
  queue_as :cron

  def archive_max_messages_per_thread(forum)
    max_messages = conf('max_messages_per_thread', forum).to_i # max messages per thread

    # first: max messages per thread (to avoid monster threads like „Test, bitte ignorieren“)
    CfThread.transaction do
      threads = CfThread.select('threads.thread_id, COUNT(*) AS cnt, threads.flags')
                  .joins(:messages)
                  .where(archived: false, forum_id: forum.forum_id, sticky: false)
                  .group('threads.thread_id')

      threads.each do |t|
        next unless t.cnt.to_i > max_messages
        if t.flags['no-archive'] == 'yes'
          Rails.logger.info('ArchiveRunnerTask: archiving (deleting!) thread ' +
                            t.thread_id.to_s + ' because of to many messages')

          audit(t, 'destroy', nil)
          SearchDocument.where('reference_id IN (?)', t.messages.map(&:message_id)).delete_all
          t.destroy

        else
          Rails.logger.info('ArchiveRunnerTask: archiving thread ' +
                            t.thread_id.to_s + ' because of to many messages')

          CfThread.connection.execute 'UPDATE threads SET archived = true WHERE thread_id = ' + t.thread_id.to_s
          Message.connection.execute 'UPDATE messages SET ip = NULL where thread_id = ' + t.thread_id.to_s
          CfThread.connection.execute 'DELETE FROM invisible_threads WHERE thread_id = ' + t.thread_id.to_s
          audit(t, 'archive', nil)
        end
      end
    end
  end

  def archive_max_threads_per_forum(forum)
    max_threads = conf('max_threads', forum).to_i # max threads per forum

    # second: max threads per forum
    CfThread.transaction do
      while CfThread.where(forum_id: forum.forum_id, archived: false).count > max_threads
        rslt = CfThread.connection.execute 'SELECT threads.thread_id, MAX(messages.created_at) AS created_at' \
                                           '  FROM threads INNER JOIN messages USING(thread_id)' \
                                           '  WHERE threads.forum_id = ' + forum.forum_id.to_s +
                                           '        AND archived = false AND sticky = false' \
                                           '  GROUP BY threads.thread_id' \
                                           '  ORDER BY MAX(messages.created_at) ASC LIMIT 1'
        tid = rslt[0]['thread_id']
        t = CfThread.find tid
        message_ids = t.messages.map(&:message_id)

        if t.flags['no-archive'] == 'yes'
          Rails.logger.info 'ArchiveRunnerTask: archiving (deleting!) thread ' + tid.to_s +
                            ' because oldest while to many threads'
          audit(t, 'destroy', nil)
          SearchDocument.where('reference_id IN (?)', message_ids).delete_all
          t.destroy
        else
          Rails.logger.info 'ArchiveRunnerTask: archiving thread ' + tid.to_s + ' because oldest while to many threads'
          audit(t, 'archive', nil)

          CfThread.connection.execute 'UPDATE threads SET archived = true WHERE thread_id = ' + tid.to_s
{'fraction_non_alphanumeric': 0.0938495857233907, 'fraction_numerical': 0.0006373486297004461, 'mean_word_length': 2.048567265662943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4591582', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1714, 'n_words': 434}