projectMetricsDetails.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 {useMetricsData} from 'sentry/utils/metrics/useMetricsData';
  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} = useMetricsData(
  70. {
  71. datetime: {
  72. period: '30d',
  73. start: '',
  74. end: '',
  75. utc: false,
  76. },
  77. environments: [],
  78. mri,
  79. projects: projectIds,
  80. op: operation,
  81. },
  82. {interval: '1d'}
  83. );
  84. const field = MRIToField(mri, operation);
  85. const series = [
  86. {
  87. seriesName: formatMRIField(field) ?? 'Metric',
  88. data:
  89. metricsData?.intervals.map((interval, index) => ({
  90. name: interval,
  91. value: metricsData.groups[0]?.series[field][index] ?? 0,
  92. })) ?? [],
  93. },
  94. ];
  95. const isChartEmpty = series[0].data.every(({value}) => value === 0);
  96. const handleMetricBlockToggle = useCallback(() => {
  97. const operationType = isBlockedMetric ? 'unblockMetric' : 'blockMetric';
  98. blockMetricMutation.mutate({operationType, mri});
  99. }, [blockMetricMutation, mri, isBlockedMetric]);
  100. const handleMetricTagBlockToggle = useCallback(
  101. (tag: string) => {
  102. const currentlyBlockedTags = blockingStatus?.blockedTags ?? [];
  103. const isBlockedTag = currentlyBlockedTags.includes(tag);
  104. const operationType = isBlockedTag ? 'unblockTags' : 'blockTags';
  105. blockMetricMutation.mutate({operationType, mri, tags: [tag]});
  106. },
  107. [blockMetricMutation, mri, blockingStatus?.blockedTags]
  108. );
  109. const tags = tagsData.sort((a, b) => a.key.localeCompare(b.key));
  110. return (
  111. <Fragment>
  112. <SentryDocumentTitle title={routeTitleGen(formatMRI(mri), project.slug, false)} />
  113. <SettingsPageHeader
  114. title={t('Metric Details')}
  115. action={
  116. <Controls>
  117. <BlockButton
  118. size="sm"
  119. hasAccess={hasAccess}
  120. disabled={blockMetricMutation.isLoading}
  121. isBlocked={isBlockedMetric}
  122. onConfirm={handleMetricBlockToggle}
  123. aria-label={t('Block Metric')}
  124. />
  125. <LinkButton
  126. to={getDdmUrl(organization.slug, {
  127. statsPeriod: '30d',
  128. project: [project.id],
  129. widgets: [
  130. {
  131. mri,
  132. displayType: MetricDisplayType.BAR,
  133. op: operation,
  134. query: '',
  135. groupBy: undefined,
  136. },
  137. ],
  138. })}
  139. size="sm"
  140. >
  141. {t('Open in Metrics')}
  142. </LinkButton>
  143. </Controls>
  144. }
  145. />
  146. <Panel>
  147. <PanelHeader>
  148. <Title>{t('Metric Details')}</Title>
  149. </PanelHeader>
  150. <PanelBody>
  151. <FieldGroup
  152. label={t('Name')}
  153. help={t('Name of the metric (invoked in your code).')}
  154. >
  155. <MetricName>
  156. <strong>{name}</strong>
  157. </MetricName>
  158. </FieldGroup>
  159. <FieldGroup
  160. label={t('Type')}
  161. help={t('Either counter, distribution, gauge, or set.')}
  162. >
  163. <div>{getReadableMetricType(type)}</div>
  164. </FieldGroup>
  165. <FieldGroup
  166. label={t('Unit')}
  167. help={t('Unit specified in the code - affects formatting.')}
  168. >
  169. <div>{unit}</div>
  170. </FieldGroup>
  171. </PanelBody>
  172. </Panel>
  173. <Panel>
  174. <PanelHeader>{t('Activity in the last 30 days (by day)')}</PanelHeader>
  175. <PanelBody withPadding>
  176. {isLoading && <Placeholder height="100px" />}
  177. {!isLoading && (
  178. <MiniBarChart
  179. series={series}
  180. colors={CHART_PALETTE[0]}
  181. height={100}
  182. isGroupedByDate
  183. stacked
  184. labelYAxisExtents
  185. />
  186. )}
  187. {!isLoading && isChartEmpty && (
  188. <EmptyMessage
  189. title={t('No activity.')}
  190. description={t("We don't have data for this metric in the last 30 days.")}
  191. />
  192. )}
  193. </PanelBody>
  194. </Panel>
  195. <PanelTable
  196. headers={[
  197. <TableHeading key="tags"> {t('Tags')}</TableHeading>,
  198. <TextAlignRight key="actions">
  199. <TableHeading> {t('Actions')}</TableHeading>
  200. </TextAlignRight>,
  201. ]}
  202. emptyMessage={t('There are no tags for this metric.')}
  203. isEmpty={tags.length === 0}
  204. isLoading={isLoading}
  205. >
  206. {tags.map(({key}) => {
  207. const isBlockedTag = blockingStatus?.blockedTags?.includes(key) ?? false;
  208. return (
  209. <Fragment key={key}>
  210. <div key={key}>{key}</div>
  211. <TextAlignRight key={key}>
  212. <BlockButton
  213. size="xs"
  214. hasAccess={hasAccess}
  215. disabled={blockMetricMutation.isLoading || isBlockedMetric}
  216. isBlocked={isBlockedTag}
  217. onConfirm={() => handleMetricTagBlockToggle(key)}
  218. aria-label={t('Block tag')}
  219. message={
  220. isBlockedTag
  221. ? t('Are you sure you want to unblock this tag?')
  222. : t(
  223. '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.'
  224. )
  225. }
  226. />
  227. </TextAlignRight>
  228. </Fragment>
  229. );
  230. })}
  231. </PanelTable>
  232. <Panel>
  233. <PanelHeader>{t('Code Location')}</PanelHeader>
  234. <PanelBody withPadding>
  235. <CodeLocations mri={mri} />
  236. </PanelBody>
  237. </Panel>
  238. </Fragment>
  239. );
  240. }
  241. const TableHeading = styled('div')`
  242. color: ${p => p.theme.textColor};
  243. `;
  244. const MetricName = styled('div')`
  245. word-break: break-word;
  246. `;
  247. const Title = styled('div')`
  248. flex: 1;
  249. margin-right: ${space(1)};
  250. `;
  251. const Controls = styled('div')`
  252. display: grid;
  253. align-items: center;
  254. gap: ${space(1)};
  255. grid-auto-flow: column;
  256. `;
  257. export default ProjectMetricsDetails;