Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-04-22 12:09:45 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2021-04-22 12:09:45 +0300
commitb81fd57f3d62db4455108c8de4b8d7b8d403de35 (patch)
treed6750edadd7a35bb6719a28f8a56b4e4174a340a /lib
parentd17c58402b28c7eabe74df21b57ae31beec56b1f (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r--lib/api/environments.rb2
-rw-r--r--lib/gitlab/alert_management/payload/base.rb2
-rw-r--r--lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb13
-rw-r--r--lib/gitlab/pagination/keyset/order.rb2
-rw-r--r--lib/gitlab/pagination/keyset/simple_order_builder.rb137
-rw-r--r--lib/gitlab/usage_data.rb2
6 files changed, 155 insertions, 3 deletions
diff --git a/lib/api/environments.rb b/lib/api/environments.rb
index b606b2e814d..57e548183b0 100644
--- a/lib/api/environments.rb
+++ b/lib/api/environments.rb
@@ -26,7 +26,7 @@ module API
get ':id/environments' do
authorize! :read_environment, user_project
- environments = ::EnvironmentsFinder.new(user_project, current_user, params).execute
+ environments = ::Environments::EnvironmentsFinder.new(user_project, current_user, params).execute
present paginate(environments), with: Entities::Environment, current_user: current_user
end
diff --git a/lib/gitlab/alert_management/payload/base.rb b/lib/gitlab/alert_management/payload/base.rb
index 786c5bf675b..11868ab4e14 100644
--- a/lib/gitlab/alert_management/payload/base.rb
+++ b/lib/gitlab/alert_management/payload/base.rb
@@ -130,7 +130,7 @@ module Gitlab
strong_memoize(:environment) do
next unless environment_name
- EnvironmentsFinder
+ ::Environments::EnvironmentsFinder
.new(project, nil, { name: environment_name })
.execute
.first
diff --git a/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb b/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
index 318c6e1734f..f1b74999897 100644
--- a/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
+++ b/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
@@ -10,6 +10,8 @@ module Gitlab
extend ActiveSupport::Concern
def ordered_items
+ raise ArgumentError, 'Relation must have a primary key' unless items.primary_key.present?
+
return super unless Gitlab::Pagination::Keyset::Order.keyset_aware?(items)
items
@@ -40,6 +42,17 @@ module Gitlab
sliced = slice_nodes(sliced, after, :after) if after.present?
sliced
end
+
+ def items
+ original_items = super
+ return original_items if Gitlab::Pagination::Keyset::Order.keyset_aware?(original_items) || Feature.disabled?(:new_graphql_keyset_pagination)
+
+ strong_memoize(:generic_keyset_pagination_items) do
+ rebuilt_items_with_keyset_order, success = Gitlab::Pagination::Keyset::SimpleOrderBuilder.build(original_items)
+
+ success ? rebuilt_items_with_keyset_order : original_items
+ end
+ end
end
end
end
diff --git a/lib/gitlab/pagination/keyset/order.rb b/lib/gitlab/pagination/keyset/order.rb
index e596e1bac9d..648b371ee9b 100644
--- a/lib/gitlab/pagination/keyset/order.rb
+++ b/lib/gitlab/pagination/keyset/order.rb
@@ -170,6 +170,8 @@ module Gitlab
self.class.build(column_definitions.map(&:reverse))
end
+ alias_method :to_sql, :to_s
+
private
# Adds extra columns to the SELECT clause
diff --git a/lib/gitlab/pagination/keyset/simple_order_builder.rb b/lib/gitlab/pagination/keyset/simple_order_builder.rb
new file mode 100644
index 00000000000..5ac5737c3be
--- /dev/null
+++ b/lib/gitlab/pagination/keyset/simple_order_builder.rb
@@ -0,0 +1,137 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Pagination
+ module Keyset
+ # This class transforms the `order()` values from an Activerecord scope into a
+ # Gitlab::Pagination::Keyset::Order instance so the query later can be used in
+ # keyset pagination.
+ #
+ # Return values:
+ # [transformed_scope, true] # true indicates that the new scope was successfully built
+ # [orginal_scope, false] # false indicates that the order values are not supported in this class
+ class SimpleOrderBuilder
+ def self.build(scope)
+ new(scope: scope).build
+ end
+
+ def initialize(scope:)
+ @scope = scope
+ @order_values = scope.order_values
+ @model_class = scope.model
+ @arel_table = @model_class.arel_table
+ @primary_key = @model_class.primary_key
+ end
+
+ def build
+ order = if order_values.empty?
+ primary_key_descending_order
+ elsif ordered_by_primary_key?
+ primary_key_order
+ elsif ordered_by_other_column?
+ column_with_tie_breaker_order
+ elsif ordered_by_other_column_with_tie_breaker?
+ tie_breaker_attribute = order_values.second
+
+ tie_breaker_column_order = Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
+ attribute_name: model_class.primary_key,
+ order_expression: tie_breaker_attribute
+ )
+
+ column_with_tie_breaker_order(tie_breaker_column_order)
+ end
+
+ order ? [scope.reorder!(order), true] : [scope, false] # [scope, success]
+ end
+
+ private
+
+ attr_reader :scope, :order_values, :model_class, :arel_table, :primary_key
+
+ def primary_key_descending_order
+ Gitlab::Pagination::Keyset::Order.build([
+ Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
+ attribute_name: model_class.primary_key,
+ order_expression: arel_table[primary_key].desc
+ )
+ ])
+ end
+
+ def primary_key_order
+ Gitlab::Pagination::Keyset::Order.build([
+ Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
+ attribute_name: model_class.primary_key,
+ order_expression: order_values.first
+ )
+ ])
+ end
+
+ def column_with_tie_breaker_order(tie_breaker_column_order = default_tie_breaker_column_order)
+ order_expression = order_values.first
+ attribute_name = order_expression.expr.name
+
+ column_nullable = model_class.columns.find { |column| column.name == attribute_name }.null
+
+ nullable = if column_nullable && order_expression.is_a?(Arel::Nodes::Ascending)
+ :nulls_last
+ elsif column_nullable && order_expression.is_a?(Arel::Nodes::Descending)
+ :nulls_first
+ else
+ :not_nullable
+ end
+
+ Gitlab::Pagination::Keyset::Order.build([
+ Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
+ attribute_name: attribute_name,
+ order_expression: order_expression,
+ nullable: nullable,
+ distinct: false
+ ),
+ tie_breaker_column_order
+ ])
+ end
+
+ def ordered_by_primary_key?
+ return unless order_values.one?
+
+ attribute = order_values.first.try(:expr)
+
+ return unless attribute
+
+ arel_table[primary_key].to_s == attribute.to_s
+ end
+
+ def ordered_by_other_column?
+ return unless order_values.one?
+
+ attribute = order_values.first.try(:expr)
+
+ return unless attribute
+ return unless attribute.try(:name)
+
+ model_class.column_names.include?(attribute.name.to_s)
+ end
+
+ def ordered_by_other_column_with_tie_breaker?
+ return unless order_values.size == 2
+
+ attribute = order_values.first.try(:expr)
+ tie_breaker_attribute = order_values.second.try(:expr)
+
+ return unless attribute
+ return unless tie_breaker_attribute
+
+ model_class.column_names.include?(attribute.name.to_s) &&
+ arel_table[primary_key].to_s == tie_breaker_attribute.to_s
+ end
+
+ def default_tie_breaker_column_order
+ Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
+ attribute_name: model_class.primary_key,
+ order_expression: arel_table[primary_key].desc
+ )
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/usage_data.rb b/lib/gitlab/usage_data.rb
index d24d172520f..d476faa42c2 100644
--- a/lib/gitlab/usage_data.rb
+++ b/lib/gitlab/usage_data.rb
@@ -164,7 +164,7 @@ module Gitlab
projects_with_repositories_enabled: count(ProjectFeature.where('repository_access_level > ?', ProjectFeature::DISABLED)),
projects_with_tracing_enabled: count(ProjectTracingSetting),
projects_with_error_tracking_enabled: count(::ErrorTracking::ProjectErrorTrackingSetting.where(enabled: true)),
- projects_with_alerts_service_enabled: count(Service.active.where(type: 'AlertsService')),
+ projects_with_alerts_service_enabled: DEPRECATED_VALUE,
projects_with_alerts_created: distinct_count(::AlertManagement::Alert, :project_id),
projects_with_enabled_alert_integrations: distinct_count(::AlertManagement::HttpIntegration.active, :project_id),
projects_with_prometheus_alerts: distinct_count(PrometheusAlert, :project_id),