artirix_data_models 0.6.7 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c56b4f635171ba067ea9c4fdd3082c412eaab23a
4
- data.tar.gz: d25c8af81bf51fa14bd5fff95c3c7a9d6506e97d
3
+ metadata.gz: f15642b0096a000e01eb83c1ce3016f0af83b001
4
+ data.tar.gz: afdfc74dd09d32112a198cf4a5f9c89b2437b84c
5
5
  SHA512:
6
- metadata.gz: 004f2a8fa562df1dddc3a5ef35986257590d298760eff8dbaead938d12a0c9be9e782c156378d53d06a8a5df3191cb9c73de457d4042fbb2e973e5914cf22fe5
7
- data.tar.gz: 242a1a52ad9feaaa30684bdfdacec0ee42c9af37fefee9cc9936ff761efd3696ab07d17edfcc71a77cf0576b6564aa47fae1492e9f6af9a54367b833b14ce22e
6
+ metadata.gz: 6484979971c7378ad68acf26d6a9ca4be25869643a111a049b66efd85fce6a2fb80222fb0d039ea8389cd20592dd258f8d95de00e5988aa309cff2540c235ddf
7
+ data.tar.gz: d15b73a1ac5588b68178842a2c456055ba4bfe674b516425dabf4baab8ed5ccdef82b89e14e8669b5c1f81ee9abb228b132be92e3767953194957ab7571aa11b
data/README.md CHANGED
@@ -250,6 +250,18 @@ end
250
250
 
251
251
  ## Changes
252
252
 
253
+ ### 0.7.1
254
+
255
+ - added `MetricAggregation`. Normal `AggregationBuilder` will build an aggregation with that class if instead of `buckets` it finds `value` in the JSON.
256
+ - normalize raw aggregations now does not ignore metric aggregations (see above)
257
+ - added `calculate_filtered(filtered_values)` to aggregations (noop in Metric aggregations). In a bucket aggregation, will mark with `filtered?` each bucket (aka Aggregation Value) if the `bucket.name` is present in the given `filtered_values`.
258
+ - added to `Aggregation` the methods:
259
+ -- `filtered_buckets` that will return only buckets marked as `filtered?`
260
+ -- `unfiltered_buckets` that will return only buckets not marked as `filtered?`
261
+ -- `filtered_first_buckets` that will concatenate `filtered_buckets` and `unfiltered_buckets`
262
+ - changed classes to stop inheriting from `Struct`, had some problems with inheritance.
263
+ - `Aggregation`, `Aggregation::Value` and `MetricAggregation` now using the same inspection as models.
264
+
253
265
  ### 0.6.7
254
266
 
255
267
  - aggregations use `key_as_string` as name of the bucket value if it exists, if not then it uses `key` and if that also does not exist then it uses `name`
@@ -288,6 +300,10 @@ end
288
300
 
289
301
  ## Yanked versions
290
302
 
303
+ ### ~0.7.0~
304
+
305
+ Yanked because of bug on Aggregations. Released 0.7.1 with the fix. Changeset moved too to 0.7.1
306
+
291
307
  ### ~0.6.3~
292
308
 
293
309
  Yanked because of typo bug on SortedBucketAggregationBase. Released 0.6.3.1 with the fix.
@@ -14,6 +14,7 @@ require 'hashie'
14
14
 
15
15
 
16
16
  # loading features
17
+ require 'artirix_data_models/inspectable'
17
18
  require 'artirix_data_models/es_collection'
18
19
  require 'artirix_data_models/aggregation'
19
20
  require 'artirix_data_models/aggregations_factory'
@@ -1,15 +1,22 @@
1
1
  module ArtirixDataModels
2
- class Aggregation < Struct.new(:name, :buckets)
2
+ class CommonAggregation
3
+ include Inspectable
4
+ attr_accessor :name
3
5
 
4
- include Enumerable
5
-
6
- delegate :each, :empty?, to: :buckets
6
+ def initialize(name)
7
+ @name = name
8
+ end
7
9
 
8
- def self.from_json(definition, value_class = Value)
10
+ def self.from_json(definition, value_class = Aggregation::Value)
9
11
  DAORegistry.aggregations_factory.aggregation_from_json(definition, value_class: value_class, aggregation_class: self)
10
12
  end
11
13
 
12
14
  def pretty_name
15
+ @pretty_name ||= load_pretty_name
16
+ end
17
+
18
+ private
19
+ def load_pretty_name
13
20
  I18n.t("aggregations.#{name.to_s.gsub('.', '_')}.name", default: default_pretty_name)
14
21
  end
15
22
 
@@ -17,6 +24,20 @@ module ArtirixDataModels
17
24
  name
18
25
  end
19
26
 
27
+ end
28
+
29
+ class Aggregation < CommonAggregation
30
+ include Enumerable
31
+
32
+ attr_accessor :buckets
33
+
34
+ def initialize(name, buckets)
35
+ super name
36
+ @buckets = buckets
37
+ end
38
+
39
+ delegate :each, :empty?, to: :buckets
40
+
20
41
  def non_empty_buckets
21
42
  buckets.reject { |x| x.empty? }
22
43
  end
@@ -28,10 +49,40 @@ module ArtirixDataModels
28
49
  }
29
50
  end
30
51
 
31
- class Value < Struct.new(:aggregation_name, :name, :count, :aggregations)
52
+ def calculate_filtered(filtered_values = [])
53
+ buckets.each do |b|
54
+ b.filtered = filtered_values.include?(b.name)
55
+ end
56
+
57
+ self
58
+ end
59
+
60
+ def filtered_buckets
61
+ buckets.select &:filtered?
62
+ end
63
+
64
+ def unfiltered_buckets
65
+ buckets.reject &:filtered?
66
+ end
67
+
68
+ def filtered_first_buckets
69
+ filtered_buckets + unfiltered_buckets
70
+ end
71
+
72
+ class Value
73
+
74
+ attr_accessor :filtered, :aggregation_name, :name, :count
75
+ attr_writer :aggregations
76
+
77
+ def initialize(aggregation_name, name, count, aggregations)
78
+ @aggregation_name = aggregation_name
79
+ @name = name
80
+ @count = count
81
+ @aggregations = aggregations
82
+ end
32
83
 
33
84
  def aggregations
34
- Array(super)
85
+ Array(@aggregations)
35
86
  end
36
87
 
37
88
  alias_method :nested_aggregations, :aggregations
@@ -69,6 +120,18 @@ module ArtirixDataModels
69
120
  }
70
121
  end
71
122
 
123
+ def mark_filtered
124
+ @filtered = true
125
+ end
126
+
127
+ def mark_unfiltered
128
+ @filtered = false
129
+ end
130
+
131
+ def filtered?
132
+ !!@filtered
133
+ end
134
+
72
135
  private
73
136
  def load_pretty_name
74
137
  tranlsation_key = "aggregations.#{aggregation_name.to_s.gsub('.', '_')}.buckets.#{name.to_s.gsub('.', '_')}"
@@ -76,4 +139,26 @@ module ArtirixDataModels
76
139
  end
77
140
  end
78
141
  end
142
+
143
+ class MetricAggregation < CommonAggregation
144
+ attr_accessor :value
145
+
146
+ def initialize(name, value)
147
+ super name
148
+ @value = value
149
+ end
150
+
151
+ def data_hash
152
+ {
153
+ name: name,
154
+ value: value
155
+ }
156
+ end
157
+
158
+ def calculate_filtered(_filtered_values = [])
159
+ # NOOP
160
+ self
161
+ end
162
+ end
163
+
79
164
  end
@@ -1,22 +1,35 @@
1
1
  module ArtirixDataModels
2
2
 
3
3
  class AggregationBuilder
4
- attr_reader :aggregations_factory, :definition, :value_class, :aggregation_class
5
-
6
- def initialize(aggregations_factory:, definition:, aggregation_class: Aggregation, value_class: Aggregation::Value)
7
- @aggregations_factory = aggregations_factory
8
- @definition = definition
9
- @aggregation_class = aggregation_class
10
- @value_class = value_class
4
+ attr_reader :aggregations_factory, :definition, :value_class, :aggregation_class, :metric_aggregation_class
5
+
6
+ def initialize(aggregations_factory:, definition:, aggregation_class: Aggregation, metric_aggregation_class: MetricAggregation, value_class: Aggregation::Value)
7
+ @aggregations_factory = aggregations_factory
8
+ @definition = definition
9
+ @aggregation_class = aggregation_class
10
+ @metric_aggregation_class = metric_aggregation_class || aggregation_class
11
+ @value_class = value_class
11
12
  end
12
13
 
13
14
  def build
14
- aggregation_class.new agg_name, buckets
15
+ if is_metric?
16
+ metric_aggregation_class.new agg_name, value
17
+ else
18
+ aggregation_class.new agg_name, buckets
19
+ end
15
20
  end
16
21
 
17
22
  alias_method :call, :build
18
23
 
19
24
  private
25
+ def is_metric?
26
+ definition.key?(:value) && !definition.key?(:buckets)
27
+ end
28
+
29
+ def value
30
+ definition[:value]
31
+ end
32
+
20
33
  def buckets
21
34
  definition[:buckets].map do |bucket|
22
35
  build_bucket(bucket)
@@ -0,0 +1,16 @@
1
+ module ArtirixDataModels
2
+ module Inspectable
3
+ def inspect
4
+ inspect_with_tab 1
5
+ end
6
+
7
+ def inspect_with_tab(tab_level = 0)
8
+ insp = data_hash.map do |at, val|
9
+ v = val.try(:inspect_with_tab, tab_level + 1) || val.inspect
10
+ tab = ' ' * tab_level * 4
11
+ "#{tab} - #{at}: #{v}"
12
+ end
13
+ "#<#{self.class} \n#{insp.join("\n")}>"
14
+ end
15
+ end
16
+ end
@@ -107,6 +107,7 @@ module ArtirixDataModels
107
107
 
108
108
  included do
109
109
  include KeywordInit
110
+ include Inspectable
110
111
  end
111
112
 
112
113
  def self.direct_getter_method_name(attribute)
@@ -121,19 +122,6 @@ module ArtirixDataModels
121
122
  data_hash.reject { |_, v| v.nil? }
122
123
  end
123
124
 
124
- def inspect_with_tab(tab_level = 0)
125
- insp = data_hash.map do |at, val|
126
- v = val.try(:inspect_with_tab, tab_level + 1) || val.inspect
127
- tab = ' ' * tab_level * 4
128
- "#{tab} - #{at}: #{v}"
129
- end
130
- "#<#{self.class} \n#{insp.join("\n")}>"
131
- end
132
-
133
- def inspect
134
- inspect_with_tab 1
135
- end
136
-
137
125
  module ClassMethods
138
126
  def attribute(*attributes)
139
127
  attributes.each { |attribute| _define_attribute attribute }
@@ -2,6 +2,7 @@ module ArtirixDataModels
2
2
  class RawAggregationDataNormaliser
3
3
 
4
4
  FIND_BUCKETS = ->(_k, v, _o) { v.respond_to?(:key?) && v.key?(:buckets) }
5
+ FIND_VALUE = ->(_k, v, _o) { v.respond_to?(:key?) && v.key?(:value) }
5
6
 
6
7
  attr_reader :raw_aggs, :aggregations_factory, :list
7
8
 
@@ -25,6 +26,11 @@ module ArtirixDataModels
25
26
  private
26
27
 
27
28
  def normalise_hash(hash)
29
+ treat_buckets(hash)
30
+ treat_values(hash)
31
+ end
32
+
33
+ def treat_buckets(hash)
28
34
  with_buckets_list = deep_locate hash, FIND_BUCKETS
29
35
 
30
36
  with_buckets_list.each do |with_buckets|
@@ -34,17 +40,29 @@ module ArtirixDataModels
34
40
  end
35
41
  end
36
42
 
43
+ def treat_values(hash)
44
+ with_values_list = deep_locate hash, FIND_VALUE
45
+
46
+ with_values_list.each do |with_values|
47
+ with_values.each do |name, value|
48
+ normalise_element(name, value)
49
+ end
50
+ end
51
+ end
52
+
37
53
  def normalise_element(name, value)
38
54
  return unless Hash === value
39
55
 
40
56
  if value.key?(:buckets)
41
- add_normalised_element_to_list(name, value)
57
+ add_normalised_buckets_element_to_list(name, value)
58
+ elsif value.key?(:value)
59
+ add_normalised_value_element_to_list(name, value)
42
60
  else
43
61
  normalise_hash(value)
44
62
  end
45
63
  end
46
64
 
47
- def add_normalised_element_to_list(name, value)
65
+ def add_normalised_buckets_element_to_list(name, value)
48
66
  buckets = value[:buckets].map do |raw_bucket|
49
67
  normalise_bucket(raw_bucket)
50
68
  end
@@ -52,6 +70,10 @@ module ArtirixDataModels
52
70
  list << { name: name, buckets: buckets }
53
71
  end
54
72
 
73
+ def add_normalised_value_element_to_list(name, value)
74
+ list << { name: name, value: value[:value] }
75
+ end
76
+
55
77
  def normalise_bucket(raw_bucket)
56
78
  basic_bucket(raw_bucket).tap do |bucket|
57
79
  nested_aggs = nested_aggs_from(raw_bucket)
@@ -1,3 +1,3 @@
1
1
  module ArtirixDataModels
2
- VERSION = '0.6.7'
2
+ VERSION = '0.7.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artirix_data_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
@@ -256,6 +256,7 @@ files:
256
256
  - lib/artirix_data_models/fake_response_factory.rb
257
257
  - lib/artirix_data_models/gateway_response_adaptors/model_adaptor.rb
258
258
  - lib/artirix_data_models/gateways/data_gateway.rb
259
+ - lib/artirix_data_models/inspectable.rb
259
260
  - lib/artirix_data_models/model.rb
260
261
  - lib/artirix_data_models/raw_aggregation_data_normaliser.rb
261
262
  - lib/artirix_data_models/spec_support.rb