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

value_stream.rb « cycle_analytics « analytics « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16446a5b463fdb6fd2b1e4284f5ee662b0308fad (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
# frozen_string_literal: true

module Analytics
  module CycleAnalytics
    class ValueStream < ApplicationRecord
      MAX_VALUE_STREAMS_PER_NAMESPACE = 50

      self.table_name = :analytics_cycle_analytics_group_value_streams

      include Analytics::CycleAnalytics::Parentable

      has_many :stages, -> { ordered },
        class_name: 'Analytics::CycleAnalytics::Stage',
        foreign_key: :group_value_stream_id,
        index_errors: true,
        inverse_of: :value_stream

      validates :name, presence: true
      validates :name, length: { minimum: 3, maximum: 100, allow_nil: false }, uniqueness: { scope: :group_id }
      validate :max_value_streams_count, on: :create

      accepts_nested_attributes_for :stages, allow_destroy: true

      scope :preload_associated_models, -> {
        includes(:namespace, stages: [:namespace, :end_event_label, :start_event_label])
      }
      scope :order_by_name_asc, -> { order(arel_table[:name].lower.asc) }

      after_save :ensure_aggregation_record_presence

      def custom?
        persisted? || name != Analytics::CycleAnalytics::Stages::BaseService::DEFAULT_VALUE_STREAM_NAME
      end

      def self.build_default_value_stream(namespace)
        new(name: Analytics::CycleAnalytics::Stages::BaseService::DEFAULT_VALUE_STREAM_NAME, namespace: namespace)
      end

      private

      def max_value_streams_count
        return unless namespace
        return unless namespace.value_streams.count >= MAX_VALUE_STREAMS_PER_NAMESPACE

        errors.add(:namespace, _('Maximum number of value streams per namespace exceeded'))
      end

      def ensure_aggregation_record_presence
        Analytics::CycleAnalytics::Aggregation.safe_create_for_namespace(namespace)
      end
    end
  end
end