projectMetricsDetails.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 {getDdmUrl} 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/ddm/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. />
  124. <LinkButton
  125. to={getDdmUrl(organization.slug, {
  126. statsPeriod: '30d',
  127. project: [project.id],
  128. widgets: [
  129. {
  130. mri,
  131. displayType: MetricDisplayType.BAR,
  132. op: operation,
  133. query: '',
  134. groupBy: undefined,
  135. },
  136. ],
  137. })}
  138. size="sm"
  139. >
  140. {t('Open in Metrics')}
  141. </LinkButton>
  142. </Controls>
  143. }
  144. />
  145. <Panel>
  146. <PanelHeader>
  147. <Title>{t('Metric Details')}</Title>
  148. </PanelHeader>
  149. <PanelBody>
  150. <FieldGroup
  151. label={t('Name')}
  152. help={t('Name of the metric (invoked in your code).')}
  153. >
  154. <MetricName>
  155. <strong>{name}</strong>
  156. </MetricName>
  157. </FieldGroup>
  158. <FieldGroup
  159. label={t('Type')}
  160. help={t('Either counter, distribution, gauge, or set.')}
  161. >
  162. <div>{getReadableMetricType(type)}</div>
  163. </FieldGroup>
  164. <FieldGroup
  165. label={t('Unit')}
  166. help={t('Unit specified in the code - affects formatting.')}
  167. >
  168. <div>{unit}</div>
  169. </FieldGroup>
  170. </PanelBody>
  171. </Panel>
  172. <Panel>
  173. <PanelHeader>{t('Activity in the last 30 days (by day)')}</PanelHeader>
  174. <PanelBody withPadding>
  175. {isLoading && <Placeholder height="100px" />}
  176. {!isLoading && (
  177. <MiniBarChart
  178. series={series}
  179. colors={CHART_PALETTE[0]}
  180. height={100}
  181. isGroupedByDate
  182. stacked
  183. labelYAxisExtents
  184. />
  185. )}
  186. {!isLoading && isChartEmpty && (
  187. <EmptyMessage
  188. title={t('No activity.')}
  189. description={t("We don't have data for this metric in the last 30 days.")}
  190. />
  191. )}
  192. </PanelBody>
  193. </Panel>
  194. <PanelTable
  195. headers={[
  196. <TableHeading key="tags"> {t('Tags')}</TableHeading>,
  197. <TextAlignRight key="actions">
  198. <TableHeading> {t('Actions')}</TableHeading>
  199. </TextAlignRight>,
  200. ]}
  201. emptyMessage={t('There are no tags for this metric.')}
  202. isEmpty={tags.length === 0}
  203. isLoading={isLoading}
  204. >
  205. {tags.map(({key}) => {
  206. const isBlockedTag = blockingStatus?.blockedTags?.includes(key) ?? false;
  207. return (
  208. <Fragment key={key}>
  209. <div>{key}</div>
  210. <TextAlignRight>
  211. <BlockButton
  212. size="xs"
  213. hasAccess={hasAccess}
  214. disabled={blockMetricMutation.isLoading || isBlockedMetric}
  215. isBlocked={isBlockedTag}
  216. onConfirm={() => handleMetricTagBlockToggle(key)}
  217. aria-label={t('Block tag')}
  218. message={
  219. isBlockedTag
  220. ? t('Are you sure you want to unblock this tag?')
  221. : t(
  222. '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.'
  223. )
  224. }
  225. />
  226. </TextAlignRight>
  227. </Fragment>
  228. );
  229. })}
  230. </PanelTable>
  231. <Panel>
  232. <PanelHeader>{t('Code Location')}</PanelHeader>
  233. <PanelBody withPadding>
  234. <CodeLocations mri={mri} />
  235. </PanelBody>
  236. </Panel>
  237. </Fragment>
  238. );
  239. }
  240. const TableHeading = styled('div')`
  241. color: ${p => p.theme.textColor};
  242. `;
  243. const MetricName = styled('div')`
  244. word-break: break-word;
  245. `;
  246. const Title = styled('div')`
  247. flex: 1;
  248. margin-right: ${space(1)};
  249. `;
  250. const Controls = styled('div')`
  251. display: grid;
  252. align-items: center;
  253. gap: ${space(1)};
  254. grid-auto-flow: column;
  255. `;
  256. export default ProjectMetricsDetails;