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