metricWidgetCard.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import {Fragment, useMemo, useRef} from 'react';
  2. import type {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import ErrorPanel from 'sentry/components/charts/errorPanel';
  6. import {HeaderTitle} from 'sentry/components/charts/styles';
  7. import TransitionChart from 'sentry/components/charts/transitionChart';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import TextOverflow from 'sentry/components/textOverflow';
  10. import {IconSearch, IconWarning} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization, PageFilters} from 'sentry/types';
  14. import type {ReactEchartsRef} from 'sentry/types/echarts';
  15. import {getWidgetTitle} from 'sentry/utils/metrics';
  16. import type {MetricQueryWidgetParams} from 'sentry/utils/metrics/types';
  17. import {MetricDisplayType} from 'sentry/utils/metrics/types';
  18. import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
  19. import {WidgetCardPanel, WidgetTitleRow} from 'sentry/views/dashboards/widgetCard';
  20. import {DashboardsMEPContext} from 'sentry/views/dashboards/widgetCard/dashboardsMEPContext';
  21. import {Toolbar} from 'sentry/views/dashboards/widgetCard/toolbar';
  22. import WidgetCardContextMenu from 'sentry/views/dashboards/widgetCard/widgetCardContextMenu';
  23. import {MetricChart} from 'sentry/views/ddm/chart';
  24. import {createChartPalette} from 'sentry/views/ddm/utils/metricsChartPalette';
  25. import {getChartTimeseries} from 'sentry/views/ddm/widget';
  26. import {LoadingScreen} from 'sentry/views/starfish/components/chart';
  27. import {convertToMetricWidget} from '../../../utils/metrics/dashboard';
  28. import {DASHBOARD_CHART_GROUP} from '../dashboard';
  29. import type {DashboardFilters, Widget} from '../types';
  30. type Props = {
  31. isEditingDashboard: boolean;
  32. location: Location;
  33. organization: Organization;
  34. router: InjectedRouter;
  35. selection: PageFilters;
  36. widget: Widget;
  37. dashboardFilters?: DashboardFilters;
  38. index?: string;
  39. isMobile?: boolean;
  40. onDelete?: () => void;
  41. onDuplicate?: () => void;
  42. onEdit?: (index: string) => void;
  43. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  44. showContextMenu?: boolean;
  45. };
  46. export function MetricWidgetCard({
  47. organization,
  48. selection,
  49. widget,
  50. isEditingDashboard,
  51. onDelete,
  52. onDuplicate,
  53. location,
  54. router,
  55. dashboardFilters,
  56. renderErrorMessage,
  57. showContextMenu = true,
  58. }: Props) {
  59. const metricWidgetQueries = useMemo(() => convertToMetricWidget(widget), [widget]);
  60. const widgetMQL = useMemo(
  61. () => getWidgetTitle(metricWidgetQueries),
  62. [metricWidgetQueries]
  63. );
  64. return (
  65. <DashboardsMEPContext.Provider
  66. value={{
  67. isMetricsData: undefined,
  68. setIsMetricsData: () => {},
  69. }}
  70. >
  71. <WidgetCardPanel isDragging={false}>
  72. <WidgetHeaderWrapper>
  73. <WidgetHeaderDescription>
  74. <WidgetTitleRow>
  75. <WidgetTitle>
  76. <TextOverflow>{widget.title || widgetMQL}</TextOverflow>
  77. </WidgetTitle>
  78. </WidgetTitleRow>
  79. </WidgetHeaderDescription>
  80. <ContextMenuWrapper>
  81. {showContextMenu && !isEditingDashboard && (
  82. <WidgetCardContextMenu
  83. organization={organization}
  84. widget={widget}
  85. selection={selection}
  86. showContextMenu
  87. isPreview={false}
  88. widgetLimitReached={false}
  89. onEdit={() => {
  90. router.push({
  91. pathname: `${location.pathname}${
  92. location.pathname.endsWith('/') ? '' : '/'
  93. }widget/${widget.id}/`,
  94. query: location.query,
  95. });
  96. }}
  97. router={router}
  98. location={location}
  99. onDelete={onDelete}
  100. onDuplicate={onDuplicate}
  101. />
  102. )}
  103. </ContextMenuWrapper>
  104. </WidgetHeaderWrapper>
  105. <MetricWidgetChartContainer
  106. metricWidgetQueries={metricWidgetQueries}
  107. selection={selection}
  108. widget={widget}
  109. dashboardFilters={dashboardFilters}
  110. renderErrorMessage={renderErrorMessage}
  111. chartHeight={!showContextMenu ? 200 : undefined}
  112. />
  113. {isEditingDashboard && <Toolbar onDelete={onDelete} onDuplicate={onDuplicate} />}
  114. </WidgetCardPanel>
  115. </DashboardsMEPContext.Provider>
  116. );
  117. }
  118. type MetricWidgetChartContainerProps = {
  119. selection: PageFilters;
  120. widget: Widget;
  121. chartHeight?: number;
  122. dashboardFilters?: DashboardFilters;
  123. metricWidgetQueries?: MetricQueryWidgetParams[];
  124. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  125. };
  126. export function MetricWidgetChartContainer({
  127. selection,
  128. dashboardFilters,
  129. renderErrorMessage,
  130. metricWidgetQueries,
  131. widget,
  132. chartHeight,
  133. }: MetricWidgetChartContainerProps) {
  134. // TODO: Remove this and the widget prop once this component is no longer used in widgetViewerModal
  135. const metricQueries = metricWidgetQueries || convertToMetricWidget(widget);
  136. const chartQueries = useMemo(() => {
  137. return metricQueries.map(({mri, op, groupBy, query}) => {
  138. return {
  139. mri,
  140. op,
  141. query: extendQuery(query, dashboardFilters),
  142. groupBy,
  143. };
  144. });
  145. }, [metricQueries, dashboardFilters]);
  146. const displayType = metricQueries[0].displayType;
  147. const {
  148. data: timeseriesData,
  149. isLoading,
  150. isError,
  151. error,
  152. } = useMetricsQuery(chartQueries, selection, {
  153. intervalLadder: displayType === MetricDisplayType.BAR ? 'bar' : 'dashboard',
  154. });
  155. const chartRef = useRef<ReactEchartsRef>(null);
  156. const chartSeries = useMemo(() => {
  157. return timeseriesData
  158. ? getChartTimeseries(timeseriesData, chartQueries, {
  159. getChartPalette: createChartPalette,
  160. })
  161. : [];
  162. }, [timeseriesData, chartQueries]);
  163. if (isError) {
  164. const errorMessage =
  165. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  166. return (
  167. <Fragment>
  168. {renderErrorMessage?.(errorMessage)}
  169. <ErrorPanel>
  170. <IconWarning color="gray500" size="lg" />
  171. </ErrorPanel>
  172. </Fragment>
  173. );
  174. }
  175. if (timeseriesData?.data.length === 0) {
  176. return (
  177. <EmptyMessage
  178. icon={<IconSearch size="xxl" />}
  179. title={t('No results')}
  180. description={t('No results found for the given query')}
  181. />
  182. );
  183. }
  184. return (
  185. <MetricWidgetChartWrapper>
  186. <TransitionChart loading={isLoading} reloading={isLoading}>
  187. <LoadingScreen loading={isLoading} />
  188. <MetricChart
  189. ref={chartRef}
  190. series={chartSeries}
  191. displayType={displayType}
  192. widgetIndex={0}
  193. group={DASHBOARD_CHART_GROUP}
  194. height={chartHeight}
  195. />
  196. </TransitionChart>
  197. </MetricWidgetChartWrapper>
  198. );
  199. }
  200. function extendQuery(query = '', dashboardFilters?: DashboardFilters) {
  201. if (!dashboardFilters?.release?.length) {
  202. return query;
  203. }
  204. const releaseQuery = convertToQuery(dashboardFilters);
  205. return `${query} ${releaseQuery}`;
  206. }
  207. function convertToQuery(dashboardFilters: DashboardFilters) {
  208. const {release} = dashboardFilters;
  209. if (!release?.length) {
  210. return '';
  211. }
  212. if (release.length === 1) {
  213. return `release:${release[0]}`;
  214. }
  215. return `release:[${release.join(',')}]`;
  216. }
  217. const WidgetHeaderWrapper = styled('div')`
  218. min-height: 36px;
  219. width: 100%;
  220. display: flex;
  221. align-items: flex-start;
  222. justify-content: space-between;
  223. `;
  224. const ContextMenuWrapper = styled('div')`
  225. padding: ${space(2)} ${space(1)} 0 ${space(3)};
  226. `;
  227. const WidgetHeaderDescription = styled('div')`
  228. ${p => p.theme.overflowEllipsis};
  229. overflow-y: visible;
  230. `;
  231. const WidgetTitle = styled(HeaderTitle)`
  232. padding-left: ${space(3)};
  233. padding-top: ${space(2)};
  234. padding-right: ${space(1)};
  235. ${p => p.theme.overflowEllipsis};
  236. font-weight: normal;
  237. `;
  238. const MetricWidgetChartWrapper = styled('div')`
  239. height: 100%;
  240. width: 100%;
  241. padding: ${space(3)};
  242. padding-top: ${space(2)};
  243. `;