Skip to content

Commit

Permalink
Introduce query helper
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka committed Jan 28, 2025
1 parent 58b09ff commit 68e9ac8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 66 deletions.
2 changes: 1 addition & 1 deletion definitions/reports/external_auth_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run
WHERE auth_sources.type = 'AuthSourceExternal' AND users.last_login_on IS NOT NULL
ORDER BY users.last_login_on DESC LIMIT 1
SQL
user = feature(:foreman_database).query(sql).first
user = query(sql).first
if user
data["last_login_on_through_external_auth_source_in_days"] =
(Date.today - Date.parse(user['last_login_on'])).to_i
Expand Down
25 changes: 10 additions & 15 deletions definitions/reports/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,40 @@ def run

# Hosts
def hosts_by_type_count
feature(:foreman_database).
query("select type, count(*) from hosts group by type").
query("select type, count(*) from hosts group by type").
to_h { |row| [(row['type'] || '').sub('Host::', ''), row['count'].to_i] }
end

# OS usage
def hosts_by_os_count
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select max(operatingsystems.name) as os_name, count(*) as hosts_count
from hosts inner join operatingsystems on operatingsystem_id = operatingsystems.id
group by operatingsystem_id
SQL
).
SQL
).
to_h { |row| [row['os_name'], row['hosts_count'].to_i] }
end

# Facts usage
def facts_by_type
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select fact_names.type,
min(fact_values.updated_at) as min_update_time,
max(fact_values.updated_at) as max_update_time,
count(fact_values.id) as values_count
from fact_values inner join fact_names on fact_name_id = fact_names.id
group by fact_names.type
SQL
).
SQL
).
to_h { |row| [row['type'].sub('FactName', ''), to_fact_hash(row)] }
end

# Audits
def audits
audits_query =
feature(:foreman_database).
query(
<<-SQL
select count(*) as records_count,
Expand All @@ -65,8 +61,7 @@ def audits

# Parameters
def parameters
feature(:foreman_database).
query("select type, count(*) from parameters group by type").
query("select type, count(*) from parameters group by type").
to_h { |row| [row['type'], row['count'].to_i] }
end

Expand Down
2 changes: 1 addition & 1 deletion definitions/reports/ldap_auth_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def record_flavor_usage(flavor)
flavored_query_base = query_base(flavor)
data["ldap_auth_source_#{flavor}_count"] = sql_count(flavored_query_base)

users = feature(:foreman_database).query(user_query(flavor))
users = query(user_query(flavor))
data["users_authenticated_through_ldap_auth_source_#{flavor}"] = users.count

data["last_login_on_through_ldap_auth_source_#{flavor}_in_days"] = last_login(users)
Expand Down
30 changes: 12 additions & 18 deletions definitions/reports/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def run
def mail_notification_fields
# Mail notifications
# users_per_mail_notification =
# feature(:foreman_database).
# query(
# <<-SQL
# select
Expand All @@ -39,8 +38,7 @@ def user_groups_fields
def general_fields
data_field('smart_proxies_count') { sql_count('smart_proxies') }
merge_data('smart_proxies_creation_date') do
feature(:foreman_database).
query("select id, created_at from smart_proxies").
query("select id, created_at from smart_proxies").
to_h { |row| [row['id'], row['created_at']] }
end
end
Expand All @@ -52,40 +50,36 @@ def rbac_fields
data_field('custom_roles_count') { sql_count('roles where origin = null') }

merge_data('taxonomies_counts') do
feature(:foreman_database).
query("select type, count(*) from taxonomies group by type").
query("select type, count(*) from taxonomies group by type").
to_h { |row| [row['type'], row['count'].to_i] }
end
end

def settings_fields
data_field('modified_settings') do
feature(:foreman_database).
query("select name from settings").
query("select name from settings").
map { |setting_line| setting_line['name'] }.
join(',')
end
end

def bookmarks_fields
merge_data('bookmarks_by_public_by_type') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select public, owner_type, count(*)
from bookmarks
group by public, owner_type
SQL
).
SQL
).
to_h do |row|
[
"#{row['public'] ? 'public' : 'private'}#{flatten_separator}#{row['owner_type']}",
row['count'].to_i,
]
end
[
"#{row['public'] ? 'public' : 'private'}#{flatten_separator}#{row['owner_type']}",
row['count'].to_i,
]
end
end
# bookmarks_by_owner =
# feature(:foreman_database).
# query(
# <<-SQL
# select owner_type, owner_id, count(*)
Expand Down
54 changes: 24 additions & 30 deletions definitions/reports/provisioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,39 @@ def compute_resources_fields
hosts_by_compute_profile

merge_data('compute_resources_by_type') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select type, count(*)
from compute_resources
group by type
SQL
).
SQL
).
to_h { |row| [row['type'], row['count'].to_i] }
end
end

def hosts_by_compute_resources_type
merge_data('hosts_by_compute_resources_type') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select compute_resources.type, count(hosts.id)
from hosts left outer join compute_resources on compute_resource_id = compute_resources.id
group by compute_resources.type
SQL
).
SQL
).
to_h { |row| [row['type'] || 'baremetal', row['count'].to_i] }
end
end

def hosts_by_compute_profile
merge_data('hosts_by_compute_profile') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select max(compute_profiles.name) as name, count(hosts.id)
from hosts left outer join compute_profiles on compute_profile_id = compute_profiles.id
group by compute_profile_id
SQL
).
SQL
).
to_h { |row| [row['name'] || 'none', row['count'].to_i] }
end
end
Expand All @@ -72,42 +69,39 @@ def bare_metal_fields

def nics_by_type_count
merge_data('nics_by_type_count') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select type, count(*)
from nics
group by type
SQL
).
SQL
).
to_h { |row| [(row['type'] || 'none').sub('Nic::', ''), row['count'].to_i] }
end
end

def hosts_by_managed_count
merge_data('hosts_by_managed_count') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select managed, count(*)
from hosts
group by managed
SQL
).
SQL
).
to_h { |row| [row['managed'] == 't' ? 'managed' : 'unmanaged', row['count'].to_i] }
end
end

def templates_fields
merge_data('non_default_templates_per_type') do
feature(:foreman_database).
query(
<<-SQL
query(
<<-SQL
select type, count(*) from templates
where templates.default = false
group by type
SQL
).
SQL
).
to_h { |row| [row['type'], row['count'].to_i] }
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/foreman_maintain/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Report < Executable

attr_accessor :data

def query(sql)
feature(:foreman_database).query(sql)
end

def sql_count(sql, column: '*', cte: '')
sql_as_count("COUNT(#{column})", sql, cte: cte)
end
Expand Down Expand Up @@ -56,7 +60,7 @@ def merge_data(prefix)
end

def run
raise NoMethodError.new('method not implemented on abstract report classes')
raise NoMethodError, 'method not implemented on abstract report classes'
end

# internal method called by executor
Expand Down

0 comments on commit 68e9ac8

Please sign in to comment.