<commit_before>class IpStats
  class << self
    def log_stats(stats)
      file = MO.ip_stats_file
      now = Time.current
      File.open(file, "a") do |fh|
        fh.puts [
          now,
          stats[:ip],
          User.current_id,
          now - stats[:time],
          stats[:controller],
          stats[:action]
        ].join(",")
      end
    end

    def read_stats
      data = {}
      now = Time.current
      read_file(MO.ip_stats_file) do |time, ip, user, load, controller, action|
        hash = data[ip] ||= { load: 0, activity: [], rate: 0 }
        # Weight turns rate into average number of requests per second,
        # and load into average server time used per minute.  It weights the
        # most recent minute 10x more heavily than the minute before cutoff.
        weight = (600 - (now - Time.parse(time))) / 600 / 600 * 2
        hash[:user] = user.to_i if user.present?
        hash[:load] += load.to_f * weight
        hash[:rate] += weight
        hash[:activity] << [time, load.to_f, controller, action]
      end
      data
    end

    def clean_stats
      cutoff = (Time.current - 10.minutes).to_s
      rewrite_ip_stats { |time| time > cutoff }
    end

    def blocked?(ip)
      populate_blocked_ips unless blocked_ips_current?
      @@blocked_ips.include?(ip)
    end

    def blocked_ips
      populate_blocked_ips unless blocked_ips_current?
      @@blocked_ips
    end

    def add_blocked_ips(ips)
      file = MO.blocked_ips_file
      File.open(file, "a") do |fh|
        ips.each do |ip|
          fh.puts "#{ip},#{Time.current}"
        end
      end
    end

    def remove_blocked_ips(ips)
      rewrite_blocked_ips { |ip, time| !ips.include?(ip) }
    end

    def clean_blocked_ips
      cutoff = (Time.current - 1.day).to_s
      rewrite_blocked_ips { |ip, time| time > cutoff }
    end

    private

    def blocked_ips_current?
      defined?(@@blocked_ips_time) &&
        @@blocked_ips_time >= File.mtime(MO.blocked_ips_file) &&
        @@blocked_ips_time >= File.mtime(MO.okay_ips_file)
    end
<commit_msg>Fix rubocop offenses for ip_stats.rb<commit_after>class IpStats
  class << self
    def log_stats(stats)
      file = MO.ip_stats_file
      now = Time.current
      File.open(file, "a") do |fh|
        fh.puts [
          now,
          stats[:ip],
          User.current_id,
          now - stats[:time],
          stats[:controller],
          stats[:action]
        ].join(",")
      end
    end

    def read_stats
      data = {}
      now = Time.current
      read_file(MO.ip_stats_file) do |time, ip, user, load, controller, action|
        hash = data[ip] ||= { load: 0, activity: [], rate: 0 }
        # Weight turns rate into average number of requests per second,
        # and load into average server time used per minute.  It weights the
        # most recent minute 10x more heavily than the minute before cutoff.
        weight = calc_weight(now, time)
        hash[:user] = user.to_i if user.present?
        hash[:load] += load.to_f * weight
        hash[:rate] += weight
        hash[:activity] << [time, load.to_f, controller, action]
      end
      data
    end

    def calc_weight(now, time)
      (600 - (now - Time.zone.parse(time))) / 600 / 600 * 2
    end

    def clean_stats
      cutoff = (Time.current - 10.minutes).to_s
      rewrite_ip_stats { |time| time > cutoff }
    end

    def blocked?(ip)
      populate_blocked_ips unless blocked_ips_current?
      @@blocked_ips.include?(ip)
    end

    def blocked_ips
      populate_blocked_ips unless blocked_ips_current?
      @@blocked_ips
    end

    def add_blocked_ips(ips)
      file = MO.blocked_ips_file
      File.open(file, "a") do |fh|
        ips.each do |ip|
          fh.puts "#{ip},#{Time.current}"
        end
      end
    end

    def remove_blocked_ips(ips)
      rewrite_blocked_ips { |ip, _time| !ips.include?(ip) }
    end

    def clean_blocked_ips
      cutoff = (Time.current - 1.day).to_s
      rewrite_blocked_ips { |_ip, time| time > cutoff }
    end

    private

    def blocked_ips_current?
      defined?(@@blocked_ips_time) &&
        @@blocked_ips_time >= File.mtime(MO.blocked_ips_file) &&
        @@blocked_ips_time >= File.mtime(MO.okay_ips_file)
    end

{'fraction_non_alphanumeric': 0.10915078804987062, 'fraction_numerical': 0.007998118089861209, 'mean_word_length': 2.1449704142011834, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2813659', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1388, 'n_words': 348}