diff --git a/spug_api/apps/monitor/urls.py b/spug_api/apps/monitor/urls.py index 293a40c..7ff0661 100644 --- a/spug_api/apps/monitor/urls.py +++ b/spug_api/apps/monitor/urls.py @@ -7,4 +7,5 @@ from .views import * urlpatterns = [ path('', DetectionView.as_view()), + path('test/', run_test), ] diff --git a/spug_api/apps/monitor/views.py b/spug_api/apps/monitor/views.py index 16aecc6..40d9d2f 100644 --- a/spug_api/apps/monitor/views.py +++ b/spug_api/apps/monitor/views.py @@ -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) diff --git a/spug_web/src/pages/monitor/Form.js b/spug_web/src/pages/monitor/Form.js index f091b69..5d653a3 100644 --- a/spug_web/src/pages/monitor/Form.js +++ b/spug_web/src/pages/monitor/Form.js @@ -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 { {page === 1 && } - {page === 0 && - } + {page === 0 && ( +
+ + +
+ )} {page !== 0 && }