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 =
"SELECT\n  projects.namespace_id,\n  ci_builds.status,\n  projects.shared_runners_enabled,\n  (COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) = 0 OR\n     COALESCE(namespace_statistics.shared_runners_seconds, 0) < COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) * 60) as has_minutes,\n  COUNT(*) AS count\nFROM ci_builds\nJOIN projects\n  ON projects.id = ci_builds.project_id\nJOIN namespaces\n  ON namespaces.id = projects.namespace_id\nLEFT JOIN namespace_statistics\n  ON namespace_statistics.namespace_id = namespaces.id\nJOIN application_settings\n  ON application_settings.id = 1\nWHERE ci_builds.type = 'Ci::Build'\n  AND ci_builds.status = '%s'\n  AND projects.pending_delete = 'f'\nGROUP BY\n  projects.namespace_id,\n  ci_builds.status,\n  projects.shared_runners_enabled,\n  namespaces.shared_runners_minutes_limit,\n  namespace_statistics.shared_runners_seconds,\n  application_settings.shared_runners_minutes\n".freeze
BUILDS_QUERY_CE =
"SELECT\n  projects.namespace_id,\n  ci_builds.status,\n  projects.shared_runners_enabled,\n  COUNT(*) AS count\nFROM ci_builds\nJOIN projects\n  ON projects.id = ci_builds.project_id\nWHERE ci_builds.type = 'Ci::Build'\n  AND ci_builds.status = '%s'\n  AND projects.pending_delete = 'f'\nGROUP BY\n  projects.namespace_id,\n  ci_builds.status,\n  projects.shared_runners_enabled\n".freeze
STALE_BUILDS_QUERY =
"SELECT\n  COUNT(*) AS count\nFROM ci_builds\nJOIN projects\n  ON projects.id = ci_builds.project_id\nWHERE ci_builds.type = 'Ci::Build'\n  AND ci_builds.status = 'running'\n  AND ci_builds.updated_at < NOW() - INTERVAL '1 hour'\n  AND projects.pending_delete = 'f'\n".freeze
PER_RUNNER_QUERY_EE =
"SELECT\n  ci_builds.runner_id,\n  ci_runners.is_shared,\n  projects.namespace_id,\n  projects.mirror,\n  projects.mirror_trigger_builds,\n  ci_pipelines.pipeline_schedule_id,\n  ci_builds.trigger_request_id,\n  (COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) = 0 OR\n     COALESCE(namespace_statistics.shared_runners_seconds, 0) < COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) * 60) as has_minutes,\n  COUNT(*) AS count\nFROM ci_builds\nJOIN ci_runners\n  ON ci_runners.id = ci_builds.runner_id\nJOIN projects\n  ON projects.id = ci_builds.project_id\nJOIN ci_pipelines\n  ON ci_pipelines.id = ci_builds.commit_id\nJOIN namespaces\n  ON namespaces.id = projects.namespace_id\nLEFT JOIN namespace_statistics\n  ON namespace_statistics.namespace_id = namespaces.id\nJOIN application_settings\n  ON application_settings.id = 1\nWHERE ci_builds.type = 'Ci::Build'\n  AND ci_builds.status = 'running'\n  AND projects.pending_delete = 'f'\nGROUP BY\n  ci_builds.runner_id,\n  ci_runners.is_shared,\n  projects.namespace_id,\n  projects.mirror,\n  projects.mirror_trigger_builds,\n  ci_pipelines.pipeline_schedule_id,\n  ci_builds.trigger_request_id,\n  namespaces.shared_runners_minutes_limit,\n  namespace_statistics.shared_runners_seconds,\n  application_settings.shared_runners_minutes\n".freeze
PER_RUNNER_QUERY_CE =
"SELECT\n  ci_builds.runner_id,\n  ci_runners.is_shared,\n  projects.namespace_id,\n  ci_pipelines.pipeline_schedule_id,\n  ci_builds.trigger_request_id,\n  COUNT(*) AS count\nFROM ci_builds\nJOIN ci_runners\n  ON ci_runners.id = ci_builds.runner_id\nJOIN projects\n  ON projects.id = ci_builds.project_id\nJOIN ci_pipelines\n  ON ci_pipelines.id = ci_builds.commit_id\nWHERE ci_builds.type = 'Ci::Build'\n  AND ci_builds.status = 'running'\n  AND projects.pending_delete = 'f'\nGROUP BY\n  ci_builds.runner_id,\n  ci_runners.is_shared,\n  projects.namespace_id,\n  ci_pipelines.pipeline_schedule_id,\n  ci_builds.trigger_request_id\n".freeze
MIRROR_COLUMN_QUERY =
"SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='projects' AND column_name='mirror')\n".freeze
REPEATED_COMMANDS_QUERY_EE =
"SELECT\n  subquery.namespace_id,\n  subquery.shared_runners_enabled,\n  subquery.project_id,\n  subquery.status,\n  subquery.has_minutes,\n  MAX(subquery.count) as count\nFROM (\n  SELECT\n    projects.namespace_id,\n    projects.shared_runners_enabled,\n    ci_builds.project_id,\n    ci_builds.commit_id,\n    ci_builds.status,\n    (COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) = 0 OR\n       COALESCE(namespace_statistics.shared_runners_seconds, 0) < COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, 0) * 60) as has_minutes,\n    COUNT(*) AS count\n  FROM ci_builds\n  JOIN projects\n    ON projects.id = ci_builds.project_id\n  JOIN namespaces\n    ON namespaces.id = projects.namespace_id\n  LEFT JOIN namespace_statistics\n    ON namespace_statistics.namespace_id = namespaces.id\n  JOIN application_settings\n    ON application_settings.id = 1\n  WHERE ci_builds.type = 'Ci::Build'\n    AND ci_builds.status IN ('running', 'pending')\n  GROUP BY\n    projects.namespace_id,\n    projects.shared_runners_enabled,\n    ci_builds.project_id,\n    ci_builds.commit_id,\n    ci_builds.status,\n    ci_builds.commands,\n    namespaces.shared_runners_minutes_limit,\n    namespace_statistics.shared_runners_seconds,\n    application_settings.shared_runners_minutes\n  HAVING COUNT(*) > %d\n) AS subquery\nGROUP BY\n  subquery.namespace_id,\n  subquery.shared_runners_enabled,\n  subquery.project_id,\n  subquery.commit_id,\n  subquery.status,\n  subquery.has_minutes\n".freeze
REPEATED_COMMANDS_QUERY_CE =
"SELECT\n  subquery.namespace_id,\n  subquery.shared_runners_enabled,\n  subquery.project_id,\n  subquery.status,\n  MAX(subquery.count) as count\nFROM (\n  SELECT\n    projects.namespace_id,\n    projects.shared_runners_enabled,\n    ci_builds.project_id,\n    ci_builds.commit_id,\n    ci_builds.status,\n    COUNT(*) AS count\n  FROM ci_builds\n  JOIN projects\n    ON projects.id = ci_builds.project_id\n  JOIN namespaces\n    ON namespaces.id = projects.namespace_id\n  WHERE ci_builds.type = 'Ci::Build'\n    AND ci_builds.status IN ('running', 'pending')\n  GROUP BY\n    projects.namespace_id,\n    projects.shared_runners_enabled,\n    ci_builds.project_id,\n    ci_builds.commit_id,\n    ci_builds.status,\n    ci_builds.commands\n  HAVING COUNT(*) > %d\n) AS subquery\nGROUP BY\n  subquery.namespace_id,\n  subquery.shared_runners_enabled,\n  subquery.project_id,\n  subquery.commit_id,\n  subquery.status\n".freeze
UNARCHIVED_TRACES_QUERY =
"SELECT\n  COUNT(*) as count\nFROM ci_builds\nJOIN ci_build_trace_chunks\n  ON ci_build_trace_chunks.build_id = ci_builds.id\nLEFT JOIN ci_job_artifacts\n  ON ci_job_artifacts.job_id = ci_builds.id\n  AND ci_job_artifacts.file_type = 3\nWHERE ci_builds.type = 'Ci::Build'\n  AND ci_builds.status IN ('success', 'failed', 'canceled')\n  AND ci_builds.finished_at < '%s'\n  AND ci_job_artifacts.job_id IS NULL\n".freeze
STATUS_CREATED =
"created".freeze
STATUS_PENDING =
"pending".freeze
DEFAULT_UNARCHIVED_TRACES_OFFSET_MINUTES =
1440

Instance Method Summary collapse

Methods inherited from Base

connection_pool, #connection_pool, #with_connection_pool

Constructor Details

#initialize(opts) ⇒ CiBuildsCollector

Returns a new instance of CiBuildsCollector.



256
257
258
259
260
261
262
# File 'lib/gitlab_exporter/database/ci_builds.rb', line 256

def initialize(opts)
  super(opts)

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

Instance Method Details

#runObject



264
265
266
267
268
269
270
271
272
273
# File 'lib/gitlab_exporter/database/ci_builds.rb', line 264

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[:repeated_commands] = repeated_commands
  results[:unarchived_traces] = unarchived_traces
  results
end