Class: Salus::Group

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Lockable
Defined in:
lib/salus/group.rb

Instance Method Summary collapse

Methods included from Lockable

#broadcast, #signal, #synchronize, #wait, #wait_until

Constructor Details

#initialize(defaults = {}, &block) ⇒ Group

Returns a new instance of Group.



18
19
20
21
22
23
24
# File 'lib/salus/group.rb', line 18

def initialize(defaults={}, &block)
  @_metrics = {}
  @_groups  = {}
  @_cache   = {}
  @_proc    = block
  @_opts    = defaults.clone
end

Instance Method Details

#default(opts) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/salus/group.rb', line 47

def default(opts)
  return unless opts.is_a?(Hash)
  opts.each do |k, v|
    next if [:value, :timestamp].include?(k)
    @_opts[k] = v
  end
end

#each(allow_mute = false, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/salus/group.rb', line 99

def each(allow_mute=false, &block)
  synchronize do
    if allow_mute
      @_metrics.each(&block)
    else
      @_metrics.select { |k, v| !v.mute? }.each(&block)
    end
  end
end

#group(title, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/salus/group.rb', line 55

def group(title, &block)
  synchronize do
    unless @_groups.key?(title)
      @_groups[title] = Group.new(@_opts, &block)
      if @_cache.key?(title)
        @_groups[title].load(@_cache[title])
        @_cache.delete(title)
      end
    end
  end
end

#groupsObject



67
68
69
# File 'lib/salus/group.rb', line 67

def groups
  synchronize { @_groups }
end

#has_subgroups?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/salus/group.rb', line 71

def has_subgroups?
  synchronize { !@_groups.empty? }
end

#keys(allow_mute = false) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/salus/group.rb', line 79

def keys(allow_mute=false)
  synchronize do
    if allow_mute
      @_metrics.keys
    else
      @_metrics.keys.select { |x| !@_metrics[x].mute? }
    end
  end
end

#load(data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/salus/group.rb', line 109

def load(data)
  return unless data
  return if data.empty?
  synchronize do
    if data.key?(:defaults)
      @_opts = data[:defaults].clone
    end
    if data.key?(:metrics)
      types = Metric.descendants.map{ |x| x.name.split("::").last }
      data[:metrics].each do |k, v|
        next unless v.key?(:type)
        next unless types.include?(v[:type])
        @_metrics[k] = Object.const_get("Salus::" + v[:type]).new(@_opts)
        @_metrics[k].load(v)
      end
    end
    if data.key?(:groups)
      @_cache = data[:groups]
    end
  end
end

#on_win?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/salus/group.rb', line 39

def on_win?
  Salus.on_win?
end

#saveObject



131
132
133
# File 'lib/salus/group.rb', line 131

def save
  to_h
end

#tickObject



153
154
155
156
157
158
159
# File 'lib/salus/group.rb', line 153

def tick
  instance_eval(&@_proc)
  @_groups.each do |k, v|
    v.tick
  end
  @_cache.clear unless @_cache.empty?
end

#to_hObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/salus/group.rb', line 135

def to_h
  ret = {}
  synchronize do
    unless @_metrics.empty?
      ret[:metrics] = {}
      @_metrics.each { |k, v| ret[:metrics][k] = v.to_h }
    end
    unless @_groups.empty?
      ret[:groups]  = {}
      @_groups.each  { |k, v| ret[:groups][k]  = v.to_h }
    end
    unless @_opts.empty?
      ret[:defaults] = @_opts
    end
    ret
  end
end

#value(title) ⇒ Object



75
76
77
# File 'lib/salus/group.rb', line 75

def value(title)
  synchronize { @_metrics.key?(title) ? @_metrics[title].value : nil }
end

#values(allow_mute = false) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/salus/group.rb', line 89

def values(allow_mute=false)
  synchronize do
    if allow_mute
      @_metrics.values
    else
      @_metrics.values.select { |x| !x.mute? }
    end
  end
end

#var(arg, default = nil, &block) ⇒ Object



43
44
45
# File 'lib/salus/group.rb', line 43

def var(arg, default=nil, &block)
  Salus.var(arg, default, &block)
end