Project

General

Profile

Download (1.82 KB) Statistics
| Branch: | Tag: | Revision:
46ac4aa8 Ohad Levy
class FactValue < Puppet::Rails::FactValue
d8a93841 Ohad Levy
belongs_to :host #ensures we uses our Host model and not Puppets
b729fb04 Paul Kelly
delegate :name, :to => :fact_name
aa5a2230 Ohad Levy
c76afe1f Ohad Levy
scoped_search :on => :value, :in_key=> :fact_name, :on_key=> :name, :rename => :facts, :complete_value => true
scoped_search :on => :value
scoped_search :in => :fact_name, :on => :name, :complete_value => true
8a40834f Ohad Levy
scoped_search :in => :host, :on => :name, :rename => :host, :default_order => true
c76afe1f Ohad Levy
aa729377 Amos Benari
named_scope :no_timestamp_facts, :include => [:fact_name],
:conditions => ["fact_names.name <> ?","--- !ruby/sym _timestamp"]

named_scope :timestamp_facts, :joins => [:fact_name],
:conditions => ["fact_names.name = ?","--- !ruby/sym _timestamp"]
c76afe1f Ohad Levy
f685dd61 Ohad Levy
# Todo: find a way to filter which values are logged,
# this generates too much useless data
#
# acts_as_audited

300c8b44 Ohad Levy
# returns the average of all facts
# required only on facts that return a unit (e.g. MB, GB etc)
# normal facts could be used via the sum and AR average
def self.mem_average(fact)
values=all(:select => "value", :joins => :fact_name, :conditions => {:fact_names => {:name => fact}})
52a909a3 Ohad Levy
return values.size > 0 ? (values.map{|fv| fv.value.to_gb}.sum / values.size).round_with_precision(1) : 0
300c8b44 Ohad Levy
end

# returns the sum of each value, e.g. how many machines with 2,4...n cpu's
def self.count_each(fact)
hash = {}
e6c75845 Ohad Levy
all(:select => "value", :joins => :fact_name, :conditions => {:fact_names => {:name => fact}}).each do |fv|
300c8b44 Ohad Levy
value = fv.value.strip.humanize
if hash[value].nil?
hash[value] = 1
else
hash[value] += 1
end
end
hash
end
b729fb04 Paul Kelly
6032a1ff Ohad Levy
def self.build_facts_hash facts
hash = {}
facts.each do |fact|
hash[fact.host.to_s] ||= {}
hash[fact.host.to_s].update({fact.name.to_s => fact.value})
end
return hash
22712cbd Ohad Levy
end

d8a93841 Ohad Levy
end