Skip to content

Commit 2cbdd4f

Browse files
committed
Rework the approach to persist storage used at the board level
So that the system is more robust and it is faster to recalculate storage taken by account
1 parent d521dac commit 2cbdd4f

File tree

11 files changed

+215
-123
lines changed

11 files changed

+215
-123
lines changed

app/models/board.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Board < ApplicationRecord
2-
include Accessible, AutoPostponing, Broadcastable, Cards, Entropic, Filterable, Publishable, Triageable
2+
include Accessible, AutoPostponing, Broadcastable, Cards, Entropic, Filterable, Publishable, StorageTracking, Triageable
33

44
belongs_to :creator, class_name: "User", default: -> { Current.user }
55
belongs_to :account, default: -> { creator.account }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Board::StorageTracking
2+
extend ActiveSupport::Concern
3+
4+
def bytes_used_changed(delta)
5+
increment!(:bytes_used, delta)
6+
account.adjust_storage_later(delta)
7+
end
8+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Card::StorageTracking
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
include ::StorageTracking
6+
end
7+
8+
def bytes_used_changed(delta)
9+
board.bytes_used_changed(delta)
10+
end
11+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Comment::StorageTracking
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
include ::StorageTracking
6+
end
7+
8+
def bytes_used_changed(delta)
9+
card.bytes_used_changed(delta)
10+
end
11+
end

app/models/concerns/storage_tracking.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ module StorageTracking
44
included do
55
after_save :track_storage_updated
66
after_destroy :track_storage_removed
7-
8-
delegate :adjust_storage_later, to: :account
97
end
108

119
def bytes_used
@@ -14,7 +12,7 @@ def bytes_used
1412

1513
private
1614
def track_storage_updated
17-
adjust_storage_later(calculate_changed_storage_delta)
15+
bytes_used_changed(calculate_changed_storage_delta)
1816
end
1917

2018
def calculate_changed_storage_delta
@@ -27,7 +25,7 @@ def calculate_changed_storage_delta
2725
end
2826

2927
def track_storage_removed
30-
adjust_storage_later(-bytes_used)
28+
bytes_used_changed(-bytes_used)
3129
end
3230

3331
def rich_text_associations
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddBytesUsedToBoards < ActiveRecord::Migration[8.2]
2+
def change
3+
add_column :boards, :bytes_used, :bigint, default: 0
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/rails_ext/action_text_storage_tracking.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ActionTextContentStorageTracking
22
def bytes_used
3-
attachables.sum { |attachable| attachable.try(:byte_size) || 0 }
3+
attachables.sum { |attachable| attachable.try(:attachable_filesize) || 0 }
44
end
55
end
66

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
require "test_helper"
2+
3+
class Card::StorageTrackingTest < ActiveSupport::TestCase
4+
setup do
5+
Current.session = sessions(:david)
6+
@account = Current.account
7+
@board = boards(:writebook)
8+
end
9+
10+
test "tracks storage when creating card with rich text attachment" do
11+
expected_bytes = active_storage_blobs(:hello_txt).byte_size
12+
13+
assert_difference -> { @board.reload.bytes_used }, expected_bytes do
14+
assert_difference -> { @account.reload.bytes_used }, expected_bytes do
15+
perform_enqueued_jobs only: Account::AdjustStorageJob do
16+
@board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
17+
end
18+
end
19+
end
20+
end
21+
22+
test "tracks storage delta when updating card with different attachment" do
23+
perform_enqueued_jobs only: Account::AdjustStorageJob do
24+
card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
25+
26+
expected_delta = active_storage_blobs(:list_pdf).byte_size - active_storage_blobs(:hello_txt).byte_size
27+
assert_difference -> { @board.reload.bytes_used }, expected_delta do
28+
assert_difference -> { @account.reload.bytes_used }, expected_delta do
29+
card.update!(description: attachment_html(active_storage_blobs(:list_pdf)))
30+
end
31+
end
32+
end
33+
end
34+
35+
test "tracks negative delta when removing attachment from card" do
36+
perform_enqueued_jobs only: Account::AdjustStorageJob do
37+
card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
38+
39+
expected_delta = -active_storage_blobs(:hello_txt).byte_size
40+
assert_difference -> { @board.reload.bytes_used }, expected_delta do
41+
assert_difference -> { @account.reload.bytes_used }, expected_delta do
42+
card.update!(description: "No attachments")
43+
end
44+
end
45+
end
46+
end
47+
48+
test "tracks negative storage when destroying card with attachment" do
49+
perform_enqueued_jobs only: Account::AdjustStorageJob do
50+
card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
51+
52+
expected_delta = -active_storage_blobs(:hello_txt).byte_size
53+
assert_difference -> { @board.reload.bytes_used }, expected_delta do
54+
assert_difference -> { @account.reload.bytes_used }, expected_delta do
55+
card.destroy!
56+
end
57+
end
58+
end
59+
end
60+
61+
test "does not change storage when no attachments change" do
62+
assert_no_difference -> { @board.reload.bytes_used } do
63+
assert_no_difference -> { @account.reload.bytes_used } do
64+
perform_enqueued_jobs only: Account::AdjustStorageJob do
65+
@board.cards.create!(title: "Test", description: "Plain text", status: :published)
66+
end
67+
end
68+
end
69+
end
70+
71+
test "does not change storage when updating title on card with attachment" do
72+
perform_enqueued_jobs only: Account::AdjustStorageJob do
73+
card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
74+
75+
assert_no_difference -> { @board.reload.bytes_used } do
76+
assert_no_difference -> { @account.reload.bytes_used } do
77+
card.update!(title: "New title")
78+
end
79+
end
80+
end
81+
end
82+
83+
test "does not change storage when updating description text but keeping same attachment" do
84+
perform_enqueued_jobs only: Account::AdjustStorageJob do
85+
card = @board.cards.create!(title: "Test", description: "Some text #{attachment_html(active_storage_blobs(:hello_txt))}", status: :published)
86+
87+
assert_no_difference -> { @board.reload.bytes_used } do
88+
assert_no_difference -> { @account.reload.bytes_used } do
89+
card.update!(description: "Different text #{attachment_html(active_storage_blobs(:hello_txt))}")
90+
end
91+
end
92+
end
93+
end
94+
95+
test "tracks storage separately for each board" do
96+
other_board = boards(:private)
97+
98+
perform_enqueued_jobs only: Account::AdjustStorageJob do
99+
@board.cards.create!(title: "Card 1", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
100+
other_board.cards.create!(title: "Card 2", description: attachment_html(active_storage_blobs(:list_pdf)), status: :published)
101+
102+
assert_equal active_storage_blobs(:hello_txt).byte_size, @board.reload.bytes_used
103+
assert_equal active_storage_blobs(:list_pdf).byte_size, other_board.reload.bytes_used
104+
assert_equal active_storage_blobs(:hello_txt).byte_size + active_storage_blobs(:list_pdf).byte_size, @account.reload.bytes_used
105+
end
106+
end
107+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require "test_helper"
2+
3+
class Comment::StorageTrackingTest < ActiveSupport::TestCase
4+
setup do
5+
Current.session = sessions(:david)
6+
@account = Current.account
7+
@board = boards(:writebook)
8+
@card = @board.cards.create!(title: "Test", status: :published)
9+
end
10+
11+
test "tracks storage when creating comment with attachment" do
12+
expected_bytes = active_storage_blobs(:hello_txt).byte_size
13+
14+
assert_difference -> { @board.reload.bytes_used }, expected_bytes do
15+
assert_difference -> { @account.reload.bytes_used }, expected_bytes do
16+
perform_enqueued_jobs only: Account::AdjustStorageJob do
17+
@card.comments.create!(body: attachment_html(active_storage_blobs(:hello_txt)))
18+
end
19+
end
20+
end
21+
end
22+
23+
test "tracks storage delta when updating comment with different attachment" do
24+
perform_enqueued_jobs only: Account::AdjustStorageJob do
25+
comment = @card.comments.create!(body: attachment_html(active_storage_blobs(:hello_txt)))
26+
27+
expected_delta = active_storage_blobs(:list_pdf).byte_size - active_storage_blobs(:hello_txt).byte_size
28+
assert_difference -> { @board.reload.bytes_used }, expected_delta do
29+
assert_difference -> { @account.reload.bytes_used }, expected_delta do
30+
comment.reload.update!(body: attachment_html(active_storage_blobs(:list_pdf)))
31+
end
32+
end
33+
end
34+
end
35+
36+
test "tracks negative storage when destroying comment with attachment" do
37+
perform_enqueued_jobs only: Account::AdjustStorageJob do
38+
comment = @card.comments.create!(body: attachment_html(active_storage_blobs(:hello_txt)))
39+
40+
expected_delta = -active_storage_blobs(:hello_txt).byte_size
41+
assert_difference -> { @board.reload.bytes_used }, expected_delta do
42+
assert_difference -> { @account.reload.bytes_used }, expected_delta do
43+
comment.destroy!
44+
end
45+
end
46+
end
47+
end
48+
49+
test "tracks negative storage for card and comments when destroying card" do
50+
perform_enqueued_jobs only: Account::AdjustStorageJob do
51+
card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published)
52+
card.comments.create!(body: attachment_html(active_storage_blobs(:hello_txt)))
53+
card.comments.create!(body: attachment_html(active_storage_blobs(:list_pdf)))
54+
55+
total_bytes = active_storage_blobs(:hello_txt).byte_size * 2 + active_storage_blobs(:list_pdf).byte_size
56+
assert_difference -> { @board.reload.bytes_used }, -total_bytes do
57+
assert_difference -> { @account.reload.bytes_used }, -total_bytes do
58+
card.destroy!
59+
end
60+
end
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)