Skip to content

Commit

Permalink
i1063-fix-tenants-rake (#1064)
Browse files Browse the repository at this point in the history
Updates the tenants.rake to add graceful error handling in order to get the tenants data for client
  • Loading branch information
aprilrieger authored Dec 11, 2024
1 parent 66f79ac commit 02d448e
Showing 1 changed file with 45 additions and 40 deletions.
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

0 comments on commit 02d448e

Please sign in to comment.