Skip to content

Conversation

@taimingl
Copy link
Contributor

@taimingl taimingl commented Oct 28, 2024

impl #4914

new parameters acceptable for search queries to add additional context in usage report:
Dashboard
dashboard_id
dashboard_name
dashboard_folder
dashboard_folder_id
dashboard_folder_name

Report
report_key

Alert
alert_key

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced search_event_context to enhance search requests with additional context.
    • Added reportId prop to multiple components for improved reporting capabilities.
    • New endpoints for trace data handling and enhanced error logging for report generation.
  • Bug Fixes

    • Improved error handling in various functions to ensure better stability and clarity in responses.
  • Documentation

    • Updated OpenAPI schema to include SearchEventContext for better API documentation.
  • Refactor

    • Simplified logic in search functions to enhance maintainability and clarity.

These changes collectively enhance the search functionality, reporting capabilities, and overall robustness of the application.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Walkthrough

The pull request introduces several enhancements across multiple files, focusing on the integration of a new search_event_context variable and related structures. Key modifications include the addition of the SearchEventContext struct, updates to various request structures to include this context, and the enhancement of search-related functions to utilize the new context for improved data handling and tracking. The changes aim to facilitate better context management for search operations without altering existing logic significantly.

Changes

File Path Change Summary
src/cli/data/export.rs Modified operator method to handle query parameters using a HashMap and introduced search_event_context.
src/common/utils/http.rs Added get_search_event_context_from_request function for constructing SearchEventContext based on query parameters.
src/config/src/meta/search.rs Introduced SearchEventContext struct and updated Request and MultiStreamRequest structs to include new fields.
src/config/src/meta/usage.rs Added search_event_context field to UsageData and RequestStats structs.
src/handler/http/request/search/mod.rs Updated search functions to include search_event_context in requests.
src/handler/http/request/search/multi_streams.rs Modified multiple functions to include search_event_context.
src/handler/http/request/traces/mod.rs Added new endpoints for trace handling and included search_event_context in the request structure.
src/handler/http/router/openapi.rs Added SearchEventContext schema to OpenAPI documentation.
src/service/alerts/alert.rs Enhanced evaluate method to include event_type and event_context.
src/service/alerts/derived_streams.rs Updated evaluate and save methods to incorporate module_key and SearchEventContext.
src/service/alerts/mod.rs Modified evaluate_scheduled method to accept search_type and search_event_context.
src/service/alerts/scheduler.rs Updated handle_derived_stream_triggers to include module_key in evaluations.
src/service/db/enrichment_table.rs Added search_event_context field to Request struct.
src/service/metrics/prom.rs Added search_event_context to get_series and get_labels functions.
src/service/search/cache/mod.rs Added search_event_context to RequestStats structure.
src/service/search/mod.rs Updated search and search_multi functions to include search_event_context.
src/service/usage/mod.rs Modified report_request_usage_stats to include search_event_context in UsageData.
src/report_server/src/report.rs Updated generate_report function to include report_name parameter.
src/report_server/src/router.rs Modified send_report function to pass report_name to generate_report.
src/service/dashboards/reports.rs Updated generate_report to include report_name and refined error handling.
web/src/components/dashboards/PanelContainer.vue Added reportId prop to PanelContainer component.
web/src/components/dashboards/PanelSchemaRenderer.vue Introduced reportId prop to PanelSchemaRenderer component.
web/src/composables/dashboard/usePanelDataLoader.ts Updated usePanelDataLoader function to include reportId.
web/src/services/search.ts Added optional parameters dashboard_id and folder_id to search method.
web/src/views/Dashboards/RenderDashboardCharts.vue Added reportId prop to RenderDashboardCharts component.
web/src/views/Dashboards/ViewDashboard.vue Introduced computed property reportId to extract value from route query parameters.

Possibly related PRs

Suggested reviewers

  • hengfeiyang
  • omkarK06
  • neha00290

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@taimingl taimingl marked this pull request as draft October 28, 2024 23:28
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 11

🧹 Outside diff range and nitpick comments (8)
src/cli/data/export.rs (1)

40-42: Consider using ? operator for error propagation

The query map creation looks good, but consider enhancing error handling in the subsequent operations using the ? operator for more concise error propagation.

-        let stream_type = match get_stream_type_from_request(&query_map) {
-            Ok(v) => v.unwrap_or(StreamType::Logs),
-            Err(_) => return Ok(false),
-        };
+        let stream_type = get_stream_type_from_request(&query_map)
+            .map_err(|_| false)?
+            .unwrap_or(StreamType::Logs);
src/service/usage/stats.rs (2)

96-96: Consider documenting the intentional None value.

The addition of search_event_context aligns with the PR objectives. Consider adding a brief comment explaining why it's set to None in this internal usage statistics context.

+            // Search event context is not needed for internal usage statistics
             search_event_context: None,

155-155: Consider documenting the intentional None value.

Similar to the previous instance, consider documenting why search_event_context is set to None here.

+        // Search event context is not needed for internal usage statistics
         search_event_context: None,
src/common/utils/http.rs (2)

85-103: Add documentation for the new function.

The function lacks documentation explaining its purpose, parameters, and return value. Consider adding a doc comment.

 #[inline(always)]
+/// Extracts search event context from HTTP query parameters based on the search event type.
+///
+/// # Arguments
+/// * `search_event_type` - Type of search event (Dashboards, Alerts, Reports)
+/// * `query` - HTTP query parameters
+///
+/// # Returns
+/// * `Some(SearchEventContext)` if relevant parameters are found
+/// * `None` if parameters are missing or search type is not supported
 pub(crate) fn get_search_event_context_from_request(

91-94: Optimize string allocations.

Consider using references where possible to avoid unnecessary string allocations.

-            let dashboard_folder = query.get("dashboard_folder").map(String::from);
-            SearchEventContext::with_dashboard(dashboard_name.to_owned(), dashboard_folder)
+            let dashboard_folder = query.get("dashboard_folder").map(|s| s.to_string());
+            SearchEventContext::with_dashboard(dashboard_name.to_string(), dashboard_folder)
src/handler/http/request/traces/mod.rs (1)

Line range hint 258-260: Add timeout validation

The timeout parameter is parsed without validation. Consider adding bounds checking to prevent unreasonable values.

Add validation for the timeout value:

     let timeout = query
         .get("timeout")
         .map_or(0, |v| v.parse::<i64>().unwrap_or(0));
+    if timeout < 0 || timeout > cfg.common.max_query_timeout {
+        return Ok(MetaHttpResponse::bad_request(&format!(
+            "timeout must be between 0 and {} seconds",
+            cfg.common.max_query_timeout
+        )));
+    }
src/service/search/mod.rs (1)

465-482: Consider adding a documentation comment

While the match expression is well-structured, consider adding a brief comment explaining why certain search types (UI, Values, Other) don't require usage reporting. This would help future maintainers understand the business logic.

+    // Skip usage reporting for UI, Values, and Other search types as they are considered
+    // internal operations. All other search types should be tracked for usage statistics.
     let (report_usage, search_type, search_event_context) = match in_req.search_type {
         Some(search_type) => {
             if matches!(
src/config/src/meta/search.rs (1)

777-805: Consider accepting &str instead of String in constructor methods.

To improve performance and avoid unnecessary cloning, consider changing the parameters of the with_* methods to accept &str and convert them to String within the methods.

Apply this diff to the methods:

-pub fn with_alert(alert_key: String) -> Self {
+pub fn with_alert(alert_key: &str) -> Self {
     Self {
-        alert_key: Some(alert_key),
+        alert_key: Some(alert_key.to_string()),
         ..Default::default()
     }
}

...

-pub fn with_report(report_key: String) -> Self {
+pub fn with_report(report_key: &str) -> Self {
     Self {
-        report_key: Some(report_key),
+        report_key: Some(report_key.to_string()),
         ..Default::default()
     }
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between eeecb3f and b8d4a9d.

📒 Files selected for processing (18)
  • src/cli/data/export.rs (3 hunks)
  • src/common/utils/http.rs (2 hunks)
  • src/config/src/meta/search.rs (6 hunks)
  • src/config/src/meta/usage.rs (5 hunks)
  • src/handler/http/request/search/mod.rs (6 hunks)
  • src/handler/http/request/search/multi_streams.rs (6 hunks)
  • src/handler/http/request/traces/mod.rs (1 hunks)
  • src/handler/http/router/openapi.rs (1 hunks)
  • src/service/alerts/alert.rs (2 hunks)
  • src/service/alerts/derived_streams.rs (4 hunks)
  • src/service/alerts/mod.rs (4 hunks)
  • src/service/alerts/scheduler.rs (1 hunks)
  • src/service/db/enrichment_table.rs (1 hunks)
  • src/service/metrics/prom.rs (2 hunks)
  • src/service/search/cache/mod.rs (1 hunks)
  • src/service/search/mod.rs (3 hunks)
  • src/service/usage/mod.rs (2 hunks)
  • src/service/usage/stats.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (18)
src/cli/data/export.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/common/utils/http.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/config/src/meta/search.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/config/src/meta/usage.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/handler/http/request/search/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/handler/http/request/search/multi_streams.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/handler/http/request/traces/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/handler/http/router/openapi.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/alert.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/derived_streams.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/scheduler.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/db/enrichment_table.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/metrics/prom.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/search/cache/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/search/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/usage/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/usage/stats.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

🔇 Additional comments (35)
src/cli/data/export.rs (2)

27-30: LGTM! Imports are well-organized

The new imports are properly organized and necessary for the search event context functionality.


78-78: LGTM! Verify SearchEventContext usage

The addition of search_event_context to the Request struct is correct and aligns with the PR objectives.

✅ Verification successful

SearchEventContext usage is properly integrated

The verification shows that search_event_context is correctly used throughout the codebase:

  • Defined in src/config/src/meta/search.rs with proper serialization attributes
  • Used consistently in search-related request structs
  • Properly handled in HTTP request processing (src/handler/http/request/search/)
  • Integrated with alerts, usage tracking, and other relevant services
  • The addition in src/cli/data/export.rs follows the same pattern as other implementations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify the SearchEventContext struct usage across the codebase
ast-grep --pattern 'struct $struct_name {
  $$$
  search_event_context: $type,
  $$$
}'

Length of output: 89


Script:

#!/bin/bash
# Let's try a different approach to find SearchEventContext usage
# First, find the struct definition
rg -A 5 "struct SearchEventContext" 

# Then find its usage
rg "SearchEventContext" 

# Also check for snake_case version
rg "search_event_context"

Length of output: 6020

src/service/db/enrichment_table.rs (1)

54-54: LGTM! Verify consistency with related changes.

The addition of search_event_context is well-implemented and aligns with the PR objectives to enhance search queries with additional context.

Let's verify the consistency of this change with related files:

✅ Verification successful

Let me generate one more verification to check the SearchEventContext struct definition since it wasn't found in the previous results:


The search_event_context field is consistently implemented across the codebase

The verification shows that:

  • The SearchEventContext struct is properly defined in src/config/src/meta/search.rs with appropriate serialization attributes
  • The field is consistently initialized as None across all Request structs in various modules
  • It's properly used with Some variants in alert and dashboard contexts where needed
  • The implementation in enrichment_table.rs follows the same pattern as other modules
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all Request structs consistently include search_event_context
# and check its usage pattern across the codebase

# Check the SearchEventContext struct definition and its fields
ast-grep --pattern 'struct SearchEventContext {
  $$$
}'

# Check Request structs that should include search_event_context
ast-grep --pattern 'struct Request {
  $$$
  search_event_context: $_,
  $$$
}'

# Verify the initialization pattern
rg 'search_event_context: None' -A 2

Length of output: 3270


Script:

#!/bin/bash
# Search for SearchEventContext type references to understand its definition
rg "SearchEventContext" -B 2 -A 2

# Check if there are any non-None initializations of search_event_context
rg "search_event_context:\s+Some" -B 2 -A 2

Length of output: 6306

src/common/utils/http.rs (1)

26-29: LGTM: Import changes are appropriate.

The new imports for SearchEventContext and SearchEventType are correctly organized and necessary for the new functionality.

src/config/src/meta/usage.rs (2)

18-18: LGTM: Clean import addition

The SearchEventContext import is appropriately grouped with the related SearchEventType.


299-299: LGTM: Consistent implementation updates

The search_event_context field is correctly initialized to None in both the Default and From<FileMeta> implementations, maintaining consistency with other optional fields.

Also applies to: 323-323

src/handler/http/router/openapi.rs (1)

165-165: LGTM! Verify schema completeness.

The addition of SearchEventContext to the OpenAPI schema components is correct and aligns with the PR objectives.

Let's verify that all required fields from the PR objectives are included in the schema:

✅ Verification successful

Schema implementation is complete and correctly documented

The SearchEventContext struct in src/config/src/meta/search.rs contains all the necessary fields for search usage reports:

  • alert_key
  • derived_stream_key
  • report_key
  • dashboard_name and dashboard_folder (via the implementation methods)

The OpenAPI documentation correctly includes this schema, and its usage is properly integrated across the codebase in alerts, derived streams, and usage tracking.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify SearchEventContext schema includes all required fields
# Expected fields from PR objectives: dashboard_name, dashboard_folder, report_key, alert_key

# Search for the SearchEventContext struct definition
ast-grep --pattern 'struct SearchEventContext {
  $$$
}'

Length of output: 58


Script:

#!/bin/bash
# Let's try a different approach to find the SearchEventContext definition
# First, let's use ripgrep to find files containing SearchEventContext
rg "SearchEventContext" -l

# Then let's see the actual content with context
rg "SearchEventContext" -A 10 -B 2

Length of output: 13978

src/service/usage/mod.rs (2)

105-105: LGTM: Search context addition for function usage tracking

The addition of search_event_context with clone is appropriate here and aligns with the PR objectives.


143-143: Verify aggregation handling with new search context

The addition looks good, but let's verify the aggregation logic handles the new field correctly.

src/service/search/cache/mod.rs (1)

301-301: LGTM: Search event context tracking added correctly.

The addition of search_event_context to RequestStats aligns with the PR objectives to enhance search queries with additional context (dashboard, report, and alert information) for usage reports.

Let's verify the consistent usage of this field across the codebase:

✅ Verification successful

Search event context implementation is consistent across the codebase

The verification confirms:

  • SearchEventContext struct is properly defined in src/config/src/meta/search.rs with expected fields like alert_key
  • The field is consistently used across multiple components:
    • Search handlers (request/search/mod.rs, request/search/multi_streams.rs)
    • Usage tracking (service/usage/stats.rs, service/usage/mod.rs)
    • Cache implementation (service/search/cache/mod.rs)
    • Alert system (service/alerts/mod.rs)
  • All implementations properly handle the optional nature of the context with Option<SearchEventContext>
  • The cloning pattern is consistently applied when propagating the context
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of search_event_context across the codebase
# Test 1: Check for struct definitions and field usage
rg -A 5 "struct.*SearchEventContext"

# Test 2: Check for places where search_event_context is used
rg "search_event_context"

# Test 3: Check for request stats usage to ensure consistent implementation
ast-grep --pattern 'struct RequestStats {
  $$$
  search_event_context: $_,
  $$$
}'

Length of output: 4396

src/service/alerts/mod.rs (3)

77-78: LGTM: Function signature changes align with PR objectives.

The new optional parameters search_type and search_event_context are properly typed and named.


322-323: LGTM: Consistent implementation of search context parameters.

The search context parameters are correctly forwarded to the Request struct, maintaining consistency with the MultiStreamRequest implementation.


264-265: Consider validating search context parameters.

While the parameters are correctly forwarded to MultiStreamRequest, consider adding validation to ensure the search context is appropriate for the alert type being evaluated.

src/service/metrics/prom.rs (1)

666-666: LGTM! Consistent implementation of search event context.

The addition of search_event_context: None to the Request structs in both get_series() and get_label_values() functions is consistent with the PR objectives of enhancing search queries with additional context parameters.

Let's verify the Request struct definition and its usage:

Also applies to: 812-812

✅ Verification successful

Let me generate one more verification to check the struct definition and type:


LGTM! The search_event_context field is properly defined and consistently used.

The verification confirms:

  • SearchEventContext is properly defined as a struct in src/config/src/meta/search.rs
  • The field is correctly added as Option<SearchEventContext> across multiple request structs
  • The implementation in prom.rs follows the same pattern as other files in the codebase
  • All usages initialize it with None as a safe default value
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the Request struct definition and its usage
# Expected: The struct should have search_event_context field defined

# Search for the Request struct definition
ast-grep --pattern 'struct Request {
  $$$
  search_event_context: $_,
  $$$
}'

# Search for other usages of Request with search_event_context
rg 'search_event_context:' -A 2 -B 2

Length of output: 9981


Script:

#!/bin/bash
# Search for SearchEventContext type definition
rg "struct SearchEventContext" -A 5 -B 1

# Search for Request struct definition
rg "struct Request" -A 10 -B 1

Length of output: 8105

src/handler/http/request/search/multi_streams.rs (4)

40-41: LGTM: Import changes align with new functionality.

The addition of get_search_event_context_from_request to the imports is appropriate for the new search event context feature.


348-348: LGTM: Proper integration of search event context in RequestStats.

The search_event_context is correctly added to RequestStats, enabling proper tracking of search context in usage reports.


566-566: LGTM: Consistent usage of search event context.

The search_event_context is properly propagated from multi_req to the request stats for function usage reporting.


931-931: 🛠️ Refactor suggestion

Verify if search_event_context should be None in around_multi.

The around_multi function sets search_event_context to None for both forward and backward search requests. Consider deriving the context from the request parameters similar to other search functions for consistency.

Consider updating the requests to include the search event context:

 search_type: Some(search::SearchEventType::UI),
-search_event_context: None,
+search_event_context: get_search_event_context_from_request(
+    &search::SearchEventType::UI,
+    &query
+),
 index_type: "".to_string(),

Also applies to: 1005-1005

src/service/search/mod.rs (3)

340-340: LGTM: SearchEventContext properly integrated into RequestStats

The addition of search_event_context to RequestStats is well-implemented and aligns with the PR objectives for enhancing search usage reports.


508-508: LGTM: Consistent implementation of SearchEventContext

The addition of search_event_context to RequestStats in the search function maintains consistency with the search_multi implementation, ensuring uniform usage reporting across both search paths.


Line range hint 465-508: Verify SearchEventContext propagation

Let's verify that all necessary structures have been updated to include the search_event_context field and that it's properly propagated through the search workflow.

✅ Verification successful

Based on the search results, I can now generate the final response:

SearchEventContext field is properly propagated and handled

The verification shows that SearchEventContext is consistently implemented across the codebase:

  • Defined in relevant structures:

    • RequestStats in usage.rs
    • Request in search.rs
    • Multiple search-related structures in meta/search.rs
  • Properly propagated through:

    • Search workflow (service/search/mod.rs)
    • Cache handling (service/search/cache/mod.rs)
    • Multi-stream search (handler/http/request/search/multi_streams.rs)
    • Usage tracking (service/usage/mod.rs)
  • Correctly defaulted to None in appropriate places:

    • Alert handling
    • Trace requests
    • Metrics/Prometheus handling
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for structures and functions that should handle SearchEventContext
echo "Searching for related structures and their SearchEventContext field..."
rg -A 5 "struct.*Request" 
rg -A 5 "struct.*Response"
rg -A 5 "struct.*Stats"

echo "Searching for places where SearchEventContext is used..."
rg "search_event_context"

Length of output: 41735

src/handler/http/request/search/mod.rs (6)

42-43: LGTM: Import changes are well-organized.

The new import for get_search_event_context_from_request is properly placed alongside related HTTP utility functions.


154-157: LGTM: Search event context is properly initialized.

The implementation correctly sets the search event context based on the search type, using idiomatic Rust Option chaining.


477-477: LGTM: Around function properly handles search event context.

The initialization of search_event_context to None is appropriate for around searches.


908-908: LGTM: Values function properly handles search event context.

The initialization of search_event_context to None is appropriate for values searches.


1229-1229: LGTM: Values_v2 function properly handles search event context.

The initialization of search_event_context to None is appropriate for values_v2 searches.


42-43: Verify the implementation of get_search_event_context_from_request.

Let's verify that the get_search_event_context_from_request function properly handles all the new parameters mentioned in the PR objectives (dashboard_name, dashboard_folder, report_key, alert_key).

✅ Verification successful

Implementation of get_search_event_context_from_request is properly implemented

The function correctly handles all required parameters:

  • For dashboards: dashboard_name and optional dashboard_folder
  • For alerts: alert_key
  • For reports: report_key

The implementation matches the struct definition and uses appropriate constructor methods for each search event type. The function properly extracts query parameters and maps them to the corresponding SearchEventContext variants.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of get_search_event_context_from_request

# Test: Search for the function implementation
ast-grep --pattern $'fn get_search_event_context_from_request($_, $_) {
  $$$
}'

Length of output: 81


Script:

#!/bin/bash
# Let's try a different approach to find the implementation
# First, let's use ripgrep to find the function definition
rg -U "fn get_search_event_context_from_request" -A 20

# Also search for the struct or type definition of SearchEventContext
rg "struct SearchEventContext" -A 10

Length of output: 2316


Script:

#!/bin/bash
# Let's get the complete SearchEventContext struct definition and its methods
rg "struct SearchEventContext" -A 20

# Also search for the with_dashboard, with_alert, and with_report methods
rg "impl SearchEventContext" -A 30

Length of output: 3114

src/config/src/meta/search.rs (6)

63-63: LGTM!

The addition of the index_type field with a default value in the Request struct looks good.


442-443: LGTM!

Initializing search_event_context and index_type to default values in the to_query_req method is appropriate.


952-953: LGTM!

Including search_event_context and index_type in the to_query_req method enhances the request generation appropriately.


1038-1039: LGTM!

Setting search_event_context to None and initializing index_type with an empty string in the test is appropriate.


60-62: ⚠️ Potential issue

Verify the use of #[serde(flatten)] with an Option field.

Using #[serde(flatten)] on an Option<SearchEventContext> may not behave as intended when the option is None. Flattening optional fields can lead to serialization issues. Consider whether flattening is necessary or adjust the implementation accordingly.


875-878: ⚠️ Potential issue

Consider moving the #[serde(default)] attribute before the field declaration.

The #[serde(default)] attribute after the search_type field may be intended for search_event_context. Attributes should be placed immediately before their associated items. Please reposition it above pub search_event_context.

Apply this diff:

 #[serde(default)]
 #[serde(skip_serializing_if = "Option::is_none")]
 pub search_type: Option<SearchEventType>,
-#[serde(default)]
 #[serde(flatten)]
+#[serde(default)]
 #[serde(skip_serializing_if = "Option::is_none")]
 pub search_event_context: Option<SearchEventContext>,

Likely invalid or redundant comment.

src/service/alerts/alert.rs (2)

25-28: Imports added correctly

The new imports for SearchEventContext and SearchEventType are appropriate and necessary for the implemented functionality.


360-370: Enhanced evaluate method appropriately

The evaluate method now includes alert_key and passes SearchEventType and SearchEventContext correctly to evaluate_scheduled, enriching the alert evaluation with additional context.

@taimingl taimingl force-pushed the feat/search-event-ctx branch 3 times, most recently from 4a0770a to 624388f Compare October 30, 2024 12:06
@taimingl taimingl force-pushed the feat/search-event-ctx branch from 624388f to aea054e Compare October 30, 2024 18:10
@taimingl taimingl marked this pull request as ready for review October 30, 2024 19:20
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 14

🧹 Outside diff range and nitpick comments (11)
src/report_server/src/router.rs (2)

Line range hint 94-99: Consider adding org_id to cache success log

For consistency with error logging, consider including the org_id in the success log message as well.

-        log::info!("Dashboard data cached by report {report_name}");
+        log::info!("Dashboard data cached by report {org_id}/{report_name}");

Line range hint 116-122: Add context to email error logging

For consistency with report generation error logging, consider including the org_id and report_name in the email error log as well.

-            log::error!("Error sending emails to recipients: {e}");
+            log::error!("Error sending emails to recipients for report {org_id}/{report_name}: {e}");
src/report_server/src/report.rs (1)

Line range hint 139-148: Consider grouping parameters into a struct.

The function has 8 parameters now, which makes it harder to maintain and use. Consider grouping related parameters into a struct, such as:

struct ReportConfig<'a> {
    org_id: &'a str,
    user_id: &'a str,
    user_pass: &'a str,
    web_url: &'a str,
    timezone: &'a str,
    report_type: ReportType,
    report_name: &'a str,
}
web/src/components/dashboards/PanelContainer.vue (1)

328-328: Consider adding TypeScript type definition for the reportId prop

While the prop addition is correct, consider enhancing type safety by properly defining the prop type:

-    "reportId"
+    {
+      name: 'reportId',
+      type: String,
+      required: false
+    }

This would:

  • Provide better type safety
  • Enable IDE autocompletion
  • Make the code more maintainable
web/src/views/Dashboards/RenderDashboardCharts.vue (1)

214-214: Document the purpose of reportId in search context

The addition of reportId is part of enhancing search queries with context for usage reports, but this isn't clear from the code. Consider adding documentation to help other developers understand its purpose and relationship with search usage reporting.

Add JSDoc comments to explain the prop:

+    /**
+     * Unique identifier for the report, used to enhance search queries with additional
+     * context for usage reports. This enables tracking search events with their
+     * originating report context.
+     * @see PR #4934 for more details about search context enhancement
+     */
     reportId: {
src/service/alerts/mod.rs (1)

77-78: Consider documenting the new parameters

The new parameters enhance the function's capabilities, but their purpose and usage should be documented.

Add documentation for the new parameters:

+    /// Evaluates scheduled alerts
+    /// # Arguments
+    /// * `stream_param` - Stream parameters
+    /// * `trigger_condition` - Trigger condition for the alert
+    /// * `start_time` - Optional start time for evaluation
+    /// * `search_type` - Optional search event type for context
+    /// * `search_event_context` - Optional context for search events
     pub async fn evaluate_scheduled(
         &self,
         stream_param: &StreamParams,
         trigger_condition: &TriggerCondition,
         start_time: Option<i64>,
         search_type: Option<SearchEventType>,
         search_event_context: Option<SearchEventContext>,
     ) -> Result<(Option<Vec<Map<String, Value>>>, i64), anyhow::Error> {
web/src/components/dashboards/PanelSchemaRenderer.vue (1)

Line range hint 4-5: Consider adding JSDoc comments for the new reportId parameter.

To improve code documentation and maintainability, consider adding JSDoc comments explaining the purpose and usage of the reportId parameter in search usage reports.

+/**
+ * @prop {string} reportId - Unique identifier for tracking search usage in reports
+ */
src/config/src/meta/search.rs (2)

441-441: Consider adding a comment explaining the default None value.

Adding a brief comment would help explain why search_event_context is initialized as None in the to_query_req method.


1049-1049: Add test coverage for SearchEventContext.

The test case should be expanded to include scenarios with non-None values for search_event_context to ensure proper serialization and conversion behavior.

Consider adding tests like:

#[test]
fn test_search_with_context() {
    let context = SearchEventContext::with_dashboard(
        Some("dash-1".to_string()),
        Some("Dashboard 1".to_string()),
        None,
        None,
    );
    let req = Request {
        // ... other fields ...
        search_event_context: Some(context),
        // ... other fields ...
    };
    let rpc_req = cluster_rpc::SearchRequest::from(req.clone());
    // Assert context is properly converted
}
web/src/composables/dashboard/usePanelDataLoader.ts (1)

67-68: Consider adding proper TypeScript types for the new parameter.

The reportId parameter is typed as any. Consider adding a more specific type to improve type safety and code maintainability.

-  reportId: any
+  reportId: string | number | null
src/job/files/parquet.rs (1)

294-294: Consider extracting the split segment count as a constant.

The change from 5 to 9 segments correctly handles both log and trace file paths. However, to improve maintainability, consider extracting the magic number 9 into a named constant.

+const PATH_SEGMENT_COUNT: usize = 9;
+
-    let columns = prefix.splitn(9, '/').collect::<Vec<&str>>();
+    let columns = prefix.splitn(PATH_SEGMENT_COUNT, '/').collect::<Vec<&str>>();
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b8d4a9d and d1a5360.

📒 Files selected for processing (28)
  • src/cli/data/export.rs (3 hunks)
  • src/common/utils/http.rs (2 hunks)
  • src/config/src/meta/search.rs (6 hunks)
  • src/config/src/meta/usage.rs (5 hunks)
  • src/handler/http/request/search/mod.rs (6 hunks)
  • src/handler/http/request/search/multi_streams.rs (6 hunks)
  • src/handler/http/request/traces/mod.rs (1 hunks)
  • src/handler/http/router/openapi.rs (1 hunks)
  • src/job/files/parquet.rs (1 hunks)
  • src/report_server/src/report.rs (5 hunks)
  • src/report_server/src/router.rs (1 hunks)
  • src/service/alerts/alert.rs (2 hunks)
  • src/service/alerts/derived_streams.rs (4 hunks)
  • src/service/alerts/mod.rs (4 hunks)
  • src/service/alerts/scheduler.rs (1 hunks)
  • src/service/dashboards/reports.rs (5 hunks)
  • src/service/db/enrichment_table.rs (1 hunks)
  • src/service/metrics/prom.rs (2 hunks)
  • src/service/search/cache/mod.rs (1 hunks)
  • src/service/search/mod.rs (3 hunks)
  • src/service/usage/mod.rs (2 hunks)
  • src/service/usage/stats.rs (2 hunks)
  • web/src/components/dashboards/PanelContainer.vue (2 hunks)
  • web/src/components/dashboards/PanelSchemaRenderer.vue (3 hunks)
  • web/src/composables/dashboard/usePanelDataLoader.ts (5 hunks)
  • web/src/services/search.ts (2 hunks)
  • web/src/views/Dashboards/RenderDashboardCharts.vue (3 hunks)
  • web/src/views/Dashboards/ViewDashboard.vue (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (10)
  • src/cli/data/export.rs
  • src/config/src/meta/usage.rs
  • src/handler/http/request/search/mod.rs
  • src/handler/http/request/search/multi_streams.rs
  • src/handler/http/router/openapi.rs
  • src/service/db/enrichment_table.rs
  • src/service/metrics/prom.rs
  • src/service/search/cache/mod.rs
  • src/service/usage/mod.rs
  • src/service/usage/stats.rs
🧰 Additional context used
📓 Path-based instructions (12)
src/common/utils/http.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/config/src/meta/search.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/handler/http/request/traces/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/job/files/parquet.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/report_server/src/report.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/report_server/src/router.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/alert.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/derived_streams.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/scheduler.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/dashboards/reports.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/search/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

🔇 Additional comments (25)
src/report_server/src/router.rs (2)

82-82: LGTM: Report name parameter addition

The addition of &report_name to generate_report call enhances context tracking and aligns with the PR's objective of improving search usage reports.


Line range hint 86-91: Good error logging enhancement

Error logging now includes both org_id and report_name, providing better context for debugging issues during report generation.

web/src/services/search.ts (1)

27-28: LGTM: Parameter declarations follow TypeScript best practices.

The new optional parameters dashboard_id and folder_id are properly typed and align with the PR objectives to enhance search queries with additional context.

Also applies to: 34-35

src/service/alerts/derived_streams.rs (2)

21-21: LGTM: Clean import addition

The new imports for search event context align with the PR objectives.


124-128: LGTM: Clean implementation of trigger module key handling

The changes properly implement the trigger module key handling and follow best practices by passing references.

src/common/utils/http.rs (1)

26-29: LGTM: Clean import organization.

The new imports are properly organized within the existing meta module block.

src/report_server/src/report.rs (2)

224-226: LGTM! Clear and correct implementation.

The search type parameter logic properly handles both cache and report scenarios.


Line range hint 139-148: Verify all callers are updated with the new parameter.

Let's ensure all callers of generate_report have been updated with the new report_name parameter.

✅ Verification successful

Let me gather more context about the function definition and its callers by checking the complete function signatures.


All callers of generate_report are properly updated with the new parameter

The verification shows that all callers of generate_report are correctly passing the report_name parameter:

  • In src/service/dashboards/reports.rs: Passes &self.name
  • In src/report_server/src/router.rs: Passes &report_name

Both implementations match the function signature in src/report_server/src/report.rs which requires the report_name: &str parameter.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for generate_report function calls
rg -A 5 "generate_report\(" --type rust

Length of output: 1589


Script:

#!/bin/bash
# Get more context around generate_report function definitions and calls
rg -A 10 "generate_report\(" --type rust

Length of output: 2848

web/src/components/dashboards/PanelContainer.vue (1)

240-240: LGTM: Prop binding follows Vue.js conventions

The addition of :report-id prop binding to PanelSchemaRenderer follows Vue.js best practices and correctly implements the report context enhancement.

src/service/alerts/mod.rs (3)

22-22: LGTM: Import addition is appropriate

The addition of SearchEventContext and SearchEventType imports aligns with the new parameters being used.


264-265: LGTM: Consistent propagation of search context

The search context parameters are consistently propagated to both MultiStreamRequest and Request structs.

Also applies to: 322-323


77-78: Verify the impact on existing alert evaluations

The addition of optional parameters maintains backward compatibility, but we should verify that existing alerts continue to work as expected.

Also applies to: 264-265, 322-323

✅ Verification successful

Let me gather more context about the alert evaluation implementation to verify the impact.


Let me try a different approach to check the implementation.


Let me check how these parameters are used in the call sites.


Optional parameters in evaluate_scheduled are safely implemented

The verification shows that:

  1. The new parameters search_type and search_event_context are added as Option<T> types, maintaining backward compatibility
  2. Existing call sites in derived_streams.rs and alert.rs continue to work without these parameters
  3. The function implementation handles the optional parameters gracefully, focusing on core alert evaluation logic with stream_param and trigger_condition
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing alert evaluations to ensure they're not affected
rg -A 2 "evaluate_scheduled\(" --type rust

Length of output: 660


Script:

#!/bin/bash
# Check the implementation of evaluate_scheduled to understand parameter usage
ast-grep --pattern 'async fn evaluate_scheduled($$$)'

Length of output: 55


Script:

#!/bin/bash
# Check the implementation details around evaluate_scheduled
rg -B 5 -A 15 "pub async fn evaluate_scheduled" --type rust

Length of output: 1350


Script:

#!/bin/bash
# Check how evaluate_scheduled is called in the codebase
rg -B 3 -A 2 "\.evaluate_scheduled\(" --type rust

Length of output: 932

web/src/components/dashboards/PanelSchemaRenderer.vue (2)

231-235: LGTM: New prop declaration follows Vue.js best practices.

The reportId prop is correctly defined with appropriate type, default value, and required flag.


267-267: Verify the impact of reportId on panel data loading.

The reportId is correctly destructured from props and passed to usePanelDataLoader. However, we should verify that:

  1. The panel data loading behavior remains consistent when reportId is empty
  2. The search usage reports are correctly associated with the provided reportId

Also applies to: 287-288

✅ Verification successful

Based on the code analysis from the provided files, I can generate the final response:

The reportId parameter is safely handled in panel data loading.

The code review shows that:

  1. reportId is properly passed through to usePanelDataLoader as an optional prop with a default empty string value
  2. The panel data loading implementation in usePanelDataLoader.ts handles reportId safely as part of the search request parameters
  3. Empty reportId values do not affect the core panel data loading functionality
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usages of usePanelDataLoader to verify consistent parameter handling
ast-grep --pattern 'usePanelDataLoader($$$)'

# Search for reportId usage in related files
rg -l "reportId" --type ts --type vue

Length of output: 112


Script:

#!/bin/bash
# Search for usages of usePanelDataLoader with proper file types
rg -t js -t jsx -t ts -t tsx -t vue "usePanelDataLoader" -A 3 -B 3

# Search for reportId usage in related files with proper file types
rg -t js -t jsx -t ts -t tsx -t vue "reportId" -A 3 -B 3

# Find all Vue files that might contain panel data loading logic
fd -e vue -e ts -e js "Panel.*" 

Length of output: 863


Script:

#!/bin/bash
# Check the implementation of usePanelDataLoader
cat web/src/composables/dashboard/usePanelDataLoader.ts

# Check PanelSchemaRenderer.vue for reportId usage context
cat web/src/components/dashboards/PanelSchemaRenderer.vue

# Check how reportId is used in panel data loading
rg -t js -t ts -t vue "reportId.*panel" -A 3 -B 3

Length of output: 85927

web/src/views/Dashboards/ViewDashboard.vue (1)

213-213: Verify report ID propagation in the codebase

The prop binding syntax is correct, but we need to ensure that the report ID is being properly propagated throughout the application.

Let's verify the usage of report ID in the codebase:

Also applies to: 988-988

✅ Verification successful

Let me try a different approach to verify the report ID propagation.


Report ID propagation is properly implemented

The verification shows that the reportId prop is correctly propagated through the component hierarchy:

  • In ViewDashboard.vue, reportId is derived from route query parameter tab and passed down
  • RenderDashboardCharts.vue accepts and forwards the reportId prop
  • PanelContainer.vue and PanelSchemaRenderer.vue components properly handle the reportId prop
  • The prop is used in the data loading logic via usePanelDataLoader.ts
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for report ID usage in route queries and component props
echo "Searching for report query parameter usage..."
rg -A 3 "route\.query\.report"

echo "Searching for reportId prop in RenderDashboardCharts component..."
ast-grep --pattern 'const props = defineProps<{
  $$$
  reportId?: $_
  $$$
}>();'

Length of output: 359


Script:

#!/bin/bash
# Search for reportId in the codebase
echo "Searching for reportId usage..."
rg -A 3 "reportId"

# Search for prop definitions containing reportId
echo -e "\nSearching for components with reportId prop..."
ast-grep --pattern 'defineProps<{
  $$$
  reportId$_: $_
  $$$
}>'

# Search for report ID bindings in templates
echo -e "\nSearching for reportId bindings in templates..."
rg -A 3 ':reportId'

Length of output: 5687

src/service/alerts/alert.rs (1)

25-28: LGTM: Clean import organization

The new imports for SearchEventContext and SearchEventType are properly organized within the existing import structure.

src/config/src/meta/search.rs (2)

56-61: LGTM: Well-structured field additions to Request struct.

The new fields are properly implemented with appropriate serde attributes for optional fields.


886-891: LGTM: Consistent implementation with Request struct.

The changes maintain consistency with the Request struct implementation and properly handle ownership in the to_query_req method.

Also applies to: 963-963

src/service/alerts/scheduler.rs (1)

873-875: LGTM! The code follows Rust best practices.

The implementation correctly passes &trigger.module_key as a reference, which is memory efficient and follows Rust's ownership principles.

src/service/search/mod.rs (2)

340-340: LGTM: Correctly adds search event context to request stats.

The addition of search_event_context to RequestStats aligns with the PR objectives to enhance search queries with additional context for usage reports.


465-482: LGTM: Clean refactoring of search type handling.

The refactoring improves readability by:

  • Using pattern matching for cleaner control flow
  • Correctly determining report_usage based on search type
  • Properly maintaining search_event_context when needed
web/src/composables/dashboard/usePanelDataLoader.ts (1)

559-564: LGTM: Search context enhancement implemented correctly.

The addition of dashboard_id and folder_id to the search API calls is implemented consistently and aligns with the PR objectives to enhance search context for usage reports.

Also applies to: 767-772

src/service/dashboards/reports.rs (2)

355-355: Ensure self.name is valid when passing to generate_report

While adding &self.name to the generate_report function, ensure that self.name is validated and non-empty to prevent potential issues during report generation.


427-427: Addition of report_name parameter is appropriate

The inclusion of report_name in the generate_report function signature aligns with the enhanced functionality and usage consistency across the codebase.

src/handler/http/request/traces/mod.rs (1)

275-275: ⚠️ Potential issue

Implement search_event_context handling

The search_event_context field is set to None but is not utilized in the subsequent code. Based on the PR objectives, this field should capture dashboard, report, and alert context parameters. Please ensure that search_event_context is properly populated and integrated where necessary.

Would you like assistance in implementing the search_event_context handling as per the PR objectives?

taimingl and others added 3 commits October 30, 2024 12:30
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
src/service/usage/mod.rs (1)

465-466: LGTM: Memory optimization using Box

Good optimization to use Box<T> for enum variants, which reduces the enum's stack size. Consider adding a comment explaining this optimization.

Add a doc comment explaining the use of Box:

 #[derive(Debug)]
+/// Uses Box to reduce the enum's stack size as UsageData and TriggerData might be large structs
 enum UsageBuffer {
     Usage(Box<UsageData>),
     Trigger(Box<TriggerData>),
 }
src/service/metrics/prom.rs (2)

740-740: Add documentation for the new field.

The search_event_context field has been added to align with the PR objectives of enhancing search queries with additional context for usage reports. However, the field lacks documentation explaining its purpose and usage.

Add a doc comment explaining the field's purpose:

        search_type: None,
+       /// Context information for search usage reporting, including dashboard, report, and alert details
        search_event_context: None,
        index_type: "".to_string(),

886-886: Maintain consistency in field documentation.

The same field is added here but also lacks documentation. Let's maintain consistency across the codebase.

Add the same doc comment here:

        search_type: None,
+       /// Context information for search usage reporting, including dashboard, report, and alert details
        search_event_context: None,
        index_type: "".to_string(),
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e8d7b3e and ffe4696.

📒 Files selected for processing (9)
  • src/handler/http/request/search/multi_streams.rs (6 hunks)
  • src/handler/http/router/openapi.rs (1 hunks)
  • src/service/alerts/alert.rs (2 hunks)
  • src/service/alerts/derived_streams.rs (6 hunks)
  • src/service/alerts/mod.rs (5 hunks)
  • src/service/alerts/scheduler.rs (1 hunks)
  • src/service/metrics/prom.rs (2 hunks)
  • src/service/search/mod.rs (3 hunks)
  • src/service/usage/mod.rs (10 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/handler/http/request/search/multi_streams.rs
  • src/handler/http/router/openapi.rs
🧰 Additional context used
📓 Path-based instructions (7)
src/service/alerts/alert.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/derived_streams.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/alerts/scheduler.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/metrics/prom.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/search/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/service/usage/mod.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

🔇 Additional comments (15)
src/service/alerts/derived_streams.rs (5)

25-25: LGTM: Clean import addition

The new imports are properly organized within the existing meta imports group.


81-82: LGTM: Proper module key handling

The changes correctly generate and pass the trigger_module_key to evaluate(), maintaining proper ownership semantics by passing it as a reference.


94-94: LGTM: Consistent module key usage

The trigger creation correctly uses the generated module_key.


131-131: LGTM: Proper trait signature update

The trait signature correctly uses &str for module_key, following Rust best practices for string references.


156-159: Skip: Optimization suggestion already provided

A previous review already suggested optimizing the SearchEventContext creation by removing double Some wrapping and unnecessary string allocation.

src/service/usage/mod.rs (2)

141-141: LGTM: Search event context field added correctly

The search_event_context field is properly added to both UsageData instances, maintaining consistency in usage tracking.

Also applies to: 179-179


198-203: LGTM: Consistent usage of boxed variants

The boxed variants are correctly used across all sites, maintaining consistency in the implementation.

Also applies to: 222-222, 331-336, 352-357, 383-388, 416-421, 556-557

src/service/alerts/mod.rs (2)

24-24: LGTM: Import added correctly

The SearchEventContext import is properly placed with related imports from the search module.


286-287: LGTM: Search context parameters correctly propagated

The search_type and search_event_context parameters are consistently passed to both MultiStreamRequest and Request structs.

Also applies to: 337-338

src/service/metrics/prom.rs (1)

740-740: Verify the type of search_event_context across the codebase.

The new field uses Option<T> which is correct for nullable fields, but we should verify that the same type is used consistently across the codebase.

Also applies to: 886-886

✅ Verification successful

Based on the search results, I can generate the final response as we have comprehensive information about the usage of search_event_context across the codebase.

The type Option<SearchEventContext> is used consistently across the codebase

The field search_event_context is consistently defined as Option<SearchEventContext> in all struct definitions and handled appropriately in the codebase:

  • In config definitions (src/config/src/meta/search.rs, src/config/src/meta/usage.rs)
  • In service implementations (src/service/alerts/mod.rs, src/service/search/mod.rs)
  • In handlers (src/handler/http/request/search/mod.rs)

The new field's type is consistent with the existing pattern.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all occurrences of SearchEventContext to verify type consistency
rg -A 1 "search_event_context.*:" 

Length of output: 4384

src/service/alerts/scheduler.rs (1)

875-877: LGTM! Method signature change looks good.

The changes correctly pass references to avoid unnecessary cloning, which is a good practice in Rust.

Let's verify the method signature change across the codebase:

✅ Verification successful

Based on the gathered information, I can now provide a final response:

Method signature change is correctly implemented and consistent

The verification confirms:

  • The DerivedStreamExt trait in src/service/alerts/derived_streams.rs defines the correct method signature with module_key: &str
  • The implementation for DerivedStream follows this signature
  • The usage in scheduler.rs correctly obtains a derived_stream from pipeline.get_derived_stream() and calls the method with proper reference passing
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the evaluate method signature in DerivedStreamExt implementations
# Expect: All implementations should accept &str for module_key parameter

ast-grep --pattern 'impl $_ for $_ {
  $$$
  fn evaluate(&$_, $_, $module_key: &str) $$ {
    $$$
  }
  $$$
}'

Length of output: 112


Script:

#!/bin/bash
# Let's try a different approach to find evaluate method implementations
# First, find the trait definition
echo "=== Searching for trait definition ==="
rg "trait.*DerivedStream.*" -A 5

echo -e "\n=== Searching for evaluate method implementations ==="
# Search for evaluate method implementations
rg "fn evaluate.*&.*module_key" -A 3

Length of output: 750


Script:

#!/bin/bash
# Let's search for implementations of DerivedStreamExt
echo "=== Searching for DerivedStreamExt implementations ==="
rg "impl.*DerivedStreamExt.*for" -A 10

# Also search for the struct that's using evaluate
echo -e "\n=== Finding derived_stream type ==="
rg -B 5 "let \(ret, end_time\) = derived_stream" src/service/alerts/scheduler.rs

Length of output: 1272


Script:

#!/bin/bash
# Let's find the complete evaluate method implementation
echo "=== Finding evaluate method implementation ==="
rg "async fn evaluate.*module_key.*" -A 15 src/service/alerts/derived_streams.rs

# Let's also check the type declaration of derived_stream variable
echo -e "\n=== Finding derived_stream variable declaration ==="
rg "let.*derived_stream.*=" -B 5 src/service/alerts/scheduler.rs

Length of output: 659

src/service/search/mod.rs (2)

340-340: LGTM: Correctly adds search event context to RequestStats.

The addition of search_event_context to RequestStats is consistent with the PR objectives and properly handles ownership through cloning.


465-482: LGTM: Clean refactoring of search event context handling.

The refactoring improves code clarity by:

  • Using idiomatic Rust pattern matching
  • Combining related logic for search_type and search_event_context
  • Clearly defining when usage reporting should occur
src/service/alerts/alert.rs (2)

32-32: Approved: Added necessary imports for search context

The import of SearchEventContext and SearchEventType is appropriate and necessary for the new functionality.


389-390: Approved: Passing additional context to evaluate_scheduled

The inclusion of Some(SearchEventType::Alerts) and Some(search_event_ctx) correctly provides the necessary context for the scheduled evaluation.

Comment on lines +49 to +58
#[allow(clippy::too_many_arguments)]
async fn evaluate_scheduled(
&self,
org_id: &str,
stream_name: Option<&str>,
stream_type: StreamType,
trigger_condition: &TriggerCondition,
start_time: Option<i64>,
search_type: Option<SearchEventType>,
search_event_context: Option<SearchEventContext>,
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider grouping parameters into a dedicated struct

The method has 8 parameters which could make it harder to maintain. Consider creating a dedicated struct to group related parameters.

Example implementation:

pub struct EvaluateScheduledParams<'a> {
    pub org_id: &'a str,
    pub stream_name: Option<&'a str>,
    pub stream_type: StreamType,
    pub trigger_condition: &'a TriggerCondition,
    pub start_time: Option<i64>,
    pub search_type: Option<SearchEventType>,
    pub search_event_context: Option<SearchEventContext>,
}

Then update the method signature:

async fn evaluate_scheduled(
    &self,
    params: EvaluateScheduledParams<'_>,
) -> Result<(Option<Vec<Map<String, Value>>>, i64), anyhow::Error>;

Comment on lines +378 to +381
let search_event_ctx = SearchEventContext::with_alert(Some(format!(
"/alerts/{}/{}/{}/{}",
self.org_id, self.stream_type, self.stream_name, self.name
)));
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring alert path construction for consistency

Constructing the alert path inline may lead to inconsistencies across the codebase. Consider creating a helper function or using a constant to build the alert path, ensuring uniformity and easier maintenance.

@taimingl taimingl merged commit 499f2bf into main Oct 31, 2024
26 checks passed
@taimingl taimingl deleted the feat/search-event-ctx branch October 31, 2024 12:16
hengfeiyang added a commit that referenced this pull request Nov 1, 2024
impl #4914 

new parameters acceptable for search queries to add additional context
in usage report:
**Dashboard**
`dashboard_id` 
`dashboard_name` 
`dashboard_folder`
`dashboard_folder_id`
`dashboard_folder_name`

**Report**
`report_key`

**Alert**
`alert_key`

Co-authored-by: Sai Nikhil <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Hengfei Yang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants