Skip to content

Conversation

@ByteBaker
Copy link
Contributor

Add support for $__range variable that represents the dashboard's total time window selection. This complements existing variables like $__interval and $__rate_interval.

Variables added:

  • $__range: formatted time range (e.g., "1h", "24h", "7d")
  • $__range_s: time range in seconds
  • $__range_ms: time range in milliseconds

The variable dynamically updates when users change the dashboard time range and works with both PromQL and SQL queries.

Fixes #8914

@github-actions
Copy link
Contributor

Failed to generate code suggestions for PR

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

Greptile Summary

added three new dashboard query variables ($__range, $__range_s, $__range_ms) to represent the total time window selection, following the existing pattern used for $__interval and $__rate_interval

Critical Issue:

  • incorrect unit conversion: divides by 1000000 (microseconds→seconds) when timestamps are in milliseconds (needs division by 1000)
  • this will cause all $__range values to be 1000x smaller than expected (e.g., 7 days becomes ~10 minutes)

Other observations:

  • variable replacement logic follows existing patterns correctly
  • tests verify variable replacement works but don't validate calculated values
  • fallback to "1s" minimum for PromQL compatibility is reasonable

Confidence Score: 0/5

  • this PR has a critical unit conversion bug that breaks the core functionality
  • the incorrect division by 1000000 instead of 1000 causes all $__range calculations to be off by 1000x, making the feature completely non-functional (7-day range would appear as ~10 minutes)
  • web/src/composables/dashboard/usePanelDataLoader.ts requires immediate attention to fix unit conversion bug on line 1912

Important Files Changed

File Analysis

Filename Score Overview
web/src/composables/dashboard/usePanelDataLoader.ts 1/5 added $__range, $__range_s, and $__range_ms variables with incorrect unit conversion (divides by 1000000 instead of 1000)
web/src/composables/dashboard/usePanelDataLoader.spec.ts 4/5 added tests for $__range variable replacement in queries, but tests don't validate actual calculated values

Sequence Diagram

sequenceDiagram
    participant User
    participant Dashboard
    participant usePanelDataLoader
    participant replaceQueryValue
    participant API

    User->>Dashboard: Select time range
    Dashboard->>usePanelDataLoader: loadData()
    usePanelDataLoader->>usePanelDataLoader: Calculate startISOTimestamp/endISOTimestamp (milliseconds)
    usePanelDataLoader->>replaceQueryValue: replaceQueryValue(query, startISOTimestamp, endISOTimestamp)
    replaceQueryValue->>replaceQueryValue: Calculate __range_seconds from timestamps
    replaceQueryValue->>replaceQueryValue: Format __range using formatRateInterval()
    replaceQueryValue->>replaceQueryValue: Create $__range, $__range_s, $__range_ms variables
    replaceQueryValue->>replaceQueryValue: Replace variables in query string
    replaceQueryValue-->>usePanelDataLoader: Modified query with variables replaced
    usePanelDataLoader->>API: Execute query with replaced variables
    API-->>usePanelDataLoader: Query results
    usePanelDataLoader-->>Dashboard: Update panel data
Loading

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +1909 to +1912
// calculate range in seconds (total time range of the dashboard)
// Note: startISOTimestamp and endISOTimestamp are in microseconds (from API)
const __range_micros = endISOTimestamp - startISOTimestamp;
const __range_seconds = __range_micros / 1000000; // Convert microseconds to seconds
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: comment says "microseconds" but startISOTimestamp and endISOTimestamp are in milliseconds (from .getTime() on line 1288-1289). division should be by 1000, not 1000000

Suggested change
// calculate range in seconds (total time range of the dashboard)
// Note: startISOTimestamp and endISOTimestamp are in microseconds (from API)
const __range_micros = endISOTimestamp - startISOTimestamp;
const __range_seconds = __range_micros / 1000000; // Convert microseconds to seconds
// calculate range in seconds (total time range of the dashboard)
// Note: startISOTimestamp and endISOTimestamp are in milliseconds (from Date.getTime())
const __range_millis = endISOTimestamp - startISOTimestamp;
const __range_seconds = __range_millis / 1000; // Convert milliseconds to seconds
Prompt To Fix With AI
This is a comment left during a code review.
Path: web/src/composables/dashboard/usePanelDataLoader.ts
Line: 1909:1912

Comment:
**logic:** comment says "microseconds" but `startISOTimestamp` and `endISOTimestamp` are in milliseconds (from `.getTime()` on line 1288-1289). division should be by 1000, not 1000000

```suggestion
    // calculate range in seconds (total time range of the dashboard)
    // Note: startISOTimestamp and endISOTimestamp are in milliseconds (from Date.getTime())
    const __range_millis = endISOTimestamp - startISOTimestamp;
    const __range_seconds = __range_millis / 1000;  // Convert milliseconds to seconds
```

How can I resolve this? If you propose a fix, please make it concise.

@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: ByteBaker | Branch: fix/promql-range-variable | Commit: 17b6cd7

Testdino Test Results

Status Total Passed Failed Skipped Flaky Pass Rate Duration
All tests passed 366 346 0 19 1 95% 4m 38s

View Detailed Results

@ByteBaker ByteBaker force-pushed the fix/promql-range-variable branch from 17b6cd7 to 52a8f1b Compare October 29, 2025 23:46
@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: ByteBaker | Branch: fix/promql-range-variable | Commit: 52a8f1b

Testdino Test Results

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

View Detailed Results

@ByteBaker ByteBaker force-pushed the fix/promql-range-variable branch from 52a8f1b to 9ef60cb Compare October 30, 2025 12:13
@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: ByteBaker | Branch: fix/promql-range-variable | Commit: 9ef60cb

Testdino Test Results

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

View Detailed Results

Add support for $__range variable that represents the dashboard's total
time window selection. This complements existing variables like $__interval
and $__rate_interval.

Variables added:
- $__range: formatted time range (e.g., "1h", "24h", "7d")
- $__range_s: time range in seconds
- $__range_ms: time range in milliseconds

The variable dynamically updates when users change the dashboard time range
and works with both PromQL and SQL queries.

Fixes #8914
@ByteBaker ByteBaker force-pushed the fix/promql-range-variable branch from 9ef60cb to 6fa55cd Compare October 30, 2025 12:52
@testdino-playwright-reporter
Copy link

⚠️ Test Run Unstable


Author: ByteBaker | Branch: fix/promql-range-variable | Commit: 6fa55cd

Testdino Test Results

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

View Detailed Results

@ByteBaker ByteBaker merged commit eb9db85 into main Oct 30, 2025
32 checks passed
@ByteBaker ByteBaker deleted the fix/promql-range-variable branch October 30, 2025 13:17
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.

Add variable $__range for dashboard

3 participants