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

check_storage_size_service.rb « namespaces « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57d2645a0c8d3076ea08db5448d4d2e0dfa2287e (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

module Namespaces
  class CheckStorageSizeService
    include ActiveSupport::NumberHelper
    include Gitlab::Allowable
    include Gitlab::Utils::StrongMemoize

    def initialize(namespace, user)
      @root_namespace = namespace.root_ancestor
      @root_storage_size = Namespace::RootStorageSize.new(root_namespace)
      @user = user
    end

    def execute
      return ServiceResponse.success unless Feature.enabled?(:namespace_storage_limit, root_namespace)
      return ServiceResponse.success if alert_level == :none

      if root_storage_size.above_size_limit?
        ServiceResponse.error(message: above_size_limit_message, payload: payload)
      else
        ServiceResponse.success(payload: payload)
      end
    end

    private

    attr_reader :root_namespace, :root_storage_size, :user

    USAGE_THRESHOLDS = {
      none: 0.0,
      info: 0.5,
      warning: 0.75,
      alert: 0.95,
      error: 1.0
    }.freeze

    def payload
      return {} unless can?(user, :admin_namespace, root_namespace)

      {
        explanation_message: explanation_message,
        usage_message: usage_message,
        alert_level: alert_level,
        root_namespace: root_namespace
      }
    end

    def explanation_message
      root_storage_size.above_size_limit? ? above_size_limit_message : below_size_limit_message
    end

    def usage_message
      s_("You reached %{usage_in_percent} of %{namespace_name}'s storage capacity (%{used_storage} of %{storage_limit})" % current_usage_params)
    end

    def alert_level
      strong_memoize(:alert_level) do
        usage_ratio = root_storage_size.usage_ratio
        current_level = USAGE_THRESHOLDS.each_key.first

        USAGE_THRESHOLDS.each do |level, threshold|
          current_level = level if usage_ratio >= threshold
        end

        current_level
      end
    end

    def below_size_limit_message
      s_("If you reach 100%% storage capacity, you will not be able to: %{base_message}" % { base_message: base_message } )
    end

    def above_size_limit_message
      s_("%{namespace_name} is now read-only. You cannot: %{base_message}" % { namespace_name: root_namespace.name, base_message: base_message })
    end

    def base_message
      s_("push to your repository, create pipelines, create issues or add comments. To reduce storage capacity, delete unused repositories, artifacts, wikis, issues, and pipelines.")
    end

    def current_usage_params
      {
        usage_in_percent: number_to_percentage(root_storage_size.usage_ratio * 100, precision: 0),
        namespace_name: root_namespace.name,
        used_storage: formatted(root_storage_size.current_size),
        storage_limit: formatted(root_storage_size.limit)
      }
    end

    def formatted(number)
      number_to_human_size(number, delimiter: ',', precision: 2)
    end
  end
end