Class: ThreeScaleToolbox::Entities::Metric

Inherits:
Object
  • Object
show all
Includes:
CRD::MetricSerializer
Defined in:
lib/3scale_toolbox/entities/metric.rb

Constant Summary collapse

METRIC_BLACKLIST =
%w[id links created_at updated_at].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::MetricSerializer

#to_cr

Constructor Details

#initialize(id:, service:, attrs: nil) ⇒ Metric

Returns a new instance of Metric.



32
33
34
35
36
37
# File 'lib/3scale_toolbox/entities/metric.rb', line 32

def initialize(id:, service:, attrs: nil)
  @id = id.to_i
  @service = service
  @remote = service.remote
  @attrs = attrs
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



30
31
32
# File 'lib/3scale_toolbox/entities/metric.rb', line 30

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



30
31
32
# File 'lib/3scale_toolbox/entities/metric.rb', line 30

def remote
  @remote
end

#serviceObject (readonly)

Returns the value of attribute service.



30
31
32
# File 'lib/3scale_toolbox/entities/metric.rb', line 30

def service
  @service
end

Class Method Details

.create(service:, attrs:) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/3scale_toolbox/entities/metric.rb', line 9

def create(service:, attrs:)
  metric = service.remote.create_metric service.id, attrs
  if (errors = metric['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been created', errors)
  end

  new(id: metric.fetch('id'), service: service, attrs: metric)
end

.find(service:, ref:) ⇒ Object

ref can be system_name or metric_id



19
20
21
22
23
# File 'lib/3scale_toolbox/entities/metric.rb', line 19

def find(service:, ref:)
  new(id: ref, service: service).tap(&:attrs)
rescue ThreeScale::API::HttpClient::NotFoundError
  find_by_system_name(service: service, system_name: ref)
end

.find_by_system_name(service:, system_name:) ⇒ Object



25
26
27
# File 'lib/3scale_toolbox/entities/metric.rb', line 25

def find_by_system_name(service:, system_name:)
  service.metrics.find { |m| m.system_name == system_name }
end

Instance Method Details

#attrsObject



39
40
41
# File 'lib/3scale_toolbox/entities/metric.rb', line 39

def attrs
  @attrs ||= metric_attrs
end

#deleteObject



93
94
95
# File 'lib/3scale_toolbox/entities/metric.rb', line 93

def delete
  remote.delete_metric service.id, id
end

#descriptionObject



55
56
57
# File 'lib/3scale_toolbox/entities/metric.rb', line 55

def description
  attrs['description']
end

#disableObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/3scale_toolbox/entities/metric.rb', line 59

def disable
  # For each plan, get limits for the current metric
  # if already disabled -> NOOP
  # if non zero eternity limit exist, update
  # if non eternity limit exist, create
  service.plans.each do |plan|
    eternity_limit = plan_eternity_limit(plan)
    if eternity_limit.nil?
      plan.create_limit(id, zero_eternity_limit_attrs)
    elsif !eternity_limit.value.zero?
      eternity_limit.update(zero_eternity_limit_attrs)
    end
  end
end

#enableObject



74
75
76
77
78
79
# File 'lib/3scale_toolbox/entities/metric.rb', line 74

def enable
  service.plans.each do |plan|
    limit = plan_zero_eternity_limit(plan)
    limit.delete unless limit.nil?
  end
end

#enriched_keyObject

enriched_key returns a metric key that will be unique for all metrics from products and backends



99
100
101
# File 'lib/3scale_toolbox/entities/metric.rb', line 99

def enriched_key
  "product.#{service.id}.#{id}"
end

#friendly_nameObject



47
48
49
# File 'lib/3scale_toolbox/entities/metric.rb', line 47

def friendly_name
  attrs['friendly_name']
end

#system_nameObject



43
44
45
# File 'lib/3scale_toolbox/entities/metric.rb', line 43

def system_name
  attrs['system_name']
end

#to_hashObject



103
104
105
# File 'lib/3scale_toolbox/entities/metric.rb', line 103

def to_hash
  attrs.reject { |key, _| METRIC_BLACKLIST.include? key }
end

#unitObject



51
52
53
# File 'lib/3scale_toolbox/entities/metric.rb', line 51

def unit
  attrs['unit']
end

#update(new_metric_attrs) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/3scale_toolbox/entities/metric.rb', line 81

def update(new_metric_attrs)
  new_attrs = remote.update_metric(service.id, id, new_metric_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end