-
Notifications
You must be signed in to change notification settings - Fork 71
Refs #39448 - improve performance on host index page #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbernhard
wants to merge
2
commits into
theforeman:master
Choose a base branch
from
ATIX-AG:refs_39448
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+326
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,7 @@ def self.deploy_by_variants | |||||||||||||||||
| validate :valid_tailoring, :valid_tailoring_profile, :no_mixed_deployments | ||||||||||||||||||
| validate :valid_cron_line, :valid_weekday, :valid_day_of_month, :if => Proc.new { |policy| policy.should_validate?('Schedule') } | ||||||||||||||||||
| after_save :assign_policy_to_hostgroups | ||||||||||||||||||
| after_save :reset_changed_assignment_hosts_to_inconclusive | ||||||||||||||||||
| # before_destroy - ensure that the policy has no hostgroups, or classes | ||||||||||||||||||
|
|
||||||||||||||||||
| default_scope do | ||||||||||||||||||
|
|
@@ -179,10 +180,14 @@ def unassign_hosts(hosts) | |||||||||||||||||
| :assetable_type => 'Host::Base', | ||||||||||||||||||
| :assetable_id => hosts.map(&:id), | ||||||||||||||||||
| :foreman_openscap_asset_policies => { :policy_id => id } | ||||||||||||||||||
| ).pluck(:id) | ||||||||||||||||||
| ).pluck(:id, :assetable_id) | ||||||||||||||||||
|
|
||||||||||||||||||
| self.asset_ids = self.asset_ids - policy_host_assets | ||||||||||||||||||
| ForemanOpenscap::Asset.where(:id => policy_host_assets).destroy_all | ||||||||||||||||||
| asset_ids = policy_host_assets.map(&:first) | ||||||||||||||||||
| host_ids_to_reset = policy_host_assets.map(&:second) | ||||||||||||||||||
|
|
||||||||||||||||||
| self.asset_ids = self.asset_ids - asset_ids | ||||||||||||||||||
| ForemanOpenscap::Asset.where(:id => asset_ids).destroy_all | ||||||||||||||||||
| ForemanOpenscap::ComplianceStatusResetter.to_inconclusive(host_ids_to_reset) | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def to_enc | ||||||||||||||||||
|
|
@@ -251,12 +256,47 @@ def profile_for_scan | |||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def assign_ids(ids, class_name) | ||||||||||||||||||
| before_host_ids = host_ids_reached_by_assignments(class_name) | ||||||||||||||||||
| new_assets = ids.uniq.reject { |id| id.respond_to?(:empty?) && id.empty? }.reduce([]) do |memo, id| | ||||||||||||||||||
| memo << assets.where(:assetable_type => class_name, :assetable_id => id).first_or_initialize | ||||||||||||||||||
| end | ||||||||||||||||||
| complimentary_class_name = class_name == 'Host::Base' ? 'Hostgroup' : 'Host::Base' | ||||||||||||||||||
| existing_assets = self.assets.select { |assigned_asset| assigned_asset.assetable_type == complimentary_class_name } | ||||||||||||||||||
| self.assets = existing_assets + new_assets | ||||||||||||||||||
| after_host_ids = host_ids_reached_by_assignments(class_name, self.assets) | ||||||||||||||||||
|
|
||||||||||||||||||
| return unless host_assignment_changed?(before_host_ids, after_host_ids) | ||||||||||||||||||
|
|
||||||||||||||||||
| remember_hosts_for_compliance_status_reset(before_host_ids + after_host_ids) | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def host_ids_reached_by_assignments(class_name, assigned_assets = assets) | ||||||||||||||||||
| assetable_ids = assigned_assets.select { |asset| asset.assetable_type == class_name }.map { |asset| asset.assetable_id.to_i } | ||||||||||||||||||
|
|
||||||||||||||||||
| if class_name == 'Host::Base' | ||||||||||||||||||
| assetable_ids | ||||||||||||||||||
| else | ||||||||||||||||||
| hostgroup_ids = ::Hostgroup.where(:id => assetable_ids).flat_map(&:subtree_ids).uniq | ||||||||||||||||||
| ::Host.where(:hostgroup_id => hostgroup_ids).pluck(:id) | ||||||||||||||||||
| end | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def remember_hosts_for_compliance_status_reset(host_ids) | ||||||||||||||||||
| @compliance_status_reset_host_ids ||= [] | ||||||||||||||||||
| @compliance_status_reset_host_ids.concat(host_ids) | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def host_assignment_changed?(before_host_ids, after_host_ids) | ||||||||||||||||||
| before_host_ids.to_set != after_host_ids.to_set | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def reset_changed_assignment_hosts_to_inconclusive | ||||||||||||||||||
| host_ids = Array(@compliance_status_reset_host_ids).compact.uniq | ||||||||||||||||||
|
Comment on lines
+293
to
+294
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a getter-method:
Suggested change
But as long as we only use the value once, it does not really make a difference. |
||||||||||||||||||
| return if host_ids.empty? | ||||||||||||||||||
|
|
||||||||||||||||||
| ForemanOpenscap::ComplianceStatusResetter.to_inconclusive(host_ids) | ||||||||||||||||||
| ensure | ||||||||||||||||||
| @compliance_status_reset_host_ids = nil | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def no_mixed_deployments | ||||||||||||||||||
|
|
||||||||||||||||||
20 changes: 20 additions & 0 deletions
20
app/services/foreman_openscap/compliance_status_resetter.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| module ForemanOpenscap | ||
| class ComplianceStatusResetter | ||
| def self.to_inconclusive(host_ids) | ||
| host_ids = Array(host_ids).compact.uniq | ||
| return if host_ids.empty? | ||
|
|
||
| now = Time.current | ||
| rows = host_ids.map do |host_id| | ||
| { | ||
| :host_id => host_id, | ||
| :type => ComplianceStatus.to_s, | ||
| :status => ComplianceStatus::INCONCLUSIVE, | ||
| :reported_at => now | ||
| } | ||
| end | ||
|
|
||
| ComplianceStatus.upsert_all(rows, :unique_by => %i[type host_id], :update_only => %i[status reported_at]) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.