Skip to content

Instantly share code, notes, and snippets.

@Ladas
Last active August 20, 2017 11:42
Show Gist options
  • Save Ladas/5686e63ca2e6d656c82199ac282b918a to your computer and use it in GitHub Desktop.
Save Ladas/5686e63ca2e6d656c82199ac282b918a to your computer and use it in GitHub Desktop.
Models Count for Containers, run as ```bundle exec rails r count_records.rb```
classes = [
Authentication,
ComputerSystem,
Container,
ContainerBuild,
ContainerBuildPod,
ContainerComponentStatus,
ContainerCondition,
ContainerEnvVar,
ContainerGroup,
ContainerImage,
ContainerImageRegistry,
ContainerLabelTagMapping,
ContainerLimit,
ContainerLimitItem,
ContainerNode,
ContainerPortConfig,
ContainerProject,
ContainerQuota,
ContainerQuotaItem,
ContainerReplicator,
ContainerRoute,
ContainerService,
ContainerServicePortConfig,
ContainerTemplate,
ContainerTemplateParameter,
ContainerVolume,
CustomAttribute,
Endpoint,
ExtManagementSystem,
Hardware,
ManageIQ::Providers::Kubernetes::ContainerManager::ContainerGroup,
ManageIQ::Providers::Kubernetes::ContainerManager::ContainerNode,
MiqEventDefinition,
MiqGroup,
MiqServer,
OperatingSystem,
PersistentVolume,
PersistentVolumeClaim,
SecurityContext,
ServerRole,
Tag,
Tagging,
Tenant,
User,
Vm
]
sorted = {}
sorted_with_soft_deleted = {}
classes.each do |model|
relation = model
relation = relation.where.not(:ems_id => nil) if model.column_names.include?("ems_id")
relation = relation.where(:deleted_on => nil) if model.column_names.include?("deleted_on")
sorted[model] = relation.count
sorted_with_soft_deleted[model] = model.count
end
puts "------------- sorted by counts ------------"
sorted.sort_by { |_key, value| value }.reverse.to_h.each do |k, v|
puts "#{k.name}: #{v}"
end
puts "------------- sorted with soft deleted by counts ------------"
sorted_with_soft_deleted.sort_by { |_key, value| value }.reverse.to_h.each do |k, v|
puts "#{k.name}: #{v}"
end
total = sorted.values.sum
total_with_soft_deleted = sorted_with_soft_deleted.values.sum
puts "\n"
puts "-------------------------------------------"
puts "Total: #{total} records"
puts "Total with soft deleted: #{total_with_soft_deleted} records"
puts "\n"
ExtManagementSystem.all.each do |ems|
next unless ems.respond_to?(:container_images)
puts "\n"
puts "---------------------------------------------------------------------------------"
puts "EMS [#{ems.name}] specific relations counts"
puts "---------------------------------------------------------------------------------"
puts "\n"
puts "--------------------------Handpicked relations--------------------------------"
handpicked_relations = {}
handpicked_relations_custom_attributes = {}
[:container_images, :container_projects, :container_nodes, :container_groups, :container_replicators, :container_services,
:container_routes, :container_templates, :container_builds, :container_build_pods].each do |rel_name|
next unless ems.respond_to?(rel_name)
relation = ems.send(rel_name)
relation_custom_attributes = CustomAttribute.where(
:resource_type => relation.model.base_class.name,
:resource_id => relation,
)
handpicked_relations[rel_name] = relation.count
handpicked_relations_custom_attributes[rel_name] = relation_custom_attributes.count
end
handpicked_relations.sort_by { |_key, value| value }.reverse.to_h.each do |k, v|
puts "#{k}: #{v}"
puts "#{k} custom_attributes: #{handpicked_relations_custom_attributes[k]}"
end
puts "--------------------------All relations--------------------------------------------"
ems.class.reflections.select { |name, reflection| reflection.macro == :has_many }.collect { |name, reflection| [name, ems.send(name).count] }.sort_by(&:second).reverse.each { |n, c| puts "#{n}: #{c}" }
end
@cben
Copy link

cben commented Aug 20, 2017

nice synthesis 👍
as you mention on gitter, this is missing ContainerEnvVar of active containers — small omission but it's 2nd–3rd biggest table so interesting.

a dirty trick we can do in a standalone script (but not rails c), is just add any missing through relations from
https://github.com/ManageIQ/manageiq/blob/master/app/models/manageiq/providers/container_manager.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment