plugins/resource_management/app/helpers/resource_management/resource_bar_helper.rb
# Reads and validates the arguments given to resource_bar(), and performs
# some pre-computations.
Reads and validates the arguments given to resource_bar(), and performs
some pre-computations.
Reads and validates the arguments given to resource_bar () and performs some pre - computations .
def resbar_prepare_options(options)
      raise ArgumentError, "missing required argument 'fill'" unless options.has_key?(:fill)
      fill          = options[:fill]
      maximum       = options.fetch(:maximum,       fill)
      threshold     = options.fetch(:threshold,     maximum)
      data_type     = options.fetch(:data_type,     nil) || Core::DataType.new(:number)
      warning_level = options.fetch(:warning_level, 0.8)
      danger_level  = options.fetch(:danger_level,  1.0)
      marker        = options.fetch(:marker,        maximum)
      fill        = { value: fill,      label: "$VALUE" } unless fill.is_a?(Hash)
      maximum     = { value: maximum,   label: "$VALUE" } unless maximum.is_a?(Hash)
      threshold   = { value: threshold, label: "$VALUE" } unless threshold.is_a?(Hash)
      marker    =   { value: marker,    label: "$VALUE" } unless marker.is_a?(Hash)
      upper_bound = maximum[:value]
      if maximum[:value] < 0
        upper_bound = [ fill[:value], threshold[:value] ].max
      end
      [ fill, maximum, threshold, marker ].each do |param|
        display_value = data_type.format(param[:value])
        param[:label] = (param[:label] || '$VALUE').gsub("$VALUE", display_value)
      end
      [ fill, threshold, marker].each do |param|
        if upper_bound == 0
          param[:percent] = param[:value] <= 0 ? 0 : (param[:value].to_i << 10) 
        else
          param[:percent] = param[:value] <= 0 ? 0 : (param[:value].to_f / upper_bound * 100).to_i
        end
      end
      if marker[:related_to_threshold]
        if marker[:value] < threshold[:value]
          one_percent = threshold[:value].to_f / 100
          factor = 1
          if threshold[:value] < upper_bound
            factor = upper_bound.to_f / threshold[:value].to_f
          end
          value = (marker[:value].to_f / one_percent).to_f / factor
          marker[:percent] = value.round(1)
        end
      end
      return [ fill, maximum, threshold, upper_bound, warning_level, danger_level, marker ]
    end
def resbar_prepare_options ( options ) raise ArgumentError , "missing required argument 'fill'" unless options . has_key? ( :fill ) fill = options [ :fill ] maximum = options . fetch ( :maximum , fill ) threshold = options . fetch ( :threshold , maximum ) data_type = options . fetch ( :data_type , nil ) || Core :: DataType . new ( :number ) warning_level = options . fetch ( :warning_level , 0.8 ) danger_level = options . fetch ( :danger_level , 1.0 ) marker = options . fetch ( :marker , maximum ) fill = { value : fill , label : "$VALUE" } unless fill . is_a? ( Hash ) maximum = { value : maximum , label : "$VALUE" } unless maximum . is_a? ( Hash ) threshold = { value : threshold , label : "$VALUE" } unless threshold . is_a? ( Hash ) marker = { value : marker , label : "$VALUE" } unless marker . is_a? ( Hash ) upper_bound = maximum [ :value ] if maximum [ :value ] < 0 upper_bound = [ fill [ :value ] , threshold [ :value ] ] . max end [ fill , maximum , threshold , marker ] . each do | param | display_value = data_type . format ( param [ :value ] ) param [ :label ] = ( param [ :label ] || '$VALUE' ) . gsub ( "$VALUE" , display_value ) end [ fill , threshold , marker ] . each do | param | if upper_bound == 0 param [ :percent ] = param [ :value ] <= 0 ? 0 : ( param [ :value ] . to_i << 10 ) else param [ :percent ] = param [ :value ] <= 0 ? 0 : ( param [ :value ] . to_f / upper_bound * 100 ) . to_i end end if marker [ :related_to_threshold ] if marker [ :value ] < threshold [ :value ] one_percent = threshold [ :value ] . to_f / 100 factor = 1 if threshold [ :value ] < upper_bound factor = upper_bound . to_f / threshold [ :value ] . to_f end value = ( marker [ :value ] . to_f / one_percent ) . to_f / factor marker [ :percent ] = value . round ( 1 ) end end return [ fill , maximum , threshold , upper_bound , warning_level , danger_level , marker ] end
Reads and validates the arguments given to resource_bar(), and performs
some pre-computations.
Reads and validates the arguments given to resource_bar () and performs some pre - computations .
# default values # when only a number is given for some parameter, use the default label "$VALUE # choose upper_bound, the value that corresponds to the full width of the bar # prepare labels #[ fill, maximum, threshold ].each do |param| # calculate percentages relative to maximum (for CSS) # percentages are calculated in relation to "maximum" # "maximum" can be the fill or the maximum level # avoid division by zero, just scale very large # special case the marker is not related to maximum but to threshold # |-----------|-----------------------|---------------------------------| # | fill |    |                       |                                 | # |-----------|-----------------------|---------------------------------| # |<---30%--->| marker                | threshold                       | max # |<-----------------100%------------>| related_to_threshold            | upper_bound # |<---------------------------------200%-------------------------------| related to threshold # |<---------------------------------100%-------------------------------| related to max (upper bound) # related_to_threshold means threshold is 100% for the scale of the marker # default it's upper_bound # calculate the factor percentage # for instance: threshold = 100 and upper_bound = 200 the factor 2 # debug # puts "threshold #{threshold[:value]}" # puts "marker #{marker[:value]}" # puts "upper_bound #{upper_bound}" # puts "one_percent #{one_percent}" # puts "factor #{factor}" # puts "value #{value}"
options
<not_specific>