AlertRuleList.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { SelectableValue } from '@grafana/data';
  4. import { config, locationService } from '@grafana/runtime';
  5. import { Button, FilterInput, LinkButton, Select, VerticalGroup } from '@grafana/ui';
  6. import appEvents from 'app/core/app_events';
  7. import Page from 'app/core/components/Page/Page';
  8. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  9. import { getNavModel } from 'app/core/selectors/navModel';
  10. import { AlertRule, StoreState } from 'app/types';
  11. import { ShowModalReactEvent } from '../../types/events';
  12. import { AlertHowToModal } from './AlertHowToModal';
  13. import AlertRuleItem from './AlertRuleItem';
  14. import { DeprecationNotice } from './components/DeprecationNotice';
  15. import { getAlertRulesAsync, togglePauseAlertRule } from './state/actions';
  16. import { setSearchQuery } from './state/reducers';
  17. import { getAlertRuleItems, getSearchQuery } from './state/selectors';
  18. function mapStateToProps(state: StoreState) {
  19. return {
  20. navModel: getNavModel(state.navIndex, 'alert-list'),
  21. alertRules: getAlertRuleItems(state),
  22. search: getSearchQuery(state.alertRules),
  23. isLoading: state.alertRules.isLoading,
  24. };
  25. }
  26. const mapDispatchToProps = {
  27. getAlertRulesAsync,
  28. setSearchQuery,
  29. togglePauseAlertRule,
  30. };
  31. const connector = connect(mapStateToProps, mapDispatchToProps);
  32. interface OwnProps extends GrafanaRouteComponentProps<{}, { state: string }> {}
  33. export type Props = OwnProps & ConnectedProps<typeof connector>;
  34. export class AlertRuleListUnconnected extends PureComponent<Props> {
  35. stateFilters = [
  36. { label: 'All', value: 'all' },
  37. { label: 'OK', value: 'ok' },
  38. { label: 'Not OK', value: 'not_ok' },
  39. { label: 'Alerting', value: 'alerting' },
  40. { label: 'No data', value: 'no_data' },
  41. { label: 'Paused', value: 'paused' },
  42. { label: 'Pending', value: 'pending' },
  43. ];
  44. componentDidMount() {
  45. this.fetchRules();
  46. }
  47. componentDidUpdate(prevProps: Props) {
  48. if (prevProps.queryParams.state !== this.props.queryParams.state) {
  49. this.fetchRules();
  50. }
  51. }
  52. async fetchRules() {
  53. await this.props.getAlertRulesAsync({ state: this.getStateFilter() });
  54. }
  55. getStateFilter(): string {
  56. return this.props.queryParams.state ?? 'all';
  57. }
  58. onStateFilterChanged = (option: SelectableValue) => {
  59. locationService.partial({ state: option.value });
  60. };
  61. onOpenHowTo = () => {
  62. appEvents.publish(new ShowModalReactEvent({ component: AlertHowToModal }));
  63. };
  64. onSearchQueryChange = (value: string) => {
  65. this.props.setSearchQuery(value);
  66. };
  67. onTogglePause = (rule: AlertRule) => {
  68. this.props.togglePauseAlertRule(rule.id, { paused: rule.state !== 'paused' });
  69. };
  70. alertStateFilterOption = ({ text, value }: { text: string; value: string }) => {
  71. return (
  72. <option key={value} value={value}>
  73. {text}
  74. </option>
  75. );
  76. };
  77. render() {
  78. const { navModel, alertRules, search, isLoading } = this.props;
  79. return (
  80. <Page navModel={navModel}>
  81. <Page.Contents isLoading={isLoading}>
  82. <div className="page-action-bar">
  83. <div className="gf-form gf-form--grow">
  84. <FilterInput placeholder="Search alerts" value={search} onChange={this.onSearchQueryChange} />
  85. </div>
  86. <div className="gf-form">
  87. <label className="gf-form-label" htmlFor="alert-state-filter">
  88. States
  89. </label>
  90. <div className="width-13">
  91. <Select
  92. inputId={'alert-state-filter'}
  93. options={this.stateFilters}
  94. onChange={this.onStateFilterChanged}
  95. value={this.getStateFilter()}
  96. />
  97. </div>
  98. </div>
  99. <div className="page-action-bar__spacer" />
  100. {config.unifiedAlertingEnabled && (
  101. <LinkButton variant="primary" href="alerting/ng/new">
  102. Add NG Alert
  103. </LinkButton>
  104. )}
  105. <Button variant="secondary" onClick={this.onOpenHowTo}>
  106. How to add an alert
  107. </Button>
  108. </div>
  109. <DeprecationNotice />
  110. <VerticalGroup spacing="none">
  111. {alertRules.map((rule) => {
  112. return (
  113. <AlertRuleItem
  114. rule={rule as AlertRule}
  115. key={rule.id}
  116. search={search}
  117. onTogglePause={() => this.onTogglePause(rule as AlertRule)}
  118. />
  119. );
  120. })}
  121. </VerticalGroup>
  122. </Page.Contents>
  123. </Page>
  124. );
  125. }
  126. }
  127. export default connector(AlertRuleListUnconnected);