-
-
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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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
40 changes: 40 additions & 0 deletions
40
...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,40 @@ | ||
| import type {PageFilters} from 'sentry/types/core'; | ||
| import {getIntervalOptionsForPageFilter} from 'sentry/utils/useChartInterval'; | ||
| import {getHeatmapXAxisBucketInterval} from 'sentry/views/dashboards/widgets/heatMapWidget/utils/getHeatmapXAxisBucketInterval'; | ||
|
|
||
| function makeSelection(period: string): PageFilters { | ||
| return { | ||
| projects: [], | ||
| environments: [], | ||
| datetime: {period, start: null, end: null, utc: null}, | ||
| }; | ||
| } | ||
|
|
||
| function optionValues(period: string) { | ||
| return getIntervalOptionsForPageFilter(makeSelection(period).datetime).map( | ||
| option => option.value | ||
| ); | ||
| } | ||
|
|
||
| describe('getHeatmapXAxisBucketInterval()', () => { | ||
| it('falls back to the largest available interval when the width is 0', () => { | ||
| const options = optionValues('24h'); | ||
| expect(getHeatmapXAxisBucketInterval(makeSelection('24h'), 0)).toBe( | ||
| options[options.length - 1] | ||
| ); | ||
| }); | ||
|
|
||
| it('picks a larger interval as the container gets narrower', () => { | ||
| const options = optionValues('24h'); | ||
| const wide = getHeatmapXAxisBucketInterval(makeSelection('24h'), 1200); | ||
| const narrow = getHeatmapXAxisBucketInterval(makeSelection('24h'), 200); | ||
| // A narrower container fits fewer columns, so each bucket spans more time. | ||
| expect(options.indexOf(narrow)).toBeGreaterThanOrEqual(options.indexOf(wide)); | ||
| }); | ||
|
|
||
| it('only returns intervals available for the selection', () => { | ||
| const options = optionValues('24h'); | ||
| const result = getHeatmapXAxisBucketInterval(makeSelection('24h'), 724); | ||
| expect(options).toContain(result); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
| import {getDiffInMinutes} from 'sentry/components/charts/utils'; | ||
| import type {PageFilters} from 'sentry/types/core'; | ||
| import {millisecondsToClosestInterval} from 'sentry/utils/duration/millisecondsToInterval'; | ||
| import {getIntervalOptionsForPageFilter} from 'sentry/utils/useChartInterval'; | ||
| import {PIXELS_PER_BUCKET} from 'sentry/views/dashboards/widgets/heatMapWidget/settings'; | ||
|
|
||
| /** | ||
| * Computes the X-axis (time) bucket interval for the heatmap API. We target | ||
| * ~`PIXELS_PER_BUCKET`-wide columns for the given width and snap that to the | ||
| * closest interval the current date range supports. Falls back to the largest | ||
| * available interval (e.g. before the container has been measured). | ||
| */ | ||
| export function getHeatmapXAxisBucketInterval( | ||
| selection: PageFilters, | ||
| chartContainerWidth: number | ||
| ): string { | ||
| const intervalOptions = getIntervalOptionsForPageFilter(selection.datetime).map( | ||
| option => option.value | ||
| ); | ||
| const timeRangeInMs = getDiffInMinutes(selection.datetime) * 60 * 1000; | ||
| const msPerXBucket = Math.round( | ||
| timeRangeInMs / (chartContainerWidth / PIXELS_PER_BUCKET) | ||
| ); | ||
|
|
||
| return ( | ||
| millisecondsToClosestInterval(msPerXBucket, intervalOptions) ?? | ||
| intervalOptions[intervalOptions.length - 1] ?? | ||
| '' | ||
| ); | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
| import {getHeatmapYAxisBucketCount} from 'sentry/views/dashboards/widgets/heatMapWidget/utils/getHeatmapYAxisBucketCount'; | ||
|
|
||
| describe('getHeatmapYAxisBucketCount()', () => { | ||
| it('returns 0 when the container has no height', () => { | ||
| expect(getHeatmapYAxisBucketCount(0)).toBe(0); | ||
| }); | ||
|
|
||
| it('divides the container height by the target bucket size', () => { | ||
| // 360 / 10 = 36 | ||
| expect(getHeatmapYAxisBucketCount(360)).toBe(36); | ||
| }); | ||
|
|
||
| it('never returns fewer than 1 bucket when there is height', () => { | ||
| expect(getHeatmapYAxisBucketCount(5)).toBe(1); | ||
| }); | ||
| }); |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
| import {PIXELS_PER_BUCKET} from 'sentry/views/dashboards/widgets/heatMapWidget/settings'; | ||
|
|
||
| /** | ||
| * Computes the number of Y-axis buckets for the heatmap API. The X-axis | ||
| * interval already targets ~`PIXELS_PER_BUCKET`-wide columns, so we size the | ||
| * Y axis the same way — dividing the container height by that target — to keep | ||
| * cells roughly square. | ||
| */ | ||
| export function getHeatmapYAxisBucketCount(chartContainerHeight: number): number { | ||
| if (chartContainerHeight <= 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return Math.max(1, Math.round(chartContainerHeight / PIXELS_PER_BUCKET)); | ||
| } |
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.