Commit 4b082006 authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Merge branch...

Merge branch '25-cibuildscollector-per_runner_query-assumes-mirror-features-not-available-in-ce' into 'master'

Resolve "CiBuildsCollector::PER_RUNNER_QUERY assumes mirror features not available in CE"

Closes #25

See merge request !52
parents 07ff1536 a8f84892
......@@ -27,7 +27,7 @@ module GitLab
" AND ci_builds.status = 'running' " \
" AND ci_builds.updated_at < NOW() - INTERVAL '1 hour'".freeze
PER_RUNNER_QUERY =
PER_RUNNER_QUERY_EE =
<<~SQL.freeze
SELECT
ci_builds.runner_id,
......@@ -49,9 +49,32 @@ module GitLab
AND ci_builds.status = 'running'
GROUP BY
ci_builds.runner_id,
projects.mirror,
ci_runners.is_shared,
ci_pipelines.pipeline_schedule_id,
ci_builds.trigger_request_id
SQL
PER_RUNNER_QUERY_CE =
<<~SQL.freeze
SELECT
ci_builds.runner_id,
ci_runners.is_shared,
projects.pending_delete,
projects.mirror_trigger_builds,
ci_pipelines.pipeline_schedule_id,
ci_builds.trigger_request_id,
COUNT(*) AS count
FROM ci_builds
JOIN ci_runners
ON ci_runners.id = ci_builds.runner_id
JOIN projects
ON projects.id = ci_builds.project_id
JOIN ci_pipelines
ON ci_pipelines.id = ci_builds.commit_id
WHERE ci_builds.type = 'Ci::Build'
AND ci_builds.status = 'running'
GROUP BY
ci_builds.runner_id,
projects.id,
ci_runners.is_shared,
ci_pipelines.pipeline_schedule_id,
ci_builds.trigger_request_id
......@@ -63,7 +86,9 @@ module GitLab
results.merge!(get_builds(BUILDS_QUERY))
results[:stale_builds] = get_general(STALE_BUILDS_QUERY)
results[:per_runner] = get_per_runner(PER_RUNNER_QUERY)
query = mirror_column? ? PER_RUNNER_QUERY_EE : PER_RUNNER_QUERY_CE
results[:per_runner] = get_per_runner(query)
results
end
......@@ -119,6 +144,20 @@ module GitLab
rescue PG::UndefinedTable, PG::UndefinedColumn
results
end
def mirror_column?
@mirror_column ||=
begin
connection.exec(
<<~SQL
SELECT EXISTS
(SELECT 1 FROM information_schema.columns WHERE table_name='projects' AND column_name='mirror')
SQL
)[0]["exists"] == "t"
rescue PG::UndefinedColumn
false
end
end
end
# The prober which is called when gathering metrics
......
......@@ -6,14 +6,16 @@ describe GitLab::Monitor::Database do
let(:builds_query) { "SELECT BUILDS" }
let(:stale_builds_query) { "SELECT NOT UPDATED RUNNING" }
let(:per_runner_query) { "SELECT ALL RUNNING PER RUNNER" }
let(:mirror_column_query) { "SELECT EXISTS \n (SELECT 1 FROM information_schema.columns WHERE table_name='projects' AND column_name='mirror')\n" }
let(:connection) { double("connection") }
before do
stub_const("GitLab::Monitor::Database::CiBuildsCollector::BUILDS_QUERY", builds_query)
stub_const("GitLab::Monitor::Database::CiBuildsCollector::STALE_BUILDS_QUERY", stale_builds_query)
stub_const("GitLab::Monitor::Database::CiBuildsCollector::PER_RUNNER_QUERY", per_runner_query)
stub_const("GitLab::Monitor::Database::CiBuildsCollector::PER_RUNNER_QUERY_EE", per_runner_query)
allow_any_instance_of(GitLab::Monitor::Database::CiBuildsCollector).to receive(:connection).and_return(connection)
allow_any_instance_of(GitLab::Monitor::Database::CiBuildsCollector).to receive(:mirror_column?).and_return(true)
allow(connection).to receive(:exec).with(builds_query)
.and_return([{ "shared_runners_enabled" => "f", "status" => "created", "namespace_id" => "1", "count" => 10 },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment