Class: Groonga::QueryLog::Analyzer::Statistic

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

Constant Summary collapse

DEFAULT_SLOW_OPERATION_THRESHOLD =
0.1
DEFAULT_SLOW_RESPONSE_THRESHOLD =
0.2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_id) ⇒ Statistic

Returns a new instance of Statistic.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 31

def initialize(context_id)
  @context_id = context_id
  @start_time = nil
  @command = nil
  @raw_command = nil
  @operations = []
  @elapsed = nil
  @return_code = 0
  @slow_operation_threshold = DEFAULT_SLOW_OPERATION_THRESHOLD
  @slow_response_threshold = DEFAULT_SLOW_RESPONSE_THRESHOLD
end

Instance Attribute Details

#context_idObject (readonly)

Returns the value of attribute context_id.



28
29
30
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 28

def context_id
  @context_id
end

#elapsedObject (readonly)

Returns the value of attribute elapsed.



29
30
31
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 29

def elapsed
  @elapsed
end

#raw_commandObject (readonly)

Returns the value of attribute raw_command.



28
29
30
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 28

def raw_command
  @raw_command
end

#return_codeObject (readonly)

Returns the value of attribute return_code.



29
30
31
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 29

def return_code
  @return_code
end

#slow_operation_thresholdObject

Returns the value of attribute slow_operation_threshold.



30
31
32
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 30

def slow_operation_threshold
  @slow_operation_threshold
end

#slow_response_thresholdObject

Returns the value of attribute slow_response_threshold.



30
31
32
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 30

def slow_response_threshold
  @slow_response_threshold
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



28
29
30
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 28

def start_time
  @start_time
end

Instance Method Details

#add_operation(operation) ⇒ Object



105
106
107
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 105

def add_operation(operation)
  @operations << operation
end

#commandObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 53

def command
  Groonga::Command::Parser.parse(@raw_command) do |status, command|
    case status
    when :on_load_start
      @loading = false
      @command ||= command
    when :on_command
      @command ||= command
    end
  end
  @command
end

#each_operationObject



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

def each_operation
  previous_elapsed = 0
  ensure_parse_command
  operation_context_context = {
    :filter_index => 0,
    :drilldown_index => 0,
  }
  @operations.each_with_index do |operation, i|
    relative_elapsed = operation[:elapsed] - previous_elapsed
    relative_elapsed_in_seconds = nano_seconds_to_seconds(relative_elapsed)
    previous_elapsed = operation[:elapsed]
    parsed_operation = {
      :i => i,
      :elapsed => operation[:elapsed],
      :elapsed_in_seconds => nano_seconds_to_seconds(operation[:elapsed]),
      :relative_elapsed => relative_elapsed,
      :relative_elapsed_in_seconds => relative_elapsed_in_seconds,
      :name => operation[:name],
      :context => operation_context(operation[:name],
                            operation_context_context),
      :n_records => operation[:n_records],
      :slow? => slow_operation?(relative_elapsed_in_seconds),
    }
    yield parsed_operation
  end
end

#elapsed_in_secondsObject



66
67
68
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 66

def elapsed_in_seconds
  nano_seconds_to_seconds(@elapsed)
end

#finish(elapsed, return_code) ⇒ Object



48
49
50
51
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 48

def finish(elapsed, return_code)
  @elapsed = elapsed
  @return_code = return_code
end

#last_timeObject



70
71
72
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 70

def last_time
  @start_time + elapsed_in_seconds
end

#operationsObject



109
110
111
112
113
114
115
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 109

def operations
  _operations = []
  each_operation do |operation|
    _operations << operation
  end
  _operations
end

#select_command?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 117

def select_command?
  command.name == "select"
end

#slow?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 74

def slow?
  elapsed_in_seconds >= @slow_response_threshold
end

#start(start_time, command) ⇒ Object



43
44
45
46
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 43

def start(start_time, command)
  @start_time = start_time
  @raw_command = command
end

#to_hashObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/groonga/query-log/analyzer/statistic.rb', line 121

def to_hash
  data = {
    "start_time" => start_time.to_i,
    "last_time" => last_time.to_i,
    "elapsed" => elapsed_in_seconds,
    "return_code" => return_code,
    "slow" => slow?,
  }
  arguments = command.arguments.collect do |key, value|
    {"key" => key, "value" => value}
  end
  data["command"] = {
    "raw" => raw_command,
    "name" => command.name,
    "parameters" => arguments,
  }
  operations = []
  each_operation do |operation|
    operation_data = {}
    operation_data["name"] = operation[:name]
    operation_data["relative_elapsed"] = operation[:relative_elapsed_in_seconds]
    operation_data["context"] = operation[:context]
    operation_data["slow"] = operation[:slow?]
    operations << operation_data
  end
  data["operations"] = operations
  data
end