Sha256: 77e790b6797e0fc33ea0521823f42f161a928a0fd290d86f80ec6378e8d3748f

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

module LogStats
  module Requests
    module KPI
      PRECISION = 5

      def self.calculate(requests, stats)
        {
          request_count: requests.size,
          response_time_avg: avg(:service, requests),
          response_time_95p: percentile(:service, 0.95, requests),
          apdex: avg(Stats.method(:apdex_metric), stats),
          error_rate: rate(method(:error?), requests),
          timeout_rate: rate(method(:timeout?), requests)
        }
      end

      def self.rate(predicate, requests)
        (count(predicate, requests).to_f/requests.size).round(PRECISION)
      end

      def self.count(predicate, requests)
        requests.select(&predicate).size
      end

      def self.avg(metric, requests)
        sum = requests.reduce(0) do |acc, request|
          value = metric.respond_to?(:call) ? metric.call(request) : request[metric]
          acc + value
        end
        (sum/requests.size.to_f).round(PRECISION)
      end

      def self.percentile(field, percentile, requests)
        index = (requests.size*percentile).round - 1
        request = requests.sort_by { |request| request[field] }[index]
        request && request[field]
      end

      def self.error?(request)
        Stats.error_status?(request[:status])
      end

      def self.timeout?(request)
        request[:code] == "H12"
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
log_stats-0.4.9 lib/log_stats/requests/kpi.rb
log_stats-0.4.8 lib/log_stats/requests/kpi.rb
log_stats-0.4.7 lib/log_stats/requests/kpi.rb
log_stats-0.4.6 lib/log_stats/requests/kpi.rb
log_stats-0.4.5 lib/log_stats/requests/kpi.rb
log_stats-0.4.4 lib/log_stats/requests/kpi.rb
log_stats-0.4.3 lib/log_stats/requests/kpi.rb
log_stats-0.4.2 lib/log_stats/requests/kpi.rb
log_stats-0.4.1 lib/log_stats/requests/kpi.rb
log_stats-0.4.0 lib/log_stats/requests/kpi.rb
log_stats-0.3.0 lib/log_stats/requests/kpi.rb