Sha256: 3f09b83931b59923e165697ae46b72759ad56df355d4274faa89dfe8eb1eec30

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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 = QPush.redis.with 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
          QPush.redis.with 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.4 lib/qpush/web/apis/stats.rb