Class: DispatchPolicy::PoliciesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dispatch_policy/policies_controller.rb

Constant Summary collapse

STALE_PENDING_THRESHOLD =
1.hour
PARTITION_LIST_PAGE_SIZE =
25

Instance Method Summary collapse

Instance Method Details

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 10

def index
  @policies = DispatchPolicy.registry.map do |name, job_class|
    scope   = StagedJob.where(policy_name: name)
    pending = scope.pending
    {
      name:            name,
      job_class:       job_class,
      policy:          job_class.resolved_dispatch_policy,
      pending_count:   pending.count,
      admitted_count:  scope.admitted.count,
      completed_24h:   scope.completed.where(completed_at: 24.hours.ago..).count,
      oldest_pending:  pending.minimum(:staged_at),
      stale_threshold: STALE_PENDING_THRESHOLD
    }
  end.sort_by { |p| -p[:pending_count] }

  @active_partitions = PartitionInflightCount.where("in_flight > 0").count
  @expired_leases    = StagedJob.expired_leases.count
end

#showObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/dispatch_policy/policies_controller.rb', line 30

def show
  scope = StagedJob.where(policy_name: @policy_name)
  @pending_count           = scope.pending.count
  @pending_eligible_count  = scope.pending.where("not_before_at IS NULL OR not_before_at <= ?", Time.current).count
  @pending_scheduled_count = @pending_count - @pending_eligible_count
  @admitted_count          = scope.admitted.count
  @completed_24h           = scope.completed.where(completed_at: 24.hours.ago..).count

  all_breakdown = partition_breakdown(scope)

  # "Watched" subset (passed via ?watch=a,b,c; the JS layer syncs it
  # with localStorage so the choice sticks across reloads).
  @watched_keys        = (params[:watch] || "").split(",").map(&:strip).reject(&:empty?)
  @partition_breakdown = @watched_keys.any? ? all_breakdown.select { |r| @watched_keys.include?(r[:partition]) } : []

  # Browsable list of every active partition with filter + sort + pagination.
  @partition_search = params[:q].to_s.strip
  @partition_page   = [ params[:page].to_i, 1 ].max
  @partition_sort   = %w[source partition pending in_flight completed_24h last_enqueued_at last_dispatched_at].include?(params[:sort]) ? params[:sort] : "activity"
  @partition_dir    = params[:dir] == "asc" ? "asc" : "desc"

  list = all_breakdown
  list = list.select { |r| r[:partition].to_s.downcase.include?(@partition_search.downcase) } if @partition_search.present?
  list = sort_partition_list(list, @partition_sort, @partition_dir)

  @partition_total_list = list.size
  offset                = (@partition_page - 1) * PARTITION_LIST_PAGE_SIZE
  @partition_list       = list[offset, PARTITION_LIST_PAGE_SIZE] || []

  load_adaptive_chart_data
  @throttle_buckets = ThrottleBucket
    .where(policy_name: @policy_name).order(:gate_name, :partition_key).limit(50)
  # Explicit select: don't load the `arguments` jsonb (job payload —
  # may contain PII / tokens) into memory just to render six fields.
  @pending_jobs = scope.pending
    .select(:id, :dedupe_key, :round_robin_key, :priority, :staged_at, :not_before_at)
    .order(:priority, :staged_at)
    .limit(50)
end