This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new 8bb71cdb2 perf(ai): fetch only timeline page run metadata (#7005)
8bb71cdb2 is described below
commit 8bb71cdb2ccca6584aa23f11cd410eab780c646e
Author: aias00 <[email protected]>
AuthorDate: Fri Nov 27 12:05:57 2026 +0800
perf(ai): fetch only timeline page run metadata (#7005)
---
.../ops/ai/conversation/AiConversationService.java | 14 ++-------
.../ops/ai/conversation/AiRunRepository.java | 3 ++
.../conversation/MybatisPlusAiRunRepository.java | 15 ++++++++++
.../AiConversationPersistenceIntegrationTest.java | 22 ++++++++++++++
.../ai/conversation/AiConversationServiceTest.java | 34 ++++++++++++++++++++++
.../ai/conversation/AiTimelineRepositoryTest.java | 24 +++++++++++++++
6 files changed, 100 insertions(+), 12 deletions(-)
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
index bd8a5d16d..a7490e006 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationService.java
@@ -320,11 +320,7 @@ public class AiConversationService implements
ApplicationRunner {
runsReferencedBy(conversation.getId(), items));
}
- /**
- * The run rows the page references, in first-appearance order. A
conversation accumulates at
- * most one run per turn, so filtering the conversation's full run list is
cheaper than a
- * per-page {@code WHERE id IN (...)} and keeps the repository port
minimal.
- */
+ /** The run rows referenced by this page, without loading the
conversation's entire history. */
private List<RmqAiRun> runsReferencedBy(Long conversationId,
List<TimelineItem> items) {
if (items.isEmpty()) {
return List.of();
@@ -333,13 +329,7 @@ public class AiConversationService implements
ApplicationRunner {
for (TimelineItem item : items) {
referenced.add(item.runId());
}
- List<RmqAiRun> runs = new ArrayList<>();
- for (RmqAiRun run :
runRepository.findByConversationId(conversationId)) {
- if (referenced.contains(run.getId())) {
- runs.add(run);
- }
- }
- return runs;
+ return runRepository.findByConversationIdAndIds(conversationId,
referenced);
}
/**
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiRunRepository.java
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiRunRepository.java
index 2b1827488..54d8babbd 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiRunRepository.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/AiRunRepository.java
@@ -48,6 +48,9 @@ public interface AiRunRepository {
List<RmqAiRun> findByConversationId(Long conversationId);
+ /** Only the runs referenced by an event page, scoped to its conversation,
ordered by turn/id. */
+ List<RmqAiRun> findByConversationIdAndIds(Long conversationId,
Collection<Long> runIds);
+
/**
* The newest run of each given conversation, keyed by conversation id;
conversations that never
* ran are simply absent.
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiRunRepository.java
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiRunRepository.java
index 6f45ad048..540c71ca1 100644
---
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiRunRepository.java
+++
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/conversation/MybatisPlusAiRunRepository.java
@@ -105,6 +105,21 @@ public class MybatisPlusAiRunRepository implements
AiRunRepository {
.orderByAsc("turn", "id"));
}
+ @Override
+ public List<RmqAiRun> findByConversationIdAndIds(Long conversationId,
Collection<Long> runIds) {
+ if (conversationId == null || runIds == null || runIds.isEmpty()) {
+ return List.of();
+ }
+ List<Long> ids =
runIds.stream().filter(Objects::nonNull).distinct().toList();
+ if (ids.isEmpty()) {
+ return List.of();
+ }
+ return runMapper.selectList(new QueryWrapper<RmqAiRun>()
+ .eq("conversation_id", conversationId)
+ .in("id", ids)
+ .orderByAsc("turn", "id"));
+ }
+
@Override
public Map<Long, RmqAiRun> findLatestByConversationIds(Collection<Long>
conversationIds) {
if (conversationIds == null || conversationIds.isEmpty()) {
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
index fd8bc7b92..a1a7dd079 100644
---
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
+++
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationPersistenceIntegrationTest.java
@@ -49,6 +49,7 @@ import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.inOrder;
@@ -334,6 +335,27 @@ class AiConversationPersistenceIntegrationTest {
assertThat(conversationMapper.selectById(seeded.conversationId)).isNull();
}
+ @Test
+ void timelineShouldReadOnlyTheRunsReferencedByEachPageTest() {
+ Seeded seeded = seed();
+ Seeded other = seed();
+ clearInvocations(runRepository);
+
+ assertThat(conversationService.timeline(seeded.conversationId, OWNER,
0, 20).runs())
+
.extracting(RmqAiRun::getId).containsExactly(seeded.firstRunId);
+ assertThat(conversationService.timeline(seeded.conversationId, OWNER,
100, 20).runs())
+
.extracting(RmqAiRun::getId).containsExactly(seeded.secondRunId);
+ assertThat(conversationService.timeline(seeded.conversationId, OWNER,
90, 20).runs())
+
.extracting(RmqAiRun::getId).containsExactly(seeded.firstRunId,
seeded.secondRunId);
+ assertThat(conversationService.timeline(seeded.conversationId, OWNER,
EVENT_COUNT, 20).runs())
+ .isEmpty();
+ verify(runRepository, never()).findByConversationId(any());
+
+
assertThat(runRepository.findByConversationIdAndIds(seeded.conversationId,
+ List.of(seeded.secondRunId, other.firstRunId,
seeded.firstRunId, seeded.secondRunId)))
+
.extracting(RmqAiRun::getId).containsExactly(seeded.firstRunId,
seeded.secondRunId);
+ }
+
@Test
void theRetentionSweepShouldPurgeChildrenBeforeParentsTest() {
Seeded seeded = seed();
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationServiceTest.java
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationServiceTest.java
index 892acc505..c246ab9af 100644
---
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationServiceTest.java
+++
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiConversationServiceTest.java
@@ -264,6 +264,40 @@ class AiConversationServiceTest {
// --- timeline
--------------------------------------------------------------
+ @Test
+ void timelineShouldNotLoadTheWholeRunHistoryForOneEventPageTest() {
+ when(conversationRepository.findByIdAndOwner(CONVERSATION_ID, OWNER))
+
.thenReturn(Optional.of(AiRunTestSupport.conversation(CONVERSATION_ID, OWNER)));
+ RmqAiEvent probe = event(3, "text",
"{\"type\":\"text\",\"text\":\"next run\"}");
+ probe.setRunId(12L);
+ when(eventRepository.findByConversationIdAfterSeq(CONVERSATION_ID, 0,
3)).thenReturn(List.of(
+ event(1, "user", "{\"type\":\"user\",\"text\":\"hi\"}"),
+ event(2, "text", "{\"type\":\"text\",\"text\":\"hello\"}"),
probe));
+ RmqAiRun run = AiRunTestSupport.run(11L, CONVERSATION_ID, 1,
RunStatus.COMPLETED);
+ when(runRepository.findByConversationIdAndIds(eq(CONVERSATION_ID),
any()))
+ .thenReturn(List.of(run));
+
+ AiConversationService.TimelinePage page =
service.timeline(CONVERSATION_ID, OWNER, 0, 2);
+ assertThat(page.items()).hasSize(2);
+ assertThat(page.runs()).containsExactly(run);
+
+ verify(runRepository, never()).findByConversationId(any());
+ verify(runRepository).findByConversationIdAndIds(eq(CONVERSATION_ID),
+ argThat(ids -> ids.size() == 1 && ids.contains(11L)));
+ }
+
+ @Test
+ void timelineShouldNotQueryRunMetadataForUnreadableEventsTest() {
+ when(conversationRepository.findByIdAndOwner(CONVERSATION_ID, OWNER))
+
.thenReturn(Optional.of(AiRunTestSupport.conversation(CONVERSATION_ID, OWNER)));
+ when(eventRepository.findByConversationIdAfterSeq(CONVERSATION_ID, 0,
3))
+ .thenReturn(List.of(event(1, "text", "invalid json")));
+
+ assertThat(service.timeline(CONVERSATION_ID, OWNER, 0,
2).runs()).isEmpty();
+ verify(runRepository, never()).findByConversationIdAndIds(any(),
any());
+ verify(runRepository, never()).findByConversationId(any());
+ }
+
@Test
void timelineShouldDeriveTheCursorFromOneExtraRowTest() {
RmqAiConversation stored =
AiRunTestSupport.conversation(CONVERSATION_ID, OWNER);
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiTimelineRepositoryTest.java
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiTimelineRepositoryTest.java
index c45c38030..26b9ac443 100644
---
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiTimelineRepositoryTest.java
+++
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/conversation/AiTimelineRepositoryTest.java
@@ -648,6 +648,30 @@ class AiTimelineRepositoryTest {
.contains("ORDER BY turn ASC,id ASC");
}
+ @Test
+ void
thePageRunLookupShouldBindOnlyDistinctReferencedIdsAndConversationTest() {
+ when(runMapper.selectList(any())).thenReturn(List.of());
+
+ runs.findByConversationIdAndIds(CONVERSATION_ID, Arrays.asList(11L,
12L, 11L, null));
+
+ QueryWrapper<RmqAiRun> query = capturedRunSelectList();
+ assertThat(query.getTargetSql())
+ .contains("conversation_id = ? AND id IN (?,?)")
+ .contains("ORDER BY turn ASC,id ASC");
+ assertThat(query.getParamNameValuePairs().values())
+ .containsExactlyInAnyOrder(CONVERSATION_ID, 11L, 12L);
+ }
+
+ @Test
+ void thePageRunLookupShouldSkipSqlWithoutConversationOrUsableIdsTest() {
+ assertThat(runs.findByConversationIdAndIds(null,
List.of(11L))).isEmpty();
+ assertThat(runs.findByConversationIdAndIds(CONVERSATION_ID,
null)).isEmpty();
+ assertThat(runs.findByConversationIdAndIds(CONVERSATION_ID,
List.of())).isEmpty();
+ assertThat(runs.findByConversationIdAndIds(CONVERSATION_ID,
Collections.singletonList(null)))
+ .isEmpty();
+ verify(runMapper, never()).selectList(any());
+ }
+
/**
* Two statements for a whole page instead of one per conversation: {@code
MAX(id)} grouped by
* conversation is an index-only lookup on {@code idx_ai_run_conversation
(conversation_id, id)}, then