diff options
author | Nick Thomas <nick@gitlab.com> | 2019-02-27 15:07:05 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2019-02-27 15:07:05 +0300 |
commit | a5174cf0fcfd0ae77d25ac5208b7aa371d7d7b73 (patch) | |
tree | 6610d4dbd0b47f666586e7f5e0e87a285a3426c5 /app | |
parent | 61c1509cc992959ac5021d10825d5dbf9dd2c091 (diff) | |
parent | 5ae9a44aa17c8929627cc450f936cd960c143e25 (diff) |
Merge branch '42086-project-fetch-statistics-api-http-only' into 'master'
Resolve "Project fetch statistics API (HTTP only)"
Closes #42086
See merge request gitlab-org/gitlab-ce!23596
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/git_http_controller.rb | 9 | ||||
-rw-r--r-- | app/finders/projects/daily_statistics_finder.rb | 21 | ||||
-rw-r--r-- | app/models/project.rb | 4 | ||||
-rw-r--r-- | app/models/project_daily_statistic.rb | 10 | ||||
-rw-r--r-- | app/policies/project_policy.rb | 1 | ||||
-rw-r--r-- | app/services/projects/fetch_statistics_increment_service.rb | 32 | ||||
-rw-r--r-- | app/workers/all_queues.yml | 1 | ||||
-rw-r--r-- | app/workers/project_daily_statistics_worker.rb | 13 |
8 files changed, 91 insertions, 0 deletions
diff --git a/app/controllers/projects/git_http_controller.rb b/app/controllers/projects/git_http_controller.rb index 30e436365de..0c5328fc941 100644 --- a/app/controllers/projects/git_http_controller.rb +++ b/app/controllers/projects/git_http_controller.rb @@ -20,6 +20,8 @@ class Projects::GitHttpController < Projects::GitHttpClientController # POST /foo/bar.git/git-upload-pack (git pull) def git_upload_pack + enqueue_fetch_statistics_update + render_ok end @@ -67,6 +69,13 @@ class Projects::GitHttpController < Projects::GitHttpClientController render plain: exception.message, status: :service_unavailable end + def enqueue_fetch_statistics_update + return if wiki? + return unless project.daily_statistics_enabled? + + ProjectDailyStatisticsWorker.perform_async(project.id) + end + def access @access ||= access_klass.new(access_actor, project, 'http', authentication_abilities: authentication_abilities, diff --git a/app/finders/projects/daily_statistics_finder.rb b/app/finders/projects/daily_statistics_finder.rb new file mode 100644 index 00000000000..912c23107bc --- /dev/null +++ b/app/finders/projects/daily_statistics_finder.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Projects + class DailyStatisticsFinder + attr_reader :project + + def initialize(project) + @project = project + end + + def fetches + ProjectDailyStatistic.of_project(project) + .of_last_30_days + .sorted_by_date_desc + end + + def total_fetch_count + fetches.sum_fetch_count + end + end +end diff --git a/app/models/project.rb b/app/models/project.rb index b016a65f0bb..47baf899222 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -639,6 +639,10 @@ class Project < ActiveRecord::Base auto_devops&.enabled.nil? && !(Gitlab::CurrentSettings.auto_devops_enabled? || Feature.enabled?(:force_autodevops_on_by_default, self)) end + def daily_statistics_enabled? + Feature.enabled?(:project_daily_statistics, self, default_enabled: true) + end + def empty_repo? repository.empty? end diff --git a/app/models/project_daily_statistic.rb b/app/models/project_daily_statistic.rb new file mode 100644 index 00000000000..ff115dd010f --- /dev/null +++ b/app/models/project_daily_statistic.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ProjectDailyStatistic < ActiveRecord::Base + belongs_to :project + + scope :of_project, -> (project) { where(project: project) } + scope :of_last_30_days, -> { where('date >= ?', 29.days.ago.utc.to_date) } + scope :sorted_by_date_desc, -> { order(project_id: :desc, date: :desc) } + scope :sum_fetch_count, -> { sum(:fetch_count) } +end diff --git a/app/policies/project_policy.rb b/app/policies/project_policy.rb index 95dd8b2795e..533782104ac 100644 --- a/app/policies/project_policy.rb +++ b/app/policies/project_policy.rb @@ -278,6 +278,7 @@ class ProjectPolicy < BasePolicy enable :admin_cluster enable :create_environment_terminal enable :destroy_release + enable :daily_statistics end rule { (mirror_available & can?(:admin_project)) | admin }.enable :admin_remote_mirror diff --git a/app/services/projects/fetch_statistics_increment_service.rb b/app/services/projects/fetch_statistics_increment_service.rb new file mode 100644 index 00000000000..8644e6bf313 --- /dev/null +++ b/app/services/projects/fetch_statistics_increment_service.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Projects + class FetchStatisticsIncrementService + attr_reader :project + + def initialize(project) + @project = project + end + + def execute + increment_fetch_count_sql = <<~SQL + INSERT INTO #{table_name} (project_id, date, fetch_count) + VALUES (#{project.id}, '#{Date.today}', 1) + SQL + + increment_fetch_count_sql += if Gitlab::Database.postgresql? + "ON CONFLICT (project_id, date) DO UPDATE SET fetch_count = #{table_name}.fetch_count + 1" + else + "ON DUPLICATE KEY UPDATE fetch_count = #{table_name}.fetch_count + 1" + end + + ActiveRecord::Base.connection.execute(increment_fetch_count_sql) + end + + private + + def table_name + ProjectDailyStatistic.table_name + end + end +end diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index d0fc130b04f..337f39b2091 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -146,3 +146,4 @@ - repository_cleanup - delete_stored_files - import_issues_csv +- project_daily_statistics diff --git a/app/workers/project_daily_statistics_worker.rb b/app/workers/project_daily_statistics_worker.rb new file mode 100644 index 00000000000..101f5c28459 --- /dev/null +++ b/app/workers/project_daily_statistics_worker.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class ProjectDailyStatisticsWorker + include ApplicationWorker + + def perform(project_id) + project = Project.find_by_id(project_id) + + return unless project&.repository&.exists? + + Projects::FetchStatisticsIncrementService.new(project).execute + end +end |