Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cache aggregated word counts on worldbuilding pages
  • Loading branch information
drusepth committed Oct 8, 2021
commit 3e0d2210c2b1fbec8c61142580a32a9b513bc05f
19 changes: 19 additions & 0 deletions app/jobs/cache_sum_attribute_word_count_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CacheSumAttributeWordCountJob < ApplicationJob
queue_as :cache

def perform(*args)
entity_type = args.shift
entity_id = args.shift

entity = entity_type.constantize.find_by(id: entity_id)
sum_attribute_word_count = Attribute.where(entity_type: entity_type, entity_id: entity_id).sum(:word_count_cache)

update = entity.word_count_updates.find_or_initialize_by(
for_date: DateTime.current,
)
update.word_count = sum_attribute_word_count
update.user_id ||= entity.user_id

update.save!
end
end
5 changes: 5 additions & 0 deletions app/models/concerns/is_content_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module IsContentPage
has_many :timeline_events, through: :timeline_event_entities
has_many :timelines, -> { distinct }, through: :timeline_events

has_many :word_count_updates, as: :entity, dependent: :destroy
def latest_word_count_cache
word_count_updates.order('for_date DESC').limit(1).first.try(:word_count) || 0
end

scope :unarchived, -> { where(archived_at: nil) }
def archive!
update!(archived_at: DateTime.now)
Expand Down
8 changes: 7 additions & 1 deletion app/models/page_data/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ class Attribute < ApplicationRecord
end

after_commit do
CacheAttributeWordCountJob.perform_later(self.id) if saved_changes.key?('value')
if saved_changes.key?('value')
# Cache the updated word count on this attribute
CacheAttributeWordCountJob.perform_later(self.id)

# Cache the updated word count on the page this attribute belongs to
CacheSumAttributeWordCountJob.perform_later(self.entity_type, self.entity_id)
end
end

after_save do
Expand Down
4 changes: 4 additions & 0 deletions app/models/word_count_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class WordCountUpdate < ApplicationRecord
belongs_to :user
belongs_to :entity, polymorphic: true
end
12 changes: 12 additions & 0 deletions db/migrate/20211007234707_create_word_count_updates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateWordCountUpdates < ActiveRecord::Migration[6.0]
def change
create_table :word_count_updates do |t|
t.references :user, null: false, foreign_key: true
t.references :entity, polymorphic: true, null: false
t.integer :word_count
t.date :for_date

t.timestamps
end
end
end
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_10_07_215520) do
ActiveRecord::Schema.define(version: 2021_10_07_234707) do

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -3637,6 +3637,18 @@
t.integer "habitat_id"
end

create_table "word_count_updates", force: :cascade do |t|
t.integer "user_id", null: false
t.string "entity_type", null: false
t.integer "entity_id", null: false
t.integer "word_count"
t.date "for_date"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["entity_type", "entity_id"], name: "index_word_count_updates_on_entity_type_and_entity_id"
t.index ["user_id"], name: "index_word_count_updates_on_user_id"
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "api_keys", "users"
add_foreign_key "api_requests", "application_integrations"
Expand Down Expand Up @@ -4048,4 +4060,5 @@
add_foreign_key "vehicles", "users"
add_foreign_key "votes", "users"
add_foreign_key "votes", "votables"
add_foreign_key "word_count_updates", "users"
end
2 changes: 1 addition & 1 deletion lib/tasks/backfill.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace :backfill do
desc "Backfill cached word counts on all attributes"
task attribute_word_count_caches: :environment do
Attribute.where(cached_word_count: nil).find_each do |attribute|
Attribute.where(word_count_cache: nil).where.not(value: ["", " ", ".", nil]).find_each do |attribute|
word_count = WordCountAnalyzer::Counter.new(
ellipsis: 'no_special_treatment',
hyperlink: 'count_as_one',
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/word_count_updates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user: one
entity: one
entity_type: Entity
word_count: 1
for_date: 2021-10-07

two:
user: two
entity: two
entity_type: Entity
word_count: 1
for_date: 2021-10-07
7 changes: 7 additions & 0 deletions test/models/word_count_update_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class WordCountUpdateTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end