bidu-house 0.1.2 → 0.2.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: 61df7ae9ad7055e431d7912230db3f33cd23c672
4
- data.tar.gz: 6e2d298063bbd83ffa628bf2da57bb314a468d47
3
+ metadata.gz: 25cc0841110cb3f8c649f8a42343f80736c1c910
4
+ data.tar.gz: e9ef4b32c0651fe1400d053d38ffae6a3a385b27
5
5
  SHA512:
6
- metadata.gz: 8fbf8718e925ea3e3d005e5178a940c32ddcbbc76edd3900693221b6144beae7ed9b430b28321f96dcb09f74e302a415b3b77082dca36a80b631bf1c3cbe977b
7
- data.tar.gz: 090601be4bbd3711920ea90fcea1853da102bd033b468ccb955ffa3ecd05aaad8d529b5aaa4f5cf11cb5eafdf42392c91d69e2d2b76a65dfcc737864d25b66a2
6
+ metadata.gz: 3e52af0063573133cffdb10b444a236a6081559622b4af79f0bd10227011a73ebdcf11ec6ca296504952d560afe53990428788f3cc9606c2cd2529c4f31d15bc
7
+ data.tar.gz: 6aa8cfde4107f3a1d4a50fc5aa937a06a672dfd8b8ef95531a5e91a4708987350728120aae37ba5733451b06fb8c7d8541eb32345c8c2468fdbd838e36c24f55
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bidu-house (0.1.2)
4
+ bidu-house (0.2.0)
5
5
  activesupport
6
- bidu-core_ext
6
+ bidu-active_ext
7
7
  concern_builder
8
8
  json_parser (~> 1.1)
9
9
 
@@ -24,10 +24,14 @@ GEM
24
24
  thread_safe (~> 0.3, >= 0.3.4)
25
25
  tzinfo (~> 1.1)
26
26
  arel (6.0.0)
27
- bidu-core_ext (1.0.0)
27
+ bidu-active_ext (1.0.0)
28
+ activesupport
29
+ bidu-core_ext
30
+ bidu-core_ext (1.2.0)
28
31
  activesupport
29
32
  builder (3.2.2)
30
- concern_builder (0.0.1)
33
+ coderay (1.1.0)
34
+ concern_builder (0.0.2)
31
35
  activesupport
32
36
  diff-lcs (1.2.5)
33
37
  docile (1.1.5)
@@ -36,7 +40,14 @@ GEM
36
40
  json_parser (1.1.0)
37
41
  activesupport
38
42
  concern_builder
43
+ method_source (0.8.2)
39
44
  minitest (5.7.0)
45
+ pry (0.10.1)
46
+ coderay (~> 1.1.0)
47
+ method_source (~> 0.8.1)
48
+ slop (~> 3.4)
49
+ pry-nav (0.2.4)
50
+ pry (>= 0.9.10, < 0.11.0)
40
51
  rake (10.4.2)
41
52
  rspec (2.99.0)
42
53
  rspec-core (~> 2.99.0)
@@ -51,6 +62,7 @@ GEM
51
62
  json (~> 1.8)
52
63
  simplecov-html (~> 0.10.0)
53
64
  simplecov-html (0.10.0)
65
+ slop (3.6.0)
54
66
  sqlite3 (1.3.10)
55
67
  thread_safe (0.3.5)
56
68
  tzinfo (1.2.2)
@@ -63,7 +75,11 @@ DEPENDENCIES
63
75
  activerecord
64
76
  bidu-house!
65
77
  bundler (~> 1.6)
78
+ pry-nav
66
79
  rake
67
80
  rspec (~> 2.14)
68
81
  simplecov
69
82
  sqlite3
83
+
84
+ BUNDLED WITH
85
+ 1.10.6
data/README.md CHANGED
@@ -6,10 +6,10 @@ report and making it avaliable in a controller
6
6
 
7
7
  Getting started
8
8
  ---------------
9
- 1. Add JsonParser to your `Gemfile` and `bundle install`:
9
+ 1. Add House to your `Gemfile` and `bundle install`:
10
10
 
11
11
  ```ruby
12
- gem 'json_parser'
12
+ gem 'bidu-house'
13
13
  ```
14
14
 
15
15
 
@@ -51,7 +51,7 @@ with error and render the report
51
51
  - period: default search period (default: 1 day)
52
52
  - on: report bucket (default: :default)
53
53
 
54
- 4. Run the server and hit the health-check routes
54
+ 4. Run the server and hit the health-check routes
55
55
 
56
56
  ```
57
57
  wget http://localhost:3000/health-check/status
@@ -62,4 +62,4 @@ with error and render the report
62
62
  ```
63
63
  wget http://localhost:3000/health-check/status?period=3.days&threshold=0.005
64
64
  wget http://localhost:3000/health-check/late-status?period=1.hours&threshold=0.1
65
- ```
65
+ ```
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_runtime_dependency 'activesupport'
21
21
  gem.add_runtime_dependency 'concern_builder'
22
- gem.add_runtime_dependency 'bidu-core_ext'
22
+ gem.add_runtime_dependency 'bidu-active_ext'
23
23
  gem.add_runtime_dependency 'json_parser', '~> 1.1'
24
24
 
25
25
  gem.add_development_dependency "activerecord"
@@ -28,5 +28,6 @@ Gem::Specification.new do |gem|
28
28
  gem.add_development_dependency "bundler", "~> 1.6"
29
29
  gem.add_development_dependency "rake"
30
30
  gem.add_development_dependency "rspec", "~> 2.14"
31
+ gem.add_development_dependency 'pry-nav'
31
32
  gem.add_development_dependency 'simplecov'
32
33
  end
@@ -1,4 +1,4 @@
1
- require 'active_record/relation_ext'
1
+ require 'bidu/active_ext'
2
2
  require 'concern_builder'
3
3
  require 'bidu/core_ext'
4
4
  require 'bidu/period_parser'
@@ -8,7 +8,8 @@ require 'json_parser/type_cast_ext'
8
8
  module Bidu
9
9
  module House
10
10
  require 'bidu/house/concern'
11
- require 'bidu/house/error_report'
11
+ require 'bidu/house/report_config'
12
+ require 'bidu/house/report'
12
13
  require 'bidu/house/status'
13
14
  require 'bidu/house/report_builder'
14
15
  require 'bidu/house/status_builder'
@@ -4,7 +4,7 @@ module Bidu
4
4
 
5
5
  def render_status(key = :default)
6
6
  status = self.class.status_builder.build(key, params)
7
- render json: status.as_json, status: status.status
7
+ render json: status.as_json.stringify_keys.to_deep_hash, status: status.status
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,7 @@
1
+ module Bidu
2
+ module House
3
+ module Report
4
+ require 'bidu/house/report/error'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,78 @@
1
+ module Bidu
2
+ module House
3
+ module Report
4
+ class Error
5
+ include JsonParser
6
+
7
+ ALLOWED_PARAMETERS=[:period, :threshold]
8
+
9
+ attr_reader :json
10
+
11
+ json_parse :threshold, type: :float
12
+ json_parse :period, type: :period
13
+ json_parse :scope, :id, :clazz, :base_scope, :external_key, case: :snake
14
+
15
+ 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
27
+ end
28
+
29
+ def percentage
30
+ @percentage ||= fetch_percentage
31
+ end
32
+
33
+ def scoped
34
+ @scoped ||= fetch_scoped(last_entries, scope)
35
+ end
36
+
37
+ def error?
38
+ percentage > threshold
39
+ end
40
+
41
+ def as_json
42
+ {
43
+ ids: scoped.pluck(external_key),
44
+ percentage: percentage
45
+ }
46
+ end
47
+
48
+ private
49
+
50
+ def fetch_percentage
51
+ if (scope.is_a?(Symbol))
52
+ last_entries.percentage(*(scope.to_s.split('.').map(&:to_sym)))
53
+ else
54
+ last_entries.percentage(scope)
55
+ end
56
+ end
57
+
58
+ def fetch_scoped(base, scope)
59
+ if (scope.is_a?(Symbol))
60
+ scope.to_s.split('.').inject(base) do |entries, method|
61
+ entries.public_send(method)
62
+ end
63
+ else
64
+ base.where(scope)
65
+ end
66
+ end
67
+
68
+ def last_entries
69
+ @last_entries ||= base.where('updated_at >= ?', period.seconds.ago)
70
+ end
71
+
72
+ def base
73
+ fetch_scoped(clazz, base_scope)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -2,17 +2,20 @@ module Bidu
2
2
  module House
3
3
  class ReportBuilder
4
4
  def build(key, parameters = {})
5
- params = parameters.slice(:period, :threshold)
6
- config = configs[key].merge(params)
7
- Bidu::House::ErrorReport.new(config)
5
+ config = config_for(key)
6
+ config.build(parameters)
8
7
  end
9
8
 
10
9
  def add_config(key, config)
11
- configs[key] = config
10
+ configs[key] = Bidu::House::ReportConfig.new(config)
12
11
  end
13
12
 
14
13
  private
15
14
 
15
+ def config_for(key)
16
+ configs[key]
17
+ end
18
+
16
19
  def configs
17
20
  @configs ||= {}
18
21
  end
@@ -0,0 +1,37 @@
1
+ module Bidu
2
+ module House
3
+ class ReportConfig
4
+ attr_accessor :config
5
+
6
+ delegate :[], :[]=, :merge, to: :config
7
+
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def build(parameters)
13
+ params = slice_parameters(parameters)
14
+ report_class.new(config.merge(params))
15
+ end
16
+
17
+ private
18
+
19
+ def type
20
+ self[:type] ||= :error
21
+ end
22
+
23
+ def report_class
24
+ return type if type.is_a?(Class)
25
+ @report_class ||= Bidu::House::Report.const_get(type.to_s.camelize)
26
+ end
27
+
28
+ def slice_parameters(parameters)
29
+ parameters.slice(*allowed_parameters)
30
+ end
31
+
32
+ def allowed_parameters
33
+ report_class::ALLOWED_PARAMETERS
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  module Bidu
2
2
  module House
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -1,29 +1,37 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Bidu::House::ErrorReport do
3
+ describe Bidu::House::Report::Error do
4
4
  let(:errors) { 1 }
5
5
  let(:successes) { 1 }
6
6
  let(:old_errors) { 2 }
7
7
  let(:threshold) { 0.02 }
8
8
  let(:period) { 1.day }
9
9
  let(:external_key) { :external_id }
10
+ let(:scope) { :with_error }
11
+ let(:base_scope) { :all }
10
12
  let(:options) do
11
13
  {
12
14
  period: period,
13
15
  threshold: threshold,
14
- scope: :with_error,
16
+ scope: scope,
17
+ base_scope: base_scope,
15
18
  clazz: Document,
16
19
  external_key: external_key
17
20
  }
18
21
  end
19
22
  let(:subject) { described_class.new(options) }
23
+ let(:types) { [:a] }
20
24
  before do
21
25
  Document.all.each(&:destroy)
22
- successes.times { Document.create status: :success }
23
- errors.times do |i|
24
- Document.create status: :error, external_id: 10 * successes + i, outter_external_id: i
26
+ types.each do |type|
27
+ successes.times { Document.create status: :success, doc_type: type }
28
+ errors.times do |i|
29
+ Document.create status: :error, external_id: 10 * successes + i, outter_external_id: i, doc_type: type
30
+ end
31
+ old_errors.times do
32
+ Document.create status: :error, created_at: 2.days.ago, updated_at: 2.days.ago, doc_type: type
33
+ end
25
34
  end
26
- old_errors.times { Document.create status: :error, created_at: 2.days.ago, updated_at: 2.days.ago }
27
35
  end
28
36
 
29
37
  describe '#status' do
@@ -108,6 +116,68 @@ describe Bidu::House::ErrorReport do
108
116
  end
109
117
  end
110
118
  end
119
+
120
+ context 'when configuring with a complex scope' do
121
+ let(:types) { [:a, :b] }
122
+ let(:old_errors) { 0 }
123
+ let(:scope) { :'with_error.type_b' }
124
+ let(:errors) { 1 }
125
+ let(:successes) { 3 }
126
+ context 'as symbol' do
127
+ let(:scope) { :'with_error.type_b' }
128
+
129
+ it 'fetches from each scope in order' do
130
+ expect(subject.percentage).to eq(0.125)
131
+ end
132
+ end
133
+
134
+ context 'as string where scope' do
135
+ let(:scope) { "status = 'error' and doc_type = 'b'" }
136
+
137
+ it 'fetches from each scope in order' do
138
+ expect(subject.percentage).to eq(0.125)
139
+ end
140
+ end
141
+
142
+ context 'as hash where scope' do
143
+ let(:scope) { { status: :error, doc_type: :b } }
144
+
145
+ it 'fetches from each scope in order' do
146
+ expect(subject.percentage).to eq(0.125)
147
+ end
148
+ end
149
+ end
150
+
151
+ context 'when using a base scope' do
152
+ let(:types) { [:a, :b, :b, :b] }
153
+ let(:old_errors) { 0 }
154
+ let(:errors) { 1 }
155
+ let(:successes) { 3 }
156
+
157
+ context 'as symbol' do
158
+ let(:base_scope) { :type_b }
159
+
160
+ it 'fetches from each scope in order' do
161
+ expect(subject.percentage).to eq(0.25)
162
+ end
163
+ end
164
+
165
+ context 'as where clause' do
166
+ let(:base_scope) { "doc_type = 'b'" }
167
+
168
+ it 'fetches from each scope in order' do
169
+ expect(subject.percentage).to eq(0.25)
170
+ end
171
+ end
172
+
173
+ context 'as hash' do
174
+ let(:base_scope) { { doc_type: :b } }
175
+
176
+ it 'fetches from each scope in order' do
177
+ expect(subject.percentage).to eq(0.25)
178
+ end
179
+ end
180
+ end
111
181
  end
112
182
 
113
183
  describe '#scoped' do
@@ -146,6 +216,70 @@ describe Bidu::House::ErrorReport do
146
216
  end
147
217
  end
148
218
  end
219
+
220
+ context 'when configured with a complex scope' do
221
+ let(:types) { [:a, :b, :b] }
222
+ let(:old_errors) { 0 }
223
+
224
+ context 'as symbol' do
225
+ let(:scope) { :'with_error.type_b' }
226
+
227
+ it 'fetches from each scope in order' do
228
+ expect(subject.scoped.count).to eq(Document.with_error.type_b.count)
229
+ expect(subject.scoped.count).to eq(2 * Document.with_error.type_a.count)
230
+ end
231
+ end
232
+
233
+ context 'as hash' do
234
+ let(:scope) { { status: :error, doc_type: :b } }
235
+
236
+ it 'fetches from each scope in order' do
237
+ expect(subject.scoped.count).to eq(Document.with_error.type_b.count)
238
+ expect(subject.scoped.count).to eq(2 * Document.with_error.type_a.count)
239
+ end
240
+ end
241
+
242
+ context 'as string where scope' do
243
+ let(:scope) { "status = 'error' and doc_type = 'b'" }
244
+
245
+ it 'fetches from each scope in order' do
246
+ expect(subject.scoped.count).to eq(Document.with_error.type_b.count)
247
+ expect(subject.scoped.count).to eq(2 * Document.with_error.type_a.count)
248
+ end
249
+ end
250
+ end
251
+
252
+ context 'when using a base scope' do
253
+ let(:types) { [:a, :b, :b, :b] }
254
+ let(:old_errors) { 0 }
255
+
256
+ context 'as symbol' do
257
+ let(:base_scope) { :type_b }
258
+
259
+ it 'fetches from each scope in order' do
260
+ expect(subject.scoped.count).to eq(Document.with_error.type_b.count)
261
+ expect(subject.scoped.count).to eq(3 * Document.with_error.type_a.count)
262
+ end
263
+ end
264
+
265
+ context 'as where clause' do
266
+ let(:base_scope) { "doc_type = 'b'" }
267
+
268
+ it 'fetches from each scope in order' do
269
+ expect(subject.scoped.count).to eq(Document.with_error.type_b.count)
270
+ expect(subject.scoped.count).to eq(3 * Document.with_error.type_a.count)
271
+ end
272
+ end
273
+
274
+ context 'as hash' do
275
+ let(:base_scope) { { doc_type: :b } }
276
+
277
+ it 'fetches from each scope in order' do
278
+ expect(subject.scoped.count).to eq(Document.with_error.type_b.count)
279
+ expect(subject.scoped.count).to eq(3 * Document.with_error.type_a.count)
280
+ end
281
+ end
282
+ end
149
283
  end
150
284
 
151
285
  describe '#error?' do
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bidu::House::ReportConfig do
4
+ let(:config) { {} }
5
+ let(:parameters) { {} }
6
+ let(:subject) { described_class.new(config) }
7
+
8
+ describe '#build' do
9
+ context 'when no config is given' do
10
+ it do
11
+ expect(subject.build(parameters)).to be_a(Bidu::House::Report::Error)
12
+ end
13
+ end
14
+
15
+ context 'when a dummy type is given' do
16
+ let(:config) { { type: :dummy } }
17
+
18
+ it do
19
+ expect(subject.build(parameters)).to be_a(Bidu::House::Report::Dummy)
20
+ end
21
+ end
22
+
23
+ context 'when a class is given as type' do
24
+ let(:config) { { type: Bidu::House::Report::Dummy } }
25
+
26
+ it do
27
+ expect(subject.build(parameters)).to be_a(Bidu::House::Report::Dummy)
28
+ end
29
+ end
30
+
31
+ context 'when a global class is given as type' do
32
+ let(:config) { { type: Dummy } }
33
+
34
+ it do
35
+ expect(subject.build(parameters)).to be_a(Dummy)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -33,7 +33,7 @@ describe Bidu::House::ReportBuilder do
33
33
  describe '#build' do
34
34
  let(:ids) { [ 10 ] }
35
35
  it do
36
- expect(report).to be_a(Bidu::House::ErrorReport)
36
+ expect(report).to be_a(Bidu::House::Report::Error)
37
37
  end
38
38
 
39
39
  it 'builds the report using the given configuration' do
@@ -59,7 +59,7 @@ describe Bidu::House::ReportBuilder do
59
59
 
60
60
  context 'when passing a custom other parameters' do
61
61
  let(:parameters) do
62
- { scope: :with_success, clazz: Bidu::House::ErrorReport, external_key: :id, id: :failures }
62
+ { scope: :with_success, clazz: Bidu::House::Report::Error, external_key: :id, id: :failures }
63
63
  end
64
64
 
65
65
  it 'ignores the non customizable parameters' do
@@ -20,9 +20,9 @@ describe Bidu::House::Status do
20
20
  end
21
21
  let(:errors) { 0 }
22
22
  let(:successes) { 1 }
23
- let(:error_report) { Bidu::House::ErrorReport.new(report_options) }
23
+ let(:error_report) { Bidu::House::Report::Error.new(report_options) }
24
24
  let(:success_report) do
25
- Bidu::House::ErrorReport.new(success_options)
25
+ Bidu::House::Report::Error.new(success_options)
26
26
  end
27
27
  let(:reports) { [ error_report ] }
28
28
  let(:subject) { described_class.new(reports) }
@@ -56,7 +56,7 @@ describe Bidu::House::Status do
56
56
 
57
57
  context 'when there are both success and error reports' do
58
58
  let(:success_report) do
59
- Bidu::House::ErrorReport.new(report_options.merge(scope: :with_success))
59
+ Bidu::House::Report::Error.new(report_options.merge(scope: :with_success))
60
60
  end
61
61
  let(:reports) { [ success_report, error_report ] }
62
62
 
@@ -1,6 +1,11 @@
1
1
  require 'simplecov'
2
- SimpleCov.start
2
+ SimpleCov.profiles.define 'gem' do
3
+ add_filter '/spec/'
4
+ end
5
+
6
+ SimpleCov.start 'gem'
3
7
 
8
+ require 'pry-nav'
4
9
  require 'bidu/house'
5
10
 
6
11
  require 'active_record'
@@ -1,4 +1,6 @@
1
1
  class Document < ActiveRecord::Base
2
2
  scope :with_error, proc { where(status: :error) }
3
3
  scope :with_success, proc { where(status: :success) }
4
+ scope :type_a, proc { where(doc_type: :a) }
5
+ scope :type_b, proc { where(doc_type: :b) }
4
6
  end
@@ -0,0 +1,17 @@
1
+ module Bidu
2
+ module House
3
+ module Report
4
+ class Dummy
5
+ ALLOWED_PARAMETERS=[:period, :threshold]
6
+ def initialize(options)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ class Dummy
14
+ ALLOWED_PARAMETERS=[:period, :threshold]
15
+ def initialize(options)
16
+ end
17
+ end
@@ -3,6 +3,7 @@ ActiveRecord::Schema.define do
3
3
 
4
4
  create_table :documents, :force => true do |t|
5
5
  t.string :status
6
+ t.string :doc_type
6
7
  t.integer :external_id
7
8
  t.integer :outter_external_id
8
9
  t.timestamps null: true
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.1.2
4
+ version: 0.2.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: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2015-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bidu-core_ext
42
+ name: bidu-active_ext
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '>='
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ~>
137
137
  - !ruby/object:Gem::Version
138
138
  version: '2.14'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-nav
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: simplecov
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -165,20 +179,21 @@ files:
165
179
  - README.md
166
180
  - Rakefile
167
181
  - house.gemspec
168
- - lib/active_record/relation_ext.rb
169
182
  - lib/bidu.rb
170
183
  - lib/bidu/house.rb
171
184
  - lib/bidu/house/class_methods.rb
172
185
  - lib/bidu/house/concern.rb
173
- - lib/bidu/house/error_report.rb
186
+ - lib/bidu/house/report.rb
187
+ - lib/bidu/house/report/error.rb
174
188
  - lib/bidu/house/report_builder.rb
189
+ - lib/bidu/house/report_config.rb
175
190
  - lib/bidu/house/status.rb
176
191
  - lib/bidu/house/status_builder.rb
177
192
  - lib/bidu/house/version.rb
178
193
  - lib/bidu/period_parser.rb
179
194
  - lib/json_parser/type_cast_ext.rb
180
- - spec/lib/active_record/relation_spec.rb
181
- - spec/lib/bidu/house/error_report_spec.rb
195
+ - spec/lib/bidu/house/report/error_spec.rb
196
+ - spec/lib/bidu/house/report/report_config_spec.rb
182
197
  - spec/lib/bidu/house/report_builder_spec.rb
183
198
  - spec/lib/bidu/house/status_builder_spec.rb
184
199
  - spec/lib/bidu/house/status_spec.rb
@@ -186,6 +201,7 @@ files:
186
201
  - spec/spec_helper.rb
187
202
  - spec/support/fixture_helpers.rb
188
203
  - spec/support/models/document.rb
204
+ - spec/support/report/dummy.rb
189
205
  - spec/support/schema.rb
190
206
  homepage: https://github.com/Bidu/house
191
207
  licenses: []
@@ -1,19 +0,0 @@
1
- require 'active_record'
2
-
3
- module ActiveRecord
4
- class Relation
5
- def percentage(*filters)
6
- return 0 if count == 0
7
-
8
- if filters.first.is_a?(Symbol)
9
- filtered = filters.inject(self) do |relation, scope|
10
- relation.public_send(scope)
11
- end
12
- else
13
- filtered = where(*filters)
14
- end
15
-
16
- filtered.count * 1.0 / count
17
- end
18
- end
19
- end
@@ -1,51 +0,0 @@
1
- module Bidu
2
- module House
3
- class ErrorReport
4
- include JsonParser
5
-
6
- attr_reader :json
7
-
8
- json_parse :threshold, type: :float
9
- json_parse :period, type: :period
10
- json_parse :scope, :id, :clazz, :external_key, case: :snake
11
-
12
- def initialize(options)
13
- @json = {
14
- external_key: :id,
15
- threshold: 0.02,
16
- period: 1.day,
17
- scope: :with_error
18
- }.merge(options)
19
- end
20
-
21
- def status
22
- @status ||= error? ? :error : :ok
23
- end
24
-
25
- def percentage
26
- @percentage ||= last_entires.percentage(scope)
27
- end
28
-
29
- def scoped
30
- @scoped ||= last_entires.public_send(scope)
31
- end
32
-
33
- def error?
34
- percentage > threshold
35
- end
36
-
37
- def as_json
38
- {
39
- ids: scoped.pluck(external_key),
40
- percentage: percentage
41
- }
42
- end
43
-
44
- private
45
-
46
- def last_entires
47
- @last_entires ||= clazz.where('updated_at >= ?', period.seconds.ago)
48
- end
49
- end
50
- end
51
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ActiveRecord::Relation do
4
- describe '#percentage' do
5
- let(:error_number) { 1 }
6
- let(:success_number) { 1 }
7
-
8
- before do
9
- Document.all.each(&:destroy)
10
- error_number.times { Document.create(status: :error) }
11
- success_number.times { Document.create(status: :success) }
12
- end
13
-
14
- context 'when there are 50% documents with error' do
15
- it do
16
- expect(Document.all.percentage(status: :error)).to eq(0.5)
17
- end
18
- end
19
-
20
- context 'when there are 25% documents with error' do
21
- let(:success_number) { 3 }
22
-
23
- it do
24
- expect(Document.all.percentage(status: :error)).to eq(0.25)
25
- end
26
- end
27
-
28
- context 'when passing a sub scope' do
29
- before do
30
- Document.create(status: :on_going)
31
- end
32
-
33
- it 'does the math inside the scope' do
34
- expect(Document.where(status: [:error, :success]).percentage(status: :error)).to eq(0.5)
35
- end
36
- end
37
-
38
- context 'when passing a scope name instead of query' do
39
- it 'does the math inside the scope' do
40
- expect(Document.all.percentage(:with_error)).to eq(0.5)
41
- end
42
- end
43
- end
44
- end