metricQueryContextMenu.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import {useMemo} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {openAddToDashboardModal} from 'sentry/actionCreators/modal';
  4. import {navigateTo} from 'sentry/actionCreators/navigation';
  5. import Feature from 'sentry/components/acl/feature';
  6. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  7. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  8. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  9. import {Hovercard} from 'sentry/components/hovercard';
  10. import {CreateMetricAlertFeature} from 'sentry/components/metrics/createMetricAlertFeature';
  11. import {
  12. IconClose,
  13. IconCopy,
  14. IconDashboard,
  15. IconEllipsis,
  16. IconSettings,
  17. IconSiren,
  18. } from 'sentry/icons';
  19. import {t} from 'sentry/locale';
  20. import type {Organization} from 'sentry/types/organization';
  21. import {trackAnalytics} from 'sentry/utils/analytics';
  22. import {isCustomMeasurement, isCustomMetric, isVirtualMetric} from 'sentry/utils/metrics';
  23. import {
  24. convertToDashboardWidget,
  25. encodeWidgetQuery,
  26. getWidgetAsQueryParams,
  27. getWidgetQuery,
  28. } from 'sentry/utils/metrics/dashboard';
  29. import {hasCustomMetrics, hasMetricAlertFeature} from 'sentry/utils/metrics/features';
  30. import {
  31. isMetricsQueryWidget,
  32. type MetricDisplayType,
  33. type MetricsQuery,
  34. } from 'sentry/utils/metrics/types';
  35. import {useVirtualMetricsContext} from 'sentry/utils/metrics/virtualMetricsContext';
  36. import useOrganization from 'sentry/utils/useOrganization';
  37. import usePageFilters from 'sentry/utils/usePageFilters';
  38. import useRouter from 'sentry/utils/useRouter';
  39. import {useMetricsContext} from 'sentry/views/metrics/context';
  40. import {openCreateAlertModal} from 'sentry/views/metrics/createAlertModal';
  41. type ContextMenuProps = {
  42. displayType: MetricDisplayType;
  43. metricsQuery: MetricsQuery;
  44. widgetIndex: number;
  45. };
  46. export function MetricQueryContextMenu({
  47. metricsQuery,
  48. displayType,
  49. widgetIndex,
  50. }: ContextMenuProps) {
  51. const organization = useOrganization();
  52. const router = useRouter();
  53. const {removeWidget, duplicateWidget, widgets} = useMetricsContext();
  54. const createAlert = getCreateAlert(organization, metricsQuery);
  55. const createDashboardWidget = useCreateDashboardWidget(
  56. organization,
  57. metricsQuery,
  58. displayType
  59. );
  60. // At least one query must remain
  61. const canDelete = widgets.filter(isMetricsQueryWidget).length > 1;
  62. const hasDashboardFeature = organization.features.includes('dashboards-edit');
  63. const items = useMemo<MenuItemProps[]>(() => {
  64. const duplicateItem = {
  65. leadingItems: [<IconCopy key="icon" />],
  66. key: 'duplicate',
  67. label: t('Duplicate'),
  68. onAction: () => {
  69. trackAnalytics('ddm.widget.duplicate', {
  70. organization,
  71. });
  72. Sentry.metrics.increment('ddm.widget.duplicate');
  73. duplicateWidget(widgetIndex);
  74. },
  75. };
  76. const createAlertItem = {
  77. leadingItems: [<IconSiren key="icon" />],
  78. key: 'add-alert',
  79. label: <CreateMetricAlertFeature>{t('Create Alert')}</CreateMetricAlertFeature>,
  80. disabled: !createAlert || !hasMetricAlertFeature(organization),
  81. onAction: () => {
  82. trackAnalytics('ddm.create-alert', {
  83. organization,
  84. source: 'widget',
  85. });
  86. Sentry.metrics.increment('ddm.widget.alert');
  87. createAlert?.();
  88. },
  89. };
  90. const addToDashboardItem = {
  91. leadingItems: [<IconDashboard key="icon" />],
  92. key: 'add-dashboard',
  93. label: (
  94. <Feature
  95. organization={organization}
  96. hookName="feature-disabled:dashboards-edit"
  97. features="dashboards-edit"
  98. renderDisabled={p => (
  99. <Hovercard
  100. body={
  101. <FeatureDisabled
  102. features={p.features}
  103. hideHelpToggle
  104. featureName={t('Metric Alerts')}
  105. />
  106. }
  107. >
  108. {typeof p.children === 'function' ? p.children(p) : p.children}
  109. </Hovercard>
  110. )}
  111. >
  112. <span>{t('Add to Dashboard')}</span>
  113. </Feature>
  114. ),
  115. disabled: !createDashboardWidget || !hasDashboardFeature,
  116. onAction: () => {
  117. if (!organization.features.includes('dashboards-edit')) {
  118. return;
  119. }
  120. trackAnalytics('ddm.add-to-dashboard', {
  121. organization,
  122. source: 'widget',
  123. });
  124. Sentry.metrics.increment('ddm.widget.dashboard');
  125. createDashboardWidget?.();
  126. },
  127. };
  128. const settingsItem = {
  129. leadingItems: [<IconSettings key="icon" />],
  130. key: 'settings',
  131. disabled: !isCustomMetric({mri: metricsQuery.mri}),
  132. label: t('Configure Metric'),
  133. onAction: () => {
  134. trackAnalytics('ddm.widget.settings', {
  135. organization,
  136. });
  137. Sentry.metrics.increment('ddm.widget.settings');
  138. if (!isVirtualMetric(metricsQuery)) {
  139. navigateTo(
  140. `/settings/${organization.slug}/projects/:projectId/metrics/${encodeURIComponent(
  141. metricsQuery.mri
  142. )}`,
  143. router
  144. );
  145. }
  146. },
  147. };
  148. const deleteItem = {
  149. leadingItems: [<IconClose key="icon" />],
  150. key: 'delete',
  151. label: t('Delete'),
  152. disabled: !canDelete,
  153. onAction: () => {
  154. trackAnalytics('ddm.widget.delete', {
  155. organization,
  156. });
  157. Sentry.metrics.increment('ddm.widget.delete');
  158. removeWidget(widgetIndex);
  159. },
  160. };
  161. if (hasCustomMetrics(organization)) {
  162. return [
  163. duplicateItem,
  164. createAlertItem,
  165. addToDashboardItem,
  166. settingsItem,
  167. deleteItem,
  168. ];
  169. }
  170. return [duplicateItem, settingsItem, deleteItem];
  171. }, [
  172. createAlert,
  173. organization,
  174. metricsQuery,
  175. createDashboardWidget,
  176. hasDashboardFeature,
  177. canDelete,
  178. duplicateWidget,
  179. widgetIndex,
  180. router,
  181. removeWidget,
  182. ]);
  183. return (
  184. <DropdownMenu
  185. items={items}
  186. triggerProps={{
  187. 'aria-label': t('Widget actions'),
  188. size: 'md',
  189. showChevron: false,
  190. icon: <IconEllipsis direction="down" size="sm" />,
  191. }}
  192. position="bottom-end"
  193. />
  194. );
  195. }
  196. function canCreateAlert(organization: Organization, metricsQuery: MetricsQuery) {
  197. return (
  198. organization.access.includes('alerts:write') &&
  199. metricsQuery.mri &&
  200. metricsQuery.aggregation &&
  201. !isCustomMeasurement(metricsQuery)
  202. );
  203. }
  204. export function getCreateAlert(organization: Organization, metricsQuery: MetricsQuery) {
  205. if (!canCreateAlert(organization, metricsQuery)) {
  206. return undefined;
  207. }
  208. return function () {
  209. openCreateAlertModal({metricsQuery: metricsQuery, organization});
  210. };
  211. }
  212. function useCreateDashboardWidget(
  213. organization: Organization,
  214. metricsQuery: MetricsQuery,
  215. displayType?: MetricDisplayType
  216. ) {
  217. const router = useRouter();
  218. const {resolveVirtualMRI} = useVirtualMetricsContext();
  219. const {selection} = usePageFilters();
  220. return useMemo(() => {
  221. if (
  222. !metricsQuery.mri ||
  223. !metricsQuery.aggregation ||
  224. isCustomMeasurement(metricsQuery)
  225. ) {
  226. return undefined;
  227. }
  228. const queryCopy = {...metricsQuery};
  229. if (isVirtualMetric(metricsQuery) && metricsQuery.condition) {
  230. const {mri, aggregation} = resolveVirtualMRI(
  231. metricsQuery.mri,
  232. metricsQuery.condition,
  233. metricsQuery.aggregation
  234. );
  235. queryCopy.mri = mri;
  236. queryCopy.aggregation = aggregation;
  237. }
  238. const widgetQuery = getWidgetQuery(queryCopy);
  239. const urlWidgetQuery = encodeWidgetQuery(widgetQuery);
  240. const widgetAsQueryParams = getWidgetAsQueryParams(
  241. selection,
  242. urlWidgetQuery,
  243. displayType
  244. );
  245. return () =>
  246. openAddToDashboardModal({
  247. organization,
  248. selection,
  249. widget: convertToDashboardWidget([queryCopy], displayType),
  250. router,
  251. widgetAsQueryParams,
  252. location: router.location,
  253. actions: ['add-and-open-dashboard', 'add-and-stay-on-current-page'],
  254. allowCreateNewDashboard: false,
  255. });
  256. }, [metricsQuery, selection, displayType, resolveVirtualMRI, organization, router]);
  257. }