File: base_count_resolver.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (57 lines) | stat: -rw-r--r-- 1,724 bytes parent folder | download
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
# frozen_string_literal: true

module Resolvers
  module Analytics
    module CycleAnalytics
      class BaseCountResolver < BaseResolver
        type Types::Analytics::CycleAnalytics::MetricType, null: true

        argument :from, Types::TimeType,
          required: true,
          description: 'Timestamp marking the start date and time.'

        argument :to, Types::TimeType,
          required: true,
          description: 'Timestamp marking the end date and time.'

        def ready?(**args)
          start_date = args[:from]
          end_date = args[:to]

          if start_date >= end_date
            raise Gitlab::Graphql::Errors::ArgumentError,
              '`from` argument must be before `to` argument'
          end

          max_days = Gitlab::Analytics::CycleAnalytics::RequestParams::MAX_RANGE_DAYS

          if (end_date.beginning_of_day - start_date.beginning_of_day) > max_days
            raise Gitlab::Graphql::Errors::ArgumentError,
              "Max of #{max_days.inspect} timespan is allowed"
          end

          super
        end

        # :project level: no customization, returning the original resolver
        # :group level: add the project_ids argument
        def self.[](context = :project)
          case context
          when :project
            self
          when :group
            Class.new(self) do
              argument :project_ids, [GraphQL::Types::ID],
                required: false,
                description: 'Project IDs within the group hierarchy.'

              define_method :finder_params do
                { group_id: object.id, include_subgroups: true }
              end
            end
          end
        end
      end
    end
  end
end