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

request_params.rb « cycle_analytics « analytics « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cea25ba2db471141d11f9ae3a3bb4fcd4f855868 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# frozen_string_literal: true

module Gitlab
  module Analytics
    module CycleAnalytics
      class RequestParams
        include ActiveModel::Model
        include ActiveModel::Validations
        include ActiveModel::Attributes
        include Gitlab::Utils::StrongMemoize

        MAX_RANGE_DAYS = 180.days.freeze
        DEFAULT_DATE_RANGE = 29.days # 30 including Date.today

        NEGATABLE_PARAMS = [
          :assignee_username,
          :author_username,
          :epic_id,
          :iteration_id,
          :label_name,
          :milestone_title,
          :my_reaction_emoji,
          :weight
        ].freeze

        LICENSED_PARAMS = [
          :weight,
          :epic_id,
          :my_reaction_emoji,
          :iteration_id
        ].freeze

        STRONG_PARAMS_DEFINITION = [
          :created_before,
          :created_after,
          :author_username,
          :milestone_title,
          :sort,
          :direction,
          :page,
          :stage_id,
          :end_event_filter,
          *LICENSED_PARAMS,
          label_name: [].freeze,
          assignee_username: [].freeze,
          project_ids: [].freeze,
          not: NEGATABLE_PARAMS
        ].freeze

        FINDER_PARAM_NAMES = [
          :assignee_username,
          :author_username,
          :milestone_title,
          :label_name
        ].freeze

        attr_writer :project_ids

        attribute :created_after, :datetime
        attribute :created_before, :datetime
        attribute :namespace
        attribute :current_user
        attribute :value_stream
        attribute :sort
        attribute :direction
        attribute :page
        attribute :stage_id
        attribute :end_event_filter
        attribute :weight
        attribute :epic_id
        attribute :my_reaction_emoji
        attribute :iteration_id
        attribute :not, default: -> { {} }

        FINDER_PARAM_NAMES.each do |param_name|
          attribute param_name
        end

        validates :created_after, presence: true
        validates :created_before, presence: true

        validate :validate_created_before
        validate :validate_date_range

        def initialize(params = {})
          super(params)

          self.created_before = (self.created_before || Time.current).at_end_of_day
          self.created_after = (created_after || default_created_after).at_beginning_of_day
          self.end_event_filter ||= Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder::DEFAULT_END_EVENT_FILTER
        end

        def to_data_collector_params
          {
            current_user: current_user,
            from: created_after,
            to: created_before,
            project_ids: project_ids,
            sort: sort&.to_sym,
            direction: direction&.to_sym,
            page: page,
            end_event_filter: end_event_filter.to_sym,
            use_aggregated_data_collector: use_aggregated_backend?
          }.merge(attributes.symbolize_keys.slice(*FINDER_PARAM_NAMES))
        end

        def to_data_attributes
          {}.tap do |attrs|
            attrs[:value_stream] = value_stream_data_attributes.to_json if value_stream
            attrs[:created_after] = created_after.to_date.iso8601
            attrs[:created_before] = created_before.to_date.iso8601
            attrs[:labels] = label_name.to_json if label_name.present?
            attrs[:assignees] = assignee_username.to_json if assignee_username.present?
            attrs[:author] = author_username if author_username.present?
            attrs[:milestone] = milestone_title if milestone_title.present?
            attrs[:sort] = sort if sort.present?
            attrs[:direction] = direction if direction.present?
            attrs[:stage] = stage_data_attributes.to_json if stage_id.present?
            attrs[:namespace] = namespace_attributes
            attrs[:enable_tasks_by_type_chart] = 'false'
            attrs[:enable_customizable_stages] = 'false'
            attrs[:enable_projects_filter] = 'false'
            attrs[:default_stages] = Gitlab::Analytics::CycleAnalytics::DefaultStages.all.map do |stage_params|
              ::Analytics::CycleAnalytics::StagePresenter.new(stage_params)
            end.to_json

            attrs.merge!(foss_project_level_params, resource_paths)
          end
        end

        def project_ids
          Array(@project_ids)
        end

        private

        delegate :url_helpers, to: Gitlab::Routing

        def foss_project_level_params
          return {} unless project

          {
            project_id: project.id,
            group_path: project.group ? "groups/#{project.group&.full_path}" : nil,
            request_path: url_helpers.project_cycle_analytics_path(project),
            full_path: project.full_path
          }
        end

        def resource_paths
          helpers = ActionController::Base.helpers

          {}.tap do |paths|
            paths[:empty_state_svg_path] = helpers.image_path("illustrations/analytics/cycle-analytics-empty-chart.svg")
            paths[:no_data_svg_path] = helpers.image_path("illustrations/analytics/cycle-analytics-empty-chart.svg")
            paths[:no_access_svg_path] = helpers.image_path("illustrations/analytics/no-access.svg")

            if project
              paths[:milestones_path] = url_helpers.project_milestones_path(project, format: :json)
              paths[:labels_path] = url_helpers.project_labels_path(project, format: :json)
            end
          end
        end

        # FOSS version doesn't use the aggregated VSA backend
        def use_aggregated_backend?
          false
        end

        def namespace_attributes
          return {} unless project

          {
            name: project.name,
            full_path: project.full_path,
            type: namespace.type
          }
        end

        def value_stream_data_attributes
          {
            id: value_stream.id,
            name: value_stream.name,
            is_custom: value_stream.custom?
          }
        end

        def stage_data_attributes
          return unless stage

          {
            id: stage.id || stage.name,
            title: stage.name
          }
        end

        def validate_created_before
          return if created_after.nil? || created_before.nil?

          errors.add(:created_before, :invalid) if created_after > created_before
        end

        def validate_date_range
          return if created_after.nil? || created_before.nil?

          if (created_before - created_after) > MAX_RANGE_DAYS
            errors.add(:created_after, s_('CycleAnalytics|The given date range is larger than 180 days'))
          end
        end

        def default_created_after
          if created_before
            (created_before - DEFAULT_DATE_RANGE)
          else
            DEFAULT_DATE_RANGE.ago
          end
        end

        def stage
          return unless value_stream

          strong_memoize(:stage) do
            ::Analytics::CycleAnalytics::StageFinder.new(parent: namespace, stage_id: stage_id).execute if stage_id
          end
        end

        def project
          strong_memoize(:project) do
            namespace.project if namespace.is_a?(Namespaces::ProjectNamespace)
          end
        end
      end
    end
  end
end

Gitlab::Analytics::CycleAnalytics::RequestParams.prepend_mod_with('Gitlab::Analytics::CycleAnalytics::RequestParams')