From 11066e520159eba22ec0282c94b6e72e7aab66c2 Mon Sep 17 00:00:00 2001 From: Shim Shtein Date: Mon, 2 Sep 2024 04:56:27 -0400 Subject: [PATCH 1/2] Basic usage report implementation with reports --- app/models/application_record.rb | 2 +- app/models/domain.rb | 2 +- app/models/smart_proxy.rb | 4 ++-- app/services/foreman/renderer/configuration.rb | 3 ++- app/services/foreman/renderer/scope/macros/loaders.rb | 8 +++++++- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index d221630946b..4165d77b598 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -15,7 +15,7 @@ class ApplicationRecord < ActiveRecord::Base end class Jail < Safemode::Jail - allow :id, :name, :present? + allow :id, :name, :present?, :created_at end self.abstract_class = true diff --git a/app/models/domain.rb b/app/models/domain.rb index c892410d1e4..76a191ad639 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -56,7 +56,7 @@ class Domain < ApplicationRecord prop_group :basic_model_props, ApplicationRecord, meta: { example: 'example.com' } property :fullname, String, desc: 'User name for this domain, e.g. "Primary domain for our company"' end - class Jail < Safemode::Jail + class Jail < Jail allow :id, :name, :fullname end diff --git a/app/models/smart_proxy.rb b/app/models/smart_proxy.rb index 35e665d5269..244a8e36eff 100644 --- a/app/models/smart_proxy.rb +++ b/app/models/smart_proxy.rb @@ -214,7 +214,7 @@ def get_features property :httpboot_https_port, Integer, desc: 'Returns proxy port for HTTPS boot' property :httpboot_https_port!, Integer, desc: 'Same as httpboot_https_port, but raises Foreman::Exception if no port is set' end - class Jail < ::Safemode::Jail - allow :id, :name, :hostname, :httpboot_http_port, :httpboot_https_port, :httpboot_http_port!, :httpboot_https_port!, :url + class Jail < Jail + allow :hostname, :httpboot_http_port, :httpboot_https_port, :httpboot_http_port!, :httpboot_https_port!, :url, :feature_details end end diff --git a/app/services/foreman/renderer/configuration.rb b/app/services/foreman/renderer/configuration.rb index b86b0213ebf..233fa30352b 100644 --- a/app/services/foreman/renderer/configuration.rb +++ b/app/services/foreman/renderer/configuration.rb @@ -122,6 +122,7 @@ class Configuration :outofsync_interval, :default_puppet_environment, :update_ip_from_built_request, + :lab_features, ] DEFAULT_ALLOWED_LOADERS = Foreman::Renderer::Scope::Macros::Loaders::LOADERS.map(&:first) @@ -138,7 +139,7 @@ def initialize :allowed_generic_helpers, :allowed_host_helpers, :allowed_loaders def allowed_helpers - allowed_generic_helpers + allowed_host_helpers + allowed_loaders + allowed_generic_helpers + allowed_host_helpers + allowed_loaders + [:load_any_resource] end end end diff --git a/app/services/foreman/renderer/scope/macros/loaders.rb b/app/services/foreman/renderer/scope/macros/loaders.rb index ba930efb3a2..5b95889a9ac 100644 --- a/app/services/foreman/renderer/scope/macros/loaders.rb +++ b/app/services/foreman/renderer/scope/macros/loaders.rb @@ -70,6 +70,11 @@ module Loaders end end + def load_any_resource(resource:, permission:, search: '', batch: nil) + model = resource.constantize + load_resource(klass: model, search: search, permission: permission, batch: batch) + end + private # returns a batched relation, use either @@ -88,7 +93,8 @@ def load_resource(klass:, search:, permission:, batch: 1_000, includes: nil, lim base = base.limit(limit) unless limit.nil? base = base.where(where) unless where.nil? base = base.select(select) unless select.nil? - base.in_batches(of: batch) + base = base.in_batches(of: batch) unless batch.nil? + base end end end From a13f9a1b0f1e40b92cf29b7d8275bd4533025e75 Mon Sep 17 00:00:00 2001 From: Shim Shtein Date: Mon, 2 Sep 2024 05:11:06 -0400 Subject: [PATCH 2/2] Add a sample report --- .../report_templates/usage_report.erb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app/views/unattended/report_templates/usage_report.erb diff --git a/app/views/unattended/report_templates/usage_report.erb b/app/views/unattended/report_templates/usage_report.erb new file mode 100644 index 00000000000..519ff25d51f --- /dev/null +++ b/app/views/unattended/report_templates/usage_report.erb @@ -0,0 +1,17 @@ +<%# +name: Usage report +snippet: false +model: ReportTemplate +-%> + +<%- report_row(item: 'SmartProxy', data: load_smart_proxies(batch: nil).size) -%> +<%- report_row(item: 'SmartProxy_creation', data: load_smart_proxies(batch: nil).map{|p| p.created_at} ) -%> +<%- report_row(item: 'SmartProxy_feature_details', data: load_smart_proxies(batch: nil).map{|p| p.feature_details } ) -%> +<%- report_row(item: 'Experimental', data: global_setting(:lab_features) ) -%> + +<%- report_row(item: 'Users', data: load_users(batch: nil).size) -%> +<%- report_row(item: 'Users', data: load_users(batch: nil, search: 'admin = false').size) -%> + +<%- report_row(item: 'AnyResource', data: load_any_resource(resource: 'User', permission: 'view_users', batch: nil, search: 'admin = false').size) -%> + +<%= report_render -%>