AlertRuleList.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import classNames from 'classnames';
  5. import PageHeader from 'app/core/components/PageHeader/PageHeader';
  6. import appEvents from 'app/core/app_events';
  7. import Highlighter from 'react-highlight-words';
  8. import { updateLocation } from 'app/core/actions';
  9. import { getNavModel } from 'app/core/selectors/navModel';
  10. import { NavModel, StoreState, AlertRule } from 'app/types';
  11. import { getAlertRulesAsync } from './state/actions';
  12. interface Props {
  13. navModel: NavModel;
  14. alertRules: AlertRule[];
  15. updateLocation: typeof updateLocation;
  16. getAlertRulesAsync: typeof getAlertRulesAsync;
  17. }
  18. interface State {
  19. rules: AlertRule[];
  20. search: string;
  21. stateFilter: string;
  22. }
  23. export class AlertRuleList extends PureComponent<Props, State> {
  24. stateFilters = [
  25. { text: 'All', value: 'all' },
  26. { text: 'OK', value: 'ok' },
  27. { text: 'Not OK', value: 'not_ok' },
  28. { text: 'Alerting', value: 'alerting' },
  29. { text: 'No Data', value: 'no_data' },
  30. { text: 'Paused', value: 'paused' },
  31. ];
  32. constructor(props) {
  33. super(props);
  34. this.state = {
  35. rules: [],
  36. search: '',
  37. stateFilter: '',
  38. };
  39. }
  40. componentDidMount() {
  41. this.fetchRules();
  42. }
  43. onStateFilterChanged = evt => {
  44. this.props.updateLocation({
  45. query: { state: evt.target.value },
  46. });
  47. this.fetchRules();
  48. };
  49. async fetchRules() {
  50. await this.props.getAlertRulesAsync();
  51. // this.props.alertList.loadRules({
  52. // state: this.props.view.query.get('state') || 'all',
  53. // });
  54. }
  55. onOpenHowTo = () => {
  56. appEvents.emit('show-modal', {
  57. src: 'public/app/features/alerting/partials/alert_howto.html',
  58. modalClass: 'confirm-modal',
  59. model: {},
  60. });
  61. };
  62. onSearchQueryChange = evt => {
  63. // this.props.alertList.setSearchQuery(evt.target.value);
  64. };
  65. render() {
  66. const { navModel, alertRules } = this.props;
  67. const { search, stateFilter } = this.state;
  68. return (
  69. <div>
  70. <PageHeader model={navModel} />
  71. <div className="page-container page-body">
  72. <div className="page-action-bar">
  73. <div className="gf-form gf-form--grow">
  74. <label className="gf-form--has-input-icon gf-form--grow">
  75. <input
  76. type="text"
  77. className="gf-form-input"
  78. placeholder="Search alerts"
  79. value={search}
  80. onChange={this.onSearchQueryChange}
  81. />
  82. <i className="gf-form-input-icon fa fa-search" />
  83. </label>
  84. </div>
  85. <div className="gf-form">
  86. <label className="gf-form-label">States</label>
  87. <div className="gf-form-select-wrapper width-13">
  88. <select className="gf-form-input" onChange={this.onStateFilterChanged} value={stateFilter}>
  89. {this.stateFilters.map(AlertStateFilterOption)}
  90. </select>
  91. </div>
  92. </div>
  93. <div className="page-action-bar__spacer" />
  94. <a className="btn btn-secondary" onClick={this.onOpenHowTo}>
  95. <i className="fa fa-info-circle" /> How to add an alert
  96. </a>
  97. </div>
  98. <section>
  99. <ol className="alert-rule-list">
  100. {alertRules.map(rule => <AlertRuleItem rule={rule} key={rule.id} search={search} />)}
  101. </ol>
  102. </section>
  103. </div>
  104. </div>
  105. );
  106. }
  107. }
  108. function AlertStateFilterOption({ text, value }) {
  109. return (
  110. <option key={value} value={value}>
  111. {text}
  112. </option>
  113. );
  114. }
  115. export interface AlertRuleItemProps {
  116. rule: AlertRule;
  117. search: string;
  118. }
  119. export class AlertRuleItem extends React.Component<AlertRuleItemProps, any> {
  120. toggleState = () => {
  121. // this.props.rule.togglePaused();
  122. };
  123. renderText(text: string) {
  124. return (
  125. <Highlighter
  126. highlightClassName="highlight-search-match"
  127. textToHighlight={text}
  128. searchWords={[this.props.search]}
  129. />
  130. );
  131. }
  132. render() {
  133. const { rule } = this.props;
  134. const stateClass = classNames({
  135. fa: true,
  136. 'fa-play': rule.state === 'paused',
  137. 'fa-pause': rule.state !== 'paused',
  138. });
  139. const ruleUrl = `${rule.url}?panelId=${rule.panelId}&fullscreen=true&edit=true&tab=alert`;
  140. return (
  141. <li className="alert-rule-item">
  142. <span className={`alert-rule-item__icon ${rule.stateClass}`}>
  143. <i className={rule.stateIcon} />
  144. </span>
  145. <div className="alert-rule-item__body">
  146. <div className="alert-rule-item__header">
  147. <div className="alert-rule-item__name">
  148. <a href={ruleUrl}>{this.renderText(rule.name)}</a>
  149. </div>
  150. <div className="alert-rule-item__text">
  151. <span className={`${rule.stateClass}`}>{this.renderText(rule.stateText)}</span>
  152. <span className="alert-rule-item__time"> for {rule.stateAge}</span>
  153. </div>
  154. </div>
  155. {rule.info && <div className="small muted alert-rule-item__info">{this.renderText(rule.info)}</div>}
  156. </div>
  157. <div className="alert-rule-item__actions">
  158. <button
  159. className="btn btn-small btn-inverse alert-list__btn width-2"
  160. title="Pausing an alert rule prevents it from executing"
  161. onClick={this.toggleState}
  162. >
  163. <i className={stateClass} />
  164. </button>
  165. <a className="btn btn-small btn-inverse alert-list__btn width-2" href={ruleUrl} title="Edit alert rule">
  166. <i className="icon-gf icon-gf-settings" />
  167. </a>
  168. </div>
  169. </li>
  170. );
  171. }
  172. }
  173. const mapStateToProps = (state: StoreState) => ({
  174. navModel: getNavModel(state.navIndex, 'alert-list'),
  175. alertRules: state.alertRules,
  176. });
  177. const mapDispatchToProps = {
  178. updateLocation,
  179. getAlertRulesAsync,
  180. };
  181. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(AlertRuleList));