-
Notifications
You must be signed in to change notification settings - Fork 762
Track storage #1960
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
Closed
Closed
Track storage #1960
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d3cfb07
Prepare account to track storage usage
jorgemanrubia 6231494
Sequentalize these jobs on a per-account basis
jorgemanrubia 19236c5
Track storage taken by attachments in cards
jorgemanrubia 007658e
Track storage taken by comments too
jorgemanrubia 78e104c
Add utility method and script to populate the storage used by accounts
jorgemanrubia 105964d
These jobs are certainly not urgent
jorgemanrubia 6b4c02e
Update sqlite schemas too
jorgemanrubia d521dac
Rename file
jorgemanrubia 1e2e9e1
Rework the approach to persist storage used at the board level
jorgemanrubia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class Account::AdjustStorageJob < ApplicationJob | ||
| queue_as :backend | ||
|
|
||
| limits_concurrency to: 1, key: ->(account, delta) { account } | ||
|
|
||
| def perform(account, delta) | ||
| account.adjust_storage(delta) | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module Account::StorageTracking | ||
| extend ActiveSupport::Concern | ||
|
|
||
| def adjust_storage(delta) | ||
| increment!(:bytes_used, delta) | ||
| end | ||
|
|
||
| def adjust_storage_later(delta) | ||
| Account::AdjustStorageJob.perform_later(self, delta) unless delta.zero? | ||
| end | ||
|
|
||
| def recalculate_bytes_used | ||
| boards.find_each(&:recalculate_bytes_used) | ||
| update_columns bytes_used: boards.sum(:bytes_used) | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module Board::StorageTracking | ||
| extend ActiveSupport::Concern | ||
|
|
||
| def bytes_used_changed(delta) | ||
| increment!(:bytes_used, delta) | ||
| account.adjust_storage_later(delta) | ||
| end | ||
|
|
||
| def recalculate_bytes_used | ||
| update_columns bytes_used: count_bytes_used | ||
| end | ||
|
|
||
| private | ||
| def count_bytes_used | ||
| total_bytes = 0 | ||
|
|
||
| cards.with_rich_text_description_and_embeds.find_each do |card| | ||
| total_bytes += card.bytes_used | ||
| total_bytes += card.comments.with_rich_text_body_and_embeds.sum(&:bytes_used) | ||
| end | ||
|
|
||
| total_bytes | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Card::StorageTracking | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| include ::StorageTracking | ||
| end | ||
|
|
||
| def bytes_used_changed(delta) | ||
| board.bytes_used_changed(delta) | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Comment::StorageTracking | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| include ::StorageTracking | ||
| end | ||
|
|
||
| def bytes_used_changed(delta) | ||
| card.bytes_used_changed(delta) | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| module StorageTracking | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| after_save :track_storage_updated | ||
| after_destroy :track_storage_removed | ||
jorgemanrubia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def bytes_used | ||
| rich_text_associations.sum { |association| send(association.name)&.bytes_used || 0 } | ||
| end | ||
|
|
||
| private | ||
| def rich_text_associations | ||
| self.class.reflect_on_all_associations(:has_one).filter { |association| association.klass == ActionText::RichText } | ||
| end | ||
|
|
||
| def track_storage_updated | ||
| bytes_used_changed(calculate_changed_storage_delta) | ||
| end | ||
|
|
||
| def calculate_changed_storage_delta | ||
| rich_text_associations.sum do |association| | ||
| rich_text = send(association.name) | ||
| next 0 unless rich_text&.body_previously_changed? | ||
|
|
||
| rich_text.bytes_used - rich_text.body_previously_was&.bytes_used.to_i | ||
| end | ||
| end | ||
|
|
||
| def track_storage_removed | ||
| bytes_used_changed(-bytes_used) | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddBytesUsedToAccounts < ActiveRecord::Migration[8.2] | ||
| def change | ||
| add_column :accounts, :bytes_used, :bigint, default: 0 | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddBytesUsedToBoards < ActiveRecord::Migration[8.2] | ||
| def change | ||
| add_column :boards, :bytes_used, :bigint, default: 0 | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module ActionTextContentStorageTracking | ||
| def bytes_used | ||
| attachables.sum { |attachable| attachable.try(:attachable_filesize) || 0 } | ||
| end | ||
| end | ||
|
|
||
| module ActionTextRichTextStorageTracking | ||
| def bytes_used | ||
| body&.bytes_used || 0 | ||
| end | ||
| end | ||
|
|
||
| ActiveSupport.on_load :action_text_content do | ||
| include ActionTextContentStorageTracking | ||
| end | ||
|
|
||
| ActiveSupport.on_load :action_text_rich_text do | ||
| include ActionTextRichTextStorageTracking | ||
| end |
8 changes: 8 additions & 0 deletions
8
script/migrations/20251205-populate_bytes_used_on_accounts.rb
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require_relative "../../config/environment" | ||
|
|
||
| Account.find_each do |account| | ||
| account.recalculate_bytes_used | ||
| puts "#{account.id}: #{account.bytes_used} bytes" | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| hello_txt: <%= ActiveStorage::FixtureSet.blob filename: "hello.txt", service_name: "local", account_id: ActiveRecord::FixtureSet.identify("37s", :uuid) %> | ||
| list_pdf: <%= ActiveStorage::FixtureSet.blob filename: "list.pdf", service_name: "local", account_id: ActiveRecord::FixtureSet.identify("37s", :uuid) %> |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Hi! |
Binary file not shown.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| require "test_helper" | ||
|
|
||
| class Account::StorageTrackingTest < ActiveSupport::TestCase | ||
| setup do | ||
| Current.session = sessions(:david) | ||
| @account = Current.account | ||
| end | ||
|
|
||
| test "track storage deltas" do | ||
| @account.adjust_storage(1000) | ||
| assert_equal 1000, @account.reload.bytes_used | ||
|
|
||
| @account.adjust_storage(-100) | ||
| assert_equal 900, @account.reload.bytes_used | ||
| end | ||
|
|
||
| test "track storage deltas in jobs" do | ||
| assert_enqueued_with(job: Account::AdjustStorageJob, args: [ @account, 1000 ]) do | ||
| @account.adjust_storage_later(1000) | ||
| end | ||
|
|
||
| assert_no_enqueued_jobs only: Account::AdjustStorageJob do | ||
| @account.adjust_storage_later(0) | ||
| end | ||
| end | ||
|
|
||
| test "recalculate bytes used from cards and comments across boards" do | ||
| board1 = boards(:writebook) | ||
| board2 = boards(:private) | ||
|
|
||
| card1 = board1.cards.create!(title: "Test 1", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) | ||
| card1.comments.create!(body: attachment_html(active_storage_blobs(:hello_txt))) | ||
|
|
||
| card2 = board2.cards.create!(title: "Test 2", description: attachment_html(active_storage_blobs(:list_pdf)), status: :published) | ||
|
|
||
| @account.recalculate_bytes_used | ||
|
|
||
| board1_expected = active_storage_blobs(:hello_txt).byte_size * 2 | ||
| board2_expected = active_storage_blobs(:list_pdf).byte_size | ||
|
|
||
| assert_equal board1_expected, board1.reload.bytes_used | ||
| assert_equal board2_expected, board2.reload.bytes_used | ||
| assert_equal board1_expected + board2_expected, @account.bytes_used | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require "test_helper" | ||
|
|
||
| class Board::StorageTrackingTest < ActiveSupport::TestCase | ||
| setup do | ||
| Current.session = sessions(:david) | ||
| @board = boards(:writebook) | ||
| end | ||
|
|
||
| test "recalculate bytes used from cards and comments" do | ||
| card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) | ||
| card.comments.create!(body: attachment_html(active_storage_blobs(:hello_txt))) | ||
| card.comments.create!(body: attachment_html(active_storage_blobs(:list_pdf))) | ||
|
|
||
| @board.update_columns(bytes_used: 0) | ||
| @board.recalculate_bytes_used | ||
|
|
||
| expected_bytes = active_storage_blobs(:hello_txt).byte_size * 2 + active_storage_blobs(:list_pdf).byte_size | ||
| assert_equal expected_bytes, @board.bytes_used | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I think we'd just iterate over the cards and ask them to recalculate themselves, taking care of their own dependents.
Each class that includes
::StorageTrackingis responsible for providingrecalculate_bytes_usedand having it return the new value. They can implement their own caching policy as they see fit. Cards and comments, for example, don't cache the value, but having them do so would be easy to add.If we need to in the future, we could implement a more efficient
calculate_bytes_used. It would use cached values where possible without performing a full recalculation.