class Oximeter::Reports::Report

Public Class Methods

for_model(entity) click to toggle source
# File lib/oximeter/reports/report.rb, line 8
def self.for_model(entity)
  new(entity, Oximeter::Configuration::get_model(entity))
end

Public Instance Methods

charts_json() click to toggle source
# 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() click to toggle source
# File lib/oximeter/reports/report.rb, line 37
def counts_view_window
  times = data.map(&:time)
  [times.min, times.max]
end
critical_value?() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# File lib/oximeter/reports/report.rb, line 12
def has_data?
  data.length > 0
end
mean() click to toggle source
# File lib/oximeter/reports/report.rb, line 42
def mean
  sum = data.sum(&:count).to_f / data.length
end
std_dev() click to toggle source
  1. Work out the Mean (the simple average of the numbers)

  2. Then for each number: subtract the Mean and square the result

  3. Then work out the mean of those squared differences.

  4. Take the square root of that

# 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() click to toggle source
# File lib/oximeter/reports/report.rb, line 33
def time_view_window
  [data.first.time - 1.hour, data.last.time + 1.hour]
end