Class: LogAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/ip-world-map/log_analyzer.rb

Direct Known Subclasses

ApacheLogAnalyzer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*filenames) ⇒ LogAnalyzer

Returns a new instance of LogAnalyzer.



4
5
6
7
8
# File 'lib/ip-world-map/log_analyzer.rb', line 4

def initialize *filenames
  @filenames = filenames.flatten.sort.uniq
  @ip_lookup_service = IpLookupService.new
  @ip_lookup_service.load_coordinates
end

Instance Attribute Details

#host_coordinatesObject

Returns the value of attribute host_coordinates.



2
3
4
# File 'lib/ip-world-map/log_analyzer.rb', line 2

def host_coordinates
  @host_coordinates
end

Instance Method Details

#analyzeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ip-world-map/log_analyzer.rb', line 10

def analyze
  details = []

  puts "\nReading files:" if $visualization_config.verbose
  @filenames.each do |filename|
    puts filename if $visualization_config.verbose
    FileUtils.open(filename).each_line do |line|
      details << details_from_line(line)
    end
  end

  hosts = details.collect{|detail| detail[:host]}
  coordinates = @ip_lookup_service.coordinates_for_hosts(hosts)

  details.collect! do |detail|
    detail[:coordinates] = coordinates[detail[:host]]
    detail
  end

  details
end

#calculate_oldest_time(details) ⇒ Object



38
39
40
# File 'lib/ip-world-map/log_analyzer.rb', line 38

def calculate_oldest_time details
  details.min{ |a, b| a[:time] <=> b[:time] }[:time]
end

#calculate_slot_start_time(time, slot_in_seconds) ⇒ Object



56
57
58
# File 'lib/ip-world-map/log_analyzer.rb', line 56

def calculate_slot_start_time time, slot_in_seconds
  Time.at(time.tv_sec - (time.tv_sec % slot_in_seconds))
end

#details_from_line(line) ⇒ Object



32
33
34
35
36
# File 'lib/ip-world-map/log_analyzer.rb', line 32

def details_from_line line
  host = extract_host_from_line(line)
  time = extract_time_from_line(line)
  { :time => time, :host => host }
end

#group_by_time(details, slot_in_seconds) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ip-world-map/log_analyzer.rb', line 42

def group_by_time details, slot_in_seconds
  return {} unless details && slot_in_seconds
  details_per_slot = {}

  # TODO: maybe assign empty arrays to missing slots where no traffic was detected
  details.each do |detail|
    slot_start_time = calculate_slot_start_time(detail[:time], slot_in_seconds)
    details_per_slot[slot_start_time] ||= []
    details_per_slot[slot_start_time] << detail
  end

  details_per_slot
end