Class: Fiveruns::Dash::Metric

Inherits:
Object
  • Object
show all
Includes:
Typable
Defined in:
lib/fiveruns/dash/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Typable

included

Constructor Details

#initialize(name, *args, &block) ⇒ Metric

Returns a new instance of Metric.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fiveruns/dash/metric.rb', line 10

def initialize(name, *args, &block)
  @@warned = false
  @name = name.to_s
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @description = args.shift || Util.titleize(@name)
  @help_text = args.shift
  @operation = block
  @virtual = !!options[:sources]
  @abstract = options[:abstract]
  validate!
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/fiveruns/dash/metric.rb', line 8

def description
  @description
end

#help_textObject (readonly)

Returns the value of attribute help_text.



8
9
10
# File 'lib/fiveruns/dash/metric.rb', line 8

def help_text
  @help_text
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/fiveruns/dash/metric.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/fiveruns/dash/metric.rb', line 8

def options
  @options
end

#recipeObject

Returns the value of attribute recipe.



9
10
11
# File 'lib/fiveruns/dash/metric.rb', line 9

def recipe
  @recipe
end

Instance Method Details

#==(other) ⇒ Object



92
93
94
# File 'lib/fiveruns/dash/metric.rb', line 92

def ==(other)
  key == other.key
end

#abstract?Boolean

Indicates that this metric is only used for virtual calculations and should not be sent to the server for storage.

Returns:

  • (Boolean)


30
31
32
# File 'lib/fiveruns/dash/metric.rb', line 30

def abstract?
  @abstract
end

#calculate(real_data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fiveruns/dash/metric.rb', line 39

def calculate(real_data)
  return nil unless virtual?

  datas = options[:sources].map {|met_name| real_data.detect { |hash| hash[:name] == met_name } }.compact

  if datas.size != options[:sources].size && options[:sources].include?('response_time')
    Fiveruns::Dash.logger.warn(<<-LOG
      ActiveRecord utilization metrics require a time metric so Dash can calculate a percentage of time spent in the database.
      Please set the :ar_total_time option when configuring Dash:

      # Define an application-specific metric cooresponding to the total processing time for this app.
      # You must mark this time so Dash can ignore any AR activity outside of the call stack.
      Fiveruns::Dash.register_recipe :loader, :url => 'http://dash.fiveruns.com' do |recipe|
        recipe.time :total_time, 'Load Time', :method => 'Loader::Engine#load', :mark => true
      end

      # Pass the name of this custom metric to Dash so it will be used in the AR metric calculations.
      Fiveruns::Dash.configure :app => token, :ar_total_time => 'total_time' do |config|
        config.add_recipe :activerecord
        config.add_recipe :loader, :url => 'http://dash.fiveruns.com'
      end
    LOG
    ) unless @@warned
    @@warned = true
    return nil
  else
    raise ArgumentError, "Could not find one or more of #{options[:sources].inspect} in #{real_data.map { |h| h[:name] }.inspect}" unless datas.size == options[:sources].size
  end

  combine(datas.map { |hsh| hsh[:values] }).merge(key)
end

#dataObject



34
35
36
37
# File 'lib/fiveruns/dash/metric.rb', line 34

def data
  return nil if virtual?
  value_hash.merge(key)
end

#find_context_with(&block) ⇒ Object

Set context finder



97
98
99
# File 'lib/fiveruns/dash/metric.rb', line 97

def find_context_with(&block)
  @context_finder = block
end

#infoObject



75
76
77
# File 'lib/fiveruns/dash/metric.rb', line 75

def info
  key
end

#keyObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fiveruns/dash/metric.rb', line 79

def key
  @key ||= begin
    {
      :name => name,
      :recipe_url => recipe ? recipe.url : nil,
      :recipe_name => recipe ? recipe.name.to_s : nil,
      :data_type => self.class.metric_type, 
      :description => description, 
      :help_text => help_text,
    }.merge(optional_info)
  end
end

#resetObject



71
72
73
# File 'lib/fiveruns/dash/metric.rb', line 71

def reset
  # Abstract
end

#virtual?Boolean

Indicates that this metric is calculated based on the value(s) of other metrics.

Returns:

  • (Boolean)


24
25
26
# File 'lib/fiveruns/dash/metric.rb', line 24

def virtual?
  @virtual
end