teamAlertsTriggered.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import round from 'lodash/round';
  5. import {LinkButton} from 'sentry/components/button';
  6. import {BarChart} from 'sentry/components/charts/barChart';
  7. import type {DateTimeObject} from 'sentry/components/charts/utils';
  8. import Link from 'sentry/components/links/link';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  12. import {PanelTable} from 'sentry/components/panels/panelTable';
  13. import {IconArrow} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {Organization} from 'sentry/types/organization';
  17. import type {Project} from 'sentry/types/project';
  18. import {formatPercentage} from 'sentry/utils/number/formatPercentage';
  19. import {useApiQuery} from 'sentry/utils/queryClient';
  20. import type {ColorOrAlias} from 'sentry/utils/theme';
  21. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  22. import {ProjectBadge, ProjectBadgeContainer} from './styles';
  23. import {barAxisLabel, convertDayValueObjectToSeries, sortSeriesByDay} from './utils';
  24. export type AlertsTriggered = Record<string, number>;
  25. type AlertsTriggeredRule = MetricRule & {
  26. totalThisWeek: number;
  27. weeklyAvg: number;
  28. };
  29. interface TeamAlertsTriggeredProps extends DateTimeObject {
  30. organization: Organization;
  31. projects: Project[];
  32. teamSlug: string;
  33. }
  34. function TeamAlertsTriggered({
  35. organization,
  36. projects,
  37. teamSlug,
  38. start,
  39. end,
  40. period,
  41. utc,
  42. }: TeamAlertsTriggeredProps) {
  43. const datetime = {start, end, period, utc};
  44. const {
  45. data: alertsTriggered,
  46. isPending: isAlertsTriggeredLoading,
  47. isError: isAlertsTriggeredError,
  48. refetch: refetchAlertsTriggered,
  49. } = useApiQuery<AlertsTriggered>(
  50. [
  51. `/teams/${organization.slug}/${teamSlug}/alerts-triggered/`,
  52. {
  53. query: {
  54. ...normalizeDateTimeParams(datetime),
  55. },
  56. },
  57. ],
  58. {staleTime: 5000}
  59. );
  60. const {
  61. data: alertsTriggeredRules,
  62. isPending: isAlertsTriggeredRulesLoading,
  63. isError: isAlertsTriggeredRulesError,
  64. refetch: refetchAlertsTriggeredRule,
  65. } = useApiQuery<AlertsTriggeredRule[]>(
  66. [
  67. `/teams/${organization.slug}/${teamSlug}/alerts-triggered-index/`,
  68. {
  69. query: {
  70. ...normalizeDateTimeParams(datetime),
  71. },
  72. },
  73. ],
  74. {staleTime: 5000}
  75. );
  76. if (isAlertsTriggeredLoading || isAlertsTriggeredRulesLoading) {
  77. return (
  78. <ChartWrapper>
  79. <LoadingIndicator />
  80. </ChartWrapper>
  81. );
  82. }
  83. if (isAlertsTriggeredError || isAlertsTriggeredRulesError) {
  84. return (
  85. <LoadingError
  86. onRetry={() => {
  87. refetchAlertsTriggered();
  88. refetchAlertsTriggeredRule();
  89. }}
  90. />
  91. );
  92. }
  93. function renderTrend(rule: AlertsTriggeredRule) {
  94. const {weeklyAvg, totalThisWeek} = rule;
  95. const diff = totalThisWeek - weeklyAvg;
  96. // weeklyAvg can only be 0 only if totalThisWeek is also 0
  97. // but those should never be returned in alerts-triggered-index request
  98. if (weeklyAvg === 0) {
  99. return '\u2014';
  100. }
  101. return (
  102. <SubText color={diff <= 0 ? 'successText' : 'errorText'}>
  103. {formatPercentage(Math.abs(diff / weeklyAvg), 0)}
  104. <PaddedIconArrow direction={diff <= 0 ? 'down' : 'up'} size="xs" />
  105. </SubText>
  106. );
  107. }
  108. const seriesData = sortSeriesByDay(
  109. convertDayValueObjectToSeries(alertsTriggered ?? {})
  110. );
  111. return (
  112. <Fragment>
  113. <ChartWrapper>
  114. <BarChart
  115. style={{height: 190}}
  116. isGroupedByDate
  117. useShortDate
  118. period="7d"
  119. legend={{right: 0, top: 0}}
  120. yAxis={{minInterval: 1}}
  121. xAxis={barAxisLabel()}
  122. series={[
  123. {
  124. seriesName: t('Alerts Triggered'),
  125. data: seriesData,
  126. silent: true,
  127. barCategoryGap: '5%',
  128. },
  129. ]}
  130. />
  131. </ChartWrapper>
  132. <StyledPanelTable
  133. isEmpty={
  134. !alertsTriggered || !alertsTriggeredRules || alertsTriggeredRules?.length === 0
  135. }
  136. emptyMessage={t('No alerts triggered for team’s projects')}
  137. emptyAction={
  138. <ButtonsContainer>
  139. <LinkButton
  140. priority="primary"
  141. size="sm"
  142. to={`/organizations/${organization.slug}/alerts/rules/`}
  143. >
  144. {t('Create Alert')}
  145. </LinkButton>
  146. <LinkButton
  147. size="sm"
  148. external
  149. href="https://docs.sentry.io/product/alerts/create-alerts/"
  150. >
  151. {t('Learn more')}
  152. </LinkButton>
  153. </ButtonsContainer>
  154. }
  155. headers={[
  156. t('Alert Rule'),
  157. t('Project'),
  158. <AlignRight key="last">{tct('Last [period] Average', {period})}</AlignRight>,
  159. <AlignRight key="curr">{t('This Week')}</AlignRight>,
  160. <AlignRight key="diff">{t('Difference')}</AlignRight>,
  161. ]}
  162. >
  163. {alertsTriggeredRules?.map(rule => {
  164. const project = projects.find(p => p.slug === rule.projects[0]);
  165. return (
  166. <Fragment key={rule.id}>
  167. <AlertNameContainer>
  168. <Link
  169. to={`/organizations/${organization.slug}/alerts/rules/details/${rule.id}/`}
  170. >
  171. {rule.name}
  172. </Link>
  173. </AlertNameContainer>
  174. <ProjectBadgeContainer>
  175. {project && <ProjectBadge avatarSize={18} project={project} />}
  176. </ProjectBadgeContainer>
  177. <AlignRight>{round(rule.weeklyAvg, 2)}</AlignRight>
  178. <AlignRight>{rule.totalThisWeek}</AlignRight>
  179. <AlignRight>{renderTrend(rule)}</AlignRight>
  180. </Fragment>
  181. );
  182. })}
  183. </StyledPanelTable>
  184. </Fragment>
  185. );
  186. }
  187. export default TeamAlertsTriggered;
  188. const ChartWrapper = styled('div')`
  189. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  190. border-bottom: 1px solid ${p => p.theme.border};
  191. `;
  192. const StyledPanelTable = styled(PanelTable)`
  193. grid-template-columns: 1fr 0.5fr 0.2fr 0.2fr 0.2fr;
  194. font-size: ${p => p.theme.fontSizeMedium};
  195. white-space: nowrap;
  196. margin-bottom: 0;
  197. border: 0;
  198. box-shadow: unset;
  199. & > div {
  200. padding: ${space(1)} ${space(2)};
  201. }
  202. ${p =>
  203. p.isEmpty &&
  204. css`
  205. & > div:last-child {
  206. padding: 48px ${space(2)};
  207. }
  208. `}
  209. `;
  210. const AlertNameContainer = styled('div')`
  211. ${p => p.theme.overflowEllipsis}
  212. `;
  213. const AlignRight = styled('div')`
  214. text-align: right;
  215. font-variant-numeric: tabular-nums;
  216. `;
  217. const PaddedIconArrow = styled(IconArrow)`
  218. margin: 0 ${space(0.5)};
  219. `;
  220. const SubText = styled('div')<{color: ColorOrAlias}>`
  221. color: ${p => p.theme[p.color]};
  222. `;
  223. const ButtonsContainer = styled('div')`
  224. & > a {
  225. margin-right: ${space(0.5)};
  226. margin-left: ${space(0.5)};
  227. }
  228. `;