Sha256: 10f2622fd0d6a44439c127c20a7db341ca170f92e9354f83894a599e69721b83

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Bidu
  module House
    module Report
      class Error
        include JsonParser

        ALLOWED_PARAMETERS=[:period, :threshold]

        attr_reader :json

        json_parse :threshold, type: :float
        json_parse :period, type: :period
        json_parse :scope, :id, :clazz, :base_scope, :external_key, case: :snake

        def initialize(options)
          @json = {
            external_key: :id,
            threshold: 0.02,
            period: 1.day,
            scope: :with_error,
            base_scope: :all
          }.merge(options)
        end

        def status
          @status ||= error? ? :error : :ok
        end

        def percentage
          @percentage ||= fetch_percentage
        end

        def scoped
          @scoped ||= fetch_scoped(last_entries, scope)
        end

        def error?
          @error ||= percentage > threshold
        end

        def status
          error? ? :error : :ok
        end

        def as_json
          {
            ids: scoped.pluck(external_key),
            percentage: percentage,
            status: status
          }
        end

        private

        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bidu-house-0.2.1 lib/bidu/house/report/error.rb