Class: Metric

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

Overview

Metric

Attributes:

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

Constant Summary collapse

NAME_LIMIT =
255
PERIOD_RANGE =
(1..365).freeze
METRIC_HTTP_400 =
'errors.http.bad_request.hit'
METRIC_HTTP_401 =
'errors.http.unauthorized.hit'
METRIC_HTTP_403 =
'errors.http.forbidden.hit'
METRIC_HTTP_404 =
'errors.http.not_found.hit'
METRIC_HTTP_422 =
'errors.http.unprocessable_entity.hit'
METRIC_HTTP_500 =
'errors.http.internal_server_error.hit'
METRIC_HTTP_503 =
'errors.http.service_unavailable.hit'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entity_parametersObject



45
46
47
# File 'app/models/metric.rb', line 45

def self.entity_parameters
  %i[incremental start_with_zero show_on_dashboard default_period description]
end

.page_for_administrationObject



41
42
43
# File 'app/models/metric.rb', line 41

def self.page_for_administration
  order('name asc')
end

.register(name, quantity = 1) ⇒ Object

Parameters:

  • name (String)
  • quantity (Integer) (defaults to: 1)


51
52
53
54
55
56
57
58
# File 'app/models/metric.rb', line 51

def self.register(name, quantity = 1)
  instance = Metric.find_by(name: name)
  if instance.nil?
    instance = create(name: name, incremental: !name.end_with?('.hit'))
  end

  instance << quantity
end

Instance Method Details

#<<(input) ⇒ Object

Parameters:

  • input (Integer)


65
66
67
68
69
# File 'app/models/metric.rb', line 65

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

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

#graph_data(period = default_period, resolution = 4) ⇒ Object

Parameters:

  • period (Integer) (defaults to: default_period)
  • resolution (Integer) (defaults to: 4)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/metric.rb', line 82

def graph_data(period = default_period, resolution = 4)
  result        = Hash.new(0)
  current_value = 0
  metric_values.since(period.days.ago).ordered_by_time.each do |v|
    key           = v.time_for_graph(resolution).strftime('%d.%m.%Y %H:%M')
    current_value = incremental? ? current_value + v.quantity : v.quantity
    if result.key?(key)
      result[key] = current_value
    else
      result[key] += current_value
    end
  end
  result
end

#quantityObject



60
61
62
# File 'app/models/metric.rb', line 60

def quantity
  metric_values.sum(:quantity)
end

#values(period = 7) ⇒ Object

Parameters:

  • period (Integer) (defaults to: 7)


72
73
74
75
76
77
78
# File 'app/models/metric.rb', line 72

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