<commit_before>require_relative 'logging'

module Kontena
  class StatsWorker
    include Kontena::Logging

    attr_reader :url, :queue

    ##
    # @param [Queue] queue
    def initialize(queue)
      @queue = queue
      info 'initialized'
    end

    def start!
      Thread.new {
        info 'waiting for cadvisor'
        sleep 1 until cadvisor_running?
        info 'cadvisor is running, starting stats loop'
        last_collected = Time.now.to_i
        loop do
          sleep 1 until last_collected < (Time.now.to_i - 60)
          self.collect_stats
        end
      }
    end

    def collect_stats
      begin
        data = fetch_stats
        if data
          data.values.each do |container|
            self.send_container_stats(container)
            sleep 0.5
          end
        end
      rescue => exc
        error "error on stats fetching: #{exc.message}"
      end
    end

    ##
    # @param [Hash] container
    def send_container_stats(container)
      prev_stat = container['stats'][-2]<commit_msg>fix agent stats collect interval<commit_after>require_relative 'logging'

module Kontena
  class StatsWorker
    include Kontena::Logging

    attr_reader :url, :queue

    ##
    # @param [Queue] queue
    def initialize(queue)
      @queue = queue
      info 'initialized'
    end

    def start!
      Thread.new {
        info 'waiting for cadvisor'
        sleep 1 until cadvisor_running?
        info 'cadvisor is running, starting stats loop'
        last_collected = Time.now.to_i
        loop do
          sleep 1 until last_collected < (Time.now.to_i - 60)
          self.collect_stats
          last_collected = Time.now.to_i
        end
      }
    end

    def collect_stats
      begin
        data = fetch_stats
        if data
          data.values.each do |container|
            self.send_container_stats(container)
            sleep 0.5
          end
        end
      rescue => exc
        error "error on stats fetching: #{exc.message}"
      end
    end

    ##
    # @param [Hash] container
    def send_container_stats(container)
      prev_stat = container['stats'][-2]
{'fraction_non_alphanumeric': 0.07283834586466166, 'fraction_numerical': 0.006578947368421052, 'mean_word_length': 1.8692722371967656, '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': '6253772', 'n_tokens_mistral': 626, 'n_tokens_neox': 609, 'n_words': 173}