Sha256: c5853829c06bcc8f1697a7ee4fa29d926f2606263c0c50eb341d1fb519438aa5

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module NFAgent
  class ChunkHandler

    attr_accessor :chunk_group

    def initialize(options = {})
      @chunk_size = options[:chunk_size] || 500
      @parser = options[:parser] || Squiggle::SquidStandardParser.new(Config.time_zone)
      @chunk_group = {}
      class << @chunk_group
        def fetch!(key, new_chunk)
          if self.has_key?(key)
            self.fetch(key)
          else
            self[key] = new_chunk
            new_chunk
          end
        end
      end
    end

    def append(line)
      if Config.parse == 'locally'
        parsed = @parser.parse(line)
        return if parsed.invalid?
        if Config.mode == 'multi'
          key = MapperProxy.find_account_id(parsed.username, parsed.client_ip)
          # TODO: Still appending line as string until Server API has been updated
          return append2(line, key)
        end
      end
      # TODO: rename append2
      append2(line)
    end

    def append2(line, key = nil)
      key ||= 'all'
      begin
        chunk = @chunk_group.fetch!(key, Chunk.new(@chunk_size))
        chunk << line
      rescue ChunkExpired, ChunkFull
        reset_chunk(key)
      end
    end

    def check_full_or_expired
      @chunk_group.each_pair do |key, chunk|
        if chunk.full?
          Log.info("Chunk Full: Resetting...")
          reset_chunk(key)
        elsif chunk.expired?
          Log.info("Chunk Expired: Resetting...")
          reset_chunk(key)
        end
      end
    end

    private
      def reset_chunk(key = nil)
        key ||= 'all'
        chunk = @chunk_group.delete(key)
        chunk.submit
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nfagent-0.9.27 lib/nfagent/chunk_handler.rb
nfagent-0.9.26 lib/nfagent/chunk_handler.rb
nfagent-0.9.20 lib/nfagent/chunk_handler.rb