mirror of https://github.com/openspug/spug
A 监控中心新增执行测试功能 #195
parent
62386f98d8
commit
b3abe6c9cd
|
@ -7,4 +7,5 @@ from .views import *
|
|||
|
||||
urlpatterns = [
|
||||
path('', DetectionView.as_view()),
|
||||
path('test/', run_test),
|
||||
]
|
||||
|
|
|
@ -6,6 +6,7 @@ from libs import json_response, JsonParser, Argument, human_datetime
|
|||
from apps.monitor.models import Detection
|
||||
from django_redis import get_redis_connection
|
||||
from django.conf import settings
|
||||
from apps.monitor.executors import dispatch
|
||||
import json
|
||||
|
||||
|
||||
|
@ -78,3 +79,15 @@ class DetectionView(View):
|
|||
return json_response(error='该监控项正在运行中,请先停止后再尝试删除')
|
||||
task.delete()
|
||||
return json_response(error=error)
|
||||
|
||||
|
||||
def run_test(request):
|
||||
form, error = JsonParser(
|
||||
Argument('type', help='请选择监控类型'),
|
||||
Argument('addr', help='请输入监控地址'),
|
||||
Argument('extra', required=False)
|
||||
).parse(request.body)
|
||||
if error is None:
|
||||
is_success, message = dispatch(form.type, form.addr, form.extra)
|
||||
return json_response({'is_success': is_success, 'message': message})
|
||||
return json_response(error=error)
|
||||
|
|
|
@ -56,7 +56,7 @@ class ComForm extends React.Component {
|
|||
switch (type) {
|
||||
case '1':
|
||||
if (addr.startsWith('http://')) {
|
||||
this.setState({sitePrefix: 'http://', domain: addr.replace('http://', '')})
|
||||
this.setState({sitePrefix: 'http://', domain: addr.replace('http://', '')})
|
||||
} else {
|
||||
this.setState({sitePrefix: 'https://', domain: addr.replace('https://', '')})
|
||||
}
|
||||
|
@ -77,34 +77,30 @@ class ComForm extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
handleSubmit = () => {
|
||||
this.setState({loading: true});
|
||||
_getFieldsValue = (type) => {
|
||||
const {sitePrefix, domain, addr, host, port, command, process} = this.state;
|
||||
const formData = this.props.form.getFieldsValue();
|
||||
const type = formData['type'];
|
||||
formData['id'] = store.record.id;
|
||||
switch (type) {
|
||||
case '1':
|
||||
formData['addr'] = sitePrefix + domain;
|
||||
break;
|
||||
return {addr: sitePrefix + domain}
|
||||
case '2':
|
||||
formData['addr'] = addr;
|
||||
formData['extra'] = port;
|
||||
break;
|
||||
return {addr, extra: port}
|
||||
case '3':
|
||||
formData['addr'] = host;
|
||||
formData['extra'] = process
|
||||
break;
|
||||
return {addr: host, extra: process}
|
||||
case '4':
|
||||
formData['addr'] = host;
|
||||
formData['extra'] = command;
|
||||
break;
|
||||
return {addr: host, extra: command}
|
||||
case '5':
|
||||
formData['addr'] = addr;
|
||||
break;
|
||||
return {addr}
|
||||
default:
|
||||
throw Error('unknown type')
|
||||
}
|
||||
}
|
||||
|
||||
handleSubmit = () => {
|
||||
this.setState({loading: true});
|
||||
const formData = this.props.form.getFieldsValue();
|
||||
const type = formData['type'];
|
||||
formData['id'] = store.record.id;
|
||||
Object.assign(formData, this._getFieldsValue(type))
|
||||
http.post('/api/monitor/', formData)
|
||||
.then(() => {
|
||||
message.success('操作成功');
|
||||
|
@ -113,6 +109,22 @@ class ComForm extends React.Component {
|
|||
}, () => this.setState({loading: false}))
|
||||
};
|
||||
|
||||
handleTest = () => {
|
||||
this.setState({loading: true});
|
||||
const type = this.props.form.getFieldValue('type');
|
||||
const formData = this._getFieldsValue(type);
|
||||
formData['type'] = type;
|
||||
http.post('/api/monitor/test/', formData)
|
||||
.then(res => {
|
||||
if (res.is_success) {
|
||||
Modal.success({content: res.message})
|
||||
} else {
|
||||
Modal.warning({content: res.message})
|
||||
}
|
||||
})
|
||||
.finally(() => this.setState({loading: false}))
|
||||
}
|
||||
|
||||
getStyle = (t) => {
|
||||
const type = this.props.form.getFieldValue('type');
|
||||
return this.fieldMap[type].includes(t) ? {display: 'block'} : {display: 'none'}
|
||||
|
@ -278,8 +290,12 @@ class ComForm extends React.Component {
|
|||
<Form.Item wrapperCol={{span: 14, offset: 6}}>
|
||||
{page === 1 &&
|
||||
<Button disabled={!b2} type="primary" onClick={this.handleSubmit} loading={loading}>提交</Button>}
|
||||
{page === 0 &&
|
||||
<Button disabled={!b1} type="primary" onClick={() => this.setState({page: page + 1})}>下一步</Button>}
|
||||
{page === 0 && (
|
||||
<div>
|
||||
<Button disabled={!b1} type="primary" onClick={() => this.setState({page: page + 1})}>下一步</Button>
|
||||
<Button disabled={!b1} type="link" loading={loading} style={{marginLeft: 20}} onClick={this.handleTest}>执行测试</Button>
|
||||
</div>
|
||||
)}
|
||||
{page !== 0 &&
|
||||
<Button style={{marginLeft: 20}} onClick={() => this.setState({page: page - 1})}>上一步</Button>}
|
||||
</Form.Item>
|
||||
|
|
Loading…
Reference in New Issue