Class: Oximeter::Reports::Report

Inherits:
Struct
  • Object
show all
Includes:
Math
Defined in:
lib/oximeter/reports/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config

Returns:

  • (Object)

    the current value of config



5
6
7
# File 'lib/oximeter/reports/report.rb', line 5

def config
  @config
end

#entityObject

Returns the value of attribute entity

Returns:

  • (Object)

    the current value of 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_jsonObject



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_windowObject



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

Returns:

  • (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

#dataObject



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/oximeter/reports/report.rb', line 12

def has_data?
  data.length > 0
end

#meanObject



42
43
44
# File 'lib/oximeter/reports/report.rb', line 42

def mean
  sum = data.sum(&:count).to_f / data.length
end

#std_devObject

  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



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_windowObject



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