Class: SolidQueueMonitor::ChartDataService

Inherits:
Object
  • Object
show all
Defined in:
app/services/solid_queue_monitor/chart_data_service.rb

Constant Summary collapse

TIME_RANGES =
{
  '15m' => { duration: 15.minutes, buckets: 15, label_format: '%H:%M', label: 'Last 15 minutes' },
  '30m' => { duration: 30.minutes, buckets: 15, label_format: '%H:%M', label: 'Last 30 minutes' },
  '1h' => { duration: 1.hour,     buckets: 12, label_format: '%H:%M', label: 'Last 1 hour' },
  '3h' => { duration: 3.hours,    buckets: 18, label_format: '%H:%M', label: 'Last 3 hours' },
  '6h' => { duration: 6.hours,    buckets: 24, label_format: '%H:%M', label: 'Last 6 hours' },
  '12h' => { duration: 12.hours, buckets: 24, label_format: '%H:%M', label: 'Last 12 hours' },
  '1d' => { duration: 1.day,      buckets: 24, label_format: '%H:%M', label: 'Last 24 hours' },
  '3d' => { duration: 3.days,     buckets: 36, label_format: '%m/%d %H:%M', label: 'Last 3 days' },
  '1w' => { duration: 7.days,     buckets: 28, label_format: '%m/%d', label: 'Last 7 days' }
}.freeze
DEFAULT_TIME_RANGE =
'1d'

Instance Method Summary collapse

Constructor Details

#initialize(time_range: DEFAULT_TIME_RANGE) ⇒ ChartDataService

Returns a new instance of ChartDataService.



19
20
21
22
# File 'app/services/solid_queue_monitor/chart_data_service.rb', line 19

def initialize(time_range: DEFAULT_TIME_RANGE)
  @time_range = TIME_RANGES.key?(time_range) ? time_range : DEFAULT_TIME_RANGE
  @config     = TIME_RANGES[@time_range]
end

Instance Method Details

#calculateObject



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
# File 'app/services/solid_queue_monitor/chart_data_service.rb', line 24

def calculate
  end_time       = Time.current
  start_time     = end_time - @config[:duration]
  bucket_seconds = (@config[:duration] / @config[:buckets]).to_i
  buckets        = build_buckets(start_time, bucket_seconds)

  created_data   = bucket_counts(SolidQueue::Job, :created_at,  start_time, end_time, bucket_seconds)
  completed_data = bucket_counts(SolidQueue::Job, :finished_at, start_time, end_time, bucket_seconds, exclude_nil: true)
  failed_data    = bucket_counts(SolidQueue::FailedExecution, :created_at, start_time, end_time, bucket_seconds)

  created_arr   = fill_buckets(buckets, created_data)
  completed_arr = fill_buckets(buckets, completed_data)
  failed_arr    = fill_buckets(buckets, failed_data)

  {
    labels: buckets.map { |b| b[:label] }, # rubocop:disable Rails/Pluck
    created: created_arr,
    completed: completed_arr,
    failed: failed_arr,
    totals: { created: created_arr.sum, completed: completed_arr.sum, failed: failed_arr.sum },
    time_range: @time_range,
    time_range_label: @config[:label],
    available_ranges: TIME_RANGES.transform_values { |v| v[:label] }
  }
end