Class: GroongaQueryLog::Statistic

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga-query-log/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.



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

def initialize(context_id)
  @context_id = context_id
  @start_time = 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.



24
25
26
# File 'lib/groonga-query-log/statistic.rb', line 24

def context_id
  @context_id
end

#elapsedObject (readonly)

Returns the value of attribute elapsed.



25
26
27
# File 'lib/groonga-query-log/statistic.rb', line 25

def elapsed
  @elapsed
end

#raw_commandObject (readonly)

Returns the value of attribute raw_command.



24
25
26
# File 'lib/groonga-query-log/statistic.rb', line 24

def raw_command
  @raw_command
end

#return_codeObject (readonly)

Returns the value of attribute return_code.



25
26
27
# File 'lib/groonga-query-log/statistic.rb', line 25

def return_code
  @return_code
end

#slow_operation_thresholdObject

Returns the value of attribute slow_operation_threshold.



26
27
28
# File 'lib/groonga-query-log/statistic.rb', line 26

def slow_operation_threshold
  @slow_operation_threshold
end

#slow_response_thresholdObject

Returns the value of attribute slow_response_threshold.



26
27
28
# File 'lib/groonga-query-log/statistic.rb', line 26

def slow_response_threshold
  @slow_response_threshold
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



24
25
26
# File 'lib/groonga-query-log/statistic.rb', line 24

def start_time
  @start_time
end

Instance Method Details

#add_operation(operation) ⇒ Object



100
101
102
# File 'lib/groonga-query-log/statistic.rb', line 100

def add_operation(operation)
  @operations << operation
end

#cache_used?Boolean

Returns:

  • (Boolean)


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

def cache_used?
  each_operation.any? do |operation|
    operation[:name] == "cache"
  end
end

#commandObject



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

def command
  @command ||= parse_command
end

#each_operationObject



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

def each_operation
  return to_enum(__method__) unless block_given?

  previous_elapsed = 0
  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,
                                    operation_context_context),
      :n_records => operation[:n_records],
      :extra => operation[:extra],
      :slow? => slow_operation?(relative_elapsed_in_seconds),
      :raw_message => operation[:raw_message],
    }
    yield parsed_operation
  end
end

#elapsed_in_secondsObject



52
53
54
# File 'lib/groonga-query-log/statistic.rb', line 52

def elapsed_in_seconds
  nano_seconds_to_seconds(@elapsed)
end

#finish(elapsed, return_code) ⇒ Object



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

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

#last_timeObject



56
57
58
# File 'lib/groonga-query-log/statistic.rb', line 56

def last_time
  @start_time + elapsed_in_seconds
end

#operationsObject



104
105
106
107
108
109
110
# File 'lib/groonga-query-log/statistic.rb', line 104

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

#select_family_command?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
# File 'lib/groonga-query-log/statistic.rb', line 112

def select_family_command?
  case command.command_name
  when "select", "range_filter"
    true
  when "logical_select", "logical_range_filter"
    true
  else
    false
  end
end

#slow?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/groonga-query-log/statistic.rb', line 60

def slow?
  elapsed_in_seconds >= @slow_response_threshold
end

#start(start_time, command) ⇒ Object



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

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

#to_hashObject



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
149
150
151
152
153
# File 'lib/groonga-query-log/statistic.rb', line 123

def to_hash
  data = {
    "start_time" => start_time.to_f,
    "last_time" => last_time.to_f,
    "elapsed" => elapsed_in_seconds,
    "return_code" => return_code,
    "slow" => slow?,
  }
  if command
    data["command"] = {
      "raw" => raw_command,
      "name" => command.name,
      "parameters" => command_arguments,
    }
  else
    data["command"] = {
      "raw" => raw_command
    }
  end
  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