-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2441 from samvera/i117-pals-vs-pals-knapsack-comp
I117 pals vs pals knapsack comp
- Loading branch information
Showing
2 changed files
with
43 additions
and
104 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,58 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :tenants do | ||
# how much space, works, files, per each tenant? | ||
# rubocop:disable Metrics/BlockNesting | ||
# 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| | ||
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 | ||
end | ||
@tenant_file_sizes.push(file_size_total) | ||
else | ||
@tenant_file_sizes.push(0) | ||
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| | ||
document = SolrDocument.find(work.id) | ||
files = document._source["file_set_ids_ssim"] || [] | ||
|
||
if files.any? | ||
file_sizes = files.map do |file| | ||
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 | ||
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") | ||
|
||
total_file_size_bytes = file_sizes.inject(0, :+) | ||
tenant_file_sizes << (total_file_size_bytes / 1.0.megabyte).round(2) | ||
else | ||
@results.push("#{account.cname}: 0 Total MB / #{works.count} Works") | ||
tenant_file_sizes << 0 | ||
end | ||
else | ||
@results.push("#{account.cname}: 0 Total MB / 0 Works") | ||
end | ||
puts "==================================================================" | ||
@results.each do |result| | ||
puts result | ||
rescue Blacklight::Exceptions::RecordNotFound => e | ||
puts "Warning: Work #{work.id} not found. Skipping. Error: #{e.message}" | ||
tenant_file_sizes << 0 | ||
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 | ||
# rubocop:enable Metrics/BlockNesting | ||
end |