grafana/public/app/features/alerting/AlertRuleList.tsx

213 lines
5.9 KiB
TypeScript
Raw Normal View History

2018-08-31 11:42:32 -05:00
import React, { PureComponent } from 'react';
import { hot } from 'react-hot-loader';
2018-08-31 11:42:32 -05:00
import { connect } from 'react-redux';
import classNames from 'classnames';
import PageHeader from 'app/core/components/PageHeader/PageHeader';
import appEvents from 'app/core/app_events';
2018-01-08 10:22:44 -06:00
import Highlighter from 'react-highlight-words';
import { initNav, updateLocation } from 'app/core/actions';
2018-08-31 11:42:32 -05:00
import { ContainerProps } from 'app/types';
2018-09-02 09:11:21 -05:00
import { getAlertRules, AlertRule } from './state/apis';
interface Props extends ContainerProps {
updateLocation: typeof updateLocation;
}
2018-08-31 11:42:32 -05:00
interface State {
rules: AlertRule[];
search: string;
stateFilter: string;
}
export class AlertRuleList extends PureComponent<Props, State> {
stateFilters = [
{ text: 'All', value: 'all' },
{ text: 'OK', value: 'ok' },
{ text: 'Not OK', value: 'not_ok' },
{ text: 'Alerting', value: 'alerting' },
{ text: 'No Data', value: 'no_data' },
{ text: 'Paused', value: 'paused' },
];
constructor(props) {
super(props);
2018-08-31 11:42:32 -05:00
this.state = {
rules: [],
search: '',
stateFilter: '',
};
this.props.initNav('alerting', 'alert-list');
}
componentDidMount() {
this.fetchRules();
}
onStateFilterChanged = evt => {
this.props.updateLocation({
query: { state: evt.target.value },
});
2018-08-31 11:42:32 -05:00
// this.fetchRules();
};
2018-08-31 11:42:32 -05:00
async fetchRules() {
try {
const rules = await getAlertRules();
this.setState({ rules });
} catch (error) {
console.error(error);
}
// this.props.alertList.loadRules({
// state: this.props.view.query.get('state') || 'all',
// });
}
onOpenHowTo = () => {
appEvents.emit('show-modal', {
src: 'public/app/features/alerting/partials/alert_howto.html',
modalClass: 'confirm-modal',
model: {},
});
};
2018-01-09 08:16:55 -06:00
onSearchQueryChange = evt => {
2018-08-31 11:42:32 -05:00
// this.props.alertList.setSearchQuery(evt.target.value);
};
2018-01-04 08:27:09 -06:00
render() {
2018-08-31 11:42:32 -05:00
const { navModel } = this.props;
const { rules, search, stateFilter } = this.state;
return (
<div>
2018-08-31 11:42:32 -05:00
<PageHeader model={navModel} />
<div className="page-container page-body">
<div className="page-action-bar">
<div className="gf-form gf-form--grow">
<label className="gf-form--has-input-icon gf-form--grow">
2018-01-04 06:08:49 -06:00
<input
type="text"
className="gf-form-input"
2018-01-10 06:17:43 -06:00
placeholder="Search alerts"
2018-08-31 11:42:32 -05:00
value={search}
2018-01-09 08:16:55 -06:00
onChange={this.onSearchQueryChange}
2018-01-04 06:08:49 -06:00
/>
<i className="gf-form-input-icon fa fa-search" />
</label>
</div>
<div className="gf-form">
<label className="gf-form-label">States</label>
<div className="gf-form-select-wrapper width-13">
2018-08-31 11:42:32 -05:00
<select className="gf-form-input" onChange={this.onStateFilterChanged} value={stateFilter}>
{this.stateFilters.map(AlertStateFilterOption)}
</select>
</div>
</div>
<div className="page-action-bar__spacer" />
<a className="btn btn-secondary" onClick={this.onOpenHowTo}>
<i className="fa fa-info-circle" /> How to add an alert
</a>
</div>
2018-01-04 06:08:49 -06:00
<section>
<ol className="alert-rule-list">
{rules.map(rule => <AlertRuleItem rule={rule} key={rule.id} search={search} />)}
2018-01-04 06:08:49 -06:00
</ol>
</section>
</div>
</div>
);
}
}
function AlertStateFilterOption({ text, value }) {
return (
<option key={value} value={value}>
{text}
</option>
);
}
export interface AlertRuleItemProps {
rule: AlertRule;
2018-01-08 10:22:44 -06:00
search: string;
}
export class AlertRuleItem extends React.Component<AlertRuleItemProps, any> {
toggleState = () => {
2018-08-31 11:42:32 -05:00
// this.props.rule.togglePaused();
};
renderText(text: string) {
return (
<Highlighter
highlightClassName="highlight-search-match"
textToHighlight={text}
searchWords={[this.props.search]}
/>
);
2018-01-08 10:22:44 -06:00
}
render() {
const { rule } = this.props;
const stateClass = classNames({
fa: true,
2018-08-31 11:42:32 -05:00
'fa-play': rule.state === 'paused',
'fa-pause': rule.state !== 'paused',
});
const ruleUrl = `${rule.url}?panelId=${rule.panelId}&fullscreen=true&edit=true&tab=alert`;
return (
2018-01-04 06:08:49 -06:00
<li className="alert-rule-item">
<span className={`alert-rule-item__icon ${rule.stateClass}`}>
<i className={rule.stateIcon} />
</span>
2018-01-04 06:08:49 -06:00
<div className="alert-rule-item__body">
<div className="alert-rule-item__header">
<div className="alert-rule-item__name">
<a href={ruleUrl}>{this.renderText(rule.name)}</a>
</div>
2018-01-04 06:08:49 -06:00
<div className="alert-rule-item__text">
<span className={`${rule.stateClass}`}>{this.renderText(rule.stateText)}</span>
2018-01-04 06:08:49 -06:00
<span className="alert-rule-item__time"> for {rule.stateAge}</span>
</div>
</div>
{rule.info && <div className="small muted alert-rule-item__info">{this.renderText(rule.info)}</div>}
2018-01-04 06:08:49 -06:00
</div>
<div className="alert-rule-item__actions">
<button
2018-01-04 06:08:49 -06:00
className="btn btn-small btn-inverse alert-list__btn width-2"
title="Pausing an alert rule prevents it from executing"
onClick={this.toggleState}
>
<i className={stateClass} />
</button>
<a className="btn btn-small btn-inverse alert-list__btn width-2" href={ruleUrl} title="Edit alert rule">
<i className="icon-gf icon-gf-settings" />
</a>
</div>
</li>
);
}
}
2018-08-31 11:42:32 -05:00
const mapStateToProps = state => ({
navModel: state.navModel,
});
const mapDispatchToProps = {
initNav,
updateLocation,
2018-08-31 11:42:32 -05:00
};
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(AlertRuleList));