Skip to content

Commit 3ab67c0

Browse files
DominikB2014billyvg
authored andcommitted
feat(insights): update alert behaviour for top 5 response code chart (#93607)
The top 5 response code chart now dynamically creates alert options based on the response. Before <img width="743" alt="image" src="https://github.com/user-attachments/assets/f7a05d25-ac1b-4fd5-866e-cdfa27623a60" /> After <img width="728" alt="image" src="https://github.com/user-attachments/assets/def468b9-dd87-48fc-bfba-16610152bb78" />
1 parent 3e82515 commit 3ab67c0

File tree

1 file changed

+73
-5
lines changed

1 file changed

+73
-5
lines changed

static/app/views/insights/http/components/charts/responseCodeCountChart.tsx

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import {t} from 'sentry/locale';
22
import {type MutableSearch} from 'sentry/utils/tokenizeSearch';
3+
import useOrganization from 'sentry/utils/useOrganization';
4+
import usePageFilters from 'sentry/utils/usePageFilters';
5+
import {Dataset} from 'sentry/views/alerts/rules/metric/types';
6+
import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
7+
import {getExploreUrl} from 'sentry/views/explore/utils';
8+
import {ChartType} from 'sentry/views/insights/common/components/chart';
9+
import {BaseChartActionDropdown} from 'sentry/views/insights/common/components/chartActionDropdown';
310
// TODO(release-drawer): Only used in httpSamplesPanel, should be easy to move data fetching in here
411
// eslint-disable-next-line no-restricted-imports
512
import {InsightsLineChartWidget} from 'sentry/views/insights/common/components/insightsLineChartWidget';
613
import type {DiscoverSeries} from 'sentry/views/insights/common/queries/useDiscoverSeries';
7-
import {type SpanFields} from 'sentry/views/insights/types';
14+
import {getAlertsUrl} from 'sentry/views/insights/common/utils/getAlertsUrl';
15+
import {useAlertsProject} from 'sentry/views/insights/common/utils/useAlertsProject';
16+
import {SpanFields} from 'sentry/views/insights/types';
817

918
interface Props {
1019
groupBy: SpanFields[];
@@ -23,13 +32,17 @@ export function ResponseCodeCountChart({
2332
groupBy,
2433
referrer,
2534
}: Props) {
35+
const organization = useOrganization();
36+
const project = useAlertsProject();
37+
const {selection} = usePageFilters();
38+
39+
const yAxis = 'count()';
40+
const title = t('Top 5 Response Codes');
41+
2642
// TODO: Temporary hack. `DiscoverSeries` meta field and the series name don't
2743
// match. This is annoying to work around, and will become irrelevant soon
2844
// enough. For now, just specify the correct meta for these series since
2945
// they're known and simple
30-
31-
const yAxis = 'count()';
32-
3346
const fieldAliases: Record<string, string> = {};
3447
const seriesWithMeta: DiscoverSeries[] = series.map(discoverSeries => {
3548
const newSeriesName = `${yAxis} ${discoverSeries.seriesName}`;
@@ -50,9 +63,60 @@ export function ResponseCodeCountChart({
5063
return transformedSeries;
5164
});
5265

66+
// TODO: kinda ugly, the series names have the format `count() 200` for 200 reponse codes
67+
const topResponseCodes = seriesWithMeta
68+
.map(s => s.seriesName.replace('count()', '').trim())
69+
.filter(isNumeric);
70+
const stringifiedSearch = search.formatString();
71+
72+
const queries = topResponseCodes.map(code => ({
73+
label: code,
74+
query: `${stringifiedSearch} ${SpanFields.RESPONSE_CODE}:${code}`,
75+
}));
76+
77+
queries.push({
78+
label: t('Other'),
79+
query: `${stringifiedSearch} !${SpanFields.RESPONSE_CODE}:[${topResponseCodes.join(',')}]`,
80+
});
81+
82+
const exploreUrl = getExploreUrl({
83+
organization,
84+
visualize: [
85+
{
86+
chartType: ChartType.LINE,
87+
yAxes: [yAxis],
88+
},
89+
],
90+
mode: Mode.AGGREGATE,
91+
title,
92+
query: search?.formatString(),
93+
sort: undefined,
94+
groupBy,
95+
});
96+
97+
const extraActions = [
98+
<BaseChartActionDropdown
99+
key="http response chart widget"
100+
exploreUrl={exploreUrl}
101+
referrer={referrer}
102+
alertMenuOptions={queries.map(query => ({
103+
key: query.label,
104+
label: query.label,
105+
to: getAlertsUrl({
106+
project,
107+
aggregate: yAxis,
108+
organization,
109+
pageFilters: selection,
110+
dataset: Dataset.EVENTS_ANALYTICS_PLATFORM,
111+
query: query.query,
112+
}),
113+
}))}
114+
/>,
115+
];
116+
53117
return (
54118
<InsightsLineChartWidget
55-
queryInfo={{search, groupBy, referrer}}
119+
extraActions={extraActions}
56120
title={t('Top 5 Response Codes')}
57121
series={seriesWithMeta}
58122
isLoading={isLoading}
@@ -61,3 +125,7 @@ export function ResponseCodeCountChart({
61125
/>
62126
);
63127
}
128+
129+
function isNumeric(maybeNumber: string) {
130+
return /^\d+$/.test(maybeNumber);
131+
}

0 commit comments

Comments
 (0)