class GitLab::Exporter::Database::CiBuildsCollector

A helper class to collect CI builds metrics.

Constants

BUILDS_QUERY_CE
BUILDS_QUERY_EE
DEFAULT_UNARCHIVED_TRACES_OFFSET_MINUTES
EE_CHECK_QUERY
PER_RUNNER_QUERY_CE
PER_RUNNER_QUERY_EE
SET_RANDOM_PAGE_COST
STALE_BUILDS_QUERY
STATUS_CREATED
STATUS_PENDING
UNARCHIVED_TRACES_QUERY

Public Class Methods

new(**opts) click to toggle source
Calls superclass method GitLab::Exporter::Database::Base::new
# File lib/gitlab_exporter/database/ci_builds.rb, line 171
def initialize(**opts)
  super

  @created_builds_counting_disabled = opts[:created_builds_counting_disabled]
  @unarchived_traces_offset_minutes = opts[:unarchived_traces_offset_minutes]
end

Public Instance Methods

run() click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 178
def run
  results = {}
  results[:created_builds] = builds(STATUS_CREATED) unless @created_builds_counting_disabled
  results[:pending_builds] = builds(STATUS_PENDING)
  results[:stale_builds] = stale_builds
  results[:per_runner] = per_runner_builds
  results[:unarchived_traces] = unarchived_traces
  results
end

Private Instance Methods

builds(status) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 190
def builds(status)
  results = []

  query = ee? ? BUILDS_QUERY_EE : BUILDS_QUERY_CE
  query = query % [status] # rubocop:disable Style/FormatString
  exec_query_with_custom_random_page_cost(query).each do |row|
    results << transform_builds_row_to_values(row)
  end

  results
rescue PG::UndefinedTable, PG::UndefinedColumn
  results
end
ee?() click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 283
def ee?
  @ee ||=
    begin
      with_connection_pool do |conn|
        conn.exec(EE_CHECK_QUERY)[0]["count"].to_i > 0 # rubocop:disable Style/NumericPredicate
      end
    rescue PG::UndefinedColumn
      false
    end
end
exec_query_with_custom_random_page_cost(query) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 274
def exec_query_with_custom_random_page_cost(query)
  with_connection_pool do |conn|
    conn.transaction do |trans|
      trans.exec(SET_RANDOM_PAGE_COST)
      trans.exec(query)
    end
  end
end
include_bool_if_row_defined(row, field) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 268
def include_bool_if_row_defined(row, field)
  return {} unless row[field.to_s]

  { field => row[field.to_s] == "t" ? "yes" : "no" }
end
include_ee_fields(values, row) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 257
def include_ee_fields(values, row)
  values.merge!(include_bool_if_row_defined(row, :mirror))
  values.merge!(include_bool_if_row_defined(row, :mirror_trigger_builds))
  include_has_minutes_field(values, row)
end
include_has_minutes_field(values, row) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 263
def include_has_minutes_field(values, row)
  values.merge!(include_bool_if_row_defined(row, :has_minutes))
  values
end
per_runner_builds() click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 219
def per_runner_builds
  results = []

  query = ee? ? PER_RUNNER_QUERY_EE : PER_RUNNER_QUERY_CE
  exec_query_with_custom_random_page_cost(query).each do |row|
    results << transform_per_runners_builds_row_to_values(row)
  end

  results
rescue PG::UndefinedTable, PG::UndefinedColumn
  []
end
stale_builds() click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 211
def stale_builds
  with_connection_pool do |conn|
    conn.exec(STALE_BUILDS_QUERY)[0]["count"].to_i
  end
rescue PG::UndefinedTable, PG::UndefinedColumn
  0
end
transform_builds_row_to_values(row) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 204
def transform_builds_row_to_values(row)
  values = { namespace: row["namespace_id"].to_s,
             shared_runners: row["shared_runners_enabled"] == "t" ? "yes" : "no",
             value: row["count"].to_i }
  include_ee_fields(values, row)
end
transform_per_runners_builds_row_to_values(row) click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 232
def transform_per_runners_builds_row_to_values(row)
  values = { runner: row["runner_id"].to_s,
             runner_type: row["runner_type"],
             namespace: row["namespace_id"].to_s,
             scheduled: row["pipeline_schedule_id"] ? "yes" : "no",
             triggered: row["trigger_request_id"] ? "yes" : "no",
             value: row["count"].to_i }
  include_ee_fields(values, row)
end
unarchived_traces() click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 242
def unarchived_traces
  time = Time.now - (unarchived_traces_offset_minutes * 60)
  query = UNARCHIVED_TRACES_QUERY % [time.strftime("%F %T")] # rubocop:disable Style/FormatString

  with_connection_pool do |conn|
    conn.exec(query)[0]["count"].to_i
  end
rescue PG::UndefinedTable, PG::UndefinedColumn
  0
end
unarchived_traces_offset_minutes() click to toggle source
# File lib/gitlab_exporter/database/ci_builds.rb, line 253
def unarchived_traces_offset_minutes
  @unarchived_traces_offset_minutes ||= DEFAULT_UNARCHIVED_TRACES_OFFSET_MINUTES
end