Class: GitLab::Exporter::Database::CiBuildsCollector

Inherits:
Base
  • Object
show all
Defined in:
lib/gitlab_exporter/database/ci_builds.rb

Overview

A helper class to collect CI builds metrics.

Constant Summary collapse

SET_RANDOM_PAGE_COST =

rubocop:disable Metrics/ClassLength

"SET LOCAL random_page_cost TO 1".freeze
BUILDS_QUERY_EE =
<<~SQL.freeze
  SELECT
    projects.namespace_id,
    p_ci_builds.status,
    projects.shared_runners_enabled,
    (COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) = 0 OR
       COALESCE(namespace_statistics.shared_runners_seconds, 0) < COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) * 60) as has_minutes,
    COUNT(*) AS count
  FROM p_ci_builds
  JOIN projects
    ON projects.id = p_ci_builds.project_id
  JOIN namespaces
    ON namespaces.id = projects.namespace_id
  LEFT JOIN namespace_statistics
    ON namespace_statistics.namespace_id = namespaces.id
  JOIN application_settings ON (TRUE)
  WHERE p_ci_builds.type = 'Ci::Build'
    AND p_ci_builds.status = '%s'
    -- The created_at filter has been introduced for performance reasons only
    AND p_ci_builds.created_at > NOW() - INTERVAL '7 days'
    AND projects.pending_delete = 'f'
  GROUP BY
    projects.namespace_id,
    p_ci_builds.status,
    projects.shared_runners_enabled,
    namespaces.shared_runners_minutes_limit,
    namespace_statistics.shared_runners_seconds,
    application_settings.shared_runners_minutes
SQL
BUILDS_QUERY_CE =
<<~SQL.freeze
  SELECT
    projects.namespace_id,
    p_ci_builds.status,
    projects.shared_runners_enabled,
    COUNT(*) AS count
  FROM p_ci_builds
  JOIN projects
    ON projects.id = p_ci_builds.project_id
  WHERE p_ci_builds.type = 'Ci::Build'
    AND p_ci_builds.status = '%s'
    -- The created_at filter has been introduced for performance reasons only
    AND p_ci_builds.created_at > NOW() - INTERVAL '7 days'
    AND projects.pending_delete = 'f'
  GROUP BY
    projects.namespace_id,
    p_ci_builds.status,
    projects.shared_runners_enabled
SQL
STALE_BUILDS_QUERY =
<<~SQL.freeze
  SELECT
    COUNT(*) AS count
  FROM p_ci_builds
  JOIN projects
    ON projects.id = p_ci_builds.project_id
  WHERE p_ci_builds.type = 'Ci::Build'
    AND p_ci_builds.status = 'running'
    AND p_ci_builds.updated_at < NOW() - INTERVAL '1 hour'
    AND projects.pending_delete = 'f'
SQL
PER_RUNNER_QUERY_EE =
<<~SQL.freeze
  SELECT
    p_ci_builds.runner_id,
    ci_runners.runner_type,
    projects.namespace_id,
    projects.mirror,
    projects.mirror_trigger_builds,
    ci_pipelines.pipeline_schedule_id,
    p_ci_builds.trigger_request_id,
    (COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) = 0 OR
       COALESCE(namespace_statistics.shared_runners_seconds, 0) < COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) * 60) as has_minutes,
    COUNT(*) AS count
  FROM p_ci_builds
  JOIN ci_runners
    ON ci_runners.id = p_ci_builds.runner_id
  JOIN projects
    ON projects.id = p_ci_builds.project_id
  JOIN ci_pipelines
    ON ci_pipelines.id = p_ci_builds.commit_id
  JOIN namespaces
    ON namespaces.id = projects.namespace_id
  LEFT JOIN namespace_statistics
    ON namespace_statistics.namespace_id = namespaces.id
  JOIN application_settings ON (TRUE)
  WHERE p_ci_builds.type = 'Ci::Build'
    AND p_ci_builds.status = 'running'
    -- The created_at filter has been introduced for performance reasons only
    AND p_ci_builds.created_at > NOW() - INTERVAL '7 days'
    AND projects.pending_delete = 'f'
  GROUP BY
    p_ci_builds.runner_id,
    ci_runners.runner_type,
    projects.namespace_id,
    projects.mirror,
    projects.mirror_trigger_builds,
    ci_pipelines.pipeline_schedule_id,
    p_ci_builds.trigger_request_id,
    namespaces.shared_runners_minutes_limit,
    namespace_statistics.shared_runners_seconds,
    application_settings.shared_runners_minutes
SQL
PER_RUNNER_QUERY_CE =
<<~SQL.freeze
  SELECT
    p_ci_builds.runner_id,
    ci_runners.runner_type,
    projects.namespace_id,
    ci_pipelines.pipeline_schedule_id,
    p_ci_builds.trigger_request_id,
    COUNT(*) AS count
  FROM p_ci_builds
  JOIN ci_runners
    ON ci_runners.id = p_ci_builds.runner_id
  JOIN projects
    ON projects.id = p_ci_builds.project_id
  JOIN ci_pipelines
    ON ci_pipelines.id = p_ci_builds.commit_id
  WHERE p_ci_builds.type = 'Ci::Build'
    AND p_ci_builds.status = 'running'
    -- The created_at filter has been introduced for performance reasons only
    AND p_ci_builds.created_at > NOW() - INTERVAL '7 days'
    AND projects.pending_delete = 'f'
  GROUP BY
    p_ci_builds.runner_id,
    ci_runners.runner_type,
    projects.namespace_id,
    ci_pipelines.pipeline_schedule_id,
    p_ci_builds.trigger_request_id
SQL
EE_CHECK_QUERY =
<<~SQL.freeze
  SELECT COUNT(*) FROM licenses
SQL
UNARCHIVED_TRACES_QUERY =
<<~SQL.freeze
  SELECT
    COUNT(*) as count
  FROM p_ci_builds
  JOIN ci_build_trace_chunks
    ON ci_build_trace_chunks.build_id = p_ci_builds.id
  LEFT JOIN ci_job_artifacts
    ON ci_job_artifacts.job_id = p_ci_builds.id
    AND ci_job_artifacts.file_type = 3
  WHERE p_ci_builds.type = 'Ci::Build'
    AND p_ci_builds.status IN ('success', 'failed', 'canceled')
    AND p_ci_builds.finished_at < '%s'
    AND ci_job_artifacts.job_id IS NULL
SQL
STATUS_CREATED =
"created".freeze
STATUS_PENDING =
"pending".freeze
DEFAULT_UNARCHIVED_TRACES_OFFSET_MINUTES =
1440

Constants inherited from Base

Base::POOL_SIZE, Base::POOL_TIMEOUT

Instance Method Summary collapse

Methods inherited from Base

configure_type_map_for_results, connection_pool, #connection_pool, #with_connection_pool

Constructor Details

#initialize(**opts) ⇒ CiBuildsCollector

Returns a new instance of CiBuildsCollector.



171
172
173
174
175
176
# 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

Instance Method Details

#runObject



178
179
180
181
182
183
184
185
186
# 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