## Description Reverts [#14726](https://github.com/chatwoot/chatwoot/pull/14726) (\"feat: Add sidebar unread counts for filters (CW-7262)\"), which shipped in 4.15.0. After 4.15.0 rolled out to prod the unread-counts-for-filters code path caused a cascading incident: - `Counter#ensure_filters_cache!` fires on every `/unread_counts/index` and `update_last_seen` request. - On cache miss it calls `Builder#build_filters_for!`, which: - invokes `store.clear_user_filters!` -> `delete_matching` -> a Redis `SCAN_each` over a per-user pattern keyspace, and - runs 4 fresh SQL passes per user (mentions, participating, unattended, and per-folder `Conversations::FilterService` queries). - Threads blocked in the SCAN held their DB connections, the connection pool exhausted, Sidekiq jobs were discarded with `ActiveJob::DeserializationError: could not obtain a connection from the pool`, and the enqueued queue blew past 200K. Related: [CW-7262](https://linear.app/chatwoot/issue/CW-7262/unread-counts-for-filters-folders) ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
76 lines
1.9 KiB
Ruby
76 lines
1.9 KiB
Ruby
class Conversations::UnreadCounts::Builder
|
|
BATCH_SIZE = 1000
|
|
|
|
attr_reader :account
|
|
|
|
def initialize(account)
|
|
@account = account
|
|
end
|
|
|
|
def build_base!
|
|
store.clear_account!(account.id)
|
|
write_memberships(assignment: false)
|
|
store.mark_base_ready!(account.id)
|
|
end
|
|
|
|
def build_assignment!
|
|
store.clear_assignment!(account.id)
|
|
write_memberships(assignment: true)
|
|
store.mark_assignment_ready!(account.id)
|
|
end
|
|
|
|
def build_all!
|
|
build_base!
|
|
build_assignment!
|
|
end
|
|
|
|
private
|
|
|
|
def write_memberships(assignment:)
|
|
unread_conversations.in_batches(of: BATCH_SIZE) do |relation|
|
|
columns = %i[id inbox_id assignee_id cached_label_list team_id]
|
|
memberships = relation.pluck(*columns).map do |id, inbox_id, assignee_id, cached_label_list, team_id|
|
|
{
|
|
conversation_id: id,
|
|
inbox_id: inbox_id,
|
|
assignee_id: assignee_id,
|
|
team_id: team_id,
|
|
label_ids: label_ids_for(cached_label_list)
|
|
}
|
|
end
|
|
|
|
store.add_memberships(account_id: account.id, memberships: memberships, assignment: assignment)
|
|
end
|
|
end
|
|
|
|
def unread_conversations
|
|
account.conversations
|
|
.open
|
|
.joins(:messages)
|
|
.merge(Message.incoming.reorder(nil))
|
|
.where(messages: { account_id: account.id })
|
|
.where(unread_since_last_seen_condition)
|
|
.distinct
|
|
end
|
|
|
|
def unread_since_last_seen_condition
|
|
conversations = Conversation.arel_table
|
|
messages = Message.arel_table
|
|
|
|
conversations[:agent_last_seen_at].eq(nil).or(messages[:created_at].gt(conversations[:agent_last_seen_at]))
|
|
end
|
|
|
|
def label_ids_for(cached_label_list)
|
|
label_titles = cached_label_list.to_s.split(',').map(&:strip).compact_blank
|
|
labels_by_title.values_at(*label_titles).compact
|
|
end
|
|
|
|
def labels_by_title
|
|
@labels_by_title ||= account.labels.pluck(:title, :id).to_h
|
|
end
|
|
|
|
def store
|
|
::Conversations::UnreadCounts::Store
|
|
end
|
|
end
|