artirix_data_models 0.21.1 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08dc025e4259518ea83c5d90095ec3caa3932272
4
- data.tar.gz: 10159471fd0bc7679806cecda55ec911fcbe42ba
3
+ metadata.gz: e584eaca369ea71ecbfbea5ae3a90d0eb318ab8c
4
+ data.tar.gz: 847edb6a1858a0fc9299f2a067ddfb211e8d33e1
5
5
  SHA512:
6
- metadata.gz: 737cd94de535612025b169a456874542fc9e452a6520307e0abd8bc0dd7b3b57cb642cb29abffed2708caa4314e81bc501dd6cb417900b2081e014e7413e5712
7
- data.tar.gz: de613eca0221355b1157208757c381b939cc5b56458405081f225cf096960f2696a99a60e606c484e97ffbc1e462cead307649b2718c2f39d23b35cdae35a0df
6
+ metadata.gz: ac9262217d906c2419e773a18910f5e56679cc75f102c28fae5cf336c06d086672b749f537691e30eb7dae12f949fb7e6d77e6c5a7088dc04d1a6d9e66af3473
7
+ data.tar.gz: 46bd490852a4e3bafaff73b8d745f514a29d39e56ee5985454e0931d029b4099f6136fdfda730698ecff056e45d5dff72c83f5a854c5d6ae085f350582400cd5
data/README.md CHANGED
@@ -315,6 +315,47 @@ end
315
315
 
316
316
  ## Changes
317
317
 
318
+ ### 0.22.0
319
+
320
+ added support for aggregations that look like
321
+ ```json
322
+ {
323
+ "aggregations": {
324
+ "global_published_states": {
325
+ "doc_count": 15,
326
+ "published_states": {
327
+ "doc_count": 15,
328
+ "live_soon": {
329
+ "doc_count": 0
330
+ },
331
+ "draft": {
332
+ "doc_count": 3
333
+ },
334
+ "expired": {
335
+ "doc_count": 0
336
+ },
337
+ "live": {
338
+ "doc_count": 12
339
+ }
340
+ }
341
+ }
342
+ }
343
+ }
344
+ ```
345
+
346
+ which will be added as an aggregation like:
347
+
348
+ ```ruby
349
+ es_collection.aggregations.first.name # => :published_states
350
+ es_collection.aggregations.first.buckets
351
+ # => [
352
+ # {name: 'live_soon', count: 0},
353
+ # {name: 'draft', count: 3},
354
+ # {name: 'expired', count: 0},
355
+ # {name: 'live', count: 12},
356
+ # ]
357
+ ```
358
+
318
359
  ### 0.21.1
319
360
 
320
361
  Fix bug in `Inspectable`, on Arrays.
@@ -1,8 +1,16 @@
1
1
  module ArtirixDataModels
2
2
  class RawAggregationDataNormaliser
3
3
 
4
+ IS_NESTED_COUNTS = ->(v) { v.respond_to?(:key?) && v.key?(:doc_count) && v.keys.size == 1 }
5
+
4
6
  FIND_BUCKETS = ->(_k, v, _o) { v.respond_to?(:key?) && v.key?(:buckets) }
5
7
  FIND_VALUE = ->(_k, v, _o) { v.respond_to?(:key?) && v.key?(:value) }
8
+ FIND_COUNTS = ->(_k, v, _o) do
9
+ v.respond_to?(:key) &&
10
+ v.key?(:doc_count) &&
11
+ v.respond_to?(:values) &&
12
+ v.values.any? { |x| IS_NESTED_COUNTS.call(x) }
13
+ end
6
14
 
7
15
  attr_reader :raw_aggs, :aggregations_factory, :list
8
16
 
@@ -28,6 +36,7 @@ module ArtirixDataModels
28
36
  def normalise_hash(hash)
29
37
  treat_buckets(hash)
30
38
  treat_values(hash)
39
+ treat_counts(hash)
31
40
  end
32
41
 
33
42
  def treat_buckets(hash)
@@ -50,6 +59,15 @@ module ArtirixDataModels
50
59
  end
51
60
  end
52
61
 
62
+ def treat_counts(hash)
63
+ with_values_list = deep_locate hash, FIND_COUNTS
64
+ with_values_list.each do |with_values|
65
+ with_values.each do |name, value|
66
+ normalise_element(name, value)
67
+ end
68
+ end
69
+ end
70
+
53
71
  def normalise_element(name, value)
54
72
  return unless Hash === value
55
73
 
@@ -58,7 +76,12 @@ module ArtirixDataModels
58
76
  elsif value.key?(:value)
59
77
  add_normalised_value_element_to_list(name, value)
60
78
  else
61
- normalise_hash(value)
79
+ nested = value.select { |_k, e| IS_NESTED_COUNTS.call e }
80
+ if nested.present?
81
+ add_normalised_nested_counts_to_list(name, nested)
82
+ else
83
+ normalise_hash(value)
84
+ end
62
85
  end
63
86
  end
64
87
 
@@ -74,6 +97,15 @@ module ArtirixDataModels
74
97
  list << { name: name, value: value[:value] }
75
98
  end
76
99
 
100
+ def add_normalised_nested_counts_to_list(name, nested)
101
+ buckets = nested.map do |bucket_name, nested_value|
102
+ { name: bucket_name.to_s, count: nested_value[:doc_count] }
103
+ end
104
+
105
+ list << { name: name, buckets: buckets }
106
+
107
+ end
108
+
77
109
  def normalise_bucket(raw_bucket)
78
110
  basic_bucket(raw_bucket).tap do |bucket|
79
111
  nested_aggs = nested_aggs_from(raw_bucket)
@@ -1,3 +1,3 @@
1
1
  module ArtirixDataModels
2
- VERSION = '0.21.1'
2
+ VERSION = '0.22.0'
3
3
  end
@@ -182,7 +182,7 @@ RSpec.describe ArtirixDataModels::EsCollection, type: :model do
182
182
  context 'with Raw ES with multiple nested aggregations' do
183
183
  Given(:fixture_file) { fixture_pathname('articles_search_multiple_nested_raw_es.json') }
184
184
 
185
- Given(:model_class) {
185
+ Given(:model_class) do
186
186
  Class.new do
187
187
  attr_reader :data
188
188
 
@@ -190,7 +190,7 @@ RSpec.describe ArtirixDataModels::EsCollection, type: :model do
190
190
  @data = { given: data }
191
191
  end
192
192
  end
193
- }
193
+ end
194
194
  Given(:es_response) { Oj.load(File.read(fixture_file), symbol_keys: true) }
195
195
 
196
196
  When(:es_collection) { ArtirixDataModels::EsCollection.new(model_class, response: es_response) }
@@ -233,6 +233,55 @@ RSpec.describe ArtirixDataModels::EsCollection, type: :model do
233
233
  Then { es_collection.aggregations.last.buckets.last.aggregations.first.buckets.last.name == 'Cognitive Impact' }
234
234
  Then { es_collection.aggregations.last.buckets.last.aggregations.first.buckets.last.count == 3 }
235
235
  end
236
+
237
+ context 'with nested aggregations of single doc_count' do
238
+ Given(:fixture_file) { fixture_pathname('editorial_content_search_dl.json') }
239
+
240
+ Given(:model_class) do
241
+ Class.new do
242
+ attr_reader :data
243
+
244
+ def initialize(data)
245
+ @data = { given: data }
246
+ end
247
+ end
248
+ end
249
+
250
+ Given(:es_response) { Oj.load(File.read(fixture_file), symbol_keys: true) }
251
+
252
+ When(:es_collection) { ArtirixDataModels::EsCollection.new(model_class, response: es_response) }
253
+ Then { es_collection.aggregations.size == 3 }
254
+
255
+ Then { es_collection.aggregations.first.name == :topics }
256
+ Then { es_collection.aggregations.first.buckets.size == 4 }
257
+
258
+ Then { es_collection.aggregations.first.buckets.first.name == 'Same Topics' }
259
+ Then { es_collection.aggregations.first.buckets.first.count == 6 }
260
+ Then { es_collection.aggregations.first.buckets.last.name == 'adaisada' }
261
+ Then { es_collection.aggregations.first.buckets.last.count == 1 }
262
+
263
+
264
+ Then { es_collection.aggregations.second.name == :content_types }
265
+ Then { es_collection.aggregations.second.buckets.size == 3 }
266
+
267
+ Then { es_collection.aggregations.second.buckets.first.name == 'article' }
268
+ Then { es_collection.aggregations.second.buckets.first.count == 11 }
269
+ Then { es_collection.aggregations.second.buckets.last.name == 'video' }
270
+ Then { es_collection.aggregations.second.buckets.last.count == 1 }
271
+
272
+
273
+ Then { es_collection.aggregations.third.name == :published_states }
274
+ Then { es_collection.aggregations.third.buckets.size == 4 }
275
+
276
+ Then { es_collection.aggregations.third.buckets.first.name == 'live_soon' }
277
+ Then { es_collection.aggregations.third.buckets.first.count == 0 }
278
+ Then { es_collection.aggregations.third.buckets.second.name == 'draft' }
279
+ Then { es_collection.aggregations.third.buckets.second.count == 3 }
280
+ Then { es_collection.aggregations.third.buckets.third.name == 'expired' }
281
+ Then { es_collection.aggregations.third.buckets.third.count == 0 }
282
+ Then { es_collection.aggregations.third.buckets.last.name == 'live' }
283
+ Then { es_collection.aggregations.third.buckets.last.count == 12 }
284
+ end
236
285
  end
237
286
 
238
287
  end
@@ -0,0 +1,192 @@
1
+ {
2
+ "took": 4,
3
+ "timed_out": false,
4
+ "_shards": {
5
+ "total": 5,
6
+ "successful": 5,
7
+ "failed": 0
8
+ },
9
+ "hits": {
10
+ "total": 15,
11
+ "max_score": null,
12
+ "hits": [
13
+ {
14
+ "_index": "editorial_contents7",
15
+ "_type": "editorial_content",
16
+ "_id": "1",
17
+ "_score": null,
18
+ "_source": {
19
+ "meta_keywords": "Arsenal , Premier League, BBC Sport",
20
+ "disease_slug": "cystic-fibrosis",
21
+ "topics": [
22
+ "interesting",
23
+ "another topic",
24
+ "interesting",
25
+ "Same Topics"
26
+ ],
27
+ "updated_at": "2016-06-13T13:26:35",
28
+ "id": 1,
29
+ "category": "pulse",
30
+ "published_on": "2016-05-16",
31
+ "author": "Luke Thomas",
32
+ "slug_base": "lukes-editorial-content",
33
+ "summary_description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In malesuada a eros a rhoncus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam tristique vestibulum iaculis. Curabitur vi",
34
+ "editorial_content_blocks": [
35
+ {
36
+ "block_type": "text",
37
+ "video_provider_sku": null,
38
+ "updated_at": "2016-06-01T10:45:01",
39
+ "text_markdown": "Key features of the UK Strategy for Rare Diseases, which was published in November 2013, include:&nbsp;\n\n- making sure patients and their families and carers have the information they need, are listened to and consulted\n- developing better methods of identifying and preventing rare diseases\n- improving diagnosis and earlier intervention for those with a rare disease\n- developing better coordination of care for those with a rare disease, including joined up consultation and treatment schedules\n- building on research to improve personalised approaches to healthcare for those with a rare disease\n",
40
+ "video_provider": null,
41
+ "id": 11,
42
+ "library_image": null,
43
+ "created_at": "2016-06-01T10:45:01",
44
+ "position": 1
45
+ },
46
+ {
47
+ "block_type": "text",
48
+ "video_provider_sku": null,
49
+ "updated_at": "2016-06-01T10:49:56",
50
+ "text_markdown": "Some other text can go here.&nbsp;\n\n",
51
+ "video_provider": null,
52
+ "id": 12,
53
+ "library_image": null,
54
+ "created_at": "2016-06-01T10:49:56",
55
+ "position": 2
56
+ },
57
+ {
58
+ "block_type": "image",
59
+ "video_provider_sku": null,
60
+ "updated_at": "2016-06-13T13:26:35",
61
+ "text_markdown": null,
62
+ "video_provider": null,
63
+ "id": 31,
64
+ "library_image": {
65
+ "external_reference": null,
66
+ "extension": ".jpg",
67
+ "url": "https://cdn.filepicker.io/api/file/g59uy385QPeRJGVqmm7r",
68
+ "created_at": "2016-05-16T10:18:50",
69
+ "updated_at": "2016-05-16T10:18:50",
70
+ "image_type": "general",
71
+ "asset": "rm_int2/rm/library_images/TPw1EVaShOVGsZVCbeUA_santi_dance",
72
+ "s3_key": "rm_int2/rm/library_images/TPw1EVaShOVGsZVCbeUA_santi_dance.jpg",
73
+ "id": 4,
74
+ "name": "HAHA"
75
+ },
76
+ "created_at": "2016-06-13T13:26:35",
77
+ "position": 3
78
+ }
79
+ ],
80
+ "disease_all_slugs": [
81
+ "cystic-fibrosis"
82
+ ],
83
+ "start_date": "2016-05-26T16:13:00",
84
+ "meta_description": "This article is about arsenal among other things",
85
+ "hero_video_provider": "youtube",
86
+ "end_date": null,
87
+ "style": "style1",
88
+ "disease_name": "Cystic fibrosis",
89
+ "content_type": "interview",
90
+ "snippet_image": {
91
+ "external_reference": null,
92
+ "extension": ".jpg",
93
+ "url": "https://cdn.filepicker.io/api/file/F6TzKANSbqlZp9JfR7Vw",
94
+ "created_at": "2016-05-16T14:52:17",
95
+ "updated_at": "2016-05-16T14:52:17",
96
+ "image_type": "general",
97
+ "asset": "rm_int2/rm/library_images/ySMw8Z2xRF6X7VYZIW6w_santi_dance",
98
+ "s3_key": "rm_int2/rm/library_images/ySMw8Z2xRF6X7VYZIW6w_santi_dance.jpg",
99
+ "id": 6,
100
+ "name": "test2"
101
+ },
102
+ "slug": "lukes-editorial-content--1",
103
+ "_timestamp": "2016-06-13T13:26:35",
104
+ "name": "Lukes Editorial Content",
105
+ "created_at": "2016-05-09T08:37:02",
106
+ "disease": {
107
+ "slug": "cystic-fibrosis",
108
+ "all_slugs": [
109
+ "cystic-fibrosis"
110
+ ],
111
+ "id": 8,
112
+ "name": "Cystic fibrosis"
113
+ },
114
+ "disease_id": 8,
115
+ "hero_video_provider_sku": "https://www.youtube.com/embed/uQereoIxioI",
116
+ "hero_image": null
117
+ },
118
+ "sort": [
119
+ 1465824395000
120
+ ]
121
+ }
122
+ ]
123
+ },
124
+ "aggregations": {
125
+ "global_topics": {
126
+ "doc_count": 15,
127
+ "global_filtered_topics": {
128
+ "doc_count": 15,
129
+ "topics": {
130
+ "buckets": [
131
+ {
132
+ "key": "Same Topics",
133
+ "doc_count": 6
134
+ },
135
+ {
136
+ "key": "another topic",
137
+ "doc_count": 4
138
+ },
139
+ {
140
+ "key": "interesting",
141
+ "doc_count": 2
142
+ },
143
+ {
144
+ "key": "adaisada",
145
+ "doc_count": 1
146
+ }
147
+ ]
148
+ }
149
+ }
150
+ },
151
+ "global_content_types": {
152
+ "doc_count": 15,
153
+ "global_filtered_content_types": {
154
+ "doc_count": 15,
155
+ "content_types": {
156
+ "buckets": [
157
+ {
158
+ "key": "article",
159
+ "doc_count": 11
160
+ },
161
+ {
162
+ "key": "interview",
163
+ "doc_count": 3
164
+ },
165
+ {
166
+ "key": "video",
167
+ "doc_count": 1
168
+ }
169
+ ]
170
+ }
171
+ }
172
+ },
173
+ "global_published_states": {
174
+ "doc_count": 15,
175
+ "published_states": {
176
+ "doc_count": 15,
177
+ "live_soon": {
178
+ "doc_count": 0
179
+ },
180
+ "draft": {
181
+ "doc_count": 3
182
+ },
183
+ "expired": {
184
+ "doc_count": 0
185
+ },
186
+ "live": {
187
+ "doc_count": 12
188
+ }
189
+ }
190
+ }
191
+ }
192
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artirix_data_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.1
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-08 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -296,6 +296,7 @@ files:
296
296
  - spec/fixtures/articles_search_nested_raw_es.json
297
297
  - spec/fixtures/articles_search_nested_single_raw_es.json
298
298
  - spec/fixtures/articles_search_raw_es.json
299
+ - spec/fixtures/editorial_content_search_dl.json
299
300
  - spec/spec_helper.rb
300
301
  - spec/support/.keep
301
302
  - spec/support/a_finder_enabled_ui_model_dao.rb
@@ -339,6 +340,7 @@ test_files:
339
340
  - spec/fixtures/articles_search_nested_raw_es.json
340
341
  - spec/fixtures/articles_search_nested_single_raw_es.json
341
342
  - spec/fixtures/articles_search_raw_es.json
343
+ - spec/fixtures/editorial_content_search_dl.json
342
344
  - spec/spec_helper.rb
343
345
  - spec/support/.keep
344
346
  - spec/support/a_finder_enabled_ui_model_dao.rb