blob: a43d3f3a100d913ce466a35b7ea3fc12058c5018 (
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
|
# frozen_string_literal: true
module Resolvers
module Ci
class RunnerJobCountResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
type GraphQL::Types::Int, null: true
authorize :read_runner
authorizes_object!
argument :statuses, [::Types::Ci::JobStatusEnum],
required: false,
description: 'Filter jobs by status.',
alpha: { milestone: '16.2' }
alias_method :runner, :object
def resolve(statuses: nil)
BatchLoader::GraphQL.for(runner.id).batch(key: [:job_count, statuses]) do |runner_ids, loader, _args|
counts_by_runner = calculate_job_count_per_runner(runner_ids, statuses)
runner_ids.each do |runner_id|
loader.call(runner_id, counts_by_runner[runner_id]&.count || 0)
end
end
end
private
def calculate_job_count_per_runner(runner_ids, statuses)
# rubocop: disable CodeReuse/ActiveRecord
builds_tbl = ::Ci::Build.arel_table
runners_tbl = ::Ci::Runner.arel_table
lateral_query = ::Ci::Build.select(1).where(builds_tbl['runner_id'].eq(runners_tbl['id']))
lateral_query = lateral_query.where(status: statuses) if statuses
# We limit to 1 above the JOB_COUNT_LIMIT to indicate that more items exist after JOB_COUNT_LIMIT
lateral_query = lateral_query.limit(::Types::Ci::RunnerType::JOB_COUNT_LIMIT + 1)
::Ci::Runner.joins("JOIN LATERAL (#{lateral_query.to_sql}) builds_with_limit ON true")
.id_in(runner_ids)
.select(:id, Arel.star.count.as('count'))
.group(:id)
.index_by(&:id)
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
end
|