Class: Cosmos::AnalyzeLog

Inherits:
Object show all
Defined in:
lib/cosmos/gui/utilities/analyze_log.rb

Overview

Class to analyze log files and report packet counts.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, packet_log_frame) ⇒ AnalyzeLog

Returns a new instance of AnalyzeLog.



16
17
18
19
20
21
22
23
24
# File 'lib/cosmos/gui/utilities/analyze_log.rb', line 16

def initialize(parent, packet_log_frame)
  @parent = parent
  @packet_log_frame = packet_log_frame
  @input_filenames = []
  @packet_log_reader = System.default_packet_log_reader.new(*System.default_packet_log_reader_params)
  @time_start = nil
  @time_end = nil
  @cancel = false
end

Class Method Details

.execute(parent, packet_log_frame) ⇒ Object



145
146
147
148
# File 'lib/cosmos/gui/utilities/analyze_log.rb', line 145

def self.execute(parent, packet_log_frame)
  log_analyzer = AnalyzeLog.new(parent, packet_log_frame)
  log_analyzer.analyze_log_files()
end

Instance Method Details

#analyze_files(progress_dialog) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cosmos/gui/utilities/analyze_log.rb', line 108

def analyze_files(progress_dialog)
  log_file_count = 1
  pkt_counts = {}
  @input_filenames.each do |log_file|
    break if @cancel
    begin
      Cosmos.check_log_configuration(@packet_log_reader, log_file)
      file_size = File.size(log_file).to_f
      progress_dialog.append_text("Analyzing File #{log_file_count}/#{@input_filenames.length}: #{log_file}")
      progress_dialog.set_step_progress(0.0)
      @packet_log_reader.each(
        log_file, # log filename
        true,     # identify and define packet
        @time_start,
        @time_end) do |packet|

        break if @cancel
        progress_dialog.set_step_progress(@packet_log_reader.bytes_read / file_size)
        pkt_counts[log_file] ||= {}
        pkt_counts[log_file]["#{packet.target_name} #{packet.packet_name}"] ||= 0
        pkt_counts[log_file]["#{packet.target_name} #{packet.packet_name}"] += 1       
      end
      progress_dialog.set_step_progress(1.0) if !@cancel
      progress_dialog.set_overall_progress(log_file_count.to_f / @input_filenames.length.to_f) if !@cancel
    rescue Exception => error
      progress_dialog.append_text("Error analyzing: #{error.formatted}\n")
    end
    log_file_count += 1
  end
  return pkt_counts
end

#analyze_log_filesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cosmos/gui/utilities/analyze_log.rb', line 26

def analyze_log_files
  @cancel = false
  pkt_counts = {}
  begin
    @packet_log_reader = @packet_log_frame.packet_log_reader
    @input_filenames = @packet_log_frame.filenames.sort
    @time_start = @packet_log_frame.time_start
    @time_end = @packet_log_frame.time_end
    unless @input_filenames and @input_filenames[0]
      Qt::MessageBox.critical(@parent, 'Error', 'Please select at least 1 input file')
      return
    end

    ProgressDialog.execute(@parent, # parent
                           'Log File Progress', # title
                           600, # width, height
                           300) do |progress_dialog|
      progress_dialog.cancel_callback = method(:cancel_callback)
      progress_dialog.enable_cancel_button

      begin
        Cosmos.set_working_dir do
          pkt_counts = analyze_files(progress_dialog)
        end
      ensure
        progress_dialog.complete
      end
    end

    if !@cancel
      results = "Log Analysis Complete.\n"
      results << "Log Reader: #{@packet_log_reader.class.to_s}\n"
      if @time_start
        results << "Start time: #{@time_start.formatted}\n"
      else
        results << "Start time: not specified\n"
      end
      if @time_end
        results << "End time:   #{@time_end.formatted}\n"
      else
        results << "End time:   not specified\n"
      end
      results << "\n"

      if pkt_counts.empty?
        results << "No files analyzed"
      else
        if pkt_counts.keys.size > 1
          pkt_counts_total = {}
          pkt_counts.each do |file, counts|
            counts.each do |pkt, count|
              pkt_counts_total[pkt] ||= 0
              pkt_counts_total[pkt] += count
            end
          end
          results << "Total count of packets found in all files:\n"
          pkt_counts_total.keys.sort.each do |pkt|
            results << "#{pkt}: #{pkt_counts_total[pkt]}\n"
          end
          results << "\n"
        end

        pkt_counts.each do |file, counts|
          results << "Packets found in file: #{file}\n"
          if counts.empty?
            results << "  No packets found\n"
          else
            counts.keys.sort.each do |pkt|
              results << "#{pkt}: #{counts[pkt]}\n"
            end
          end
          results << "\n"
        end
      end
      ScrollTextDialog.new(@parent, 'Packet Counts', results)
    end

  rescue => error
    Qt::MessageBox.critical(@parent, 'Error!', "Error Analyzing Log File(s)\n#{error.formatted}")
  end
end

#cancel_callback(progress_dialog = nil) ⇒ Object



140
141
142
143
# File 'lib/cosmos/gui/utilities/analyze_log.rb', line 140

def cancel_callback(progress_dialog = nil)
  @cancel = true
  return true, false
end