summaryrefslogtreecommitdiff
path: root/app/models/analytics/instance_statistics/measurement.rb
blob: eaaf9e999b34c18952d24ef2a5785fdf26dec4e2 (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
# frozen_string_literal: true

module Analytics
  module InstanceStatistics
    class Measurement < ApplicationRecord
      enum identifier: {
        projects: 1,
        users: 2,
        issues: 3,
        merge_requests: 4,
        groups: 5,
        pipelines: 6
      }

      IDENTIFIER_QUERY_MAPPING = {
        identifiers[:projects] => -> { Project },
        identifiers[:users] => -> { User },
        identifiers[:issues] => -> { Issue },
        identifiers[:merge_requests] => -> { MergeRequest },
        identifiers[:groups] => -> { Group },
        identifiers[:pipelines] => -> { Ci::Pipeline }
      }.freeze

      validates :recorded_at, :identifier, :count, presence: true
      validates :recorded_at, uniqueness: { scope: :identifier }

      scope :order_by_latest, -> { order(recorded_at: :desc) }
      scope :with_identifier, -> (identifier) { where(identifier: identifier) }
    end
  end
end