Class: NFAgent::ChunkHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/nfagent/chunk_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ChunkHandler

Returns a new instance of ChunkHandler.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nfagent/chunk_handler.rb', line 9

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

Instance Attribute Details

#chunk_groupObject

Returns the value of attribute chunk_group.



7
8
9
# File 'lib/nfagent/chunk_handler.rb', line 7

def chunk_group
  @chunk_group
end

Instance Method Details

#append(line) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nfagent/chunk_handler.rb', line 25

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

#append2(line, key = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/nfagent/chunk_handler.rb', line 43

def append2(line, key = nil)
  key ||= 'all'
  begin
    chunk = @chunk_group.fetch!(key, Chunk.new(@chunk_size))
    chunk << line
  rescue ChunkExpired, ChunkFull
    Log.info("Chunk full or expired, cannot add lines")
    reset_chunk(key)
  end
end

#check_full_or_expiredObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nfagent/chunk_handler.rb', line 54

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