AlertRuleList.tsx 5.9 KB

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