lib/ustate/aggregator.rb in ustate-client-0.0.7 vs lib/ustate/aggregator.rb in ustate-client-0.0.8

- old
+ new

@@ -20,10 +20,21 @@ def average(query, *a) fold query do |states| State.average states, *a end end + + # Average states with the same service across varying hosts. + # Useful when you don't know the exact services you'd like to fold. + # The resulting host for each folded state will be nil. + def average_over_hosts(query) + fold query do |states| + State.partition(states, :service).values.map do |slice| + State.sum slice, {host: nil} + end + end + end # Combines states matching query with the given block. The block # receives an array of states which presently match. # # Example: @@ -49,12 +60,17 @@ begin interval = (@interval.to_f / @folds.size) rescue @interval @folds.each do |f, query| matching = @index.query(Query.new(string: query)) unless matching.empty? - if combined = f[matching] + case combined = f[matching] + when State @index << combined + when Array + combined.each do |s| + @index << s + end end end sleep interval end rescue Exception => e @@ -67,9 +83,19 @@ # Combines states matching query with State.sum def sum(query, *a) fold query do |states| State.sum states, *a + end + end + + # Sum states with the same service across varying hosts. + # Useful when you don't know the exact services you'd like to fold. + def sum_over_hosts(query) + fold query do |states| + State.partition(states, :service).values.map do |slice| + State.sum slice, {host: nil} + end end end end end