Class: GroongaQueryLog::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga-query-log/parser.rb

Constant Summary collapse

PATTERN =
/\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d)
\ (?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\.(?<microsecond>\d+)
\|(?<context_id>.+?)
\|(?<type>[>:<])/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



40
41
42
43
44
45
46
47
48
49
# File 'lib/groonga-query-log/parser.rb', line 40

def initialize(options={})
  @options = options
  @slow_operation_threshold = options[:slow_operation_threshold]
  @slow_response_threshold = options[:slow_response_threshold]
  @target_commands = options[:target_commands]
  @target_tables = options[:target_tables]
  @parsing_statistics = {}

  @current_path = nil
end

Instance Attribute Details

#current_pathObject (readonly)

Returns the value of attribute current_path.



39
40
41
# File 'lib/groonga-query-log/parser.rb', line 39

def current_path
  @current_path
end

Class Method Details

.target_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/groonga-query-log/parser.rb', line 28

def target_line?(line)
  if line.respond_to?(:valid_encoding?)
    return false unless line.valid_encoding?
  end

  return false unless PATTERN.match(line)

  true
end

Instance Method Details

#parse(input) {|statistics| ... } ⇒ Object

Parses query-log file as stream to Analyzer::Statistics including some informations for each query.

Parameters:

  • input (IO)

    IO for input query log file.

Yields:

  • (statistics)

    if a block is specified, it is called every time a query is finished parsing.

Yield Parameters:



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
# File 'lib/groonga-query-log/parser.rb', line 60

def parse(input, &block)
  return to_enum(__method__, input) unless block_given?

  input.each_line do |line|
    next unless line.valid_encoding?

    match_data = PATTERN.match(line)
    next if match_data.nil?

    year = Integer(match_data[:year], 10)
    month = Integer(match_data[:month], 10)
    day = Integer(match_data[:day], 10)
    hour = Integer(match_data[:hour], 10)
    minute = Integer(match_data[:minute], 10)
    second = Integer(match_data[:second], 10)
    microsecond = Integer(match_data[:microsecond], 10)
    context_id = match_data[:context_id]
    type = match_data[:type]
    rest = match_data.post_match.strip
    time_stamp = Time.local(year,
                            month,
                            day,
                            hour,
                            minute,
                            second,
                            microsecond)
    parse_line(time_stamp, context_id, type, rest, &block)
  end
end

#parse_paths(paths, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/groonga-query-log/parser.rb', line 90

def parse_paths(paths, &block)
  return to_enum(__method__, paths) unless block_given?

  target_paths = GroongaLog::Parser.sort_paths(paths)
  target_paths.each do |path|
    GroongaLog::Input.open(path) do |log|
      @current_path = path
      begin
        parse(log, &block)
      ensure
        @current_path = nil
      end
    end
  end
end

#parsing_statisticsObject



106
107
108
# File 'lib/groonga-query-log/parser.rb', line 106

def parsing_statistics
  @parsing_statistics.values
end