Class: ThreeScale::Backend::Metric

Inherits:
Object
  • Object
show all
Extended by:
ThreeScale::Backend::Memoizer::Decorator, KeyHelpers
Includes:
KeyHelpers, Storable
Defined in:
lib/3scale/backend/metric.rb,
lib/3scale/backend/metric/collection.rb

Defined Under Namespace

Modules: KeyHelpers Classes: Collection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThreeScale::Backend::Memoizer::Decorator

included

Methods included from KeyHelpers

id_key, id_set_key, key

Methods included from Storable

included, #initialize, #storage

Methods included from StorageKeyHelpers

#encode_key

Instance Attribute Details

#childrenObject



43
44
45
# File 'lib/3scale/backend/metric.rb', line 43

def children
  @children ||= []
end

#idObject

Returns the value of attribute id.



25
26
27
# File 'lib/3scale/backend/metric.rb', line 25

def id
  @id
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/3scale/backend/metric.rb', line 25

def name
  @name
end

#parent_idObject

Returns the value of attribute parent_id.



25
26
27
# File 'lib/3scale/backend/metric.rb', line 25

def parent_id
  @parent_id
end

#service_idObject

Returns the value of attribute service_id.



25
26
27
# File 'lib/3scale/backend/metric.rb', line 25

def service_id
  @service_id
end

Class Method Details

.ascendants(service_id, metric_name) ⇒ Object

Returns the “ascendants” of a metric, that is, its parent, grandparent, etc. The “ascendants” of a metric are its parent plus the ascendants of the parent.



169
170
171
172
173
174
175
# File 'lib/3scale/backend/metric.rb', line 169

def ascendants(service_id, metric_name)
  parents_of_metric = parents(service_id, [metric_name])

  parents_of_metric.reduce(parents_of_metric) do |acc, parent|
    acc + ascendants(service_id, parent)
  end
end

.attribute_namesObject



66
67
68
# File 'lib/3scale/backend/metric.rb', line 66

def attribute_names
  %i[service_id id parent_id name children].freeze
end

.children(service_id, id) ⇒ Object



148
149
150
# File 'lib/3scale/backend/metric.rb', line 148

def children(service_id, id)
  hierarchy(service_id, false)[id.to_s]
end

.clear_cache(service_id, id, name) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/3scale/backend/metric.rb', line 210

def clear_cache(service_id, id, name)
  metric_ids = load_all_ids(service_id)
  Memoizer.clear(Memoizer.build_keys_for_class(self,
                                load_all: [service_id],
                                load_all_names: [service_id, metric_ids],
                                load_name: [service_id, id],
                                load_id: [service_id, name],
                                load_all_ids: [service_id]))
end

.delete(service_id, id) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/3scale/backend/metric.rb', line 194

def delete(service_id, id)
  name = load_name(service_id, id)
  return false unless name and not name.empty?
  clear_cache(service_id, id, name)

  storage.pipelined do
    storage.srem(id_set_key(service_id), id)

    storage.del(key(service_id, id, :name),
                key(service_id, id, :parent_id),
                id_key(service_id, name))
  end

  true
end

.descendants(service_id, metric_name) ⇒ Object

Returns the “descendants” of a metric, that is, its children, grandchildren, etc. in the metric hierarchy of the given service. In other words, the “descendants” of a metric are its children plus the descendants of each of them.



156
157
158
159
160
161
162
163
# File 'lib/3scale/backend/metric.rb', line 156

def descendants(service_id, metric_name)
  metrics_hierarchy = hierarchy(service_id)
  children = metrics_hierarchy[metric_name] || []

  children.reduce(children) do |acc, child|
    acc + descendants(service_id, child)
  end
end

.hierarchy(service_id, as_names = true) ⇒ Object

Returns a hash where the keys can only be parent metric ids (as Strings) and their values are arrays of children.

The as_names optional parameter (default: true) returns metric names instead of metric ids when true.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/3scale/backend/metric.rb', line 127

def hierarchy(service_id, as_names = true)
  h_ids = hierarchy_ids(service_id)
  return h_ids unless as_names

  metric_ids = Set.new(h_ids.keys + h_ids.values.flatten)
  return {} if metric_ids.empty?

  res = {}
  metric_names = load_all_names(service_id, metric_ids)

  h_ids.each do |m_id, c_ids|
    m_name = metric_names[m_id]
    res[m_name] = c_ids.map do |c_id|
      metric_names[c_id]
    end
  end

  res
end

.load(service_id, id) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/3scale/backend/metric.rb', line 70

def load(service_id, id)
  name, parent_id = storage.mget(key(service_id, id, :name),
                                 key(service_id, id, :parent_id))

  name && new(id: id.to_s,
              service_id: service_id.to_s,
              name: name,
              parent_id: parent_id)
end

.load_all(service_id) ⇒ Object



80
81
82
# File 'lib/3scale/backend/metric.rb', line 80

def load_all(service_id)
  Collection.new(service_id)
end

.load_all_ids(service_id) ⇒ Object



90
91
92
93
# File 'lib/3scale/backend/metric.rb', line 90

def load_all_ids(service_id)
  # smembers is guaranteed to return an array of strings, even if empty
  storage.smembers(id_set_key(service_id))
end

.load_all_names(service_id, ids) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/3scale/backend/metric.rb', line 101

def load_all_names(service_id, ids)
  if ids.nil? || ids.empty?
    {}
  else
    name_keys = ids.map { |id| key(service_id, id, :name) }
    Hash[ids.zip(storage.mget(name_keys))]
  end
end

.load_id(service_id, name) ⇒ Object



85
86
87
# File 'lib/3scale/backend/metric.rb', line 85

def load_id(service_id, name)
  storage.get(id_key(service_id, name))
end

.load_name(service_id, id) ⇒ Object



96
97
98
# File 'lib/3scale/backend/metric.rb', line 96

def load_name(service_id, id)
  storage.get(key(service_id, id, :name))
end

.load_parent_id(service_id, id) ⇒ Object



111
112
113
# File 'lib/3scale/backend/metric.rb', line 111

def load_parent_id(service_id, id)
  storage.get(key(service_id, id, :parent_id))
end

.parents(service_id, metric_names) ⇒ Object

Given an array of metrics, returns an array without duplicates that includes the names of the metrics that are parent of at least one of the given metrics.



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/3scale/backend/metric.rb', line 181

def parents(service_id, metric_names)
  parents = []

  metric_names.each do |name|
    parent_id = load_parent_id service_id, load_id(service_id, name)
    if parent_id
      parents << load_name(service_id, parent_id)
    end
  end

  parents.uniq
end

.save(attributes) ⇒ Object



116
117
118
119
120
# File 'lib/3scale/backend/metric.rb', line 116

def save(attributes)
  metrics = new(attributes)
  metrics.save
  metrics
end

Instance Method Details

#saveObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/3scale/backend/metric.rb', line 28

def save
  old_name = self.class.load_name(service_id, id)
  storage.pipelined do
    save_attributes
    save_to_list
    remove_reverse_mapping(service_id, old_name) if old_name != name
  end

  # can't include this in the pipeline since it is a potentially
  # large number of commands.
  save_children

  self.class.clear_cache(service_id, id, name)
end

#to_hashObject



47
48
49
50
51
52
53
54
# File 'lib/3scale/backend/metric.rb', line 47

def to_hash
  {
    service_id: service_id,
    id: id,
    parent_id: parent_id,
    name: name
  }
end

#update(attributes) ⇒ Object



56
57
58
59
60
61
# File 'lib/3scale/backend/metric.rb', line 56

def update(attributes)
  attributes.each do |attr, val|
    public_send("#{attr}=", val)
  end
  self
end