Class: Mouth::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/mouth/source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tuple) ⇒ Source

Returns a new instance of Source.



6
7
8
# File 'lib/mouth/source.rb', line 6

def initialize(tuple)
  self.all_attributes = tuple
end

Instance Attribute Details

#all_attributesObject

Returns the value of attribute all_attributes.



4
5
6
# File 'lib/mouth/source.rb', line 4

def all_attributes
  @all_attributes
end

Class Method Details

.allObject

Returns an array of tuples:

“auth.inline_logged_in”, kind: “counter|timer”, …


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mouth/source.rb', line 12

def self.all
  col_names = Mouth.mongo.collections.collect(&:name) - %w(dashboards graphs system.indexes)
  col_names.select! {|c| c =~ /^mouth_.+/ }
  
  tuples = []
  col_names.each do |col_name|
    namespace = col_name.match(/mouth_(.+)/)[1]
    counters, timers, gauges = all_for_collection(Mouth.collection(col_name))
    counters.each {|s| tuples << {:source => "#{namespace}.#{s}", :kind => "counter"} }
    timers.each {|s| tuples << {:source => "#{namespace}.#{s}", :kind => "timer"} }
    gauges.each {|s| tuples << {:source => "#{namespace}.#{s}", :kind => "gauge"} }
  end
  
  tuples.sort_by {|t| "#{t[:kind]}#{t[:source]}" }.collect {|t| new(t) }
end

.all_for_collection(col, window = Mouth.current_timestamp - 120) ⇒ Object



28
29
30
31
32
33
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
# File 'lib/mouth/source.rb', line 28

def self.all_for_collection(col, window = Mouth.current_timestamp - 120)
  map_function = <<-JS
    function() {
      var k, vh;
      for (k in this.c) {
        vh = {};
        vh[k] = true;
        emit("counters", {ks: vh});
      }
      for (k in this.m) {
        vh = {};
        vh[k] = true;
        emit("timers", {ks: vh});
      }
      for (k in this.g) {
        vh = {};
        vh[k] = true;
        emit("gauges", {ks: vh});
      }
    }
  JS
  
  reduce_function = <<-JS
    function(key, values) {
      var ret = {ks: {}}, k;
      
      values.forEach(function(value) {
        for (k in value.ks) {
          ret.ks[k] = true
        }
      });
      return ret;
    }
  JS
  
  result = col.map_reduce(map_function, reduce_function, :out => {:inline => true}, :raw => true, :query => {"t" => {"$gte" => window}})
  
  counters = []
  timers = []
  gauges = []
  if result && result["results"].is_a?(Array)
    result["results"].each do |r|
      k, v = r["_id"], r["value"]
      if k == "timers"
        timers << v["ks"].keys
      elsif k == "counters"
        counters << v["ks"].keys
      elsif k == "gauges"
        gauges << v["ks"].keys
      end
    end
  end
  
  [counters.flatten.compact.uniq, timers.flatten.compact.uniq, gauges.flatten.compact.uniq]
end