Class: Roundhouse::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/roundhouse/api.rb

Defined Under Namespace

Classes: History, Queues

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



6
7
8
# File 'lib/roundhouse/api.rb', line 6

def initialize
  fetch_stats!
end

Instance Method Details

#avg_queue_lenObject



14
# File 'lib/roundhouse/api.rb', line 14

def avg_queue_len;         stat :avg_queue_len         end

#dead_sizeObject



32
33
34
# File 'lib/roundhouse/api.rb', line 32

def dead_size
  stat :dead_size
end

#default_queue_latencyObject



48
49
50
# File 'lib/roundhouse/api.rb', line 48

def default_queue_latency
  stat :default_queue_latency
end

#enqueuedObject



36
37
38
# File 'lib/roundhouse/api.rb', line 36

def enqueued
  stat :enqueued
end

#failedObject



20
21
22
# File 'lib/roundhouse/api.rb', line 20

def failed
  stat :failed
end

#fetch_stats!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/roundhouse/api.rb', line 56

def fetch_stats!
  pipe1_res = Roundhouse.redis do |conn|
    conn.pipelined do
      conn.get('stat:processed'.freeze)
      conn.get('stat:failed'.freeze)
      conn.zcard('schedule'.freeze)
      conn.zcard('retry'.freeze)
      conn.zcard('dead'.freeze)
      conn.scard('processes'.freeze)
      conn.llen(Roundhouse::Monitor::TURNTABLE.freeze)
      conn.smembers('processes'.freeze)
      conn.smembers(Roundhouse::Monitor::BUCKETS)
    end
  end

  queues_res = Roundhouse.redis do |conn|
    conn.pipelined do
      pipe1_res[8].each { |bucket| conn.hgetall("#{Roundhouse::Monitor::STATUS}:#{bucket}") }
    end
  end

  all_queue_ids = queues_res.map(&:keys).flatten

  pipe2_res = Roundhouse.redis do |conn|
    conn.pipelined do
      pipe1_res[7].each  {|key| conn.hget(key, 'busy'.freeze) }
      all_queue_ids.each {|queue| conn.llen("queue:#{queue}") }
    end
  end

  s = pipe1_res[7].size
  workers_size = pipe2_res[0...s].map(&:to_i).inject(0, &:+)
  enqueued     = pipe2_res[s..-1].map(&:to_i).inject(0, &:+)

  # Calculate queue status
  all_queue_count = all_queue_ids.size
  empty_queues = 0
  suspended_queues = 0
  queues_res.each do |h|
    h.each do |_,v|
      case v.to_i
      when Roundhouse::Monitor::EMPTY     then empty_queues += 1
      when Roundhouse::Monitor::SUSPENDED then suspended_queues +=1
      end
    end
  end

  avg_queue_len = (all_queue_count == 0 ? nil : enqueued / all_queue_count)

  #default_queue_latency = if (entry = pipe1_res[6].first)
  #                          Time.now.to_f - Roundhouse.load_json(entry)['enqueued_at'.freeze]
  #                        else
  #                          0
  #                        end

  @stats = {
    in_rotation:           pipe1_res[6].to_i,
    processed:             pipe1_res[0].to_i,
    failed:                pipe1_res[1].to_i,
    scheduled_size:        pipe1_res[2],
    retry_size:            pipe1_res[3],
    dead_size:             pipe1_res[4],
    processes_size:        pipe1_res[5],

    #default_queue_latency: default_queue_latency,
    workers_size:          workers_size,
    enqueued:              enqueued,
    num_queues:            all_queue_count,
    num_empty_queues:      empty_queues,
    num_suspended_queues:  suspended_queues,
    avg_queue_len:         avg_queue_len
  }
end

#in_rotationObject



10
# File 'lib/roundhouse/api.rb', line 10

def in_rotation;           stat :in_rotation           end

#num_empty_queuesObject



12
# File 'lib/roundhouse/api.rb', line 12

def num_empty_queues;      stat :num_empty_queues      end

#num_queuesObject



11
# File 'lib/roundhouse/api.rb', line 11

def num_queues;            stat :num_queues            end

#num_suspended_queuesObject



13
# File 'lib/roundhouse/api.rb', line 13

def num_suspended_queues;  stat :num_suspended_queues  end

#processedObject



16
17
18
# File 'lib/roundhouse/api.rb', line 16

def processed
  stat :processed
end

#processes_sizeObject



40
41
42
# File 'lib/roundhouse/api.rb', line 40

def processes_size
  stat :processes_size
end

#queuesObject



52
53
54
# File 'lib/roundhouse/api.rb', line 52

def queues
  Roundhouse::Stats::Queues.new.lengths
end

#reset(*stats) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/roundhouse/api.rb', line 130

def reset(*stats)
  all   = %w(failed processed)
  stats = stats.empty? ? all : all & stats.flatten.compact.map(&:to_s)

  mset_args = []
  stats.each do |stat|
    mset_args << "stat:#{stat}"
    mset_args << 0
  end
  Roundhouse.redis do |conn|
    conn.mset(*mset_args)
  end
end

#retry_sizeObject



28
29
30
# File 'lib/roundhouse/api.rb', line 28

def retry_size
  stat :retry_size
end

#scheduled_sizeObject



24
25
26
# File 'lib/roundhouse/api.rb', line 24

def scheduled_size
  stat :scheduled_size
end

#workers_sizeObject



44
45
46
# File 'lib/roundhouse/api.rb', line 44

def workers_size
  stat :workers_size
end