summaryrefslogtreecommitdiff
path: root/app/models/ci_platform_metric.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-09-19 01:45:44 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-09-19 01:45:44 +0000
commit85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch)
tree9160f299afd8c80c038f08e1545be119f5e3f1e1 /app/models/ci_platform_metric.rb
parent15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff)
downloadgitlab-ce-85dc423f7090da0a52c73eb66faf22ddb20efff9.tar.gz
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'app/models/ci_platform_metric.rb')
-rw-r--r--app/models/ci_platform_metric.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/app/models/ci_platform_metric.rb b/app/models/ci_platform_metric.rb
new file mode 100644
index 00000000000..5e6e3eddce9
--- /dev/null
+++ b/app/models/ci_platform_metric.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+class CiPlatformMetric < ApplicationRecord
+ include BulkInsertSafe
+
+ PLATFORM_TARGET_MAX_LENGTH = 255
+
+ validates :recorded_at, presence: true
+ validates :platform_target,
+ exclusion: [nil], # allow '' (the empty string), but not nil
+ length: { maximum: PLATFORM_TARGET_MAX_LENGTH }
+ validates :count,
+ presence: true,
+ numericality: { only_integer: true, greater_than: 0 }
+
+ CI_VARIABLE_KEY = 'AUTO_DEVOPS_PLATFORM_TARGET'
+ ALLOWED_TARGETS = %w[ECS FARGATE].freeze
+
+ def self.insert_auto_devops_platform_targets!
+ recorded_at = Time.zone.now
+
+ # This work can NOT be done in-database because value is encrypted.
+ # However, for 'AUTO_DEVOPS_PLATFORM_TARGET', these values are only
+ # encrypted as a matter of course, rather than as a need for secrecy.
+ # So this is not a security risk, but exposing other keys possibly could be.
+ variables = Ci::Variable.by_key(CI_VARIABLE_KEY)
+
+ counts = variables.group_by(&:value).map do |value, variables|
+ # While this value is, in theory, not secret. A user could accidentally
+ # put a secret in here so we need to make sure we filter invalid values.
+ next unless ALLOWED_TARGETS.include?(value)
+
+ count = variables.count
+ self.new(recorded_at: recorded_at, platform_target: value, count: count)
+ end.compact
+
+ bulk_insert!(counts, validate: true)
+ end
+end