teamAlertsTriggered.tsx 6.9 KB

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