1
1
import { t } from 'sentry/locale' ;
2
2
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' ;
3
10
// TODO(release-drawer): Only used in httpSamplesPanel, should be easy to move data fetching in here
4
11
// eslint-disable-next-line no-restricted-imports
5
12
import { InsightsLineChartWidget } from 'sentry/views/insights/common/components/insightsLineChartWidget' ;
6
13
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' ;
8
17
9
18
interface Props {
10
19
groupBy : SpanFields [ ] ;
@@ -23,13 +32,17 @@ export function ResponseCodeCountChart({
23
32
groupBy,
24
33
referrer,
25
34
} : 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
+
26
42
// TODO: Temporary hack. `DiscoverSeries` meta field and the series name don't
27
43
// match. This is annoying to work around, and will become irrelevant soon
28
44
// enough. For now, just specify the correct meta for these series since
29
45
// they're known and simple
30
-
31
- const yAxis = 'count()' ;
32
-
33
46
const fieldAliases : Record < string , string > = { } ;
34
47
const seriesWithMeta : DiscoverSeries [ ] = series . map ( discoverSeries => {
35
48
const newSeriesName = `${ yAxis } ${ discoverSeries . seriesName } ` ;
@@ -50,9 +63,60 @@ export function ResponseCodeCountChart({
50
63
return transformedSeries ;
51
64
} ) ;
52
65
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
+
53
117
return (
54
118
< InsightsLineChartWidget
55
- queryInfo = { { search , groupBy , referrer } }
119
+ extraActions = { extraActions }
56
120
title = { t ( 'Top 5 Response Codes' ) }
57
121
series = { seriesWithMeta }
58
122
isLoading = { isLoading }
@@ -61,3 +125,7 @@ export function ResponseCodeCountChart({
61
125
/>
62
126
) ;
63
127
}
128
+
129
+ function isNumeric ( maybeNumber : string ) {
130
+ return / ^ \d + $ / . test ( maybeNumber ) ;
131
+ }
0 commit comments