2020-08-27 18:10:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CiPlatformMetric < ApplicationRecord
|
2020-09-10 21:08:28 +00:00
|
|
|
include BulkInsertSafe
|
|
|
|
|
|
|
|
PLATFORM_TARGET_MAX_LENGTH = 255
|
|
|
|
|
2020-08-27 18:10:29 +00:00
|
|
|
validates :recorded_at, presence: true
|
2020-09-10 21:08:28 +00:00
|
|
|
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 }
|
2020-08-27 18:10:29 +00:00
|
|
|
|
2020-09-10 21:08:28 +00:00
|
|
|
CI_VARIABLE_KEY = 'AUTO_DEVOPS_PLATFORM_TARGET'
|
2020-08-27 18:10:29 +00:00
|
|
|
|
2020-09-10 21:08:28 +00:00
|
|
|
def self.insert_auto_devops_platform_targets!
|
2020-08-27 18:10:29 +00:00
|
|
|
# This work can NOT be done in-database because value is encrypted.
|
2020-09-10 21:08:28 +00:00
|
|
|
# However, for 'AUTO_DEVOPS_PLATFORM_TARGET', these values are only
|
2020-08-27 18:10:29 +00:00
|
|
|
# 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)
|
2020-09-10 21:08:28 +00:00
|
|
|
recorded_at = Time.zone.now
|
2020-08-27 18:10:29 +00:00
|
|
|
counts = variables.group_by(&:value).map do |value, variables|
|
2020-09-10 21:08:28 +00:00
|
|
|
target = value.truncate(PLATFORM_TARGET_MAX_LENGTH, separator: '', omission: '')
|
|
|
|
count = variables.count
|
|
|
|
self.new(recorded_at: recorded_at, platform_target: target, count: count)
|
2020-08-27 18:10:29 +00:00
|
|
|
end
|
|
|
|
|
2020-09-10 21:08:28 +00:00
|
|
|
bulk_insert!(counts, validate: true)
|
2020-08-27 18:10:29 +00:00
|
|
|
end
|
|
|
|
end
|