AlertRuleList.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 { initNav } from 'app/core/actions';
  9. import { ContainerProps } from 'app/types';
  10. import { getAlertRules, AlertRule } from '../apis';
  11. interface Props extends ContainerProps {}
  12. interface State {
  13. rules: AlertRule[];
  14. search: string;
  15. stateFilter: string;
  16. }
  17. export class AlertRuleList extends PureComponent<Props, State> {
  18. stateFilters = [
  19. { text: 'All', value: 'all' },
  20. { text: 'OK', value: 'ok' },
  21. { text: 'Not OK', value: 'not_ok' },
  22. { text: 'Alerting', value: 'alerting' },
  23. { text: 'No Data', value: 'no_data' },
  24. { text: 'Paused', value: 'paused' },
  25. ];
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. rules: [],
  30. search: '',
  31. stateFilter: '',
  32. };
  33. this.props.initNav('alerting', 'alert-list');
  34. }
  35. componentDidMount() {
  36. this.fetchRules();
  37. }
  38. onStateFilterChanged = evt => {
  39. // this.props.view.updateQuery({ state: evt.target.value });
  40. // this.fetchRules();
  41. };
  42. async fetchRules() {
  43. try {
  44. const rules = await getAlertRules();
  45. this.setState({ rules });
  46. } catch (error) {
  47. console.error(error);
  48. }
  49. // this.props.alertList.loadRules({
  50. // state: this.props.view.query.get('state') || 'all',
  51. // });
  52. }
  53. onOpenHowTo = () => {
  54. appEvents.emit('show-modal', {
  55. src: 'public/app/features/alerting/partials/alert_howto.html',
  56. modalClass: 'confirm-modal',
  57. model: {},
  58. });
  59. };
  60. onSearchQueryChange = evt => {
  61. // this.props.alertList.setSearchQuery(evt.target.value);
  62. };
  63. render() {
  64. const { navModel } = this.props;
  65. const { rules, search, stateFilter } = this.state;
  66. return (
  67. <div>
  68. <PageHeader model={navModel} />
  69. <div className="page-container page-body">
  70. <div className="page-action-bar">
  71. <div className="gf-form gf-form--grow">
  72. <label className="gf-form--has-input-icon gf-form--grow">
  73. <input
  74. type="text"
  75. className="gf-form-input"
  76. placeholder="Search alerts"
  77. value={search}
  78. onChange={this.onSearchQueryChange}
  79. />
  80. <i className="gf-form-input-icon fa fa-search" />
  81. </label>
  82. </div>
  83. <div className="gf-form">
  84. <label className="gf-form-label">States</label>
  85. <div className="gf-form-select-wrapper width-13">
  86. <select className="gf-form-input" onChange={this.onStateFilterChanged} value={stateFilter}>
  87. {this.stateFilters.map(AlertStateFilterOption)}
  88. </select>
  89. </div>
  90. </div>
  91. <div className="page-action-bar__spacer" />
  92. <a className="btn btn-secondary" onClick={this.onOpenHowTo}>
  93. <i className="fa fa-info-circle" /> How to add an alert
  94. </a>
  95. </div>
  96. <section>
  97. <ol className="alert-rule-list">
  98. {rules.map(rule => (
  99. <AlertRuleItem rule={rule} key={rule.id} search={search} />
  100. ))}
  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 => ({
  174. navModel: state.navModel,
  175. });
  176. const mapDispatchToProps = {
  177. initNav,
  178. };
  179. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(AlertRuleList));