Class: Workerholic::StatsAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/workerholic/statistics_api.rb

Constant Summary collapse

CATEGORIES =
%w(completed_jobs failed_jobs)
POLLING_INTERVAL =
10

Class Method Summary collapse

Class Method Details

.active_proccessesObject



66
67
68
69
# File 'lib/workerholic/statistics_api.rb', line 66

def self.active_proccesses
  namespace = 'workerholic:stats:memory:processes'
  storage.hash_keys(namespace)
end

.history_for_period(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/workerholic/statistics_api.rb', line 71

def self.history_for_period(options={})
  raise ArgumentError, 'Please provide a category namespace' unless options[:category]

  if options[:klass]
    namespace = "workerholic:stats:historical:#{options[:category]}:#{options[:klass]}"
  else
    namespace = "workerholic:stats:historical:#{options[:category]}"
  end

  period = options[:period] || 30
  date_ranges = get_past_dates(period)

  job_counts = storage.hash_get_multiple_elements(namespace, date_ranges)

  combine_ranges(job_counts: job_counts, date_ranges: date_ranges)
end

.job_statistics(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/workerholic/statistics_api.rb', line 6

def self.job_statistics(options={})
  raise ArgumentError, "Please specify one of the following categories: 'completed_jobs', 'failed_jobs'" unless CATEGORIES.include? options[:category]

  job_classes = storage.get_keys_for_namespace("workerholic:stats:#{options[:category]}:*")

  if options[:count_only]
    parse_job_classes(job_classes)
  else
    parse_job_classes(job_classes, false)
  end
end

.job_statistics_history(category) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/workerholic/statistics_api.rb', line 18

def self.job_statistics_history(category)
  raise ArgumentError, "Please specify one of the following categories: 'completed_jobs', 'failed_jobs'" unless CATEGORIES.include? category

  current_time = Time.now.to_i
  all_job_stats(category).reduce([]) do |result, job|
    completed_time = job.last.to_i
    index = (current_time - completed_time) / POLLING_INTERVAL

    result[index] = result[index] ? result[index] + 1 : 1

    result
  end
end

.jobs_classes(historical) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/workerholic/statistics_api.rb', line 42

def self.jobs_classes(historical)
  base_namespace = historical ? 'workerholic:stats:historical:' : 'workerholic:stats:'

  completed_classes = storage.get_keys_for_namespace( base_namespace + 'completed_jobs:*')
  failed_classes = storage.get_keys_for_namespace(base_namespace + 'failed_jobs:*')
  combined_classes = completed_classes + failed_classes

  combined_classes.map { |klass| klass.split(':').last }.uniq
end

.process_statsObject



61
62
63
64
# File 'lib/workerholic/statistics_api.rb', line 61

def self.process_stats
  namespace = 'workerholic:stats:memory:processes'
  storage.hash_get_all(namespace)
end

.queued_jobsObject



52
53
54
55
56
57
58
59
# File 'lib/workerholic/statistics_api.rb', line 52

def self.queued_jobs
  fetched_queues = storage.fetch_queue_names
  parsed_queues = fetched_queues.map do |queue|
    [queue, storage.list_length(queue)]
  end

  parsed_queues
end

.scheduled_jobs(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/workerholic/statistics_api.rb', line 32

def self.scheduled_jobs(options={})
  namespace = 'workerholic:scheduled_jobs'
  if options[:count_only]
    storage.sorted_set_size(namespace)
  else
    serialized_jobs = storage.sorted_set_all_members(namespace)
    parse_scheduled_jobs(serialized_jobs)
  end
end