Class: ActiveMetric::Subject

Inherits:
Object
  • Object
show all
Includes:
GraphCalculation, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/active_metric/subject.rb

Constant Summary

Constants included from GraphCalculation

GraphCalculation::MONGO_MAX_LIMIT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GraphCalculation

#calculate_data_to_pop, #debug, #graph_view_model_starting_at, #has_graph_data, included, #initialize_graph_view_model, #pop_from_series, #pop_necessary_samples, #series, #start_time, #time, #update_graph_model

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



15
16
17
18
# File 'lib/active_metric/subject.rb', line 15

def method_missing(method, *args)
  self.class.send(:define_method, method.to_sym) { value_from_summary(method) }
  value_from_summary(method)
end

Class Method Details

.calculated_with(sample_type, interval_length, summary_type = sample_type) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_metric/subject.rb', line 103

def self.calculated_with(sample_type, interval_length, summary_type = sample_type)
  instance_eval %Q|
    def self.sample_type
      #{sample_type}
    end

    def self.interval_length
      #{interval_length}
    end

    def self.summary_type
      #{summary_type}
    end
  |
end

.interval_lengthObject



99
100
101
# File 'lib/active_metric/subject.rb', line 99

def self.interval_length
  raise
end

.sample_typeObject



95
96
97
# File 'lib/active_metric/subject.rb', line 95

def self.sample_type
  nil
end

Instance Method Details

#calculate(measurement) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/active_metric/subject.rb', line 50

def calculate(measurement)
  handle_sample_rollover(measurement)

  summary.calculate(measurement)
  current_sample.calculate(measurement)
  update_subject_calculators(measurement)
end

#completeObject



78
79
80
81
82
83
84
85
# File 'lib/active_metric/subject.rb', line 78

def complete
  summary.complete
  summary.save!
  current_sample.complete
  update_graph_model(current_sample)

  save!
end

#current_sampleObject



87
88
89
90
91
92
93
# File 'lib/active_metric/subject.rb', line 87

def current_sample
  @current_sample ||= self.class.sample_type.new({:samplable => self,
                                                  :interval => self.class.interval_length},
                                                 {},
                                                 nil,
                                                 graph_view_model.size)
end

#ensure_standard_deviator_for(property) ⇒ Object



46
47
48
# File 'lib/active_metric/subject.rb', line 46

def ensure_standard_deviator_for(property)
  standard_deviators[property] ||= StandardDeviator.new(property)
end

#handle_sample_rollover(measurement) ⇒ Object



58
59
60
61
62
63
# File 'lib/active_metric/subject.rb', line 58

def handle_sample_rollover(measurement)
  if need_new_sample?(measurement)
    complete
    @current_sample = current_sample.new_sample
  end
end

#is_new_sample?(returned_sample) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/active_metric/subject.rb', line 69

def is_new_sample?(returned_sample)
  returned_sample != @current_sample
end

#need_new_sample?(measurement) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/active_metric/subject.rb', line 65

def need_new_sample?(measurement)
  !current_sample.within_interval? measurement
end

#not_inherited_attributesObject



20
21
22
23
24
# File 'lib/active_metric/subject.rb', line 20

def not_inherited_attributes
  attributes.reject do |key|
    Subject.fields.has_key? key
  end
end

#reservoirObject



38
39
40
# File 'lib/active_metric/subject.rb', line 38

def reservoir
  @reservoir ||= Reservoir.new(2000)
end

#standard_deviatorsObject



42
43
44
# File 'lib/active_metric/subject.rb', line 42

def standard_deviators
  @standard_deviators ||= {}
end

#summaryObject



34
35
36
# File 'lib/active_metric/subject.rb', line 34

def summary
  self.samples ||= self.class.summary_type.find_or_create_by(:samplable => self)
end

#update_subject_calculators(measurement) ⇒ Object



73
74
75
76
# File 'lib/active_metric/subject.rb', line 73

def update_subject_calculators(measurement)
  reservoir.fill(measurement)
  standard_deviators.values.each { |sd| sd.calculate(measurement) }
end

#value_from_summary(method) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/active_metric/subject.rb', line 26

def value_from_summary(method)
  ret_value = summary.send(method)
  if ret_value.is_a?(Stat)
    return ret_value.value
  end
  ret_value
end