123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- import {Fragment} from 'react';
- import styled from '@emotion/styled';
- import {Location} from 'history';
- import isEmpty from 'lodash/isEmpty';
- import uniq from 'lodash/uniq';
- import {
- addErrorMessage,
- addMessage,
- addSuccessMessage,
- } from 'sentry/actionCreators/indicator';
- import Alert from 'sentry/components/alert';
- import * as Layout from 'sentry/components/layouts/thirds';
- import Link from 'sentry/components/links/link';
- import LoadingError from 'sentry/components/loadingError';
- import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
- import Pagination from 'sentry/components/pagination';
- import PanelTable from 'sentry/components/panels/panelTable';
- import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
- import {IconArrow, IconWarning} from 'sentry/icons';
- import {t, tct} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {Project} from 'sentry/types';
- import {defined} from 'sentry/utils';
- import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
- import Projects from 'sentry/utils/projects';
- import {
- ApiQueryKey,
- setApiQueryData,
- useApiQuery,
- useQueryClient,
- } from 'sentry/utils/queryClient';
- import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
- import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
- import useApi from 'sentry/utils/useApi';
- import {useLocation} from 'sentry/utils/useLocation';
- import useOrganization from 'sentry/utils/useOrganization';
- import useRouter from 'sentry/utils/useRouter';
- import {
- hasMigrationFeatureFlag,
- ruleNeedsMigration,
- useOrgNeedsMigration,
- } from 'sentry/views/alerts/utils/migrationUi';
- import FilterBar from '../../filterBar';
- import {AlertRuleType, CombinedMetricIssueAlerts} from '../../types';
- import {
- DatasetOption,
- datasetToQueryParam,
- getQueryDataset,
- getTeamParams,
- isIssueAlert,
- } from '../../utils';
- import AlertHeader from '../header';
- import RuleListRow from './row';
- type SortField = 'date_added' | 'name' | ['incident_status', 'date_triggered'];
- const defaultSort: SortField = ['incident_status', 'date_triggered'];
- function getAlertListQueryKey(orgSlug: string, query: Location['query']): ApiQueryKey {
- const queryParams = {...query};
- queryParams.expand = ['latestIncident', 'lastTriggered'];
- queryParams.team = getTeamParams(queryParams.team!);
- queryParams.dataset = datasetToQueryParam[getQueryDataset(queryParams.dataset!)];
- if (!queryParams.sort) {
- queryParams.sort = defaultSort;
- }
- return [`/organizations/${orgSlug}/combined-rules/`, {query: queryParams}];
- }
- function AlertRulesList() {
- const location = useLocation();
- const router = useRouter();
- const api = useApi();
- const queryClient = useQueryClient();
- const organization = useOrganization();
- useRouteAnalyticsEventNames('alert_rules.viewed', 'Alert Rules: Viewed');
- useRouteAnalyticsParams({
- sort: Array.isArray(location.query.sort)
- ? location.query.sort.join(',')
- : location.query.sort,
- });
- const {
- data: ruleListResponse = [],
- refetch,
- getResponseHeader,
- isLoading,
- isError,
- } = useApiQuery<Array<CombinedMetricIssueAlerts | null>>(
- getAlertListQueryKey(organization.slug, location.query),
- {
- staleTime: 0,
- }
- );
- const hasMigrationUIFeatureFlag = hasMigrationFeatureFlag(organization);
- const showMigrationUI = useOrgNeedsMigration();
- const handleChangeFilter = (activeFilters: string[]) => {
- const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
- router.push({
- pathname: location.pathname,
- query: {
- ...currentQuery,
- team: activeFilters.length > 0 ? activeFilters : '',
- },
- });
- };
- const handleChangeSearch = (name: string) => {
- const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
- router.push({
- pathname: location.pathname,
- query: {
- ...currentQuery,
- name,
- },
- });
- };
- const handleChangeDataset = (value: DatasetOption): void => {
- const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
- router.push({
- pathname: location.pathname,
- query: {
- ...currentQuery,
- dataset: value === DatasetOption.ALL ? undefined : value,
- },
- });
- };
- const handleOwnerChange = (
- projectId: string,
- rule: CombinedMetricIssueAlerts,
- ownerValue: string
- ) => {
- const endpoint =
- rule.type === 'alert_rule'
- ? `/organizations/${organization.slug}/alert-rules/${rule.id}`
- : `/projects/${organization.slug}/${projectId}/rules/${rule.id}/`;
- const updatedRule = {...rule, owner: ownerValue};
- api.request(endpoint, {
- method: 'PUT',
- data: updatedRule,
- success: () => {
- addMessage(t('Updated alert rule'), 'success');
- },
- error: () => {
- addMessage(t('Unable to save change'), 'error');
- },
- });
- };
- const handleDeleteRule = async (projectId: string, rule: CombinedMetricIssueAlerts) => {
- try {
- await api.requestPromise(
- isIssueAlert(rule)
- ? `/projects/${organization.slug}/${projectId}/rules/${rule.id}/`
- : `/organizations/${organization.slug}/alert-rules/${rule.id}/`,
- {
- method: 'DELETE',
- }
- );
- setApiQueryData<Array<CombinedMetricIssueAlerts | null>>(
- queryClient,
- getAlertListQueryKey(organization.slug, location.query),
- data => data?.filter(r => r?.id !== rule.id && r?.type !== rule.type)
- );
- refetch();
- addSuccessMessage(t('Deleted rule'));
- } catch (_err) {
- addErrorMessage(t('Error deleting rule'));
- }
- };
- const hasEditAccess = organization.access.includes('alerts:write');
- const ruleList = ruleListResponse.filter(defined);
- const projectsFromResults = uniq(ruleList.flatMap(({projects}) => projects));
- const ruleListPageLinks = getResponseHeader?.('Link');
- const sort: {asc: boolean; field: SortField} = {
- asc: location.query.asc === '1',
- field: (location.query.sort as SortField) || defaultSort,
- };
- const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
- const isAlertRuleSort =
- sort.field.includes('incident_status') || sort.field.includes('date_triggered');
- const sortArrow = (
- <IconArrow color="gray300" size="xs" direction={sort.asc ? 'up' : 'down'} />
- );
- return (
- <Fragment>
- <SentryDocumentTitle title={t('Alerts')} orgSlug={organization.slug} />
- <PageFiltersContainer>
- <AlertHeader router={router} activeTab="rules" />
- <Layout.Body>
- <Layout.Main fullWidth>
- {showMigrationUI ? (
- <Alert showIcon type="warning">
- {tct(
- 'Our performance alerts just got a lot more accurate, which is why we recommend you review the thresholds of all rules marked with a “[warningIcon]“',
- {warningIcon: <StyledIconWarning />}
- )}
- </Alert>
- ) : null}
- <FilterBar
- location={location}
- showMigrationWarning={showMigrationUI}
- onChangeDataset={handleChangeDataset}
- onChangeFilter={handleChangeFilter}
- onChangeSearch={handleChangeSearch}
- />
- <StyledPanelTable
- isLoading={isLoading}
- isEmpty={ruleList.length === 0 && !isError}
- emptyMessage={t('No alert rules found for the current query.')}
- headers={[
- <StyledSortLink
- key="name"
- role="columnheader"
- aria-sort={
- sort.field !== 'name' ? 'none' : sort.asc ? 'ascending' : 'descending'
- }
- to={{
- pathname: location.pathname,
- query: {
- ...currentQuery,
- // sort by name should start by ascending on first click
- asc: sort.field === 'name' && sort.asc ? undefined : '1',
- sort: 'name',
- },
- }}
- >
- {t('Alert Rule')} {sort.field === 'name' ? sortArrow : null}
- </StyledSortLink>,
- <StyledSortLink
- key="status"
- role="columnheader"
- aria-sort={
- !isAlertRuleSort ? 'none' : sort.asc ? 'ascending' : 'descending'
- }
- to={{
- pathname: location.pathname,
- query: {
- ...currentQuery,
- asc: isAlertRuleSort && !sort.asc ? '1' : undefined,
- sort: ['incident_status', 'date_triggered'],
- },
- }}
- >
- {t('Status')} {isAlertRuleSort ? sortArrow : null}
- </StyledSortLink>,
- t('Project'),
- t('Team'),
- t('Actions'),
- ]}
- >
- {isError ? (
- <StyledLoadingError
- message={t('There was an error loading alerts.')}
- onRetry={refetch}
- />
- ) : null}
- <VisuallyCompleteWithData id="AlertRules-Body" hasData={!isEmpty(ruleList)}>
- <Projects orgId={organization.slug} slugs={projectsFromResults}>
- {({initiallyLoaded, projects}) =>
- ruleList.map(rule => (
- <RuleListRow
- // Metric and issue alerts can have the same id
- key={`${
- isIssueAlert(rule) ? AlertRuleType.METRIC : AlertRuleType.ISSUE
- }-${rule.id}`}
- showMigrationWarning={
- hasMigrationUIFeatureFlag && ruleNeedsMigration(rule)
- }
- projectsLoaded={initiallyLoaded}
- projects={projects as Project[]}
- rule={rule}
- orgId={organization.slug}
- onOwnerChange={handleOwnerChange}
- onDelete={handleDeleteRule}
- hasEditAccess={hasEditAccess}
- />
- ))
- }
- </Projects>
- </VisuallyCompleteWithData>
- </StyledPanelTable>
- <Pagination
- pageLinks={ruleListPageLinks}
- onCursor={(cursor, path, _direction) => {
- let team = currentQuery.team;
- // Keep team parameter, but empty to remove parameters
- if (!team || team.length === 0) {
- team = '';
- }
- router.push({
- pathname: path,
- query: {...currentQuery, team, cursor},
- });
- }}
- />
- </Layout.Main>
- </Layout.Body>
- </PageFiltersContainer>
- </Fragment>
- );
- }
- export default AlertRulesList;
- const StyledLoadingError = styled(LoadingError)`
- grid-column: 1 / -1;
- margin-bottom: ${space(4)};
- border-radius: 0;
- border-width: 1px 0;
- `;
- const StyledSortLink = styled(Link)`
- color: inherit;
- display: flex;
- align-items: center;
- gap: ${space(0.5)};
- :hover {
- color: inherit;
- }
- `;
- const StyledPanelTable = styled(PanelTable)`
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- overflow: initial;
- }
- grid-template-columns: minmax(250px, 4fr) auto auto 60px auto;
- white-space: nowrap;
- font-size: ${p => p.theme.fontSizeMedium};
- `;
- const StyledIconWarning = styled(IconWarning)`
- vertical-align: middle;
- color: ${p => p.theme.yellow400};
- `;
|