|
| 1 | +import {Fragment} from 'react'; |
| 2 | +import {useTheme} from '@emotion/react'; |
| 3 | + |
| 4 | +import {openInsightChartModal} from 'sentry/actionCreators/modal'; |
| 5 | +import ExternalLink from 'sentry/components/links/externalLink'; |
| 6 | +import {t, tct} from 'sentry/locale'; |
| 7 | +import getDuration from 'sentry/utils/duration/getDuration'; |
| 8 | +import useOrganization from 'sentry/utils/useOrganization'; |
| 9 | +import {Line} from 'sentry/views/dashboards/widgets/timeSeriesWidget/plottables/line'; |
| 10 | +import {TimeSeriesWidgetVisualization} from 'sentry/views/dashboards/widgets/timeSeriesWidget/timeSeriesWidgetVisualization'; |
| 11 | +import {Widget} from 'sentry/views/dashboards/widgets/widget/widget'; |
| 12 | +import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode'; |
| 13 | +import {useCombinedQuery} from 'sentry/views/insights/agentMonitoring/hooks/useCombinedQuery'; |
| 14 | +import {ChartType} from 'sentry/views/insights/common/components/chart'; |
| 15 | +import {useEAPSpans} from 'sentry/views/insights/common/queries/useDiscover'; |
| 16 | +import {useTopNSpanEAPSeries} from 'sentry/views/insights/common/queries/useTopNDiscoverSeries'; |
| 17 | +import {convertSeriesToTimeseries} from 'sentry/views/insights/common/utils/convertSeriesToTimeseries'; |
| 18 | +import {usePageFilterChartParams} from 'sentry/views/insights/pages/platform/laravel/utils'; |
| 19 | +import {WidgetVisualizationStates} from 'sentry/views/insights/pages/platform/laravel/widgetVisualizationStates'; |
| 20 | +import { |
| 21 | + ModalChartContainer, |
| 22 | + ModalTableWrapper, |
| 23 | + SeriesColorIndicator, |
| 24 | + WidgetFooterTable, |
| 25 | +} from 'sentry/views/insights/pages/platform/shared/styles'; |
| 26 | +import {Toolbar} from 'sentry/views/insights/pages/platform/shared/toolbar'; |
| 27 | +import type {SpanStringFields} from 'sentry/views/insights/types'; |
| 28 | +import {GenericWidgetEmptyStateWarning} from 'sentry/views/performance/landing/widgets/components/selectableList'; |
| 29 | + |
| 30 | +interface GroupedDurationWidgetProps { |
| 31 | + groupBy: SpanStringFields; |
| 32 | + query: string; |
| 33 | + referrer: string; |
| 34 | + title: string; |
| 35 | +} |
| 36 | + |
| 37 | +export default function GroupedDurationWidget(props: GroupedDurationWidgetProps) { |
| 38 | + const organization = useOrganization(); |
| 39 | + const pageFilterChartParams = usePageFilterChartParams({ |
| 40 | + granularity: 'spans-low', |
| 41 | + }); |
| 42 | + |
| 43 | + const theme = useTheme(); |
| 44 | + const fullQuery = useCombinedQuery(props.query); |
| 45 | + |
| 46 | + const topEventsRequest = useEAPSpans( |
| 47 | + { |
| 48 | + fields: [props.groupBy, 'avg(span.duration)'], |
| 49 | + sorts: [{field: 'avg(span.duration)', kind: 'desc'}], |
| 50 | + search: fullQuery, |
| 51 | + limit: 3, |
| 52 | + }, |
| 53 | + props.referrer |
| 54 | + ); |
| 55 | + |
| 56 | + const timeSeriesRequest = useTopNSpanEAPSeries( |
| 57 | + { |
| 58 | + ...pageFilterChartParams, |
| 59 | + search: fullQuery, |
| 60 | + fields: [props.groupBy, 'avg(span.duration)'], |
| 61 | + yAxis: ['avg(span.duration)'], |
| 62 | + sort: {field: 'avg(span.duration)', kind: 'desc'}, |
| 63 | + topN: 3, |
| 64 | + enabled: !!topEventsRequest.data && topEventsRequest.data.length > 0, |
| 65 | + }, |
| 66 | + props.referrer |
| 67 | + ); |
| 68 | + |
| 69 | + const timeSeries = timeSeriesRequest.data; |
| 70 | + |
| 71 | + const isLoading = timeSeriesRequest.isLoading || topEventsRequest.isLoading; |
| 72 | + const error = timeSeriesRequest.error || topEventsRequest.error; |
| 73 | + |
| 74 | + const events = topEventsRequest.data; |
| 75 | + |
| 76 | + const hasData = events && events.length > 0 && timeSeries.length > 0; |
| 77 | + |
| 78 | + const colorPalette = theme.chart.getColorPalette(timeSeries.length - 1); |
| 79 | + |
| 80 | + const visualization = ( |
| 81 | + <WidgetVisualizationStates |
| 82 | + isEmpty={!hasData} |
| 83 | + isLoading={isLoading} |
| 84 | + error={error} |
| 85 | + emptyMessage={ |
| 86 | + <GenericWidgetEmptyStateWarning |
| 87 | + message={tct( |
| 88 | + 'No MCP spans found. Try updating your filters or learn more about MCP monitoring in our [link:documentation].', |
| 89 | + { |
| 90 | + link: <ExternalLink href="https://docs.sentry.io/product/insights/mcp/" />, |
| 91 | + } |
| 92 | + )} |
| 93 | + /> |
| 94 | + } |
| 95 | + VisualizationType={TimeSeriesWidgetVisualization} |
| 96 | + visualizationProps={{ |
| 97 | + showLegend: 'never', |
| 98 | + plottables: timeSeries.map( |
| 99 | + (ts, index) => |
| 100 | + new Line(convertSeriesToTimeseries(ts), { |
| 101 | + color: |
| 102 | + ts.seriesName === 'Other' ? theme.chart.neutral : colorPalette[index], |
| 103 | + alias: ts.seriesName, |
| 104 | + }) |
| 105 | + ), |
| 106 | + }} |
| 107 | + /> |
| 108 | + ); |
| 109 | + |
| 110 | + const footer = hasData && ( |
| 111 | + <WidgetFooterTable> |
| 112 | + {events?.map((item, index) => { |
| 113 | + const groupName = item[props.groupBy] ?? t('Other'); |
| 114 | + return ( |
| 115 | + <Fragment key={groupName}> |
| 116 | + <div> |
| 117 | + <SeriesColorIndicator |
| 118 | + style={{ |
| 119 | + backgroundColor: colorPalette[index], |
| 120 | + }} |
| 121 | + /> |
| 122 | + </div> |
| 123 | + <div>{groupName}</div> |
| 124 | + <span>{getDuration((item['avg(span.duration)'] ?? 0) / 1000, 2, true)}</span> |
| 125 | + </Fragment> |
| 126 | + ); |
| 127 | + })} |
| 128 | + </WidgetFooterTable> |
| 129 | + ); |
| 130 | + |
| 131 | + return ( |
| 132 | + <Widget |
| 133 | + Title={<Widget.WidgetTitle title={props.title} />} |
| 134 | + Visualization={visualization} |
| 135 | + Actions={ |
| 136 | + organization.features.includes('visibility-explore-view') && |
| 137 | + hasData && ( |
| 138 | + <Toolbar |
| 139 | + showCreateAlert |
| 140 | + referrer={props.referrer} |
| 141 | + exploreParams={{ |
| 142 | + mode: Mode.AGGREGATE, |
| 143 | + visualize: [ |
| 144 | + { |
| 145 | + chartType: ChartType.BAR, |
| 146 | + yAxes: ['avg(span.duration)'], |
| 147 | + }, |
| 148 | + ], |
| 149 | + groupBy: [props.groupBy], |
| 150 | + query: fullQuery, |
| 151 | + sort: `-avg(span.duration)`, |
| 152 | + interval: pageFilterChartParams.interval, |
| 153 | + }} |
| 154 | + onOpenFullScreen={() => { |
| 155 | + openInsightChartModal({ |
| 156 | + title: props.title, |
| 157 | + children: ( |
| 158 | + <Fragment> |
| 159 | + <ModalChartContainer>{visualization}</ModalChartContainer> |
| 160 | + <ModalTableWrapper>{footer}</ModalTableWrapper> |
| 161 | + </Fragment> |
| 162 | + ), |
| 163 | + }); |
| 164 | + }} |
| 165 | + /> |
| 166 | + ) |
| 167 | + } |
| 168 | + noFooterPadding |
| 169 | + Footer={footer} |
| 170 | + /> |
| 171 | + ); |
| 172 | +} |
0 commit comments