Class: GitLab::Exporter::Database::RowCountCollector
- Defined in:
- lib/gitlab_exporter/database/row_count.rb
Overview
A helper class that executes the query its given and returns an int of the row count 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
Constant Summary collapse
- WHERE_MIRROR_ENABLED =
"projects.mirror = true\nAND project_mirror_data.retry_count <= 14\nAND (projects.visibility_level = 20 OR plans.name IN ('early_adopter', 'bronze', 'silver', 'gold'))\n".freeze
- MIRROR_QUERY =
{ select: :projects, joins: " INNER JOIN project_mirror_data ON project_mirror_data.project_id = projects.id\n INNER JOIN namespaces ON projects.namespace_id = namespaces.id\n LEFT JOIN plans ON namespaces.plan_id = plans.id\n SQL\n check: \"SELECT 1 FROM information_schema.tables WHERE table_name='plans'\"\n}.freeze\n",
- QUERIES =
{ mirrors_ready_to_sync: MIRROR_QUERY.merge( # EE only where: " \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status NOT IN ('scheduled', 'started')\n AND project_mirror_data.next_execution_timestamp <= NOW()\n SQL\n ),\n mirrors_not_updated_recently: MIRROR_QUERY.merge( # EE only\n where: <<~SQL\n \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status NOT IN ('scheduled', 'started')\n AND (project_mirror_data.next_execution_timestamp - project_mirror_data.last_update_at) <= '30 minutes'::interval\n AND project_mirror_data.last_update_at < NOW() - '30 minutes'::interval\n SQL\n ),\n mirrors_updated_very_recently: MIRROR_QUERY.merge( # EE only\n where: <<~SQL\n \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status NOT IN ('scheduled', 'started')\n AND project_mirror_data.last_update_at >= NOW() - '30 seconds'::interval\n SQL\n ),\n mirrors_behind_schedule: MIRROR_QUERY.merge( # EE only\n where: <<~SQL\n \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status NOT IN ('scheduled', 'started')\n AND project_mirror_data.next_execution_timestamp <= NOW() - '10 seconds'::interval\n SQL\n ),\n mirrors_scheduled_or_started: MIRROR_QUERY.merge( # EE only\n where: <<~SQL\n \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status IN ('scheduled', 'started')\n SQL\n ),\n mirrors_scheduled: MIRROR_QUERY.merge( # EE only\n where: <<~SQL\n \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status = 'scheduled'\n SQL\n ),\n mirrors_started: MIRROR_QUERY.merge( # EE only\n where: <<~SQL\n \#{WHERE_MIRROR_ENABLED}\n AND project_mirror_data.status = 'started'\n SQL\n ),\n soft_deleted_projects: { select: :projects, where: \"pending_delete=true\" },\n orphaned_projects: {\n select: :projects,\n joins: \"LEFT JOIN namespaces ON projects.namespace_id = namespaces.id\",\n where: \"namespaces.id IS NULL\"\n },\n uploads: { select: :uploads },\n users: {\n select: :users,\n joins: \"LEFT JOIN\n (\n SELECT\n members.user_id,\n MAX(access_level) as access_level\n FROM members\n GROUP BY members.user_id\n ) AS u\n ON users.id = u.user_id\",\n where: \"ghost IS NULL AND bot_type IS NULL\",\n fields: {\n admin: {},\n external: {},\n state: {},\n access_level: { definition: \"COALESCE(u.access_level, 0)\" }\n }\n },\n projects: {\n select: :projects,\n fields: {\n visibility_level: {},\n archived: {}\n }\n },\n groups: {\n select: :namespaces,\n fields: {\n visibility_level: {},\n root: { definition: \"(parent_id IS NULL)\" }\n }\n }\n}.freeze\n"
Instance Method Summary collapse
-
#initialize(args) ⇒ RowCountCollector
constructor
A new instance of RowCountCollector.
- #run ⇒ Object
Methods inherited from Base
connection_pool, #connection_pool, #with_connection_pool
Constructor Details
#initialize(args) ⇒ RowCountCollector
Returns a new instance of RowCountCollector.
118 119 120 121 122 |
# File 'lib/gitlab_exporter/database/row_count.rb', line 118 def initialize(args) super(args) @selected_queries = Set.new(args[:selected_queries].map(&:to_sym)) unless args[:selected_queries].nil? end |
Instance Method Details
#run ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/gitlab_exporter/database/row_count.rb', line 124 def run results = Hash.new(0) QUERIES.each do |key, query_hash| next if query_hash[:check] && !successful_check?(query_hash[:check]) next if !@selected_queries.nil? && !@selected_queries.include?(key) results[key] = count_from_query_hash(query_hash) end results end |