|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | namespace :tenants do
|
4 |
| - # how much space, works, files, per each tenant? |
| 4 | + # How much space, works, files, per each tenant? |
5 | 5 | task calculate_usage: :environment do
|
6 | 6 | @results = []
|
7 | 7 | Account.where(search_only: false).find_each do |account|
|
8 |
| - if account.cname.present? |
9 |
| - AccountElevator.switch!(account.cname) |
10 |
| - puts "---------------#{account.cname}-------------------------" |
11 |
| - models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" } |
12 |
| - works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000) |
13 |
| - if works&.any? |
14 |
| - puts "#{works.count} works found" |
15 |
| - @tenant_file_sizes = [] |
16 |
| - works.each do |work| |
| 8 | + next if account.cname.blank? |
| 9 | + |
| 10 | + AccountElevator.switch!(account.cname) |
| 11 | + puts "---------------#{account.cname}-------------------------" |
| 12 | + |
| 13 | + models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" } |
| 14 | + works = ActiveFedora::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", rows: 100_000) |
| 15 | + |
| 16 | + if works&.any? |
| 17 | + puts "#{works.count} works found" |
| 18 | + tenant_file_sizes = [] # Declare and initialize within the block |
| 19 | + |
| 20 | + works.each do |work| |
| 21 | + begin |
17 | 22 | document = SolrDocument.find(work.id)
|
18 |
| - files = document._source["file_set_ids_ssim"] |
19 |
| - if files&.any? |
20 |
| - file_sizes = [] |
21 |
| - files.each do |file| |
22 |
| - f = SolrDocument.find(file.to_s) |
23 |
| - if file |
24 |
| - file_sizes.push(f.to_h['file_size_lts']) unless f.to_h['file_size_lts'].nil? |
25 |
| - else |
26 |
| - files_sizes.push(0) |
27 |
| - end |
28 |
| - end |
29 |
| - if file_sizes.any? |
30 |
| - file_sizes_total_bytes = file_sizes.inject(0, :+) |
31 |
| - file_size_total = (file_sizes_total_bytes / 1.0.megabyte).round(2) |
32 |
| - else |
33 |
| - file_size_total = 0 |
| 23 | + files = document._source["file_set_ids_ssim"] || [] |
| 24 | + |
| 25 | + if files.any? |
| 26 | + file_sizes = files.map do |file| |
| 27 | + begin |
| 28 | + f = SolrDocument.find(file.to_s) |
| 29 | + f.to_h['file_size_lts'] || 0 |
| 30 | + rescue Blacklight::Exceptions::RecordNotFound => e |
| 31 | + puts "Warning: File #{file} not found. Skipping. Error: #{e.message}" |
| 32 | + 0 |
| 33 | + end |
34 | 34 | end
|
35 |
| - @tenant_file_sizes.push(file_size_total) |
| 35 | + |
| 36 | + total_file_size_bytes = file_sizes.inject(0, :+) |
| 37 | + tenant_file_sizes << (total_file_size_bytes / 1.0.megabyte).round(2) |
36 | 38 | else
|
37 |
| - @tenant_file_sizes.push(0) |
| 39 | + tenant_file_sizes << 0 |
38 | 40 | end
|
| 41 | + rescue Blacklight::Exceptions::RecordNotFound => e |
| 42 | + puts "Warning: Work #{work.id} not found. Skipping. Error: #{e.message}" |
| 43 | + tenant_file_sizes << 0 |
39 | 44 | end
|
40 |
| - if @tenant_file_sizes |
41 |
| - tenant_file_sizes_total_megabytes = @tenant_file_sizes.inject(0, :+) |
42 |
| - @results.push("#{account.cname}: #{tenant_file_sizes_total_megabytes} Total MB / #{works.count} Works") |
43 |
| - else |
44 |
| - @results.push("#{account.cname}: 0 Total MB / #{works.count} Works") |
45 |
| - end |
46 |
| - else |
47 |
| - @results.push("#{account.cname}: 0 Total MB / 0 Works") |
48 |
| - end |
49 |
| - puts "==================================================================" |
50 |
| - @results.each do |result| |
51 |
| - puts result |
52 | 45 | end
|
| 46 | + |
| 47 | + total_mb = tenant_file_sizes.inject(0, :+) |
| 48 | + @results << "#{account.cname}: #{total_mb} Total MB / #{works.count} Works" |
| 49 | + else |
| 50 | + @results << "#{account.cname}: 0 Total MB / 0 Works" |
53 | 51 | end
|
| 52 | + |
| 53 | + puts "==================================================================" |
| 54 | + end |
| 55 | + |
| 56 | + # Output results |
| 57 | + @results.each do |result| |
| 58 | + puts result |
54 | 59 | end
|
55 | 60 | end
|
56 | 61 | end
|
0 commit comments