Sha256: 68ac4506a2837a5a7da03f0e5062d77b8cacfafb56a78df01bd99cddd99b3a14

Contents?: true

Size: 1006 Bytes

Versions: 2

Compression:

Stored size: 1006 Bytes

Contents

module Bidu
  module House
    class ErrorReport
      include JsonParser

      attr_reader :json

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

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

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

      def percentage
        @percentage ||= last_entires.percentage(scope)
      end

      def scoped
        @scoped ||= last_entires.public_send(scope)
      end

      def error?
        percentage > threshold
      end

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

      private

      def last_entires
        @last_entires ||= clazz.where('updated_at >= ?', period.seconds.ago)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bidu-house-0.1.2 lib/bidu/house/error_report.rb
bidu-house-0.1.1 lib/bidu/house/error_report.rb