AlertRuleList.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import { inject, observer } from 'mobx-react';
  4. import PageHeader from 'app/core/components/PageHeader/PageHeader';
  5. import { IAlertRule } from 'app/stores/AlertListStore/AlertListStore';
  6. import appEvents from 'app/core/app_events';
  7. import IContainerProps from 'app/containers/IContainerProps';
  8. @inject('view', 'nav', 'alertList')
  9. @observer
  10. export class AlertRuleList extends React.Component<IContainerProps, any> {
  11. stateFilters = [
  12. { text: 'All', value: 'all' },
  13. { text: 'OK', value: 'ok' },
  14. { text: 'Not OK', value: 'not_ok' },
  15. { text: 'Alerting', value: 'alerting' },
  16. { text: 'No Data', value: 'no_data' },
  17. { text: 'Paused', value: 'paused' },
  18. ];
  19. constructor(props) {
  20. super(props);
  21. this.props.nav.load('alerting', 'alert-list');
  22. this.fetchRules();
  23. this.state = { search: '' };
  24. }
  25. onStateFilterChanged = evt => {
  26. this.props.view.updateQuery({ state: evt.target.value });
  27. this.fetchRules();
  28. };
  29. fetchRules() {
  30. this.props.alertList.loadRules({
  31. state: this.props.view.query.get('state') || 'all',
  32. });
  33. }
  34. onOpenHowTo = () => {
  35. appEvents.emit('show-modal', {
  36. src: 'public/app/features/alerting/partials/alert_howto.html',
  37. modalClass: 'confirm-modal',
  38. model: {},
  39. });
  40. };
  41. onSearchFilter(event) {
  42. this.setState({ search: event.target.value });
  43. console.log('yo');
  44. }
  45. render() {
  46. const { nav, alertList } = this.props;
  47. let regex = new RegExp(this.state.search, 'ig');
  48. const filteredAlerts = alertList.rules.filter(alert => {
  49. return regex.test(alert.name) || regex.test(alert.stateText);
  50. });
  51. return (
  52. <div>
  53. <PageHeader model={nav as any} />
  54. <div className="page-container page-body">
  55. <div className="page-action-bar">
  56. <div className="gf-form">
  57. <label className="gf-form--has-input-icon">
  58. <input
  59. type="text"
  60. className="gf-form-input width-13"
  61. placeholder="Search alert"
  62. value={this.state.search}
  63. onChange={this.onSearchFilter.bind(this)}
  64. />
  65. <i className="gf-form-input-icon fa fa-search" />
  66. </label>
  67. </div>
  68. <div className="gf-form">
  69. <label className="gf-form-label">Filter by state</label>
  70. <div className="gf-form-select-wrapper width-13">
  71. <select className="gf-form-input" onChange={this.onStateFilterChanged} value={alertList.stateFilter}>
  72. {this.stateFilters.map(AlertStateFilterOption)}
  73. </select>
  74. </div>
  75. </div>
  76. <div className="page-action-bar__spacer" />
  77. <a className="btn btn-secondary" onClick={this.onOpenHowTo}>
  78. <i className="fa fa-info-circle" /> How to add an alert
  79. </a>
  80. </div>
  81. <section>
  82. <ol className="alert-rule-list">
  83. {filteredAlerts.map(rule => <AlertRuleItem rule={rule} key={rule.id} />)}
  84. </ol>
  85. </section>
  86. </div>
  87. </div>
  88. );
  89. }
  90. }
  91. function AlertStateFilterOption({ text, value }) {
  92. return (
  93. <option key={value} value={value}>
  94. {text}
  95. </option>
  96. );
  97. }
  98. export interface AlertRuleItemProps {
  99. rule: IAlertRule;
  100. }
  101. @observer
  102. export class AlertRuleItem extends React.Component<AlertRuleItemProps, any> {
  103. toggleState = () => {
  104. this.props.rule.togglePaused();
  105. };
  106. render() {
  107. const { rule } = this.props;
  108. let stateClass = classNames({
  109. fa: true,
  110. 'fa-play': rule.isPaused,
  111. 'fa-pause': !rule.isPaused,
  112. });
  113. let ruleUrl = `dashboard/${rule.dashboardUri}?panelId=${rule.panelId}&fullscreen&edit&tab=alert`;
  114. return (
  115. <li className="alert-rule-item">
  116. <div className="alert-rule-item__body">
  117. <span className={`alert-rule-item__icon ${rule.stateClass}`}>
  118. <i className={rule.stateIcon} />
  119. </span>
  120. <div className="alert-rule-item__header">
  121. <div className="alert-rule-item__name">
  122. <a href={ruleUrl}>{rule.name}</a>
  123. </div>
  124. <div className="alert-rule-item__text">
  125. <span className={`${rule.stateClass}`}>{rule.stateText}</span>
  126. <span className="alert-rule-item__time"> for {rule.stateAge}</span>
  127. </div>
  128. </div>
  129. {rule.info && <div className="small muted alert-rule-item__info">{rule.info}</div>}
  130. </div>
  131. <div className="alert-rule-item__footer">
  132. <a
  133. className="btn btn-small btn-inverse alert-list__btn width-2"
  134. title="Pausing an alert rule prevents it from executing"
  135. onClick={this.toggleState}
  136. >
  137. <i className={stateClass} />
  138. </a>
  139. <a className="btn btn-small btn-inverse alert-list__btn width-2" href={ruleUrl} title="Edit alert rule">
  140. <i className="icon-gf icon-gf-settings" />
  141. </a>
  142. </div>
  143. </li>
  144. );
  145. }
  146. }