Commit 0c737b8b authored by Z.J. van de Weg's avatar Z.J. van de Weg
Browse files

Add additional queries for bussiness stats

Using the previous commit I was able to come up with a couple more
queries. The ordering is on table name, so the index don't have to be
swapped hopefully.
parent 10343fea
Pipeline #23546 passed with stage
in 1 minute and 13 seconds
...@@ -3,16 +3,43 @@ module GitLab ...@@ -3,16 +3,43 @@ module GitLab
module Database module Database
# A helper class that executes the query its given and returns an int of # A helper class that executes the query its given and returns an int of
# the row count # the row count
# This class works under the assumption you do COUNT(*) queries # This class works under the assumption you do COUNT(*) queries, define
# queries in the QUERIES constant. If in doubt how these work, read
# #construct_query
class RowCountCollector < Base class RowCountCollector < Base
QUERIES = { QUERIES = {
award_emoji: { select: :award_emoji },
boards: { select: :boards },
ci_builds: { select: :ci_builds },
ci_commits: { select: :ci_commits },
active_ci_runners: { select: :ci_runners, where: "active=true" },
inactive_ci_runners: { select: :ci_runners, where: "active=false" },
ci_variables: { select: :ci_variables },
deployments: { select: :deployments },
environments: { select: :environments },
events: { select: :events },
fork_count: { select: :forked_project_links },
issues: { select: :issues, where: "deleted_at IS NULL" },
keys: { select: :keys },
labels: { select: :labels },
merge_requests: { select: :merge_requests, where: "deleted_at IS NULL" },
milestones: { select: :milestones },
groups: { select: :namespaces, where: "type='Group'" },
project_count: { select: :projects, where: "pending_delete=false" },
soft_deleted_projects: { select: :projects, where: "pending_delete=true" }, soft_deleted_projects: { select: :projects, where: "pending_delete=true" },
orphaned_projects: { orphaned_projects: {
select: :projects, select: :projects,
joins: "LEFT JOIN namespaces ON projects.namespace_id = namespaces.id", joins: "LEFT JOIN namespaces ON projects.namespace_id = namespaces.id",
where: "namespaces.id IS NULL" where: "namespaces.id IS NULL"
}, },
soft_deleted_namespaces: { select: :namespaces, where: "deleted_at IS NOT NULL" } soft_deleted_namespaces: { select: :namespaces, where: "deleted_at IS NOT NULL" },
active_project_services: { select: :services, where: "active=true" },
personal_snippets: { select: :snippets, where: "type='PersonalSnippet'" },
project_snippets: { select: :snippets, where: "type='ProjectSnippet'" },
user_notes: { select: :notes, where: "system=false" },
system_note: { select: :notes, where: "system=true" },
starred_projects: { select: :users_star_projects },
user_count: { select: :users }
}.freeze }.freeze
def run def run
...@@ -29,6 +56,8 @@ module GitLab ...@@ -29,6 +56,8 @@ module GitLab
def execute(query) def execute(query)
connection.exec(construct_query(query))[0]["count"] connection.exec(construct_query(query))[0]["count"]
rescue PG::UndefinedTable, PG::UndefinedColumn
0
end end
# Not private so I can test it without meta programming tricks # Not private so I can test it without meta programming tricks
......
...@@ -2,10 +2,10 @@ require "spec_helper" ...@@ -2,10 +2,10 @@ require "spec_helper"
require "gitlab_monitor/database/row_count" require "gitlab_monitor/database/row_count"
describe GitLab::Monitor::Database::RowCountCollector do describe GitLab::Monitor::Database::RowCountCollector do
describe "#run" do let(:query) { { project_1: { select: :projects, where: "id=1" } } }
let(:query) { { project_1: { select: :projects, where: "id=1" } } } let(:collector) { described_class.new(connection_string: "host=localhost") }
let(:collector) { described_class.new(connection_string: "host=localhost") }
describe "#run" do
before do before do
stub_const("GitLab::Monitor::Database::RowCountCollector::QUERIES", query) stub_const("GitLab::Monitor::Database::RowCountCollector::QUERIES", query)
end end
...@@ -15,11 +15,11 @@ describe GitLab::Monitor::Database::RowCountCollector do ...@@ -15,11 +15,11 @@ describe GitLab::Monitor::Database::RowCountCollector do
expect(collector.run).to eq(project_1: 3) expect(collector.run).to eq(project_1: 3)
end end
end
describe "#construct_query" do describe "#construct_query" do
it "accepts a table and where clause" do it "accepts a table and where clause" do
expect(collector.send(:construct_query, query[:project_1])).to eq "SELECT COUNT(*) FROM projects WHERE id=1;" expect(collector.send(:construct_query, query[:project_1])).to eq "SELECT COUNT(*) FROM projects WHERE id=1;"
end
end end
end end
end end
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