widgetCard.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {useMemo} from 'react';
  2. import type {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {ErrorBoundary} from '@sentry/react';
  5. import type {Location} from 'history';
  6. import ErrorPanel from 'sentry/components/charts/errorPanel';
  7. import {HeaderTitle} from 'sentry/components/charts/styles';
  8. import TextOverflow from 'sentry/components/textOverflow';
  9. import {IconWarning} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import type {PageFilters} from 'sentry/types/core';
  13. import type {Organization} from 'sentry/types/organization';
  14. import {parseMRI} from 'sentry/utils/metrics/mri';
  15. import {MetricExpressionType} from 'sentry/utils/metrics/types';
  16. import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
  17. import {useVirtualMetricsContext} from 'sentry/utils/metrics/virtualMetricsContext';
  18. import {MetricBigNumberContainer} from 'sentry/views/dashboards/metrics/bigNumber';
  19. import {MetricChartContainer} from 'sentry/views/dashboards/metrics/chart';
  20. import {MetricTableContainer} from 'sentry/views/dashboards/metrics/table';
  21. import {
  22. expressionsToApiQueries,
  23. getMetricExpressions,
  24. toMetricDisplayType,
  25. } from 'sentry/views/dashboards/metrics/utils';
  26. import type {DashboardFilters, Widget} from 'sentry/views/dashboards/types';
  27. import {DisplayType} from 'sentry/views/dashboards/types';
  28. import {WidgetCardPanel, WidgetTitleRow} from 'sentry/views/dashboards/widgetCard';
  29. import {DashboardsMEPContext} from 'sentry/views/dashboards/widgetCard/dashboardsMEPContext';
  30. import {Toolbar} from 'sentry/views/dashboards/widgetCard/toolbar';
  31. import WidgetCardContextMenu from 'sentry/views/dashboards/widgetCard/widgetCardContextMenu';
  32. import {useMetricsIntervalOptions} from 'sentry/views/metrics/utils/useMetricsIntervalParam';
  33. import {getWidgetTitle} from 'sentry/views/metrics/widget';
  34. type Props = {
  35. isEditingDashboard: boolean;
  36. location: Location;
  37. organization: Organization;
  38. router: InjectedRouter;
  39. selection: PageFilters;
  40. widget: Widget;
  41. dashboardFilters?: DashboardFilters;
  42. index?: string;
  43. isMobile?: boolean;
  44. onDelete?: () => void;
  45. onDuplicate?: () => void;
  46. onEdit?: (index: string) => void;
  47. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  48. showContextMenu?: boolean;
  49. };
  50. const EMPTY_FN = () => {};
  51. export function MetricWidgetCard({
  52. organization,
  53. selection,
  54. widget,
  55. isEditingDashboard,
  56. onDelete,
  57. onDuplicate,
  58. location,
  59. router,
  60. dashboardFilters,
  61. renderErrorMessage,
  62. showContextMenu = true,
  63. }: Props) {
  64. const {getVirtualMRIQuery, isLoading: isLoadingVirtualMetrics} =
  65. useVirtualMetricsContext();
  66. const metricQueries = useMemo(
  67. () =>
  68. expressionsToApiQueries(
  69. getMetricExpressions(widget, dashboardFilters, getVirtualMRIQuery)
  70. ),
  71. [widget, dashboardFilters, getVirtualMRIQuery]
  72. );
  73. const hasSetMetric = useMemo(
  74. () =>
  75. getMetricExpressions(widget, dashboardFilters, getVirtualMRIQuery).some(
  76. expression =>
  77. expression.type === MetricExpressionType.QUERY &&
  78. parseMRI(expression.mri)!.type === 's'
  79. ),
  80. [widget, dashboardFilters, getVirtualMRIQuery]
  81. );
  82. const widgetMQL = useMemo(
  83. () => (isLoadingVirtualMetrics ? '' : getWidgetTitle(metricQueries)),
  84. [isLoadingVirtualMetrics, metricQueries]
  85. );
  86. const {interval: validatedInterval} = useMetricsIntervalOptions({
  87. // TODO: Figure out why this can be undefined
  88. interval: widget.interval ?? '',
  89. hasSetMetric,
  90. datetime: selection.datetime,
  91. onIntervalChange: EMPTY_FN,
  92. });
  93. const {
  94. data: timeseriesData,
  95. isLoading,
  96. isError,
  97. error,
  98. } = useMetricsQuery(metricQueries, selection, {
  99. interval: validatedInterval,
  100. });
  101. const vizualizationComponent = useMemo(() => {
  102. if (widget.displayType === DisplayType.TABLE) {
  103. return (
  104. <MetricTableContainer
  105. metricQueries={metricQueries}
  106. timeseriesData={timeseriesData}
  107. isLoading={isLoading}
  108. />
  109. );
  110. }
  111. if (widget.displayType === DisplayType.BIG_NUMBER) {
  112. return (
  113. <MetricBigNumberContainer timeseriesData={timeseriesData} isLoading={isLoading} />
  114. );
  115. }
  116. return (
  117. <MetricChartContainer
  118. timeseriesData={timeseriesData}
  119. isLoading={isLoading}
  120. metricQueries={metricQueries}
  121. displayType={toMetricDisplayType(widget.displayType)}
  122. chartHeight={!showContextMenu ? 200 : undefined}
  123. showLegend
  124. />
  125. );
  126. }, [widget.displayType, metricQueries, timeseriesData, isLoading, showContextMenu]);
  127. return (
  128. <DashboardsMEPContext.Provider
  129. value={{
  130. isMetricsData: undefined,
  131. setIsMetricsData: () => {},
  132. }}
  133. >
  134. <WidgetCardPanel isDragging={false}>
  135. <WidgetHeaderWrapper>
  136. <WidgetHeaderDescription>
  137. <WidgetTitleRow>
  138. <WidgetTitle>
  139. <TextOverflow>{widget.title || widgetMQL}</TextOverflow>
  140. </WidgetTitle>
  141. </WidgetTitleRow>
  142. </WidgetHeaderDescription>
  143. <ContextMenuWrapper>
  144. {showContextMenu && !isEditingDashboard && (
  145. <WidgetCardContextMenu
  146. organization={organization}
  147. widget={widget}
  148. selection={selection}
  149. showContextMenu
  150. isPreview={false}
  151. widgetLimitReached={false}
  152. onEdit={() => {
  153. router.push({
  154. pathname: `${location.pathname}${
  155. location.pathname.endsWith('/') ? '' : '/'
  156. }widget/${widget.id}/`,
  157. query: location.query,
  158. });
  159. }}
  160. router={router}
  161. location={location}
  162. onDelete={onDelete}
  163. onDuplicate={onDuplicate}
  164. />
  165. )}
  166. </ContextMenuWrapper>
  167. </WidgetHeaderWrapper>
  168. <ErrorBoundary>
  169. <WidgetCardBody
  170. isError={isError}
  171. timeseriesData={timeseriesData}
  172. renderErrorMessage={renderErrorMessage}
  173. error={error}
  174. >
  175. {vizualizationComponent}
  176. </WidgetCardBody>
  177. </ErrorBoundary>
  178. {isEditingDashboard && <Toolbar onDelete={onDelete} onDuplicate={onDuplicate} />}
  179. </WidgetCardPanel>
  180. </DashboardsMEPContext.Provider>
  181. );
  182. }
  183. function WidgetCardBody({children, isError, timeseriesData, renderErrorMessage, error}) {
  184. if (isError && !timeseriesData) {
  185. const errorMessage =
  186. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  187. return (
  188. <ErrorWrapper>
  189. {renderErrorMessage?.(errorMessage)}
  190. <ErrorPanel>
  191. <IconWarning color="gray500" size="lg" />
  192. </ErrorPanel>
  193. </ErrorWrapper>
  194. );
  195. }
  196. return children;
  197. }
  198. const WidgetHeaderWrapper = styled('div')`
  199. min-height: 36px;
  200. width: 100%;
  201. display: flex;
  202. align-items: flex-start;
  203. justify-content: space-between;
  204. `;
  205. const ContextMenuWrapper = styled('div')`
  206. padding: ${space(2)} ${space(1)} 0 ${space(3)};
  207. `;
  208. const WidgetHeaderDescription = styled('div')`
  209. ${p => p.theme.overflowEllipsis};
  210. overflow-y: visible;
  211. `;
  212. const WidgetTitle = styled(HeaderTitle)`
  213. padding-left: ${space(3)};
  214. padding-top: ${space(2)};
  215. padding-right: ${space(1)};
  216. ${p => p.theme.overflowEllipsis};
  217. font-weight: ${p => p.theme.fontWeightNormal};
  218. `;
  219. const ErrorWrapper = styled('div')`
  220. padding-top: ${space(1)};
  221. `;