<commit_before>require 'analysand'
require 'uri'

require File.expand_path('../../lib/history_db', __FILE__)
require File.expand_path('../../lib/job', __FILE__)
require File.expand_path('../../lib/log_update_listener', __FILE__)

class JobRecorder < LogUpdateListener
  def initialize(redis_url, update_channel, db_url, db_credentials)
    @db = HistoryDb.new(URI(db_url), db_credentials)

    super
  end

  def on_receive(ident)
    job = ::Job.from_ident(ident, uredis)

    return unless job

    if job.finished?
      doc_id = "#{job.ident}:#{job.queued_at.to_i}"

      begin
        @db.put!(doc_id, job)
      rescue Analysand::DocumentNotSaved => e
        if e.response.conflict?
          warn "Document #{doc_id} already exists"
        else
          throw e
        end
      end
    end
  end
end
<commit_msg>We don't really need to warn about conflicts in the recorder.<commit_after>require 'analysand'
require 'uri'

require File.expand_path('../../lib/history_db', __FILE__)
require File.expand_path('../../lib/job', __FILE__)
require File.expand_path('../../lib/log_update_listener', __FILE__)

class JobRecorder < LogUpdateListener
  def initialize(redis_url, update_channel, db_url, db_credentials)
    @db = HistoryDb.new(URI(db_url), db_credentials)

    super
  end

  def on_receive(ident)
    job = ::Job.from_ident(ident, uredis)

    return unless job

    if job.finished?
      doc_id = "#{job.ident}:#{job.queued_at.to_i}"

      begin
        @db.put!(doc_id, job)
      rescue Analysand::DocumentNotSaved => e
        # A conflict indicates that doc_id already exists.  The ident is unique
        # with high probability, so this situation is a very strong indication
        # that we just received a duplicate message.  As such, we ignore
        # conflicts.
        #
        # However, other issues are treated as fatal.
        if !e.response.conflict?
          throw e
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11014344262295082, 'fraction_numerical': 0.0, 'mean_word_length': 2.807017543859649, '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': '6396385', 'n_tokens_mistral': 621, 'n_tokens_neox': 610, 'n_words': 163}