projectMetricsDetails.tsx 8.3 KB

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