Class: UState::State

Inherits:
Object
  • Object
show all
Includes:
Beefcake::Message
Defined in:
lib/ustate/state.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ State

Returns a new instance of State.



151
152
153
154
155
# File 'lib/ustate/state.rb', line 151

def initialize(*a)
  super *a

  @time ||= Time.now.to_i
end

Class Method Details

.average(states, init = State.new) ⇒ Object

Average a set of states together. Chooses the mean metric, the mode state, mode service, and the mean time. If init is provided, its values override (where present) the computed ones.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ustate/state.rb', line 16

def self.average(states, init = State.new)
  init = case init
         when State
           init.dup
         else
           State.new init
         end
  
  # Metric
  init.metric_f ||= states.inject(0.0) { |a, state|
      a + (state.metric || 0)
    } / states.size
  if init.metric_f.nan?
    init.metric_f = 0.0
  end

  # State
  init.state ||= mode states.map(&:state)
  init.service ||= mode states.map(&:service)

  # Time
  init.time = begin 
    times = states.map(&:time).compact
    (times.inject(:+) / times.size).to_i
  rescue 
  end
  init.time ||= Time.now.to_i

  init
end

.max(states, init = State.new) ⇒ Object

Finds the maximum of a set of states. Metric is the maximum. State is the highest, as defined by Dash.config.state_order. Time is the mean.



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
# File 'lib/ustate/state.rb', line 83

def self.max(states, init = State.new)
  init = case init
         when State
           init.dup
         else
           State.new init
         end
  
  # Metric
  init.metric_f ||= states.inject(0.0) { |a, state|
      a + (state.metric || 0)
    }
  if init.metric_f.nan?
    init.metric_f = 0.0
  end

  # State
  init.state ||= states.inject(nil) do |max, state|
    state.state if Dash.config[:state_order][state.state] > Dash.config[:state_order][max]
  end

  # Time
  init.time = begin 
    times = states.map(&:time).compact
    (times.inject(:+) / times.size).to_i
  rescue 
  end
  init.time ||= Time.now.to_i

  init
end

.mode(array) ⇒ Object



115
116
117
118
119
120
# File 'lib/ustate/state.rb', line 115

def self.mode(array)
  array.inject(Hash.new(0)) do |counts, e|
    counts[e] += 1
    counts
  end.sort_by { |e, count| count }.last.first rescue nil
end

.partition(states, field) ⇒ Object

Partition a list of states by a field Returns a hash of field_value => state



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ustate/state.rb', line 124

def self.partition(states, field)
  states.inject({}) do |p, state|
    k = state.send field
    if p.include? k
      p[k] << state
    else
      p[k] = [state]
    end
    p
  end
end

.sort(states, field) ⇒ Object

Sorts states by a field. nil values first.



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ustate/state.rb', line 137

def self.sort(states, field)
  states.sort do |a, b|
    a = a.send field
    b = b.send field
    if a.nil?
      -1
    elsif b.nil?
      1
    else
      a <=> b
    end
  end
end

.sum(states, init = State.new) ⇒ Object

Sum a set of states together. Adds metrics, takes the mode state, mode service and the mean time. If init is provided, its values override (where present) the computed ones.



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
# File 'lib/ustate/state.rb', line 50

def self.sum(states, init = State.new)
  init = case init
         when State
           init.dup
         else
           State.new init
         end
  
  # Metric
  init.metric_f ||= states.inject(0.0) { |a, state|
      a + (state.metric || 0)
    }
  if init.metric_f.nan?
    init.metric_f = 0.0
  end

  # State
  init.state ||= mode states.map(&:state)
  init.service ||= mode states.map(&:service)

  # Time
  init.time = begin 
    times = states.map(&:time).compact
    (times.inject(:+) / times.size).to_i
  rescue 
  end
  init.time ||= Time.now.to_i

  init
end

Instance Method Details

#metricObject



157
158
159
# File 'lib/ustate/state.rb', line 157

def metric
  @metric || metric_f
end

#metric=(m) ⇒ Object



161
162
163
# File 'lib/ustate/state.rb', line 161

def metric=(m)
  @metric = m
end