lato_storage 3.0.5 → 3.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e3025eada87329a0eb5a53da4eabcb1e3876c09c5730fdcec324904ec420604
4
- data.tar.gz: d17b7f2355f1118aa280b0c59df2a323666313f5db0b755e3ad137c5d18998b8
3
+ metadata.gz: 8b3a3c172eaed5975747221e31a8d12d8ef1062fd543e5022d6c44f65dbec033
4
+ data.tar.gz: 1c0698af7f7b8bb298bdcc5107c9d63e45ec7b01764abec4b34167a01d0f5a47
5
5
  SHA512:
6
- metadata.gz: 9a6ad7617bcda1e29b62374f1e5c947a16a0c06b67f0b692ba41c45968773d354b307dbae0a387349a7988a5ed0db8425ba567815ff482c5f1e4b209a04dbdb6
7
- data.tar.gz: cdd52bbe6f56d9990cb2edd821500c248addaa841ce72353081e70528953a5ba752423425a2cf48489a66b5f786e4b273183883fc0d1f9247433b9f31e139355
6
+ metadata.gz: e832d19f93086c0a947c09166bb4d6f97d4d24f965787f73299330df4569eb59cdc78b7327883988707e1fa97caff6e6b1be8751873ee0ece5bea52dd116ac58
7
+ data.tar.gz: 183d883e09d82607c19de8281313bbfda12eb139b53c86b8dde0741fb15fddfcbad9fea9f737d72e787829268efb5dc03bcca00f44225a6013e4b630cb2fed26
@@ -6,17 +6,19 @@ module LatoStorage
6
6
  before_action { active_sidebar(:lato_storage); active_navbar(:lato_storage) }
7
7
 
8
8
  def index
9
- @blobs_count = blobs_count
10
- @attachments_count = attachments_count
11
- @variant_records_count = variant_records_count
12
- @deletable_blobs_count = ActiveStorage::Blob.unattached.where('active_storage_blobs.created_at < ?', 12.hours.ago).count
9
+ # @blobs_count = ActiveStorage::Blob.count
10
+ # @attachments_count = ActiveStorage::Attachment.count
11
+ # @variant_records_count = defined?(ActiveStorage::VariantRecord) ? ActiveStorage::VariantRecord.count : 0
12
+ # @deletable_blobs_count = ActiveStorage::Blob.unattached.where('active_storage_blobs.created_at < ?', 12.hours.ago).count
13
13
 
14
- @total_storage = ActiveStorage::Blob.sum(:byte_size)
15
- @avg_storage = @total_storage / @blobs_count if @blobs_count.positive?
14
+ # @total_storage = ActiveStorage::Blob.sum(:byte_size)
15
+ # @avg_storage = @total_storage / @blobs_count if @blobs_count.positive?
16
16
 
17
- @content_types = ActiveStorage::Blob.group(:content_type).count.sort_by { |_, count| -count }.first(5)
17
+ # @content_types = ActiveStorage::Blob.group(:content_type).count.sort_by { |_, count| -count }.first(5)
18
18
 
19
- @largest_blobs = ActiveStorage::Blob.order(byte_size: :desc).limit(3)
19
+ # @largest_blobs = ActiveStorage::Blob.order(byte_size: :desc).limit(3)
20
+
21
+ @data = LatoStorage::PreloadDashboardDataJob.perform_now
20
22
  end
21
23
 
22
24
  def cleaner
@@ -40,39 +42,5 @@ module LatoStorage
40
42
 
41
43
  redirect_to lato.root_path, alert: 'You have not access to this section.'
42
44
  end
43
-
44
- private
45
-
46
- def blobs_count
47
- return ActiveStorage::Blob.count unless LatoStorage.config.optimize_performances
48
-
49
- if ActiveRecord::Base.connection.adapter_name.downcase == "postgresql"
50
- ActiveRecord::Base.connection.execute("SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname = 'active_storage_blobs';").first["estimate"]
51
- else
52
- ActiveStorage::Blob.count
53
- end
54
- end
55
-
56
- def attachments_count
57
- return ActiveStorage::Attachment.count unless LatoStorage.config.optimize_performances
58
-
59
- if ActiveRecord::Base.connection.adapter_name.downcase == "postgresql"
60
- ActiveRecord::Base.connection.execute("SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname = 'active_storage_attachments';").first["estimate"]
61
- else
62
- ActiveStorage::Attachment.count
63
- end
64
- end
65
-
66
- def variant_records_count
67
- return 0 unless defined?(ActiveStorage::VariantRecord)
68
- return ActiveStorage::VariantRecord.count unless LatoStorage.config.optimize_performances
69
-
70
- if defined?(ActiveStorage::VariantRecord) && ActiveRecord::Base.connection.adapter_name.downcase == "postgresql"
71
- ActiveRecord::Base.connection.execute("SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname = 'active_storage_variant_records';").first["estimate"]
72
- else
73
- ActiveStorage::VariantRecord.count
74
- end
75
- end
76
-
77
45
  end
78
46
  end
@@ -0,0 +1,63 @@
1
+ module LatoStorage
2
+ class PreloadDashboardDataJob < ApplicationJob
3
+ SEMAPHORE_CACHE_KEY = 'LatoStorage::PreloadDashboardDataJob/semaphore'
4
+ DATA_CACHE_KEY = 'LatoStorage::PreloadDashboardDataJob/data'
5
+
6
+ def perform(force_execution = false)
7
+ return load_data unless LatoStorage.config.optimize_performances
8
+
9
+ data = Rails.cache.read(DATA_CACHE_KEY)
10
+ return data || {} if Rails.cache.read(SEMAPHORE_CACHE_KEY)
11
+
12
+ if data && !force_execution
13
+ LatoStorage::PreloadDashboardDataJob.perform_later(true)
14
+ return data
15
+ end
16
+
17
+ Rails.cache.write(SEMAPHORE_CACHE_KEY, true, expires_in: 30.minutes)
18
+ data = load_data
19
+ Rails.cache.write(DATA_CACHE_KEY, data, expires_in: 12.hours)
20
+ return data
21
+ rescue StandardError => e
22
+ Rails.logger.error "Error in LatoStorage::PreloadDashboardDataJob: #{e.message}"
23
+ Rails.logger.error e.backtrace.join("\n")
24
+ Rails.cache.delete(SEMAPHORE_CACHE_KEY)
25
+ nil
26
+ end
27
+
28
+ private
29
+
30
+ def load_data
31
+ blobs_count = ActiveStorage::Blob.count
32
+ attachments_count = ActiveStorage::Attachment.count
33
+ variant_records_count = defined?(ActiveStorage::VariantRecord) ? ActiveStorage::VariantRecord.count : 0
34
+ deletable_blobs_count = ActiveStorage::Blob.unattached.where('active_storage_blobs.created_at < ?', 12.hours.ago).count
35
+ total_storage = ActiveStorage::Blob.sum(:byte_size)
36
+ avg_storage = blobs_count.positive? ? total_storage / blobs_count : 0
37
+ content_types = ActiveStorage::Blob.group(:content_type).count.sort_by { |_, count| -count }.first(5)
38
+ largest_blobs = ActiveStorage::Blob.order(byte_size: :desc).limit(3).map do |blob|
39
+ {
40
+ id: blob.id,
41
+ filename: blob.filename.to_s,
42
+ byte_size: blob.byte_size,
43
+ created_at: blob.created_at,
44
+ content_type: blob.content_type,
45
+ service_name: blob.service_name,
46
+ checksum: blob.checksum,
47
+ url: Rails.application.routes.url_helpers.rails_blob_url(blob, only_path: true, expires_in: 12.hours)
48
+ }
49
+ end
50
+
51
+ {
52
+ blobs_count: blobs_count,
53
+ attachments_count: attachments_count,
54
+ variant_records_count: variant_records_count,
55
+ deletable_blobs_count: deletable_blobs_count,
56
+ total_storage: total_storage,
57
+ avg_storage: avg_storage,
58
+ content_types: content_types,
59
+ largest_blobs: largest_blobs
60
+ }
61
+ end
62
+ end
63
+ end
@@ -1,10 +1,16 @@
1
1
  <%= lato_page_head I18n.t('lato_storage.storage') %>
2
2
 
3
+ <% if LatoStorage.config.optimize_performances %>
4
+ <div class="alert alert-info mb-3">
5
+ <%= I18n.t('lato_storage.optimize_performances_active') %>
6
+ </div>
7
+ <% end %>
8
+
3
9
  <div class="row">
4
10
  <div class="col col-6 col-lg-3 mb-3">
5
11
  <div class="card">
6
12
  <div class="card-body text-center">
7
- <div class="fw-bold fs-2"><%= @blobs_count %></div>
13
+ <div class="fw-bold fs-2"><%= @data[:blobs_count] %></div>
8
14
  <div><%= I18n.t('lato_storage.total_blobs') %></div>
9
15
  <%= link_to I18n.t('lato_storage.cta_see_all'), lato_storage.blobs_path, class: 'btn btn-primary btn-sm mt-3', data: { lato_action_target: 'trigger', turbo_frame: 'lato_index_default_active_storage_blob', action_size: 'xl', action_title: I18n.t('lato_storage.blobs') } %>
10
16
  </div>
@@ -14,7 +20,7 @@
14
20
  <div class="col col-6 col-lg-3 mb-3">
15
21
  <div class="card">
16
22
  <div class="card-body text-center">
17
- <div class="fw-bold fs-2"><%= @attachments_count %></div>
23
+ <div class="fw-bold fs-2"><%= @data[:attachments_count] %></div>
18
24
  <div><%= I18n.t('lato_storage.total_attachments') %></div>
19
25
  <%= link_to I18n.t('lato_storage.cta_see_all'), lato_storage.attachments_path, class: 'btn btn-primary btn-sm mt-3', data: { lato_action_target: 'trigger', turbo_frame: 'lato_index_default_active_storage_attachment', action_size: 'xl', action_title: I18n.t('lato_storage.attachments') } %>
20
26
  </div>
@@ -24,7 +30,7 @@
24
30
  <div class="col col-6 col-lg-3 mb-3">
25
31
  <div class="card">
26
32
  <div class="card-body text-center">
27
- <div class="fw-bold fs-2"><%= @variant_records_count %></div>
33
+ <div class="fw-bold fs-2"><%= @data[:variant_records_count] %></div>
28
34
  <div><%= I18n.t('lato_storage.total_variant_records') %></div>
29
35
  <%= link_to I18n.t('lato_storage.cta_see_all'), lato_storage.variant_records_path, class: 'btn btn-primary btn-sm mt-3', data: { lato_action_target: 'trigger', turbo_frame: 'lato_index_default_active_storage_variant_record', action_size: 'xl', action_title: I18n.t('lato_storage.variant_records') } %>
30
36
  </div>
@@ -34,7 +40,7 @@
34
40
  <div class="col col-6 col-lg-3 mb-3">
35
41
  <div class="card">
36
42
  <div class="card-body text-center">
37
- <div class="fw-bold fs-2"><%= @deletable_blobs_count %></div>
43
+ <div class="fw-bold fs-2"><%= @data[:deletable_blobs_count] %></div>
38
44
  <div><%= I18n.t('lato_storage.deletable_blobs') %></div>
39
45
  <%= link_to I18n.t('lato_storage.cta_free_space'), lato_storage.cleaner_path, class: 'btn btn-primary btn-sm mt-3', data: { turbo_method: :post, lato_action_target: 'trigger', turbo_frame: 'lato_operation' } %>
40
46
  </div>
@@ -53,10 +59,10 @@
53
59
  </h5>
54
60
  </div>
55
61
  <div class="card-body">
56
- <% if @content_types.any? %>
57
- <% total = @content_types.sum { |_, count| count } %>
62
+ <% if @data[:content_types].any? %>
63
+ <% total = @data[:content_types].sum { |_, count| count } %>
58
64
  <ul class="list-group list-group-flush">
59
- <% @content_types.each do |content_type, count| %>
65
+ <% @data[:content_types].each do |content_type, count| %>
60
66
  <li class="list-group-item">
61
67
  <div class="d-flex justify-content-between align-items-center">
62
68
  <strong><%= content_type %></strong>
@@ -91,7 +97,7 @@
91
97
  <div class="col col-12 col-md-6 mb-3 mb-md-0">
92
98
  <div class="card">
93
99
  <div class="card-body text-center">
94
- <div class="fw-bold fs-2"><%= format_bytes @total_storage %></div>
100
+ <div class="fw-bold fs-2"><%= format_bytes @data[:total_storage] %></div>
95
101
  <div><%= I18n.t('lato_storage.total_storage') %></div>
96
102
  </div>
97
103
  </div>
@@ -100,7 +106,7 @@
100
106
  <div class="col col-12 col-md-6 mb-3 mb-md-0">
101
107
  <div class="card">
102
108
  <div class="card-body text-center">
103
- <div class="fw-bold fs-2"><%= format_bytes @avg_storage %></div>
109
+ <div class="fw-bold fs-2"><%= format_bytes @data[:avg_storage] %></div>
104
110
  <div><%= I18n.t('lato_storage.avg_storage') %></div>
105
111
  </div>
106
112
  </div>
@@ -115,15 +121,15 @@
115
121
  </h5>
116
122
  </div>
117
123
  <div class="card-body">
118
- <% if @largest_blobs.any? %>
124
+ <% if @data[:largest_blobs].any? %>
119
125
  <div class="list-group list-group-flush">
120
- <% @largest_blobs.each do |blob| %>
121
- <a class="list-group-item list-group-item-action" href="<%= main_app.url_for(blob) %>" target="_blank" rel="noopener noreferrer" download>
126
+ <% @data[:largest_blobs].each do |blob| %>
127
+ <a class="list-group-item list-group-item-action" href="<%= blob[:url] %>" target="_blank" rel="noopener noreferrer" download>
122
128
  <div class="d-flex justify-content-between align-items-center">
123
- <strong class="text-truncate"><%= blob.filename %></strong>
129
+ <strong class="text-truncate"><%= blob[:filename] %></strong>
124
130
  <div class="d-flex align-items-center ms-3">
125
131
  <span class="badge bg-primary rounded-pill">
126
- <%= format_bytes blob.byte_size %>
132
+ <%= format_bytes blob[:byte_size] %>
127
133
  </span>
128
134
  <i class="bi bi-download ms-2"></i>
129
135
  </div>
@@ -16,4 +16,5 @@ en:
16
16
  cta_see_all: "See all"
17
17
  blobs: "Blobs"
18
18
  attachments: "Attachments"
19
- variant_records: "Variant Records"
19
+ variant_records: "Variant Records"
20
+ optimize_performances_active: "The displayed data is cached to optimize performance. The shown values are therefore not updated in real-time and may not reflect the most recent changes."
@@ -17,3 +17,4 @@ it:
17
17
  blobs: "Blob"
18
18
  attachments: "Allegati"
19
19
  variant_records: "Varianti"
20
+ optimize_performances_active: "I dati visualizzati sono cachati per ottimizzare le prestazioni. I valori mostrati non sono quindi aggiornati in tempo reale e potrebbero non riflettere le modifiche più recenti."
@@ -1,3 +1,3 @@
1
1
  module LatoStorage
2
- VERSION = "3.0.5"
2
+ VERSION = "3.0.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lato_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 3.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante
@@ -57,6 +57,7 @@ files:
57
57
  - app/controllers/lato_storage/variant_records_controller.rb
58
58
  - app/helpers/lato_storage/application_helper.rb
59
59
  - app/jobs/lato_storage/application_job.rb
60
+ - app/jobs/lato_storage/preload_dashboard_data_job.rb
60
61
  - app/mailers/lato_storage/application_mailer.rb
61
62
  - app/views/lato_storage/application/index.html.erb
62
63
  - app/views/lato_storage/attachments/index.html.erb