Sha256: 011a8bcc77e91f5b393711d07e6260397d0de05d6b687f4e7870e39039520aa4

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module QPush
  module Web
    module Apis
      class Stats
        DEFAULTS = {
          'queued' => 0,
          'success' => 0,
          'failed' => 0,
          'performed' => 0,
          'dead' => 0,
          'retries' => 0,
          'delayed' => 0,
          'current_queue' => 0,
          'percent_success' => 100.00
        }.freeze

        def initialize
          @stats = nil
        end

        def call
          retrieve_stats
          apply_defaults
          calculate_stats
          @stats
        end

        private

        def retrieve_stats
          @stats = Web.redis do |conn|
            conn.hgetall(QPush.keys.stats)
          end
        end

        def apply_defaults
          @stats.each { |k, v| @stats[k] = v.to_i }
          @stats = DEFAULTS.merge(@stats)
        end

        def calculate_stats
          @stats['percent_success'] = calculate_success
          @stats['current_queue'] = calculate_current
        end

        def calculate_success
          (100.00 - ((@stats['failed'].to_f / @stats['performed'].to_f) * 100.00)).round(2)
        end

        def calculate_current
          Web.redis do |c|
            QPush.keys.perform_lists.collect { |list| c.llen(list) }.reduce(:+)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qpush-0.1.6 lib/qpush/web/apis/stats.rb