projectMetricsDetails.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import {Fragment, useCallback} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {LinkButton} from 'sentry/components/button';
  5. import MiniBarChart from 'sentry/components/charts/miniBarChart';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  7. import FieldGroup from 'sentry/components/forms/fieldGroup';
  8. import Panel from 'sentry/components/panels/panel';
  9. import PanelBody from 'sentry/components/panels/panelBody';
  10. import PanelHeader from 'sentry/components/panels/panelHeader';
  11. import {PanelTable} from 'sentry/components/panels/panelTable';
  12. import Placeholder from 'sentry/components/placeholder';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {
  18. MetricsOperation,
  19. MetricType,
  20. MRI,
  21. Organization,
  22. Project,
  23. } from 'sentry/types';
  24. import {getMetricsUrl} from 'sentry/utils/metrics';
  25. import {getReadableMetricType} from 'sentry/utils/metrics/formatters';
  26. import {formatMRI, formatMRIField, MRIToField, parseMRI} from 'sentry/utils/metrics/mri';
  27. import {MetricDisplayType} from 'sentry/utils/metrics/types';
  28. import {useBlockMetric} from 'sentry/utils/metrics/useBlockMetric';
  29. import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
  30. import {useMetricsTags} from 'sentry/utils/metrics/useMetricsTags';
  31. import routeTitleGen from 'sentry/utils/routeTitle';
  32. import {CodeLocations} from 'sentry/views/metrics/codeLocations';
  33. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  34. import {useAccess} from 'sentry/views/settings/projectMetrics/access';
  35. import {BlockButton} from 'sentry/views/settings/projectMetrics/blockButton';
  36. import {TextAlignRight} from 'sentry/views/starfish/components/textAlign';
  37. import {useProjectMetric} from '../../../utils/metrics/useMetricsMeta';
  38. function getSettingsOperationForType(type: MetricType): MetricsOperation {
  39. switch (type) {
  40. case 'c':
  41. return 'sum';
  42. case 's':
  43. return 'count_unique';
  44. case 'd':
  45. return 'count';
  46. case 'g':
  47. return 'count';
  48. default:
  49. return 'sum';
  50. }
  51. }
  52. type Props = {
  53. organization: Organization;
  54. project: Project;
  55. } & RouteComponentProps<{mri: MRI; projectId: string}, {}>;
  56. function ProjectMetricsDetails({project, params, organization}: Props) {
  57. const {mri} = params;
  58. const projectId = parseInt(project.id, 10);
  59. const projectIds = [projectId];
  60. const {
  61. data: {blockingStatus},
  62. } = useProjectMetric(mri, projectId);
  63. const {data: tagsData = []} = useMetricsTags(mri, {projects: projectIds}, false);
  64. const isBlockedMetric = blockingStatus?.isBlocked ?? false;
  65. const blockMetricMutation = useBlockMetric(project);
  66. const {hasAccess} = useAccess({access: ['project:write']});
  67. const {type, name, unit} = parseMRI(mri) ?? {};
  68. const operation = getSettingsOperationForType(type ?? 'c');
  69. const {data: metricsData, isLoading} = useMetricsQuery(
  70. [{mri, op: operation, name: 'query'}],
  71. {
  72. datetime: {
  73. period: '30d',
  74. start: '',
  75. end: '',
  76. utc: false,
  77. },
  78. environments: [],
  79. projects: projectIds,
  80. },
  81. {interval: '1d'}
  82. );
  83. const field = MRIToField(mri, operation);
  84. const series = [
  85. {
  86. seriesName: formatMRIField(field) ?? 'Metric',
  87. data:
  88. metricsData?.intervals.map((interval, index) => ({
  89. name: interval,
  90. value: metricsData.data[0]?.[0]?.series[index] ?? 0,
  91. })) ?? [],
  92. },
  93. ];
  94. const isChartEmpty = series[0].data.every(({value}) => value === 0);
  95. const handleMetricBlockToggle = useCallback(() => {
  96. const operationType = isBlockedMetric ? 'unblockMetric' : 'blockMetric';
  97. blockMetricMutation.mutate({operationType, mri});
  98. }, [blockMetricMutation, mri, isBlockedMetric]);
  99. const handleMetricTagBlockToggle = useCallback(
  100. (tag: string) => {
  101. const currentlyBlockedTags = blockingStatus?.blockedTags ?? [];
  102. const isBlockedTag = currentlyBlockedTags.includes(tag);
  103. const operationType = isBlockedTag ? 'unblockTags' : 'blockTags';
  104. blockMetricMutation.mutate({operationType, mri, tags: [tag]});
  105. },
  106. [blockMetricMutation, mri, blockingStatus?.blockedTags]
  107. );
  108. const tags = tagsData.sort((a, b) => a.key.localeCompare(b.key));
  109. return (
  110. <Fragment>
  111. <SentryDocumentTitle title={routeTitleGen(formatMRI(mri), project.slug, false)} />
  112. <SettingsPageHeader
  113. title={t('Metric Details')}
  114. action={
  115. <Controls>
  116. <BlockButton
  117. size="sm"
  118. hasAccess={hasAccess}
  119. disabled={blockMetricMutation.isLoading}
  120. isBlocked={isBlockedMetric}
  121. onConfirm={handleMetricBlockToggle}
  122. aria-label={t('Block Metric')}
  123. message={
  124. isBlockedMetric
  125. ? t('Are you sure you want to unblock this metric?')
  126. : t(
  127. '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.'
  128. )
  129. }
  130. />
  131. <LinkButton
  132. to={getMetricsUrl(organization.slug, {
  133. statsPeriod: '30d',
  134. project: [project.id],
  135. widgets: [
  136. {
  137. mri,
  138. displayType: MetricDisplayType.BAR,
  139. op: operation,
  140. query: '',
  141. groupBy: undefined,
  142. },
  143. ],
  144. })}
  145. size="sm"
  146. >
  147. {t('Open in Metrics')}
  148. </LinkButton>
  149. </Controls>
  150. }
  151. />
  152. <Panel>
  153. <PanelHeader>
  154. <Title>{t('Metric Details')}</Title>
  155. </PanelHeader>
  156. <PanelBody>
  157. <FieldGroup
  158. label={t('Name')}
  159. help={t('Name of the metric (invoked in your code).')}
  160. >
  161. <MetricName>
  162. <strong>{name}</strong>
  163. </MetricName>
  164. </FieldGroup>
  165. <FieldGroup
  166. label={t('Type')}
  167. help={t('Either counter, distribution, gauge, or set.')}
  168. >
  169. <div>{getReadableMetricType(type)}</div>
  170. </FieldGroup>
  171. <FieldGroup
  172. label={t('Unit')}
  173. help={t('Unit specified in the code - affects formatting.')}
  174. >
  175. <div>{unit}</div>
  176. </FieldGroup>
  177. </PanelBody>
  178. </Panel>
  179. <Panel>
  180. <PanelHeader>{t('Activity in the last 30 days (by day)')}</PanelHeader>
  181. <PanelBody withPadding>
  182. {isLoading && <Placeholder height="100px" />}
  183. {!isLoading && (
  184. <MiniBarChart
  185. series={series}
  186. colors={CHART_PALETTE[0]}
  187. height={100}
  188. isGroupedByDate
  189. stacked
  190. labelYAxisExtents
  191. />
  192. )}
  193. {!isLoading && isChartEmpty && (
  194. <EmptyMessage
  195. title={t('No activity.')}
  196. description={t("We don't have data for this metric in the last 30 days.")}
  197. />
  198. )}
  199. </PanelBody>
  200. </Panel>
  201. <PanelTable
  202. headers={[
  203. <TableHeading key="tags"> {t('Tags')}</TableHeading>,
  204. <TextAlignRight key="actions">
  205. <TableHeading> {t('Actions')}</TableHeading>
  206. </TextAlignRight>,
  207. ]}
  208. emptyMessage={t('There are no tags for this metric.')}
  209. isEmpty={tags.length === 0}
  210. isLoading={isLoading}
  211. >
  212. {tags.map(({key}) => {
  213. const isBlockedTag = blockingStatus?.blockedTags?.includes(key) ?? false;
  214. return (
  215. <Fragment key={key}>
  216. <div>{key}</div>
  217. <TextAlignRight>
  218. <BlockButton
  219. size="xs"
  220. hasAccess={hasAccess}
  221. disabled={blockMetricMutation.isLoading || isBlockedMetric}
  222. isBlocked={isBlockedTag}
  223. onConfirm={() => handleMetricTagBlockToggle(key)}
  224. aria-label={t('Block tag')}
  225. message={
  226. isBlockedTag
  227. ? t('Are you sure you want to unblock this tag?')
  228. : t(
  229. 'Are you sure you want to block this tag? It will no longer be ingested, and will not be available for use in Metrics, Alerts, or Dashboards.'
  230. )
  231. }
  232. />
  233. </TextAlignRight>
  234. </Fragment>
  235. );
  236. })}
  237. </PanelTable>
  238. <Panel>
  239. <PanelHeader>{t('Code Location')}</PanelHeader>
  240. <PanelBody withPadding>
  241. <CodeLocations mri={mri} />
  242. </PanelBody>
  243. </Panel>
  244. </Fragment>
  245. );
  246. }
  247. const TableHeading = styled('div')`
  248. color: ${p => p.theme.textColor};
  249. `;
  250. const MetricName = styled('div')`
  251. word-break: break-word;
  252. `;
  253. const Title = styled('div')`
  254. flex: 1;
  255. margin-right: ${space(1)};
  256. `;
  257. const Controls = styled('div')`
  258. display: grid;
  259. align-items: center;
  260. gap: ${space(1)};
  261. grid-auto-flow: column;
  262. `;
  263. export default ProjectMetricsDetails;