class Skylight::Core::Normalizers::Moped::Query

Constants

CAT

Public Instance Methods

normalize(_trace, _name, payload) click to toggle source

Normalizes a Moped operation

@param trace [Skylight::Messages::Trace::Builder] ignored, only present to match API @param name [String] ignored, only present to match API @param payload [Hash] @option payload [Array<Moped::Protocol::*>] :ops array of performed operations. Supported operations are

`Query`, `GetMore`, `Insert`, `Update`, and `Delete`.

@option payload [String] :prefix ignored, provided by Moped @return [Array, :skip] the normalized array or `:skip` if not a known operation type

# File lib/skylight/core/normalizers/moped/query.rb, line 18
def normalize(_trace, _name, payload)
  # payload: { prefix: "  MOPED: #{address.resolved}", ops: operations }

  # We can sometimes have multiple operations. However, it seems like this only happens when doing things
  # like an insert, followed by a get last error, so we can probably ignore all but the first.
  operation = payload[:ops] ? payload[:ops].first : nil
  type = operation && operation.class.to_s =~ /^Moped::Protocol::(.+)$/ ? $1 : nil

  case type
  when "Query".freeze   then normalize_query(operation)
  when "GetMore".freeze then normalize_get_more(operation)
  when "Insert".freeze  then normalize_insert(operation)
  when "Update".freeze  then normalize_update(operation)
  when "Delete".freeze  then normalize_delete(operation)
  else :skip
  end
end

Private Instance Methods

extract_binds(hash) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 83
def extract_binds(hash)
  ret = {}

  hash.each do |k, v|
    ret[k] = v.is_a?(Hash) ? extract_binds(v) : "?".freeze
  end

  ret
end
normalize_delete(operation) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 70
def normalize_delete(operation)
  title = normalize_title("DELETE".freeze, operation)

  hash = extract_binds(operation.selector)
  description = hash.to_json

  [CAT, title, description, { database: operation.database }]
end
normalize_get_more(operation) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 47
def normalize_get_more(operation)
  title = normalize_title("GET_MORE".freeze, operation)

  [CAT, title, nil, { database: operation.database }]
end
normalize_insert(operation) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 53
def normalize_insert(operation)
  title = normalize_title("INSERT".freeze, operation)

  [CAT, title, nil, { database: operation.database }]
end
normalize_query(operation) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 38
def normalize_query(operation)
  title = normalize_title("QUERY".freeze, operation)

  hash = extract_binds(operation.selector)
  description = hash.to_json

  [CAT, title, description, { database: operation.database }]
end
normalize_title(type, operation) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 79
def normalize_title(type, operation)
  "#{type} #{operation.collection}"
end
normalize_update(operation) click to toggle source
# File lib/skylight/core/normalizers/moped/query.rb, line 59
def normalize_update(operation)
  title = normalize_title("UPDATE".freeze, operation)

  selector_hash = extract_binds(operation.selector)
  update_hash = extract_binds(operation.update)

  description = { selector: selector_hash, update: update_hash }.to_json

  [CAT, title, description, { database: operation.database }]
end