Convert to modern syntax
parent
89d5f7de2c
commit
7aec032aa4
|
@ -1,4 +1,4 @@
|
|||
import React, {PropTypes} from 'react'
|
||||
import React, {PropTypes, Component} from 'react'
|
||||
|
||||
import DataSection from 'src/kapacitor/components/DataSection'
|
||||
import ValuesSection from 'src/kapacitor/components/ValuesSection'
|
||||
|
@ -11,28 +11,100 @@ import {createRule, editRule} from 'src/kapacitor/apis'
|
|||
import buildInfluxQLQuery from 'utils/influxql'
|
||||
import timeRanges from 'hson!shared/data/timeRanges.hson'
|
||||
|
||||
export const KapacitorRule = React.createClass({
|
||||
propTypes: {
|
||||
source: PropTypes.shape({}).isRequired,
|
||||
rule: PropTypes.shape({}).isRequired,
|
||||
query: PropTypes.shape({}).isRequired,
|
||||
queryConfigs: PropTypes.shape({}).isRequired,
|
||||
queryConfigActions: PropTypes.shape({}).isRequired,
|
||||
ruleActions: PropTypes.shape({}).isRequired,
|
||||
addFlashMessage: PropTypes.func.isRequired,
|
||||
isEditing: PropTypes.bool.isRequired,
|
||||
enabledAlerts: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
|
||||
router: PropTypes.shape({
|
||||
push: PropTypes.func.isRequired,
|
||||
}).isRequired,
|
||||
kapacitor: PropTypes.shape({}).isRequired,
|
||||
},
|
||||
|
||||
getInitialState() {
|
||||
return {
|
||||
class KapacitorRule extends Component {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
timeRange: timeRanges.find(tr => tr.lower === 'now() - 15m'),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
handleChooseTimeRange = ({lower}) => {
|
||||
const timeRange = timeRanges.find(range => range.lower === lower)
|
||||
this.setState({timeRange})
|
||||
}
|
||||
|
||||
handleCreate = () => {
|
||||
const {
|
||||
addFlashMessage,
|
||||
queryConfigs,
|
||||
rule,
|
||||
source,
|
||||
router,
|
||||
kapacitor,
|
||||
} = this.props
|
||||
|
||||
const newRule = Object.assign({}, rule, {
|
||||
query: queryConfigs[rule.queryID],
|
||||
})
|
||||
delete newRule.queryID
|
||||
|
||||
createRule(kapacitor, newRule)
|
||||
.then(() => {
|
||||
router.push(`/sources/${source.id}/alert-rules`)
|
||||
addFlashMessage({type: 'success', text: 'Rule successfully created'})
|
||||
})
|
||||
.catch(() => {
|
||||
addFlashMessage({
|
||||
type: 'error',
|
||||
text: 'There was a problem creating the rule',
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
handleEdit = () => {
|
||||
const {addFlashMessage, queryConfigs, rule} = this.props
|
||||
const updatedRule = Object.assign({}, rule, {
|
||||
query: queryConfigs[rule.queryID],
|
||||
})
|
||||
|
||||
editRule(updatedRule)
|
||||
.then(() => {
|
||||
addFlashMessage({type: 'success', text: 'Rule successfully updated!'})
|
||||
})
|
||||
.catch(() => {
|
||||
addFlashMessage({
|
||||
type: 'error',
|
||||
text: 'There was a problem updating the rule',
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
handleAddEvery = frequency => {
|
||||
const {rule: {id: ruleID}, ruleActions: {addEvery}} = this.props
|
||||
addEvery(ruleID, frequency)
|
||||
}
|
||||
|
||||
handleRemoveEvery = () => {
|
||||
const {rule: {id: ruleID}, ruleActions: {removeEvery}} = this.props
|
||||
removeEvery(ruleID)
|
||||
}
|
||||
|
||||
validationError = () => {
|
||||
const {rule, query} = this.props
|
||||
if (rule.trigger === 'deadman') {
|
||||
return this.deadmanValidation()
|
||||
}
|
||||
|
||||
if (!buildInfluxQLQuery({}, query)) {
|
||||
return 'Please select a database, measurement, and field'
|
||||
}
|
||||
|
||||
if (!rule.values.value) {
|
||||
return 'Please enter a value in the Rule Conditions section'
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
deadmanValidation = () => {
|
||||
const {query} = this.props
|
||||
if (query && (!query.database || !query.measurement)) {
|
||||
return 'Deadman requires a database and measurement'
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
|
@ -97,94 +169,23 @@ export const KapacitorRule = React.createClass({
|
|||
</FancyScrollbar>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
handleChooseTimeRange({lower}) {
|
||||
const timeRange = timeRanges.find(range => range.lower === lower)
|
||||
this.setState({timeRange})
|
||||
},
|
||||
|
||||
handleCreate() {
|
||||
const {
|
||||
addFlashMessage,
|
||||
queryConfigs,
|
||||
rule,
|
||||
source,
|
||||
router,
|
||||
kapacitor,
|
||||
} = this.props
|
||||
|
||||
const newRule = Object.assign({}, rule, {
|
||||
query: queryConfigs[rule.queryID],
|
||||
})
|
||||
delete newRule.queryID
|
||||
|
||||
createRule(kapacitor, newRule)
|
||||
.then(() => {
|
||||
router.push(`/sources/${source.id}/alert-rules`)
|
||||
addFlashMessage({type: 'success', text: 'Rule successfully created'})
|
||||
})
|
||||
.catch(() => {
|
||||
addFlashMessage({
|
||||
type: 'error',
|
||||
text: 'There was a problem creating the rule',
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
handleEdit() {
|
||||
const {addFlashMessage, queryConfigs, rule} = this.props
|
||||
const updatedRule = Object.assign({}, rule, {
|
||||
query: queryConfigs[rule.queryID],
|
||||
})
|
||||
|
||||
editRule(updatedRule)
|
||||
.then(() => {
|
||||
addFlashMessage({type: 'success', text: 'Rule successfully updated!'})
|
||||
})
|
||||
.catch(() => {
|
||||
addFlashMessage({
|
||||
type: 'error',
|
||||
text: 'There was a problem updating the rule',
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
handleAddEvery(frequency) {
|
||||
const {rule: {id: ruleID}, ruleActions: {addEvery}} = this.props
|
||||
addEvery(ruleID, frequency)
|
||||
},
|
||||
|
||||
handleRemoveEvery() {
|
||||
const {rule: {id: ruleID}, ruleActions: {removeEvery}} = this.props
|
||||
removeEvery(ruleID)
|
||||
},
|
||||
|
||||
validationError() {
|
||||
const {rule, query} = this.props
|
||||
if (rule.trigger === 'deadman') {
|
||||
return this.deadmanValidation()
|
||||
}
|
||||
|
||||
if (!buildInfluxQLQuery({}, query)) {
|
||||
return 'Please select a database, measurement, and field'
|
||||
}
|
||||
|
||||
if (!rule.values.value) {
|
||||
return 'Please enter a value in the Rule Conditions section'
|
||||
}
|
||||
|
||||
return ''
|
||||
},
|
||||
|
||||
deadmanValidation() {
|
||||
const {query} = this.props
|
||||
if (query && (!query.database || !query.measurement)) {
|
||||
return 'Deadman requires a database and measurement'
|
||||
}
|
||||
|
||||
return ''
|
||||
},
|
||||
})
|
||||
KapacitorRule.propTypes = {
|
||||
source: PropTypes.shape({}).isRequired,
|
||||
rule: PropTypes.shape({}).isRequired,
|
||||
query: PropTypes.shape({}).isRequired,
|
||||
queryConfigs: PropTypes.shape({}).isRequired,
|
||||
queryConfigActions: PropTypes.shape({}).isRequired,
|
||||
ruleActions: PropTypes.shape({}).isRequired,
|
||||
addFlashMessage: PropTypes.func.isRequired,
|
||||
isEditing: PropTypes.bool.isRequired,
|
||||
enabledAlerts: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
|
||||
router: PropTypes.shape({
|
||||
push: PropTypes.func.isRequired,
|
||||
}).isRequired,
|
||||
kapacitor: PropTypes.shape({}).isRequired,
|
||||
}
|
||||
|
||||
export default KapacitorRule
|
||||
|
|
|
@ -18,8 +18,6 @@ class KapacitorRulePage extends Component {
|
|||
enabledAlerts: [],
|
||||
kapacitor: {},
|
||||
}
|
||||
|
||||
this.isEditing = ::this.isEditing
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
|
@ -97,7 +95,7 @@ class KapacitorRulePage extends Component {
|
|||
)
|
||||
}
|
||||
|
||||
isEditing() {
|
||||
isEditing = () => {
|
||||
const {params} = this.props
|
||||
return params.ruleID && params.ruleID !== 'new'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue