<commit_before># frozen_string_literal: true

module Artery
  module ActiveRecord
    class Message < ::ActiveRecord::Base
      include MessageModel

      self.table_name = 'artery_messages'

      serialize :data, JSON

      around_create :lock_on_model
      after_commit :send_to_artery, on: :create

      alias :index :id

      class << self
        def since(model, since)
          where(model: model)
            .where('created_at > ?', Time.zone.at(since)) # TODO: ZONE? SEARCH IN MCS?
        end

        def after_index(model, index)
          where(model: model)
            .where(arel_table[:id].gt(index)).order(:id)
        end

        def latest_index(model)
          where(model: model).last&.id.to_i
        end

        def delete_old
          max_aged_id = where(arel_table[:created_at].lt(MAX_MESSAGE_AGE.ago)).maximum(:id)
          where(arel_table[:id].lt(max_aged_id)).delete_all
        end
      end

      def load_previous_index
        scope = self.class.where(model: model).order(:id)
        scope = scope.where(self.class.arel_table[:id].lt(index)) if index

        @previous_index = scope.select(:id).last&.id.to_i
      end

      def previous_index
        @previous_index || load_previous_index
      end

      protected

      def lock_on_model
        self.class.with_advisory_lock("#{self.class.table_name}:#{model}") do
          load_previous_index<commit_msg>A little better artery message cleaning<commit_after># frozen_string_literal: true

module Artery
  module ActiveRecord
    class Message < ::ActiveRecord::Base
      include MessageModel

      self.table_name = 'artery_messages'

      serialize :data, JSON

      around_create :lock_on_model
      after_commit :send_to_artery, on: :create

      alias :index :id

      class << self
        def since(model, since)
          where(model: model)
            .where('created_at > ?', Time.zone.at(since)) # TODO: ZONE? SEARCH IN MCS?
        end

        def after_index(model, index)
          where(model: model)
            .where(arel_table[:id].gt(index)).order(:id)
        end

        def latest_index(model)
          where(model: model).last&.id.to_i
        end

        def delete_old
          max_aged_id = where(arel_table[:created_at].lt(MAX_MESSAGE_AGE.ago)).maximum(:id)
          where(arel_table[:id].lteq(max_aged_id)).delete_all if max_aged_id.to_i > 0
        end
      end

      def load_previous_index
        scope = self.class.where(model: model).order(:id)
        scope = scope.where(self.class.arel_table[:id].lt(index)) if index

        @previous_index = scope.select(:id).last&.id.to_i
      end

      def previous_index
        @previous_index || load_previous_index
      end

      protected

      def lock_on_model
        self.class.with_advisory_lock("#{self.class.table_name}:#{model}") do
          load_previous_index
{'fraction_non_alphanumeric': 0.10347222222222222, 'fraction_numerical': 0.00034722222222222224, 'mean_word_length': 2.4256837098692032, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, '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': '6797015', 'n_tokens_mistral': 905, 'n_tokens_neox': 875, 'n_words': 173}