bidu-house 0.2.1 → 1.0.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: a983db5f6db921362ce62f4d7f0a0cbe09272968
4
- data.tar.gz: bfc7a4ace34f0eb1ba940ad1e562e2ed0351d98c
3
+ metadata.gz: e81c76e00406e53d4990c5cc225f38e81fa4de1c
4
+ data.tar.gz: cf6bd8a5b1f369c44cf2b3e4c67464a13c4e4700
5
5
  SHA512:
6
- metadata.gz: b4c2f0f4e0c0c5151a98171e5ae307cf332c965b00e3dacc44dfa05ea369f9aa2fedd5b984c5231ebe3d96c5514b3abc7746471e02d5aaf5cf267c825179e943
7
- data.tar.gz: 535155a3de8ecb7e6a101e80a1d3bd287c75d3be780a9d53df3221deb1cb38d1be5286b988e56cf2b9f1b4bc1b98f460c9fe69ca1b1f79366a11351d3a816273
6
+ metadata.gz: 6dc1bf73244b34f0a5c84c8ca65238f4f43bcf94b7278d6d028f41e3e9d0351a4711cd08ab95a93499ff7033c7ff9e52e3c0cdb84cc3bba7ac5b67ead002be67
7
+ data.tar.gz: a6d7313fa19994b600c4a87bb83191d1ca8e289563756e8b23a52a122712d104daed6e6561a4975a4ebb2d1a029a8b44e57c9ac0550f3947de666e0a6f3852af
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bidu-house (0.2.1)
4
+ bidu-house (1.0.0)
5
5
  activesupport
6
6
  bidu-active_ext
7
7
  concern_builder
@@ -27,7 +27,7 @@ GEM
27
27
  bidu-active_ext (1.0.0)
28
28
  activesupport
29
29
  bidu-core_ext
30
- bidu-core_ext (1.2.1)
30
+ bidu-core_ext (1.2.2)
31
31
  activesupport
32
32
  builder (3.2.2)
33
33
  coderay (1.1.0)
@@ -82,4 +82,4 @@ DEPENDENCIES
82
82
  sqlite3
83
83
 
84
84
  BUNDLED WITH
85
- 1.10.6
85
+ 1.12.5
data/README.md CHANGED
@@ -50,6 +50,9 @@ with error and render the report
50
50
  - threshold: default report threshold (default: 0.02)
51
51
  - period: default search period (default: 1 day)
52
52
  - on: report bucket (default: :default)
53
+ - base_scope: scope to be universal sample
54
+ - uniq: when the output ids should not be repeated
55
+ - limit: limit of ids to be outputed
53
56
 
54
57
  4. Run the server and hit the health-check routes
55
58
 
@@ -1,29 +1,22 @@
1
1
  module Bidu
2
2
  module House
3
- module Report
4
- class Error
5
- include JsonParser
6
-
3
+ class Report
4
+ class Error < Report
7
5
  ALLOWED_PARAMETERS=[:period, :threshold]
8
-
9
- attr_reader :json
6
+ DEFAULT_OPTION = {
7
+ external_key: :id,
8
+ threshold: 0.02,
9
+ period: 1.day,
10
+ scope: :with_error,
11
+ base_scope: :all,
12
+ uniq: false
13
+ }
10
14
 
11
15
  json_parse :threshold, type: :float
12
- json_parse :period, type: :period
13
- json_parse :scope, :id, :clazz, :base_scope, :external_key, case: :snake
16
+ json_parse :scope, :id, :external_key, :uniq, :limit, case: :snake
14
17
 
15
18
  def initialize(options)
16
- @json = {
17
- external_key: :id,
18
- threshold: 0.02,
19
- period: 1.day,
20
- scope: :with_error,
21
- base_scope: :all
22
- }.merge(options)
23
- end
24
-
25
- def status
26
- @status ||= error? ? :error : :ok
19
+ super(DEFAULT_OPTION.merge(options))
27
20
  end
28
21
 
29
22
  def percentage
@@ -38,13 +31,9 @@ module Bidu
38
31
  @error ||= percentage > threshold
39
32
  end
40
33
 
41
- def status
42
- error? ? :error : :ok
43
- end
44
-
45
34
  def as_json
46
35
  {
47
- ids: scoped.pluck(external_key),
36
+ ids: ids,
48
37
  percentage: percentage,
49
38
  status: status
50
39
  }
@@ -52,6 +41,14 @@ module Bidu
52
41
 
53
42
  private
54
43
 
44
+ def ids
45
+ relation = scoped
46
+ relation = relation.uniq if uniq
47
+ relation = relation.limit(limit) if limit
48
+
49
+ relation.pluck(external_key)
50
+ end
51
+
55
52
  def fetch_percentage
56
53
  if (scope.is_a?(Symbol))
57
54
  last_entries.percentage(*(scope.to_s.split('.').map(&:to_sym)))
@@ -59,24 +56,6 @@ module Bidu
59
56
  last_entries.percentage(scope)
60
57
  end
61
58
  end
62
-
63
- def fetch_scoped(base, scope)
64
- if (scope.is_a?(Symbol))
65
- scope.to_s.split('.').inject(base) do |entries, method|
66
- entries.public_send(method)
67
- end
68
- else
69
- base.where(scope)
70
- end
71
- end
72
-
73
- def last_entries
74
- @last_entries ||= base.where('updated_at >= ?', period.seconds.ago)
75
- end
76
-
77
- def base
78
- fetch_scoped(clazz, base_scope)
79
- end
80
59
  end
81
60
  end
82
61
  end
@@ -1,7 +1,51 @@
1
1
  module Bidu
2
2
  module House
3
- module Report
3
+ class Report
4
+ include JsonParser
4
5
  require 'bidu/house/report/error'
6
+ ALLOWED_PARAMETERS = []
7
+ DEFAULT_OPTION = {}
8
+
9
+ attr_reader :json
10
+
11
+ json_parse :period, type: :period
12
+ json_parse :clazz, :base_scope, case: :snake
13
+
14
+ def initialize(options)
15
+ @json = DEFAULT_OPTION.merge(options)
16
+ end
17
+
18
+ def status
19
+ @status ||= error? ? :error : :ok
20
+ end
21
+
22
+ def error?
23
+ raise 'Not implemented yet'
24
+ end
25
+
26
+ def as_json
27
+ { status: status }
28
+ end
29
+
30
+ private
31
+
32
+ def fetch_scoped(base, scope)
33
+ if (scope.is_a?(Symbol))
34
+ scope.to_s.split('.').inject(base) do |entries, method|
35
+ entries.public_send(method)
36
+ end
37
+ else
38
+ base.where(scope)
39
+ end
40
+ end
41
+
42
+ def last_entries
43
+ @last_entries ||= base.where('updated_at >= ?', period.seconds.ago)
44
+ end
45
+
46
+ def base
47
+ fetch_scoped(clazz, base_scope)
48
+ end
5
49
  end
6
50
  end
7
51
  end
@@ -1,5 +1,5 @@
1
1
  module Bidu
2
2
  module House
3
- VERSION = '0.2.1'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
@@ -341,6 +341,35 @@ describe Bidu::House::Report::Error do
341
341
  it 'returns the correct external keys' do
342
342
  expect(subject.as_json).to eq(expected)
343
343
  end
344
+
345
+ context 'when some external ids are the same' do
346
+ let(:ids_expected) { [10, 10, 10] }
347
+ before do
348
+ Document.update_all(outter_external_id: 10)
349
+ end
350
+
351
+ it 'returns the correct external keys' do
352
+ expect(subject.as_json).to eq(expected)
353
+ end
354
+
355
+ context 'and passing uniq option' do
356
+ before { options[:uniq] = true }
357
+ let(:ids_expected) { [10] }
358
+
359
+ it 'returns the correct external keys only once' do
360
+ expect(subject.as_json).to eq(expected)
361
+ end
362
+ end
363
+ end
364
+
365
+ context 'with a limit' do
366
+ before { options[:limit] = 2 }
367
+ let(:ids_expected) { [0, 1] }
368
+
369
+ it 'returns only the limited ids' do
370
+ expect(subject.as_json).to eq(expected)
371
+ end
372
+ end
344
373
  end
345
374
 
346
375
  context 'when configurated without external key' do
@@ -1,7 +1,7 @@
1
1
  module Bidu
2
2
  module House
3
- module Report
4
- class Dummy
3
+ class Report
4
+ class Dummy < Report
5
5
  ALLOWED_PARAMETERS=[:period, :threshold]
6
6
  def initialize(options)
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bidu-house
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bidu Dev's Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -222,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
222
  version: '0'
223
223
  requirements: []
224
224
  rubyforge_project:
225
- rubygems_version: 2.4.8
225
+ rubygems_version: 2.5.1
226
226
  signing_key:
227
227
  specification_version: 4
228
228
  summary: Gem for easy health check