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

populate_personal_snippet_statistics.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8f436b183e1dcf12b7820c4e182ee6facd679e4 (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 Gitlab
  module BackgroundMigration
    # This class creates/updates those personal snippets statistics
    # that haven't been created nor initialized.
    # It also updates the related root storage namespace stats
    class PopulatePersonalSnippetStatistics
      def perform(snippet_ids)
        personal_snippets(snippet_ids).group_by(&:author).each do |author, author_snippets|
          upsert_snippet_statistics(author_snippets)
          update_namespace_statistics(author.namespace)
        end
      end

      private

      def personal_snippets(snippet_ids)
        PersonalSnippet
          .where(id: snippet_ids)
          .includes(author: :namespace)
          .includes(:statistics)
          .includes(snippet_repository: :shard)
      end

      def upsert_snippet_statistics(snippets)
        snippets.each do |snippet|
          response = Snippets::UpdateStatisticsService.new(snippet).execute

          error_message("#{response.message} snippet: #{snippet.id}") if response.error?
        end
      end

      def update_namespace_statistics(namespace)
        Namespaces::StatisticsRefresherService.new.execute(namespace)
      rescue => e
        error_message("Error updating statistics for namespace #{namespace.id}: #{e.message}")
      end

      def logger
        @logger ||= Gitlab::BackgroundMigration::Logger.build
      end

      def error_message(message)
        logger.error(message: "Snippet Statistics Migration: #{message}")
      end
    end
  end
end