projectMetrics.tsx 7.9 KB

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