AlertRuleList.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  24. onStateFilterChanged = evt => {
  25. this.props.view.updateQuery({ state: evt.target.value });
  26. this.fetchRules();
  27. };
  28. fetchRules() {
  29. this.props.alertList.loadRules({
  30. state: this.props.view.query.get('state') || 'all',
  31. });
  32. }
  33. onOpenHowTo = () => {
  34. appEvents.emit('show-modal', {
  35. src: 'public/app/features/alerting/partials/alert_howto.html',
  36. modalClass: 'confirm-modal',
  37. model: {},
  38. });
  39. };
  40. render() {
  41. const { nav, alertList } = this.props;
  42. return (
  43. <div>
  44. <PageHeader model={nav as any} />
  45. <div className="page-container page-body">
  46. <div className="page-action-bar">
  47. <div className="gf-form">
  48. <label className="gf-form-label">Filter by state</label>
  49. <div className="gf-form-select-wrapper width-13">
  50. <select className="gf-form-input" onChange={this.onStateFilterChanged} value={alertList.stateFilter}>
  51. {this.stateFilters.map(AlertStateFilterOption)}
  52. </select>
  53. </div>
  54. </div>
  55. <div className="page-action-bar__spacer" />
  56. <a className="btn btn-secondary" onClick={this.onOpenHowTo}>
  57. <i className="fa fa-info-circle" /> How to add an alert
  58. </a>
  59. </div>
  60. <section className="card-section card-list-layout-list">
  61. <ol className="card-list">{alertList.rules.map(rule => <AlertRuleItem rule={rule} key={rule.id} />)}</ol>
  62. </section>
  63. </div>
  64. </div>
  65. );
  66. }
  67. }
  68. function AlertStateFilterOption({ text, value }) {
  69. return (
  70. <option key={value} value={value}>
  71. {text}
  72. </option>
  73. );
  74. }
  75. export interface AlertRuleItemProps {
  76. rule: IAlertRule;
  77. }
  78. @observer
  79. export class AlertRuleItem extends React.Component<AlertRuleItemProps, any> {
  80. toggleState = () => {
  81. this.props.rule.togglePaused();
  82. };
  83. render() {
  84. const { rule } = this.props;
  85. let stateClass = classNames({
  86. fa: true,
  87. 'fa-play': rule.isPaused,
  88. 'fa-pause': !rule.isPaused,
  89. });
  90. let ruleUrl = `dashboard/${rule.dashboardUri}?panelId=${rule.panelId}&fullscreen&edit&tab=alert`;
  91. return (
  92. <li className="card-item-wrapper">
  93. <div className="card-item card-item--alert">
  94. <div className="card-item-header">
  95. <div className="card-item-type">
  96. <a
  97. className="card-item-cog"
  98. title="Pausing an alert rule prevents it from executing"
  99. onClick={this.toggleState}
  100. >
  101. <i className={stateClass} />
  102. </a>
  103. <a className="card-item-cog" href={ruleUrl} title="Edit alert rule">
  104. <i className="icon-gf icon-gf-settings" />
  105. </a>
  106. </div>
  107. </div>
  108. <div className="card-item-body">
  109. <div className="card-item-details">
  110. <div className="card-item-name">
  111. <a href={ruleUrl}>{rule.name}</a>
  112. </div>
  113. <div className="card-item-sub-name">
  114. <span className={`alert-list-item-state ${rule.stateClass}`}>
  115. <i className={rule.stateIcon} /> {rule.stateText}
  116. </span>
  117. <span> for {rule.stateAge}</span>
  118. </div>
  119. {rule.info && <div className="small muted">{rule.info}</div>}
  120. </div>
  121. </div>
  122. </div>
  123. </li>
  124. );
  125. }
  126. }