Class: Oximeter::Reports::Report
- Inherits:
-
Struct
- Object
- Struct
- Oximeter::Reports::Report
- Includes:
- Math
- Defined in:
- lib/oximeter/reports/report.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#entity ⇒ Object
Returns the value of attribute entity.
Class Method Summary collapse
Instance Method Summary collapse
- #charts_json ⇒ Object
- #counts_view_window ⇒ Object
- #critical_value? ⇒ Boolean
- #data ⇒ Object
- #has_data? ⇒ Boolean
- #mean ⇒ Object
-
#std_dev ⇒ Object
1.
- #time_view_window ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config
5 6 7 |
# File 'lib/oximeter/reports/report.rb', line 5 def config @config end |
#entity ⇒ Object
Returns the value of attribute entity
5 6 7 |
# File 'lib/oximeter/reports/report.rb', line 5 def entity @entity end |
Class Method Details
.for_model(entity) ⇒ Object
8 9 10 |
# File 'lib/oximeter/reports/report.rb', line 8 def self.for_model(entity) new(entity, Oximeter::Configuration::get_model(entity)) end |
Instance Method Details
#charts_json ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/oximeter/reports/report.rb', line 55 def charts_json { entity_name: entity.to_s, view_window: { time: time_view_window, counts: counts_view_window }, rows: data } end |
#counts_view_window ⇒ Object
37 38 39 40 |
# File 'lib/oximeter/reports/report.rb', line 37 def counts_view_window times = data.map(&:time) [times.min, times.max] end |
#critical_value? ⇒ Boolean
16 17 18 19 20 21 22 23 |
# File 'lib/oximeter/reports/report.rb', line 16 def critical_value? time_period_fraction = (Time.now - data.last.time).to_f / 1.send(config.period) weighted_mean = time_period_fraction * mean weighted_std_dev = time_period_fraction * std_dev (data.last.count - weighted_mean).abs > weighted_std_dev end |
#data ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/oximeter/reports/report.rb', line 25 def data @data ||= entity .where('created_at BETWEEN ? AND ?', (config.pull_last + 1.minute).ago, Time.now) .group_by_period(config.period, config.group_field) .count .map { |point| Period.new(point[0], point[1]) } end |
#has_data? ⇒ Boolean
12 13 14 |
# File 'lib/oximeter/reports/report.rb', line 12 def has_data? data.length > 0 end |
#mean ⇒ Object
42 43 44 |
# File 'lib/oximeter/reports/report.rb', line 42 def mean sum = data.sum(&:count).to_f / data.length end |
#std_dev ⇒ Object
-
Work out the Mean (the simple average of the numbers)
-
Then for each number: subtract the Mean and square the result
-
Then work out the mean of those squared differences.
-
Take the square root of that
50 51 52 53 |
# File 'lib/oximeter/reports/report.rb', line 50 def std_dev n = data.length sqrt data.sum { |x| (x.count - mean) ** 2 } / n end |
#time_view_window ⇒ Object
33 34 35 |
# File 'lib/oximeter/reports/report.rb', line 33 def time_view_window [data.first.time - 1.hour, data.last.time + 1.hour] end |