build_consumption.rb 689 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
# frozen_string_literal: true

module Gitlab
  module Ci
    module Minutes
      # Calculate the consumption of CI minutes based on a cost factor
      # assigned to the involved Runner.
      # The amount returned is a float so that internally we could track
      # an accurate usage of minutes/credits.
      class BuildConsumption
11
        def initialize(build, duration)
12
          @build = build
13
          @duration = duration
14 15 16
        end

        def amount
17
          @amount ||= (@duration.to_f / 60 * cost_factor).round(2)
18 19 20 21 22
        end

        private

        def cost_factor
23
          @build.runner.cost_factor_for_project(@build.project)
24 25 26 27 28
        end
      end
    end
  end
end