Class: Metric

Inherits:
ApplicationRecord
  • Object
show all
Includes:
RequiredUniqueName, Toggleable
Defined in:
app/models/metric.rb

Overview

Metric for component

Attributes:

biovision_component_id [BiovisionComponent]
created_at [DateTime]
default_period [integer]
incremental [boolean]
previous_value [integer]
show_on_dashboard [boolean]
start_with_zero [boolean]
name [string]
updated_at [DateTime]
value [integer]

Constant Summary collapse

NAME_LIMIT =
255
PERIOD_RANGE =
(1..366).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object

Parameters:

  • name (String)


29
30
31
# File 'app/models/metric.rb', line 29

def self.[](name)
  find_by(name: name)
end

Instance Method Details

#<<(input) ⇒ Object

Parameters:

  • input (Integer)


38
39
40
41
42
# File 'app/models/metric.rb', line 38

def <<(input)
  metric_values.create(time: Time.now, quantity: input)

  update(value: incremental? ? quantity : input, previous_value: value)
end

#quantityObject



33
34
35
# File 'app/models/metric.rb', line 33

def quantity
  metric_values.sum(:quantity)
end

#values(period = 7) ⇒ Object

Parameters:

  • period (Integer) (defaults to: 7)


45
46
47
48
49
50
51
# File 'app/models/metric.rb', line 45

def values(period = 7)
  current_value = 0
  metric_values.since(period.days.ago).ordered_by_time.map do |v|
    current_value = incremental? ? current_value + v.quantity : v.quantity
    [v.time.strftime('%d.%m.%Y %H:%M'), current_value]
  end.to_h
end