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/app
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-08 03:09:30 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-08 03:09:30 +0300
commit060c842402c00f830a810702600cbe39dfa6cf62 (patch)
tree743bd65ac0c1d4d6518ae8cdd4af5718ec7fb890 /app
parent6867eff1f997a881cd3ea64109f7ba2d4b42fde4 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app')
-rw-r--r--app/controllers/projects/environments_controller.rb2
-rw-r--r--app/finders/environments_finder.rb28
-rw-r--r--app/graphql/resolvers/environments_resolver.rb6
-rw-r--r--app/models/environment.rb4
-rw-r--r--app/models/users_statistics.rb57
-rw-r--r--app/services/metrics/dashboard/transient_embed_service.rb35
-rw-r--r--app/workers/all_queues.yml7
-rw-r--r--app/workers/users/create_statistics_worker.rb19
8 files changed, 142 insertions, 16 deletions
diff --git a/app/controllers/projects/environments_controller.rb b/app/controllers/projects/environments_controller.rb
index 5c49fa842a4..e51a5c7b84d 100644
--- a/app/controllers/projects/environments_controller.rb
+++ b/app/controllers/projects/environments_controller.rb
@@ -222,7 +222,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
def metrics_dashboard_params
params
- .permit(:embedded, :group, :title, :y_label, :dashboard_path, :environment, :sample_metrics)
+ .permit(:embedded, :group, :title, :y_label, :dashboard_path, :environment, :sample_metrics, :embed_json)
.merge(dashboard_path: params[:dashboard], environment: environment)
end
diff --git a/app/finders/environments_finder.rb b/app/finders/environments_finder.rb
index 32942c46208..32ca1a42db7 100644
--- a/app/finders/environments_finder.rb
+++ b/app/finders/environments_finder.rb
@@ -3,6 +3,8 @@
class EnvironmentsFinder
attr_reader :project, :current_user, :params
+ InvalidStatesError = Class.new(StandardError)
+
def initialize(project, current_user, params = {})
@project, @current_user, @params = project, current_user, params
end
@@ -45,6 +47,9 @@ class EnvironmentsFinder
environments = by_name(environments)
environments = by_search(environments)
+ # Raises InvalidStatesError if params[:states] contains invalid states.
+ environments = by_states(environments)
+
environments
end
@@ -91,4 +96,27 @@ class EnvironmentsFinder
environments
end
end
+
+ def by_states(environments)
+ if params[:states].present?
+ environments_with_states(environments)
+ else
+ environments
+ end
+ end
+
+ def environments_with_states(environments)
+ # Convert to array of strings
+ states = Array(params[:states]).map(&:to_s)
+
+ raise InvalidStatesError, _('Requested states are invalid') unless valid_states?(states)
+
+ environments.with_states(states)
+ end
+
+ def valid_states?(states)
+ valid_states = Environment.valid_states.map(&:to_s)
+
+ (states - valid_states).empty?
+ end
end
diff --git a/app/graphql/resolvers/environments_resolver.rb b/app/graphql/resolvers/environments_resolver.rb
index 868abef98eb..4e9a17f1e17 100644
--- a/app/graphql/resolvers/environments_resolver.rb
+++ b/app/graphql/resolvers/environments_resolver.rb
@@ -10,6 +10,10 @@ module Resolvers
required: false,
description: 'Search query'
+ argument :states, [GraphQL::STRING_TYPE],
+ required: false,
+ description: 'States of environments that should be included in result'
+
type Types::EnvironmentType, null: true
alias_method :project, :object
@@ -18,6 +22,8 @@ module Resolvers
return unless project.present?
EnvironmentsFinder.new(project, context[:current_user], args).find
+ rescue EnvironmentsFinder::InvalidStatesError => exception
+ raise Gitlab::Graphql::Errors::ArgumentError, exception.message
end
end
end
diff --git a/app/models/environment.rb b/app/models/environment.rb
index 23c2296688d..b2391f33aca 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -119,6 +119,10 @@ class Environment < ApplicationRecord
find_or_create_by(name: name)
end
+ def self.valid_states
+ self.state_machine.states.map(&:name)
+ end
+
class << self
##
# This method returns stop actions (jobs) for multiple environments within one
diff --git a/app/models/users_statistics.rb b/app/models/users_statistics.rb
index 5b4c0ef37d0..1a500717efd 100644
--- a/app/models/users_statistics.rb
+++ b/app/models/users_statistics.rb
@@ -12,21 +12,48 @@ class UsersStatistics < ApplicationRecord
:blocked
].freeze
- private
-
- def highest_role_stats
- return unless Feature.enabled?(:users_statistics)
-
- {
- owner: batch_count_for_access_level(Gitlab::Access::OWNER),
- maintainer: batch_count_for_access_level(Gitlab::Access::MAINTAINER),
- developer: batch_count_for_access_level(Gitlab::Access::DEVELOPER),
- reporter: batch_count_for_access_level(Gitlab::Access::REPORTER),
- guest: batch_count_for_access_level(Gitlab::Access::GUEST)
- }
- end
+ class << self
+ def create_current_stats!
+ stats_by_role = highest_role_stats
+
+ create!(
+ without_groups_and_projects: without_groups_and_projects_stats,
+ with_highest_role_guest: stats_by_role[:guest],
+ with_highest_role_reporter: stats_by_role[:reporter],
+ with_highest_role_developer: stats_by_role[:developer],
+ with_highest_role_maintainer: stats_by_role[:maintainer],
+ with_highest_role_owner: stats_by_role[:owner],
+ bots: bot_stats,
+ blocked: blocked_stats
+ )
+ end
+
+ private
+
+ def highest_role_stats
+ {
+ owner: batch_count_for_access_level(Gitlab::Access::OWNER),
+ maintainer: batch_count_for_access_level(Gitlab::Access::MAINTAINER),
+ developer: batch_count_for_access_level(Gitlab::Access::DEVELOPER),
+ reporter: batch_count_for_access_level(Gitlab::Access::REPORTER),
+ guest: batch_count_for_access_level(Gitlab::Access::GUEST)
+ }
+ end
+
+ def without_groups_and_projects_stats
+ batch_count_for_access_level(nil)
+ end
+
+ def bot_stats
+ Gitlab::Database::BatchCount.batch_count(User.bots)
+ end
+
+ def blocked_stats
+ Gitlab::Database::BatchCount.batch_count(User.blocked)
+ end
- def batch_count_for_access_level(access_level)
- Gitlab::Database::BatchCount.batch_count(UserHighestRole.with_highest_access_level(access_level))
+ def batch_count_for_access_level(access_level)
+ Gitlab::Database::BatchCount.batch_count(UserHighestRole.with_highest_access_level(access_level))
+ end
end
end
diff --git a/app/services/metrics/dashboard/transient_embed_service.rb b/app/services/metrics/dashboard/transient_embed_service.rb
new file mode 100644
index 00000000000..035707dceb9
--- /dev/null
+++ b/app/services/metrics/dashboard/transient_embed_service.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+# Acts as a pass-through to allow embeddable dashboards to be
+# generated based on external data, but still processed with the
+# required attributes that allow the FE to render them appropriately.
+#
+# Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards.
+module Metrics
+ module Dashboard
+ class TransientEmbedService < ::Metrics::Dashboard::BaseEmbedService
+ extend ::Gitlab::Utils::Override
+
+ class << self
+ def valid_params?(params)
+ [
+ embedded?(params[:embedded]),
+ params[:embed_json]
+ ].all?
+ end
+ end
+
+ private
+
+ override :get_raw_dashboard
+ def get_raw_dashboard
+ JSON.parse(params[:embed_json])
+ end
+
+ override :sequence
+ def sequence
+ [STAGES::EndpointInserter]
+ end
+ end
+ end
+end
diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml
index e37fa52f5ff..3ee8901b23b 100644
--- a/app/workers/all_queues.yml
+++ b/app/workers/all_queues.yml
@@ -262,6 +262,13 @@
:resource_boundary: :unknown
:weight: 1
:idempotent:
+- :name: cronjob:users_create_statistics
+ :feature_category: :users
+ :has_external_dependencies:
+ :urgency: :low
+ :resource_boundary: :unknown
+ :weight: 1
+ :idempotent:
- :name: deployment:deployments_finished
:feature_category: :continuous_delivery
:has_external_dependencies:
diff --git a/app/workers/users/create_statistics_worker.rb b/app/workers/users/create_statistics_worker.rb
new file mode 100644
index 00000000000..fb1b192577f
--- /dev/null
+++ b/app/workers/users/create_statistics_worker.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Users
+ class CreateStatisticsWorker # rubocop:disable Scalability/IdempotentWorker
+ include ApplicationWorker
+ # rubocop:disable Scalability/CronWorkerContext
+ # This worker does not perform work scoped to a context
+ include CronjobQueue
+ # rubocop:enable Scalability/CronWorkerContext
+
+ feature_category :users
+
+ def perform
+ UsersStatistics.create_current_stats!
+ rescue ActiveRecord::RecordInvalid => exception
+ Gitlab::ErrorTracking.track_exception(exception)
+ end
+ end
+end