Skip to content
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

i1063-fix-tenants-rake #1064

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 45 additions & 40 deletions lib/tasks/tenants.rake
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
# frozen_string_literal: true

namespace :tenants do
# how much space, works, files, per each tenant?
# How much space, works, files, per each tenant?
task calculate_usage: :environment do
@results = []
Account.where(search_only: false).find_each do |account|
if account.cname.present?
AccountElevator.switch!(account.cname)
puts "---------------#{account.cname}-------------------------"
models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000)
if works&.any?
puts "#{works.count} works found"
@tenant_file_sizes = []
works.each do |work|
next if account.cname.blank?

AccountElevator.switch!(account.cname)
puts "---------------#{account.cname}-------------------------"

models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000)

if works&.any?
puts "#{works.count} works found"
tenant_file_sizes = [] # Declare and initialize within the block

works.each do |work|
begin
document = SolrDocument.find(work.id)
files = document._source["file_set_ids_ssim"]
if files&.any?
file_sizes = []
files.each do |file|
f = SolrDocument.find(file.to_s)
if file
file_sizes.push(f.to_h['file_size_lts']) unless f.to_h['file_size_lts'].nil?
else
files_sizes.push(0)
end
end
if file_sizes.any?
file_sizes_total_bytes = file_sizes.inject(0, :+)
file_size_total = (file_sizes_total_bytes / 1.0.megabyte).round(2)
else
file_size_total = 0
files = document._source["file_set_ids_ssim"] || []

if files.any?
file_sizes = files.map do |file|
begin
f = SolrDocument.find(file.to_s)
f.to_h['file_size_lts'] || 0
rescue Blacklight::Exceptions::RecordNotFound => e
puts "Warning: File #{file} not found. Skipping. Error: #{e.message}"
0
end
end
@tenant_file_sizes.push(file_size_total)

total_file_size_bytes = file_sizes.inject(0, :+)
tenant_file_sizes << (total_file_size_bytes / 1.0.megabyte).round(2)
else
@tenant_file_sizes.push(0)
tenant_file_sizes << 0
end
rescue Blacklight::Exceptions::RecordNotFound => e
puts "Warning: Work #{work.id} not found. Skipping. Error: #{e.message}"
tenant_file_sizes << 0
end
if @tenant_file_sizes
tenant_file_sizes_total_megabytes = @tenant_file_sizes.inject(0, :+)
@results.push("#{account.cname}: #{tenant_file_sizes_total_megabytes} Total MB / #{works.count} Works")
else
@results.push("#{account.cname}: 0 Total MB / #{works.count} Works")
end
else
@results.push("#{account.cname}: 0 Total MB / 0 Works")
end
puts "=================================================================="
@results.each do |result|
puts result
end

total_mb = tenant_file_sizes.inject(0, :+)
@results << "#{account.cname}: #{total_mb} Total MB / #{works.count} Works"
else
@results << "#{account.cname}: 0 Total MB / 0 Works"
end

puts "=================================================================="
end

# Output results
@results.each do |result|
puts result
end
end
end