Class: CouchProxy::Rack::Stats

Inherits:
Base
  • Object
show all
Defined in:
lib/couchproxy/rack/stats.rb

Constant Summary

Constants inherited from Base

Base::APPLICATION_JSON, Base::DESIGN_ID, Base::INVALID_JSON, Base::METHODS, Base::SERVER_VERSION, Base::TEXT_PLAIN

Instance Attribute Summary

Attributes inherited from Base

#cluster, #request

Instance Method Summary collapse

Methods inherited from Base

#initialize, #method_missing, #proxy_to, #proxy_to_all_nodes, #proxy_to_all_partitions, #proxy_to_any_node, #proxy_to_any_partition, #replicate_to_all_partitions, #rewrite_location, #send_error_response, #send_response, #uuids

Constructor Details

This class inherits a constructor from CouchProxy::Rack::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CouchProxy::Rack::Base

Instance Method Details

#getObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/couchproxy/rack/stats.rb', line 6

def get
  proxy_to_all_nodes do |responses|
    docs = responses.map {|res| parse(res.response) }
    total = docs.shift.tap do |doc|
      each_stat(doc) do |group, name, values|
        %w[means stddevs].each {|k| values[k] = [values[k.chop]] }
      end
    end
    docs.each do |doc|
      each_stat(total) do |group, name, values|
        %w[current sum].each {|k| values[k] += doc[group][name][k] }
        %w[means stddevs].each {|k| values[k] << doc[group][name][k.chop] }
        %w[min max].each {|k| values[k] = [values[k], doc[group][name][k]].send(k) }
      end
    end
    each_stat(total) do |group, name, values|
      means, stddevs = %w[means stddevs].map {|k| values.delete(k) }
      mean = means.inject(:+) / means.size.to_f
      sums = means.zip(stddevs).map {|m, sd| m**2 + sd**2 }
      stddev = Math.sqrt(sums.inject(:+) / means.size.to_f - mean**2)
      mean, stddev = [mean, stddev].map {|f| sprintf('%.3f', f).to_f }
      values.update('stddev' => stddev, 'mean' => mean)
    end
    send_response(responses.first.response_header.status,
      response_headers, [total.to_json])
  end
end

#headObject



34
35
36
# File 'lib/couchproxy/rack/stats.rb', line 34

def head
  # FIXME
end