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

Instance Method Summary collapse

Instance Method Details

#<<(input) ⇒ Object

Parameters:

  • input (Integer)


34
35
36
37
38
# File 'app/models/metric.rb', line 34

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

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

#quantityObject



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

def quantity
  metric_values.sum(:quantity)
end

#values(period = 7) ⇒ Object

Parameters:

  • period (Integer) (defaults to: 7)


41
42
43
44
45
46
47
# File 'app/models/metric.rb', line 41

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