Project

General

Profile

Download (4.69 KB) Statistics
| Branch: | Tag: | Revision:
4deab2f3 Lukas Zapletal
class FactValue < ApplicationRecord
acfbc458 Marek Hulan
include Authorizable
e44f5c1c Daniel Lobato
include ScopedSearchExtensions
d7611b24 Greg Sutcliffe
9ba40b5e Tomer Brisker
belongs_to :host, {:class_name => "Host::Base", :foreign_key => :host_id}
d7611b24 Greg Sutcliffe
belongs_to :fact_name
f20020ce Daniel Lobato Garcia
delegate :name, :short_name, :compose, :origin, :to => :fact_name
18c29e63 Ohad Levy
has_many :hostgroup, :through => :host
aa5a2230 Ohad Levy
9e1b7578 Marek Hulan
has_one :parent_fact_name, :through => :fact_name, :source => :parent

9d43fc71 Michael Moll
scoped_search :on => :value, :in_key => :fact_name, :on_key => :name, :rename => :facts, :complete_value => true, :only_explicit => true, :ext_method => :search_cast_facts
3f8e6c33 imriz
scoped_search :on => :value, :default_order => true, :ext_method => :search_value_cast_facts
adc69935 lizagilman
scoped_search :on => :updated_at, :rename => :reported_at, :only_explicit => true, :complete_value => true
06b2899f Tomer Brisker
scoped_search :on => :host_id, :only_explicit => true, :complete_value => false

ed67b8c9 Dominic Cleal
scoped_search :relation => :fact_name, :on => :name, :complete_value => true, :aliases => ["fact"]
scoped_search :relation => :host, :on => :name, :complete_value => true, :rename => :host, :ext_method => :search_by_host_or_hostgroup, :only_explicit => true
scoped_search :relation => :hostgroup, :on => :name, :complete_value => true, :rename => :"host.hostgroup", :ext_method => :search_by_host_or_hostgroup, :only_explicit => true
scoped_search :relation => :fact_name, :on => :short_name, :complete_value => true, :aliases => ["fact_short_name"]
c83077a6 lizagilman
scoped_search :relation => :fact_name, :on => :type, :complete_value => false, :only_explicit => true, :aliases => ["origin"]
c76afe1f Ohad Levy
f2c78d4a Joseph Magen
scope :no_timestamp_facts, lambda {
3cd8c84b Michael Moll
joins(:fact_name).where("fact_names.name <> ?", :_timestamp)
96144a47 Daniel Lobato
}
f2c78d4a Joseph Magen
scope :timestamp_facts, lambda {
3cd8c84b Michael Moll
joins(:fact_name).where("fact_names.name = ?", :_timestamp)
96144a47 Daniel Lobato
}
caa5fcf0 Greg Sutcliffe
scope :my_facts, lambda {
9926db42 Dominic Cleal
if !User.current.admin? || Organization.expand(Organization.current).present? || Location.expand(Location.current).present?
joins_authorized(Host, :view_hosts, :where => Host.taxonomy_conditions)
3061e381 Ohad Levy
end
caa5fcf0 Greg Sutcliffe
}

bb3572ff Daniel Lobato
scope :facts_counter, ->(value, name_id) { where(:value => value, :fact_name_id => name_id) }
scope :with_fact_parent_id, ->(find_ids) { joins(:fact_name).merge FactName.with_parent_id(find_ids) }
e135dc98 Tomer Brisker
scope :with_roots, -> { joins(:fact_name) }
bb3572ff Daniel Lobato
scope :root_only, -> { with_roots.where(:fact_names => {:ancestry => nil}) }
c76afe1f Ohad Levy
c22f5db1 Timo Goebel
validates_lengths_from_database
fb68fd22 Ohad Levy
validates :fact_name_id, :uniqueness => { :scope => :host_id }

5ce4c4c5 Shlomi Zadok
def self.search_by_host_or_hostgroup(key, operator, value)
da9865b8 Michael Moll
host_or_hg = (key == 'host.hostgroup') ? 'hostgroup' : 'host'
search_term = (value =~ /\A\d+\Z/) ? 'id' : 'name'
5ce4c4c5 Shlomi Zadok
conditions = sanitize_sql_for_conditions(["#{host_or_hg.pluralize}.#{search_term} #{operator} ?", value_to_sql(operator, value)])
{ :joins => host_or_hg.to_sym, :conditions => conditions }
e44f5c1c Daniel Lobato
end

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)
b2cf234d Mikael Fridh
total, count = to_gb(fact)
return 0 if count == 0
017e1049 Ohad Levy
(total / count).round(1)
b2cf234d Mikael Fridh
end

# returns the rounded total of memory fact values (e.g. MB, GB etc)
def self.mem_sum(fact)
017e1049 Ohad Levy
to_gb(fact).first.to_f.round(1)
b2cf234d Mikael Fridh
rescue
0
300c8b44 Ohad Levy
end

# returns the sum of each value, e.g. how many machines with 2,4...n cpu's
4f7a4d0b David Davis
def self.count_each(fact, options = {})
870e7fcc Amos Benari
output = []
3cd8c84b Michael Moll
where({:fact_names => {:name => fact}}).joins(:fact_name).group(:value).count.each do |k, v|
070b83fe Amos Benari
label = case options[:unit]
when String
_(options[:unit]) % k
when Array
n_args = options[:unit].push(k.to_i)
n_(*n_args) % k
else
k
end
9d43fc71 Michael Moll
output << {:label => label, :data => v } unless v == 0
300c8b44 Ohad Levy
end
870e7fcc Amos Benari
output
300c8b44 Ohad Levy
end
b729fb04 Paul Kelly
8ebbbecf Shira Maximov
def self.build_facts_hash(values, host_id = nil)
hash = values.group_by(&:host_name).transform_values!{|val| val.map{|v| [v.fact_name_name, v.value]}.to_h}
if host_id && hash.any?
hash.merge! hash.values[0]
6032a1ff Ohad Levy
end
96144a47 Daniel Lobato
hash
22712cbd Ohad Levy
end

b2cf234d Mikael Fridh
# converts all strings with units (such as 1 MB) to GB scale and Sum them
# returns an array with total sum and number of elements
5f029ed6 Daniel Lobato
def self.to_gb(fact)
f2c78d4a Joseph Magen
values = select(:value).joins(:fact_name).where(:fact_names => {:name => fact}).map do |fv|
b2cf234d Mikael Fridh
fv.value.to_gb
end
19d5fb05 Ohad Levy
[ values.sum, values.size ]
b2cf234d Mikael Fridh
end
3f8e6c33 imriz
def self.search_cast_facts(key, operator, value)
{
f4459c11 David Davis
:conditions => "#{sanitize_sql_for_conditions(['fact_names.name = ?', key.split('.')[1]])} AND #{cast_facts('fact_values', key, operator, value)}",
:include => :fact_name
3f8e6c33 imriz
}
end

def self.search_value_cast_facts(key, operator, value)
{
57e9d8a3 Dominic Cleal
:conditions => cast_facts('fact_values', key, operator, value)
3f8e6c33 imriz
}
end
d8a93841 Ohad Levy
end