Class: Rack::Insight::SphinxPanel::Stats::Query

Inherits:
Object
  • Object
show all
Includes:
FilteredBacktrace
Defined in:
lib/rack/insight/panels/sphinx_panel/stats.rb

Constant Summary collapse

MatchModes =
Riddle::Client::MatchModes.invert
RankModes =
Riddle::Client::RankModes.invert
SortModes =
Riddle::Client::SortModes.invert
AttributeTypes =
Riddle::Client::AttributeTypes.invert
GroupFunctions =
Riddle::Client::GroupFunctions.invert
FilterTypes =
Riddle::Client::FilterTypes.invert

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FilteredBacktrace

#backtrace, backtrace_regexp, #filtered_backtrace, #has_backtrace?, root_for_backtrace_filtering

Constructor Details

#initialize(time, command_args, method_call) ⇒ Query

Returns a new instance of Query.



19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 19

def initialize(time, command_args, method_call)
  riddle_command, messages = *command_args
  @time = time
  if riddle_command == :search
    @command = "search: " + decode_message(messages.first).inspect
  else
    @command = command_args.inspect + ": No more info is available for this Sphinx request type"
  end
  @backtrace = method_call.backtrace
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



17
18
19
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 17

def command
  @command
end

#timeObject (readonly)

Returns the value of attribute time.



16
17
18
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 16

def time
  @time
end

Instance Method Details

#consume_64intObject



158
159
160
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 158

def consume_64int
  @m.slice!(0, 8).unpack("NN").first
end

#consume_floatObject



162
163
164
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 162

def consume_float
  @m.slice!(0, 4).unpack('N').pack('L*').unpack('f').first
end

#consume_intObject



154
155
156
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 154

def consume_int
  @m.slice!(0, 4).unpack("N").first
end

#consume_stringObject



166
167
168
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 166

def consume_string
  @m.slice!(0, consume_int)
end

#decode_message(m) ⇒ Object



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
107
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 34

def decode_message(m)
  @m = m.clone
  params = ActiveSupport::OrderedHash.new

  # Mode, Limits
  params[:offset] = consume_int
  params[:limit] = consume_int
  params[:match_mode] = MatchModes[consume_int]

  # Ranking
  params[:rank_mode] = RankModes[consume_int]
  if params[:rank_mode] == :expr
    params[:rank_expr] = consume_string
  end

  # Sort Mode
  params[:sorting] = {
    mode: SortModes[consume_int],
    by: consume_string,
  }

  # Query
  params[:query] = consume_string

  # Weights
  params[:weights] = (1..consume_int).map { consume_int }

  # Index
  params[:index] = consume_string

  # ID Range
  consume_int
  params[:id_range] = consume_64int..consume_64int

  # Filters
  params[:filters] = (1..consume_int).map do
    attribute = consume_string
    type = FilterTypes[consume_int]
    values =
      case type
      when :values
        (1..consume_int).map { consume_64int }
      when :range
        consume_64int..consume_64int
      when :float_range
        consume_float..consume_float
      end
    exclude = consume_int
    {attribute: attribute, values: values, exclude: exclude}
  end

  # Grouping
  params[:group] = {
    function: GroupFunctions[consume_int],
    by: consume_string,
    max_matches: consume_int,
    clause: consume_string,
    retry: {cutoff: consume_int, count: consume_int, delay: consume_int},
    distinct: consume_string,
  }

  # Anchor Point
  if consume_int == 0
    params[:anchor] = nil
  else
    params[:anchor] = {
      attributes: [consume_string, consume_string],
      values: [consume_int, consume_int],
    }
  end

  # Per Index Weights
  per_index_weights = params[:per_index_weights] = {}
  (1..consume_int).each do |key, value|
    key = consume_string
    value = consume_int
    per_index_weights[key] = value
  end

  # Max Query Time
  params[:max_query_time] = consume_int

  # Per Field Weights
  per_field_weights = params[:per_field_weights] = {}
  (1..consume_int).each do |key, value|
    key = consume_string
    value = consume_int
    per_field_weights[key] = value
  end

  params[:comments] = consume_string

  return params if Riddle::Client::Versions[:search] < 0x116

  # Overrides
  overrides = params[:overrides] = {}
  (1..consume_int).each do
    key = consume_string
    type = AttributeTypes[consume_int]
    method =
      case type
      when :float
        :consume_float
      when :bigint
        :consume_64int
      else
        :consume_int
      end
    values = (1..consume_int).map { send(method) }
    overrides[key] = values
  end

  params[:select] = consume_string

  @m.empty? or
    params[:unknown] = @m

  params
end

#display_timeObject



30
31
32
# File 'lib/rack/insight/panels/sphinx_panel/stats.rb', line 30

def display_time
  "%.2fms" % time
end