Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 9576272

Browse files
authored
PERF: Preload only gists when including summaries in topic list (#948)
* PERF: Preload only gists when including summaries in topic list * Add unique index on summaries and dedup existing records * Make hot topics batch size setting hidden
1 parent 5fb1177 commit 9576272

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

app/models/ai_summary.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ def outdated
5151
#
5252
# Indexes
5353
#
54-
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
54+
# idx_on_target_id_target_type_summary_type_3355609fbb (target_id,target_type,summary_type) UNIQUE
55+
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
5556
#

config/settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ discourse_ai:
368368
default: 0
369369
min: 0
370370
max: 1000
371+
hidden: true
371372
ai_hot_topic_gists_allowed_groups:
372373
type: group_list
373374
list_type: compact
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
class UniqueAiSummaries < ActiveRecord::Migration[7.1]
3+
def up
4+
execute <<~SQL
5+
DELETE FROM ai_summaries ais1
6+
USING ai_summaries ais2
7+
WHERE ais1.id < ais2.id
8+
AND ais1.target_id = ais2.target_id
9+
AND ais1.target_type = ais2.target_type
10+
AND ais1.summary_type = ais2.summary_type
11+
SQL
12+
13+
add_index :ai_summaries, %i[target_id target_type summary_type], unique: true
14+
end
15+
16+
def down
17+
remove_index :ai_summaries, column: %i[target_id target_type summary_type]
18+
end
19+
end

lib/summarization/entry_point.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def inject_into(plugin)
2020
plugin.register_modifier(:topic_query_create_list_topics) do |topics, options|
2121
if Discourse.filters.include?(options[:filter]) && SiteSetting.ai_summarization_enabled &&
2222
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0
23-
topics.includes(:ai_summaries)
23+
topics.includes(:ai_gist_summary)
2424
else
2525
topics
2626
end
@@ -32,11 +32,7 @@ def inject_into(plugin)
3232
include_condition: -> { scope.can_see_gists? },
3333
) do
3434
return if !Discourse.filters.include?(options[:filter])
35-
summaries = object.ai_summaries.to_a
36-
37-
# Summaries should always have one or zero elements here.
38-
# This is an extra safeguard to avoid including regular summaries.
39-
summaries.find { |s| s.summary_type == "gist" }&.summarized_text
35+
object.ai_gist_summary&.summarized_text
4036
end
4137

4238
# To make sure hot topic gists are inmediately up to date, we rely on this event

lib/topic_extensions.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ module DiscourseAi
44
module TopicExtensions
55
extend ActiveSupport::Concern
66

7-
prepended { has_many :ai_summaries, as: :target }
7+
prepended do
8+
has_many :ai_summaries, as: :target
9+
10+
has_one :ai_gist_summary,
11+
-> { where(summary_type: AiSummary.summary_types[:gist]) },
12+
class_name: "AiSummary",
13+
as: :target
14+
end
815
end
916
end

0 commit comments

Comments
 (0)