lib/bidu/house/report/error.rb in bidu-house-0.2.1 vs lib/bidu/house/report/error.rb in bidu-house-1.0.0
- old
+ new
@@ -1,33 +1,26 @@
module Bidu
module House
- module Report
- class Error
- include JsonParser
-
+ class Report
+ class Error < Report
ALLOWED_PARAMETERS=[:period, :threshold]
+ DEFAULT_OPTION = {
+ external_key: :id,
+ threshold: 0.02,
+ period: 1.day,
+ scope: :with_error,
+ base_scope: :all,
+ uniq: false
+ }
- attr_reader :json
-
json_parse :threshold, type: :float
- json_parse :period, type: :period
- json_parse :scope, :id, :clazz, :base_scope, :external_key, case: :snake
+ json_parse :scope, :id, :external_key, :uniq, :limit, case: :snake
def initialize(options)
- @json = {
- external_key: :id,
- threshold: 0.02,
- period: 1.day,
- scope: :with_error,
- base_scope: :all
- }.merge(options)
+ super(DEFAULT_OPTION.merge(options))
end
- def status
- @status ||= error? ? :error : :ok
- end
-
def percentage
@percentage ||= fetch_percentage
end
def scoped
@@ -36,47 +29,33 @@
def error?
@error ||= percentage > threshold
end
- def status
- error? ? :error : :ok
- end
-
def as_json
{
- ids: scoped.pluck(external_key),
+ ids: ids,
percentage: percentage,
status: status
}
end
private
+ def ids
+ relation = scoped
+ relation = relation.uniq if uniq
+ relation = relation.limit(limit) if limit
+
+ relation.pluck(external_key)
+ end
+
def fetch_percentage
if (scope.is_a?(Symbol))
last_entries.percentage(*(scope.to_s.split('.').map(&:to_sym)))
else
last_entries.percentage(scope)
end
- end
-
- def fetch_scoped(base, scope)
- if (scope.is_a?(Symbol))
- scope.to_s.split('.').inject(base) do |entries, method|
- entries.public_send(method)
- end
- else
- base.where(scope)
- end
- end
-
- def last_entries
- @last_entries ||= base.where('updated_at >= ?', period.seconds.ago)
- end
-
- def base
- fetch_scoped(clazz, base_scope)
end
end
end
end
end