spug/spug_api/apps/monitor/views.py

77 lines
3.4 KiB
Python
Raw Normal View History

2019-12-02 04:37:59 +00:00
from django.views.generic import View
2019-12-19 15:49:40 +00:00
from libs import json_response, JsonParser, Argument, human_datetime
2019-12-02 04:37:59 +00:00
from apps.monitor.models import Detection
2019-12-02 15:18:38 +00:00
from django_redis import get_redis_connection
from django.conf import settings
import json
2019-12-02 04:37:59 +00:00
class DetectionView(View):
def get(self, request):
detections = Detection.objects.all()
return json_response(detections)
def post(self, request):
form, error = JsonParser(
Argument('id', type=int, required=False),
Argument('name', help='请输入任务名称'),
Argument('addr', help='请输入监控地址'),
Argument('type', filter=lambda x: x in dict(Detection.TYPES), help='请选择监控类型'),
Argument('extra', required=False),
Argument('desc', required=False),
Argument('rate', type=int, default=5),
Argument('threshold', type=int, default=3),
2019-12-05 15:08:37 +00:00
Argument('quiet', type=int, default=24 * 60),
Argument('notify_grp', type=list, help='请选择报警联系组'),
Argument('notify_mode', type=list, help='请选择报警方式'),
2019-12-02 04:37:59 +00:00
).parse(request.body)
if error is None:
2019-12-05 15:08:37 +00:00
form.notify_grp = json.dumps(form.notify_grp)
form.notify_mode = json.dumps(form.notify_mode)
2019-12-02 04:37:59 +00:00
if form.id:
2019-12-02 15:18:38 +00:00
Detection.objects.filter(pk=form.id).update(
2019-12-19 15:49:40 +00:00
updated_at=human_datetime(),
2019-12-02 15:18:38 +00:00
updated_by=request.user,
**form)
task = Detection.objects.filter(pk=form.id).first()
if task and task.is_active:
form.action = 'modify'
rds_cli = get_redis_connection()
rds_cli.rpush(settings.MONITOR_KEY, json.dumps(form))
2019-12-02 04:37:59 +00:00
else:
2019-12-02 15:18:38 +00:00
Detection.objects.create(created_by=request.user, **form)
form.action = 'add'
rds_cli = get_redis_connection()
rds_cli.rpush(settings.MONITOR_KEY, json.dumps(form))
return json_response(error=error)
def patch(self, request):
form, error = JsonParser(
Argument('id', type=int, help='请指定操作对象'),
Argument('is_active', type=bool, required=False)
).parse(request.body, True)
if error is None:
Detection.objects.filter(pk=form.id).update(**form)
if form.get('is_active') is not None:
if form.is_active:
task = Detection.objects.filter(pk=form.id).first()
message = {'id': form.id, 'action': 'add'}
message.update(task.to_dict(selects=('addr', 'extra', 'rate', 'type')))
else:
message = {'id': form.id, 'action': 'remove'}
rds_cli = get_redis_connection()
rds_cli.rpush(settings.MONITOR_KEY, json.dumps(message))
2019-12-02 04:37:59 +00:00
return json_response(error=error)
def delete(self, request):
form, error = JsonParser(
Argument('id', type=int, help='请指定操作对象')
).parse(request.GET)
if error is None:
2019-12-02 15:18:38 +00:00
task = Detection.objects.filter(pk=form.id).first()
if task:
if task.is_active:
return json_response(error='该监控项正在运行中,请先停止后再尝试删除')
task.delete()
2019-12-02 04:37:59 +00:00
return json_response(error=error)