summaryrefslogtreecommitdiff
path: root/lib/oxidized/node/stats.rb
blob: fbc2cf749f21294b88e167038b85eef1a12de31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Oxidized
  class Node
    class Stats
      attr_accessor :mtime
      MAX_STAT = 10

      # @param [Job] job job whose information add to stats
      # @return [void]
      def add job
        stat = {
          :start  => job.start,
          :end    => job.end,
          :time   => job.time,
        }
        @stats[job.status] ||= []
        @stats[job.status].shift if @stats[job.status].size > MAX_STAT
        @stats[job.status].push stat
        @stats[:counter][job.status] += 1
      end

      # @param [Symbol] status stats for specific status
      # @return [Hash,Array] Hash of stats for every status or Array of stats for specific status
      def get status = nil
        status ? @stats[status] : @stats
      end

      def get_counter counter = nil
        counter ? @stats[:counter][counter] : @stats[:counter]
      end

      def successes
        @stats[:counter][:success]
      end

      def failures
        @stats[:counter].reduce(0) { |m, h| h[0] == :success ? m : m + h[1] }
      end

      def update_mtime
        @mtime = Time.now.utc
      end

      private

      def initialize
        @mtime = "unknown"
        @stats = {}
        @stats[:counter] = Hash.new 0
      end
    end
  end
end