-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(dashboards): Extract shared heat map chart utilities #117873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gggritso
merged 6 commits into
master
from
georgegritsouk/extract-shared-heat-map-utils
Jun 17, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b180cc
ref(dashboards): Extract shared heat map chart utilities
gggritso 6033c95
Update settings.ts
gggritso b6bf50d
ref(dashboards): Make heat map util extraction behavior-preserving
gggritso 1093b93
Merge remote-tracking branch 'origin/georgegritsouk/extract-shared-he…
gggritso fc64c6b
test(dashboards): Use PageFiltersFixture in heat map bucket specs
gggritso 980e909
Merge branch 'master' into georgegritsouk/extract-shared-heat-map-utils
gggritso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...c/app/views/dashboards/widgets/heatMapWidget/utils/getHeatmapXAxisBucketInterval.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import {PageFiltersFixture} from 'sentry-fixture/pageFilters'; | ||
|
|
||
| import {getHeatmapXAxisBucketInterval} from 'sentry/views/dashboards/widgets/heatMapWidget/utils/getHeatmapXAxisBucketInterval'; | ||
|
|
||
| function makeSelection(period: string) { | ||
| return PageFiltersFixture({datetime: {period, start: null, end: null, utc: null}}); | ||
| } | ||
|
|
||
| const INTERVAL_OPTIONS = [ | ||
| {value: '1m', label: '1 minute'}, | ||
| {value: '5m', label: '5 minutes'}, | ||
| {value: '1h', label: '1 hour'}, | ||
| {value: '12h', label: '12 hours'}, | ||
| {value: '1d', label: '1 day'}, | ||
| ]; | ||
|
|
||
| describe('getHeatmapXAxisBucketInterval()', () => { | ||
| it('snaps to a wider interval as the container gets narrower', () => { | ||
| const values = INTERVAL_OPTIONS.map(option => option.value); | ||
| const wide = getHeatmapXAxisBucketInterval( | ||
| makeSelection('24h'), | ||
| '1h', | ||
| 1200, | ||
| INTERVAL_OPTIONS | ||
| ); | ||
| const narrow = getHeatmapXAxisBucketInterval( | ||
| makeSelection('24h'), | ||
| '1h', | ||
| 200, | ||
| INTERVAL_OPTIONS | ||
| ); | ||
| // A narrower container fits fewer columns, so each bucket spans more time. | ||
| expect(values.indexOf(narrow)).toBeGreaterThanOrEqual(values.indexOf(wide)); | ||
| }); | ||
|
|
||
| it('only returns one of the provided interval options', () => { | ||
| const result = getHeatmapXAxisBucketInterval( | ||
| makeSelection('24h'), | ||
| '1h', | ||
| 724, | ||
| INTERVAL_OPTIONS | ||
| ); | ||
| expect(INTERVAL_OPTIONS.map(option => option.value)).toContain(result); | ||
| }); | ||
|
|
||
| it('falls back to the given interval before the container has been measured', () => { | ||
| // Width 0 yields no finite bucket size, so there is nothing to snap to and | ||
| // the currently selected interval is kept. | ||
| expect( | ||
| getHeatmapXAxisBucketInterval(makeSelection('24h'), '1h', 0, INTERVAL_OPTIONS) | ||
| ).toBe('1h'); | ||
| }); | ||
| }); |
26 changes: 26 additions & 0 deletions
26
static/app/views/dashboards/widgets/heatMapWidget/utils/getHeatmapXAxisBucketInterval.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import {getDiffInMinutes} from 'sentry/components/charts/utils'; | ||
| import type {PageFilters} from 'sentry/types/core'; | ||
| import {millisecondsToClosestInterval} from 'sentry/utils/duration/millisecondsToInterval'; | ||
| import {PIXELS_PER_X_BUCKET} from 'sentry/views/dashboards/widgets/heatMapWidget/settings'; | ||
|
|
||
| /** | ||
| * Computes the X-axis bucket interval for the heatmap API. | ||
| * The X-axis bucket interval is derived from the container width and the number of | ||
| * pixels per X bucket. | ||
| */ | ||
| export function getHeatmapXAxisBucketInterval( | ||
| selection: PageFilters, | ||
| interval: string, | ||
| chartContainerWidth: number, | ||
| intervalOptions: Array<{label: string; value: string}> | ||
| ): string { | ||
| const timeRangeInMs = getDiffInMinutes(selection.datetime) * 60 * 1000; | ||
| const msPerXBucket = Math.round( | ||
| timeRangeInMs / (chartContainerWidth / PIXELS_PER_X_BUCKET) | ||
| ); | ||
| const xBucketInterval = millisecondsToClosestInterval( | ||
| msPerXBucket, | ||
| intervalOptions.map(option => option.value) | ||
| ); | ||
| return xBucketInterval || interval; | ||
| } |
29 changes: 29 additions & 0 deletions
29
static/app/views/dashboards/widgets/heatMapWidget/utils/getHeatmapYAxisBucketCount.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import {PageFiltersFixture} from 'sentry-fixture/pageFilters'; | ||
|
|
||
| import {getHeatmapYAxisBucketCount} from 'sentry/views/dashboards/widgets/heatMapWidget/utils/getHeatmapYAxisBucketCount'; | ||
|
|
||
| function makeSelection(period: string) { | ||
| return PageFiltersFixture({datetime: {period, start: null, end: null, utc: null}}); | ||
| } | ||
|
|
||
| describe('getHeatmapYAxisBucketCount()', () => { | ||
| it('returns 0 before the container has been measured', () => { | ||
| expect(getHeatmapYAxisBucketCount(makeSelection('24h'), '1h', 0)).toBe(0); | ||
| }); | ||
|
|
||
| it('returns 0 for a non-positive interval', () => { | ||
| expect(getHeatmapYAxisBucketCount(makeSelection('24h'), '0', 800)).toBe(0); | ||
| }); | ||
|
|
||
| it('returns a positive bucket count once the container is measured', () => { | ||
| expect( | ||
| getHeatmapYAxisBucketCount(makeSelection('24h'), '1h', 800) | ||
| ).toBeGreaterThanOrEqual(1); | ||
| }); | ||
|
|
||
| it('fits fewer Y buckets into a wider container', () => { | ||
| const narrow = getHeatmapYAxisBucketCount(makeSelection('24h'), '1h', 400); | ||
| const wide = getHeatmapYAxisBucketCount(makeSelection('24h'), '1h', 1600); | ||
| expect(wide).toBeLessThanOrEqual(narrow); | ||
| }); | ||
| }); |
28 changes: 28 additions & 0 deletions
28
static/app/views/dashboards/widgets/heatMapWidget/utils/getHeatmapYAxisBucketCount.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import {getDiffInMinutes} from 'sentry/components/charts/utils'; | ||
| import type {PageFilters} from 'sentry/types/core'; | ||
| import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds'; | ||
| import {STACKED_GRAPH_HEIGHT} from 'sentry/views/explore/metrics/settings'; | ||
|
|
||
| /** | ||
| * Computes the number of Y-axis buckets for the heatmap API so that cells | ||
| * are roughly square. The X-axis bucket count comes from the time range | ||
| * divided by the selected interval. We derive Y buckets by scaling | ||
| * xBuckets by the container's height/width aspect ratio. | ||
| */ | ||
| export function getHeatmapYAxisBucketCount( | ||
| selection: PageFilters, | ||
| interval: string, | ||
| chartContainerWidth: number | ||
| ): number { | ||
| const timeRangeInMs = getDiffInMinutes(selection.datetime) * 60 * 1000; | ||
| const intervalInMs = intervalToMilliseconds(interval); | ||
| if (intervalInMs <= 0 || chartContainerWidth <= 0) { | ||
| return 0; | ||
| } | ||
| const xBuckets = Math.round(timeRangeInMs / intervalInMs); | ||
| if (xBuckets <= 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return Math.max(1, Math.round(xBuckets * (STACKED_GRAPH_HEIGHT / chartContainerWidth))); | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
static/app/views/dashboards/widgets/heatMapWidget/utils/mergeMetricUnit.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type {DataUnit} from 'sentry/utils/discover/fields'; | ||
| import type {HeatMapSeries} from 'sentry/views/dashboards/widgets/common/types'; | ||
| import {mapMetricUnitToFieldType} from 'sentry/views/explore/metrics/utils'; | ||
|
|
||
| /** | ||
| * The heatmap API returns the Y axis using the generic `value` field, so the | ||
| * response meta carries no metric unit. Patch the Y axis with the selected | ||
| * metric's unit/type so the chart formats values correctly (e.g. durations, | ||
| * sizes) instead of rendering raw numbers. | ||
| * | ||
| * Ideally the backend would return the metric's unit in the response meta and | ||
| * this client-side patch wouldn't be needed. | ||
| */ | ||
| export function mergeMetricUnit( | ||
| series: HeatMapSeries, | ||
| metricUnit: string | undefined | ||
| ): HeatMapSeries { | ||
| const {fieldType, unit} = mapMetricUnitToFieldType(metricUnit); | ||
| if (!unit) { | ||
| return series; | ||
| } | ||
| return { | ||
| ...series, | ||
| meta: { | ||
| ...series.meta, | ||
| yAxis: { | ||
| ...series.meta.yAxis, | ||
| valueType: fieldType, | ||
| valueUnit: unit as DataUnit, | ||
| }, | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.