Split order_most_active_desc scope in EE::Ci::Runner to make it possible to compute most active runners based on namespace ID
Currently, the order_most_active_desc
scope is doing too much. It is joining with ci_running_builds
and grouping by :id
/sorting by most used. In order to allow finding the top active group runners in a namespace, it would be better to split the current scope in 2. Something like:
scope :with_top_running_builds, -> do
joins(
<<~SQL
INNER JOIN (
SELECT "runner_id", ROW_NUMBER() OVER(PARTITION BY "runner_id" ORDER BY "runner_id") as rn
FROM "ci_running_builds"
) as "limited_builds" ON "limited_builds"."runner_id" = "ci_runners"."id"
AND "limited_builds".rn <= #{MOST_ACTIVE_RUNNERS_BUILDS_LIMIT}
SQL
)
end
scope :with_top_running_builds_by_namespace_id, ->(namespace_id) do
joins(
<<~SQL
INNER JOIN (
SELECT "runner_id", ROW_NUMBER() OVER(PARTITION BY "runner_id" ORDER BY "runner_id") as rn
FROM "ci_running_builds"
WHERE "ci_running_builds"."namespace_id" = #{namespace_id}
) as "limited_builds" ON "limited_builds"."runner_id" = "ci_runners"."id"
AND "limited_builds".rn <= #{MOST_ACTIVE_RUNNERS_BUILDS_LIMIT}
SQL
)
end
scope :order_most_active_desc, -> do
group(:id).reorder('COUNT(limited_builds.runner_id) DESC NULLS LAST', arel_table['id'].desc)
end
This would allow writing the following query:
Ci::Runner.group_type.with_top_running_builds_by_namespace_id(namespace.id).order_most_active_desc.limit(5)
This page may contain information related to upcoming products, features and functionality. It is important to note that the information presented is for informational purposes only, so please do not rely on the information for purchasing or planning purposes. Just like with all projects, the items mentioned on the page are subject to change or delay, and the development, release, and timing of any products, features, or functionality remain at the sole discretion of GitLab Inc.