projectMetrics.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {Fragment, useMemo, useState} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {browserHistory} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import * as Sentry from '@sentry/react';
  6. import debounce from 'lodash/debounce';
  7. import {Button} from 'sentry/components/button';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import Link from 'sentry/components/links/link';
  10. import {PanelTable} from 'sentry/components/panels/panelTable';
  11. import SearchBar from 'sentry/components/searchBar';
  12. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  13. import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  14. import {Tag} from 'sentry/components/tag';
  15. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  16. import {t, tct} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import type {MetricMeta, Organization, Project} from 'sentry/types';
  19. import {METRICS_DOCS_URL} from 'sentry/utils/metrics/constants';
  20. import {getReadableMetricType} from 'sentry/utils/metrics/formatters';
  21. import {formatMRI} from 'sentry/utils/metrics/mri';
  22. import {useBlockMetric} from 'sentry/utils/metrics/useBlockMetric';
  23. import {useMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta';
  24. import {middleEllipsis} from 'sentry/utils/middleEllipsis';
  25. import {decodeScalar} from 'sentry/utils/queryString';
  26. import routeTitleGen from 'sentry/utils/routeTitle';
  27. import {useMetricsOnboardingSidebar} from 'sentry/views/ddm/ddmOnboarding/useMetricsOnboardingSidebar';
  28. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  29. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  30. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  31. import {useAccess} from 'sentry/views/settings/projectMetrics/access';
  32. import {BlockButton} from 'sentry/views/settings/projectMetrics/blockButton';
  33. type Props = {
  34. organization: Organization;
  35. project: Project;
  36. } & RouteComponentProps<{projectId: string}, {}>;
  37. enum BlockingStatusTab {
  38. ACTIVE = 'active',
  39. BLOCKED = 'blocked',
  40. }
  41. function ProjectMetrics({project, location}: Props) {
  42. const {data: meta, isLoading} = useMetricsMeta(
  43. {projects: [parseInt(project.id, 10)]},
  44. ['custom'],
  45. false
  46. );
  47. const query = decodeScalar(location.query.query, '').trim();
  48. const {activateSidebar} = useMetricsOnboardingSidebar();
  49. const [selectedTab, setSelectedTab] = useState(BlockingStatusTab.ACTIVE);
  50. const debouncedSearch = useMemo(
  51. () =>
  52. debounce(
  53. (searchQuery: string) =>
  54. browserHistory.replace({
  55. pathname: location.pathname,
  56. query: {...location.query, query: searchQuery},
  57. }),
  58. DEFAULT_DEBOUNCE_DURATION
  59. ),
  60. [location.pathname, location.query]
  61. );
  62. const metrics = meta.filter(
  63. ({mri, type, unit}) =>
  64. mri.includes(query) ||
  65. getReadableMetricType(type).includes(query) ||
  66. unit.includes(query)
  67. );
  68. return (
  69. <Fragment>
  70. <SentryDocumentTitle title={routeTitleGen(t('Metrics'), project.slug, false)} />
  71. <SettingsPageHeader
  72. title={t('Metrics')}
  73. action={
  74. <Button
  75. priority="primary"
  76. onClick={() => {
  77. Sentry.metrics.increment('ddm.add_custom_metric', 1, {
  78. tags: {
  79. referrer: 'settings',
  80. },
  81. });
  82. activateSidebar();
  83. }}
  84. size="sm"
  85. >
  86. {t('Add Metric')}
  87. </Button>
  88. }
  89. />
  90. <TextBlock>
  91. {tct(
  92. `Metrics are numerical values that can track anything about your environment over time, from latency to error rates to user signups. To learn more about metrics, [link:read the docs].`,
  93. {
  94. link: <ExternalLink href={METRICS_DOCS_URL} />,
  95. }
  96. )}
  97. </TextBlock>
  98. <PermissionAlert project={project} />
  99. <SearchWrapper>
  100. <SearchBar
  101. placeholder={t('Search Metrics')}
  102. onChange={debouncedSearch}
  103. query={query}
  104. />
  105. </SearchWrapper>
  106. <Tabs value={selectedTab} onChange={setSelectedTab}>
  107. <TabList>
  108. <TabList.Item key={BlockingStatusTab.ACTIVE}>{t('Active')}</TabList.Item>
  109. <TabList.Item key={BlockingStatusTab.BLOCKED}>{t('Blocked')}</TabList.Item>
  110. </TabList>
  111. <TabPanelsWrapper>
  112. <TabPanels.Item key={BlockingStatusTab.ACTIVE}>
  113. <MetricsTable
  114. metrics={metrics.filter(
  115. ({blockingStatus}) => !blockingStatus[0]?.isBlocked
  116. )}
  117. isLoading={isLoading}
  118. query={query}
  119. project={project}
  120. />
  121. </TabPanels.Item>
  122. <TabPanels.Item key={BlockingStatusTab.BLOCKED}>
  123. <MetricsTable
  124. metrics={metrics.filter(({blockingStatus}) => blockingStatus[0]?.isBlocked)}
  125. isLoading={isLoading}
  126. query={query}
  127. project={project}
  128. />
  129. </TabPanels.Item>
  130. </TabPanelsWrapper>
  131. </Tabs>
  132. </Fragment>
  133. );
  134. }
  135. interface MetricsTableProps {
  136. isLoading: boolean;
  137. metrics: MetricMeta[];
  138. project: Project;
  139. query: string;
  140. }
  141. function MetricsTable({metrics, isLoading, query, project}: MetricsTableProps) {
  142. const blockMetricMutation = useBlockMetric(project);
  143. const {hasAccess} = useAccess({access: ['project:write']});
  144. return (
  145. <StyledPanelTable
  146. headers={[
  147. t('Metric'),
  148. <Cell right key="type">
  149. {' '}
  150. {t('Type')}
  151. </Cell>,
  152. <Cell right key="unit">
  153. {t('Unit')}
  154. </Cell>,
  155. <Cell right key="actions">
  156. {t('Actions')}
  157. </Cell>,
  158. ]}
  159. emptyMessage={
  160. query
  161. ? t('No metrics match the query.')
  162. : t('There are no custom metrics to display.')
  163. }
  164. isEmpty={metrics.length === 0}
  165. isLoading={isLoading}
  166. >
  167. {metrics.map(({mri, type, unit, blockingStatus}) => {
  168. const isBlocked = blockingStatus[0]?.isBlocked;
  169. return (
  170. <Fragment key={mri}>
  171. <Cell>
  172. <Link
  173. to={`/settings/projects/${project.slug}/metrics/${encodeURIComponent(
  174. mri
  175. )}`}
  176. >
  177. {middleEllipsis(formatMRI(mri), 65, /\.|-|_/)}
  178. </Link>
  179. </Cell>
  180. <Cell right>
  181. <Tag>{getReadableMetricType(type)}</Tag>
  182. </Cell>
  183. <Cell right>
  184. <Tag>{unit}</Tag>
  185. </Cell>
  186. <Cell right>
  187. <BlockButton
  188. size="xs"
  189. hasAccess={hasAccess}
  190. disabled={blockMetricMutation.isLoading}
  191. isBlocked={isBlocked}
  192. aria-label={t('Block Metric')}
  193. message={
  194. isBlocked
  195. ? t('Are you sure you want to unblock this metric?')
  196. : t(
  197. 'Are you sure you want to block this metric? It will no longer be ingested, and will not be available for use in Metrics, Alerts, or Dashboards.'
  198. )
  199. }
  200. onConfirm={() => {
  201. blockMetricMutation.mutate({
  202. mri,
  203. operationType: isBlocked ? 'unblockMetric' : 'blockMetric',
  204. });
  205. }}
  206. />
  207. </Cell>
  208. </Fragment>
  209. );
  210. })}
  211. </StyledPanelTable>
  212. );
  213. }
  214. const TabPanelsWrapper = styled(TabPanels)`
  215. margin-top: ${space(2)};
  216. `;
  217. const SearchWrapper = styled('div')`
  218. margin-bottom: ${space(2)};
  219. `;
  220. const StyledPanelTable = styled(PanelTable)`
  221. grid-template-columns: 1fr repeat(3, minmax(115px, min-content));
  222. `;
  223. const Cell = styled('div')<{right?: boolean}>`
  224. display: flex;
  225. align-items: center;
  226. align-self: stretch;
  227. justify-content: ${p => (p.right ? 'flex-end' : 'flex-start')};
  228. `;
  229. export default ProjectMetrics;