import {Fragment} from 'react'; import {css} from '@emotion/react'; import styled from '@emotion/styled'; import round from 'lodash/round'; import AsyncComponent from 'sentry/components/asyncComponent'; import Button from 'sentry/components/button'; import {BarChart} from 'sentry/components/charts/barChart'; import {DateTimeObject} from 'sentry/components/charts/utils'; import Link from 'sentry/components/links/link'; import LoadingIndicator from 'sentry/components/loadingIndicator'; import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse'; import PanelTable from 'sentry/components/panels/panelTable'; import {IconArrow} from 'sentry/icons'; import {t, tct} from 'sentry/locale'; import space from 'sentry/styles/space'; import {Organization, Project} from 'sentry/types'; import {formatPercentage} from 'sentry/utils/formatters'; import {Color} from 'sentry/utils/theme'; import {MetricRule} from 'sentry/views/alerts/rules/metric/types'; import {ProjectBadge, ProjectBadgeContainer} from './styles'; import {barAxisLabel, convertDayValueObjectToSeries, sortSeriesByDay} from './utils'; type AlertsTriggered = Record; type AlertsTriggeredRule = MetricRule & { totalThisWeek: number; weeklyAvg: number; }; type Props = AsyncComponent['props'] & { organization: Organization; projects: Project[]; teamSlug: string; } & DateTimeObject; type State = AsyncComponent['state'] & { alertsTriggered: AlertsTriggered | null; alertsTriggeredRules: AlertsTriggeredRule[] | null; }; class TeamAlertsTriggered extends AsyncComponent { shouldRenderBadRequests = true; getDefaultState(): State { return { ...super.getDefaultState(), alertsTriggered: null, alertsTriggeredRules: null, }; } getEndpoints(): ReturnType { const {organization, start, end, period, utc, teamSlug} = this.props; const datetime = {start, end, period, utc}; return [ [ 'alertsTriggered', `/teams/${organization.slug}/${teamSlug}/alerts-triggered/`, { query: { ...normalizeDateTimeParams(datetime), }, }, ], [ 'alertsTriggeredRules', `/teams/${organization.slug}/${teamSlug}/alerts-triggered-index/`, { query: { ...normalizeDateTimeParams(datetime), }, }, ], ]; } componentDidUpdate(prevProps: Props) { const {start, end, period, utc, teamSlug} = this.props; if ( prevProps.start !== start || prevProps.end !== end || prevProps.period !== period || prevProps.utc !== utc || prevProps.teamSlug !== teamSlug ) { this.remountComponent(); } } renderTrend(rule: AlertsTriggeredRule) { const {weeklyAvg, totalThisWeek} = rule; const diff = totalThisWeek - weeklyAvg; // weeklyAvg can only be 0 only if totalThisWeek is also 0 // but those should never be returned in alerts-triggered-index request if (weeklyAvg === 0) { return '\u2014'; } return ( {formatPercentage(Math.abs(diff / weeklyAvg), 0)} ); } renderLoading() { return ( ); } renderBody() { const {organization, period, projects} = this.props; const {alertsTriggered, alertsTriggeredRules} = this.state; const seriesData = sortSeriesByDay( convertDayValueObjectToSeries(alertsTriggered ?? {}) ); return ( } headers={[ t('Alert Rule'), t('Project'), {tct('Last [period] Average', {period})}, {t('This Week')}, {t('Difference')}, ]} > {alertsTriggeredRules?.map(rule => { const project = projects.find(p => p.slug === rule.projects[0]); return ( {rule.name} {project && } {round(rule.weeklyAvg, 2)} {rule.totalThisWeek} {this.renderTrend(rule)} ); })} ); } } export default TeamAlertsTriggered; const ChartWrapper = styled('div')` padding: ${space(2)} ${space(2)} 0 ${space(2)}; border-bottom: 1px solid ${p => p.theme.border}; `; const StyledPanelTable = styled(PanelTable)` grid-template-columns: 1fr 0.5fr 0.2fr 0.2fr 0.2fr; font-size: ${p => p.theme.fontSizeMedium}; white-space: nowrap; margin-bottom: 0; border: 0; box-shadow: unset; & > div { padding: ${space(1)} ${space(2)}; } ${p => p.isEmpty && css` & > div:last-child { padding: 48px ${space(2)}; } `} `; const AlertNameContainer = styled('div')` ${p => p.theme.overflowEllipsis} `; const AlignRight = styled('div')` text-align: right; font-variant-numeric: tabular-nums; `; const PaddedIconArrow = styled(IconArrow)` margin: 0 ${space(0.5)}; `; const SubText = styled('div')<{color: Color}>` color: ${p => p.theme[p.color]}; `; const ButtonsContainer = styled('div')` & > a { margin-right: ${space(0.5)}; margin-left: ${space(0.5)}; } `;