Skip to content

Conversation

@omkarK06
Copy link
Contributor

@omkarK06 omkarK06 commented Oct 3, 2025

User description

The histogram total count was getting set to 0 in case of streaming aggs when the first partition returns results and next all partitions return empty hits. In this case it should consider the count of the first partition.


PR Type

Bug fix, Enhancement


Description

  • Fix histogram total with streaming aggs

  • Gate total assignment on presence of total

  • Improve streaming partition/chunk handling

  • Refactor for readability and safety


Diagram Walkthrough

flowchart LR
  WS["WebSocket search responses"] -- hits/metadata/response --> Handler["handleSearchResponse"]
  Handler -- partition/chunk tracking --> Partitions["partition/chunk map"]
  Handler -- streaming flags --> AppendLogic["append results decision"]
  Handler -- aggregation path --> Aggregation["handleAggregation"]
  Aggregation -- streaming_aggs with total --> TotalSet["set searchAggData.total"]
  Aggregation -- non-streaming or no total --> TotalAcc["accumulate searchAggData.total"]
Loading

File Walkthrough

Relevant files
Bug fix
useSearchStream.ts
Streaming aggregation handling and total fix                         

web/src/composables/useLogs/useSearchStream.ts

  • Add checks when incrementing chunk counters.
  • Break long expressions; improve readability.
  • Refine append decision for streaming partitions/chunks.
  • Fix aggregation total: set only when streaming with total.
+40/-19 

@github-actions github-actions bot added the ☢️ Bug Something isn't working label Oct 3, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Overview

Summary

This PR fixes a bug in the streaming aggregation logic where histogram total counts were incorrectly being reset to 0. The issue occurred when processing streaming aggregations across multiple partitions: the first partition would return valid results with a proper total count, but subsequent partitions with empty hits would overwrite this total to 0.

The fix adds a null/undefined check (response.content?.results?.total) to the streaming aggregation condition in useSearchStream.ts. This ensures that the total count is only set when there's actually a valid total value in the response, preserving the accumulated total from previous partitions when subsequent ones are empty.

This change integrates with the existing streaming aggregation system that processes search results in partitions. The streaming aggregation feature allows for real-time processing of large datasets by breaking them into manageable chunks. Without this fix, the UI would show incorrect histogram totals (0) when later partitions had no results, even though earlier partitions contained valid data.

Important Files Changed

Changed Files
Filename Score Overview
web/src/composables/useLogs/useSearchStream.ts 4/5 Added null check to prevent histogram total from being overwritten to 0 in streaming aggregations

Confidence score: 4/5

  • This PR is safe to merge with minimal risk as it addresses a specific edge case without affecting core functionality
  • Score reflects a targeted bug fix that adds defensive programming without changing existing behavior for valid responses
  • No files require special attention as the change is simple and well-isolated

Sequence Diagram

sequenceDiagram
    participant User
    participant UI as "UI Component"
    participant SearchStream as "useSearchStream"
    participant WebSocket as "WebSocket/Stream"
    participant Backend as "Backend Service"

    User->>UI: "Initiate search with streaming aggs"
    UI->>SearchStream: "getDataThroughStream(false)"
    SearchStream->>SearchStream: "getQueryReq(false)"
    SearchStream->>SearchStream: "buildWebSocketPayload()"
    SearchStream->>SearchStream: "initializeSearchConnection()"
    SearchStream->>WebSocket: "Send search request"
    WebSocket->>Backend: "Execute search query"
    
    Backend->>WebSocket: "First partition response (with hits)"
    WebSocket->>SearchStream: "handleSearchResponse() - metadata"
    SearchStream->>SearchStream: "handleStreamingMetadata() - partition 1"
    Note over SearchStream: Set searchObj.data.queryResults with first partition data
    
    Backend->>WebSocket: "Second partition response (empty hits)"
    WebSocket->>SearchStream: "handleSearchResponse() - metadata"
    SearchStream->>SearchStream: "handleStreamingMetadata() - partition 2"
    Note over SearchStream: Check streaming_aggs and !hits.length
    SearchStream->>SearchStream: "shouldAppendStreamingResults = false"
    Note over SearchStream: Preserve total from first partition instead of resetting to 0
    
    SearchStream->>SearchStream: "refreshPagination()"
    SearchStream->>SearchStream: "getAggsTotal()"
    Note over SearchStream: Calculate total from accumulated aggs data
    SearchStream->>UI: "Update histogram with correct total count"
    UI->>User: "Display histogram with proper total count"
Loading

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Possible Issue

The guard for streaming aggregation total uses a truthy check on results.total; if total can be 0 for a valid first partition, the assignment will be skipped and the else-branch will add 0 to an uninitialized or previous total. Confirm that total=0 is not a valid value for the first streaming partition or adjust the condition to explicitly check for undefined/null.

if (
  response.content?.streaming_aggs &&
  response.content?.results?.total
) {
  searchAggData.total = response.content?.results?.total;
} else {
  searchAggData.total =
    searchAggData.total + response.content?.results?.total;
State Safety

Access to searchPartitionMap[payload.traceId] and its nested fields assumes existence before increment/use across branches; ensure all code paths initialize the map entry and chunks for the traceId before reads to avoid undefined access in rare ordering or duplicate message scenarios.

searchPartitionMap[payload.traceId].chunks[
  searchPartitionMap[payload.traceId].partition
]++;
// If single partition has more than 1 chunk, then we need to append the results
const isChunkedHits =
  searchPartitionMap[payload.traceId].chunks[
    searchPartitionMap[payload.traceId].partition
  ] > 1;

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Preserve zero totals correctly

Treat zero totals explicitly to avoid being skipped due to falsy checks. Use nullish
coalescing and an explicit zero comparison so histogram totals of 0 are preserved.

web/src/composables/useLogs/useSearchStream.ts [1017-1024]

-if (
-  response.content?.streaming_aggs &&
-  response.content?.results?.total
-) {
-  searchAggData.total = response.content?.results?.total;
+const incomingTotal = response.content?.results?.total ?? 0;
+if (response.content?.streaming_aggs && (incomingTotal || incomingTotal === 0)) {
+  searchAggData.total = incomingTotal;
 } else {
-  searchAggData.total =
-    searchAggData.total + response.content?.results?.total;
+  searchAggData.total = (searchAggData.total ?? 0) + incomingTotal;
 }
Suggestion importance[1-10]: 7

__

Why: The current condition may skip zero totals due to falsy checks; the proposed handling preserves 0 correctly and uses nullish coalescing, improving accuracy for aggregations.

Medium
Safely check hits presence

Guard against undefined results to avoid treating missing data as empty hits.
Default hits.length to 0 only when results exists to prevent unintended appends.

web/src/composables/useLogs/useSearchStream.ts [832-834]

 const shouldAppendStreamingResults = isStreamingAggs
-  ? !response.content?.results?.hits?.length
+  ? !(response.content?.results && response.content.results.hits?.length)
   : true;
Suggestion importance[1-10]: 6

__

Why: The guard refines the check to avoid treating missing results as empty hits; it matches the PR context and slightly improves correctness, though the change is small.

Low
Enforce boolean evaluation

Use a strict boolean cast to avoid truthy non-boolean values causing incorrect
streaming behavior. This prevents strings or numbers from being treated as enabled
flags and altering append logic.

web/src/composables/useLogs/useSearchStream.ts [829-831]

-const isStreamingAggs =
+const isStreamingAggs = Boolean(
   response.content?.streaming_aggs ||
-  searchObj.data.queryResults.streaming_aggs;
+  searchObj.data.queryResults.streaming_aggs
+);
Suggestion importance[1-10]: 5

__

Why: Casting to Boolean can prevent unintended truthy values from affecting isStreamingAggs, a minor robustness improvement; it aligns with the existing code lines but impact is moderate.

Low

@omkarK06 omkarK06 force-pushed the fix/streaming-aggs-ui-fix-main branch 2 times, most recently from cd96b3c to df832f7 Compare October 10, 2025 07:43
@omkarK06 omkarK06 force-pushed the fix/streaming-aggs-ui-fix-main branch from df832f7 to 1055cd5 Compare October 28, 2025 10:50
@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: omkarK06 | Branch: fix/streaming-aggs-ui-fix-main | Commit: 1055cd5

Testdino Test Results

Status Total Passed Failed Skipped Flaky Pass Rate Duration
All tests passed 365 341 0 19 5 93% 4m 38s

View Detailed Results

@omkarK06 omkarK06 force-pushed the fix/streaming-aggs-ui-fix-main branch from 1055cd5 to 2992b7b Compare October 28, 2025 11:26
@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: omkarK06 | Branch: fix/streaming-aggs-ui-fix-main | Commit: 2992b7b

Testdino Test Results

Status Total Passed Failed Skipped Flaky Pass Rate Duration
All tests passed 366 344 0 19 3 94% 4m 39s

View Detailed Results

@omkarK06 omkarK06 force-pushed the fix/streaming-aggs-ui-fix-main branch from 2992b7b to 5db8075 Compare October 29, 2025 09:09
@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: omkarK06 | Branch: fix/streaming-aggs-ui-fix-main | Commit: 5db8075

Testdino Test Results

Status Total Passed Failed Skipped Flaky Pass Rate Duration
All tests passed 366 342 0 19 5 93% 4m 40s

View Detailed Results

@omkarK06 omkarK06 merged commit dca9164 into main Oct 29, 2025
32 checks passed
@omkarK06 omkarK06 deleted the fix/streaming-aggs-ui-fix-main branch October 29, 2025 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug Something isn't working Review effort 2/5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants