<commit_before>require 'curb'
require 'mongo'
require 'bencodr'
require 'logger'

class UpdateStats
  include Mongo

  def initialize(tracker_uri, database, collection)
    @log = Logger.new(STDOUT)
    @log.level = Logger::INFO

    @tracker_uri = tracker_uri
    @database    = database
    @collection  = collection

    connect_to_db(database)
  end

  def connect_to_db(database)
    begin
      @con = Connection.new
    rescue Mongo::ConnectionFailure
      @log.fatal "Couldn't connect to the database"
      @log.info  "Waiting for 600 seconds..."
      sleep 600
      retry
    else
      @db  = @con.db(database)
    end
  end

  def update_stats(time_diff)
    if @con.connected?
      @coll   = @db.collection(@collection) if @con.connected?
      subset  = @coll.find({ "updated_at" => { "$lt" => Time.now - time_diff }})
      if subset.count > 0
        subset.each do |release|
          infohash = release["trhash"]
          begin
            get = Curl::Easy.perform("#{@tracker_uri}/announce?info_hash=#{infohash}")
          rescue Curl::Err::ConnectionFailedError
            @log.fatal "Couldn't connect to tracker"
            @log.info "Waiting for 600 seconds..."
            sleep 600
            retry
          end
          fetched               = get.body_str.bdecode
          release["stats"]      = Array[fetched["incomplete"], fetched["complete"]]
          release["updated_at"] = Time.now
          @coll.save(release)
        end
      end
      @log.info "Everything is ok, waiting 180 sec for next update"
      sleep 180
    else
      connect_to_db(@database)
    end
  end
end

TRACKER_URI = 'http://192.168.1.1:6969'
DATABASE    = 'easybt_development'
COLLECTION  = 'releases'

updater = UpdateStats.new(TRACKER_URI, DATABASE, COLLECTION)
loop { updater.update_stats(480) }<commit_msg>a little bit more refactoring<commit_after>require 'curb'
require 'mongo'
require 'bencodr'
require 'logger'

class UpdateStats
  include Mongo

  def initialize(tracker_uri, db_name, coll_name)
    @log = Logger.new(STDOUT)
    @log.level = Logger::INFO

    @tracker_uri = tracker_uri
    @db_name     = db_name
    @coll_name   = coll_name

    connect_to_db(db_name)
  end

  def connect_to_db(db_name)
    begin
      @con = Connection.new
    rescue Mongo::ConnectionFailure
      @log.fatal "Couldn't connect to the database"
      @log.info  "Waiting for 600 seconds..."
      sleep 600
      retry
    else
      @db  = @con.db(db_name)
    end
  end

  def update_stats(time_diff)
    if @con.connected?
      coll   = @db.collection(@coll_name)
      subset  = coll.find({ "updated_at" => { "$lt" => Time.now - time_diff }})
      if subset.count > 0
        subset.each do |release|
          infohash = release["trhash"]
          begin
            get = Curl::Easy.perform("#{@tracker_uri}/announce?info_hash=#{infohash}")
          rescue Curl::Err::ConnectionFailedError
            @log.fatal "Couldn't connect to tracker"
            @log.info "Waiting for 600 seconds..."
            sleep 600
            retry
          end
          fetched               = get.body_str.bdecode
          release["stats"]      = [fetched["incomplete"], fetched["complete"]]
          release["updated_at"] = Time.now
          coll.save(release)
        end
      end
      @log.info "Everything is ok, waiting 180 sec for next update"
      sleep 180
    else
      connect_to_db(@db_name)
    end
  end
end

TRACKER_URI = 'http://192.168.1.1:6969'
DB_NAME     = 'easybt_development'
COLL_NAME   = 'releases'

updater = UpdateStats.new(TRACKER_URI, DB_NAME, COLL_NAME)
loop { updater.update_stats(480) }
{'fraction_non_alphanumeric': 0.1087912087912088, 'fraction_numerical': 0.01868131868131868, 'mean_word_length': 2.383828996282528, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1638000', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1162, 'n_words': 270}