spug_expand/dashboard/views.py

105 lines
3.1 KiB
Python

import json
from django.db import connection
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def monotoring(request):
with connection.cursor() as cursor:
cursor.execute("select * from detections")
row = cursor.fetchall()
res = []
for r in row:
o = dict()
o['name'] = r[1]
if r[2] == '1':
o['type'] = '站点检测'
elif r[2] == '2':
o['type'] = '端口检测'
elif r[2] == '3':
o['type'] = '进程检测'
elif r[2] == '4':
o['type'] = '自定义脚本'
elif r[2] == '5':
o['type'] = 'Ping检测'
o['addr'] = r[3]
o['rate'] = r[7]
o['latest'] = r[13]
o['latest_run_time'] = r[14]
res.append(o)
return HttpResponse(json.dumps({
"status": "1",
"res": res
}))
def alarm(request):
name = []
with connection.cursor() as cursor:
cursor.execute("select name from alarms group by name")
row = cursor.fetchall()
for r in row:
name.append(r[0])
res = []
for n in name:
with connection.cursor() as cursor1:
cursor1.execute("select * from alarms where name = '%s' order by created_at desc " % (n))
row1 = cursor1.fetchall()
data = []
data_all = dict()
# status = []
# duration=[]
# created_at=[]
data_all['title'] = n
for r1 in row1:
jc_data = []
jc_data.append(r1[5])
jc_data.append(r1[6])
jc_data.append(r1[7])
data.append(jc_data)
# data.append(duration)
# data.append(created_at)
data_all['data'] = data
res.append(data_all)
print("===================================================================")
# res.append(alarms)
# print(res)
return HttpResponse(json.dumps({
"status": "1",
"name": name,
"res":res
}))
def task_histories(request):
histories = []
task_id = []
with connection.cursor() as cursor:
cursor.execute("select * from task_histories order by run_time desc limit 20;")
row = cursor.fetchall()
res = []
for r in row:
o = dict()
print(r)
histories.append(r)
task_id.append(r[1])
o['status'] = r[2]
o['run_time'] = r[3]
o['output'] = r[4]
with connection.cursor() as cursor1:
cursor1.execute("select * from tasks where id = '%s'" % (r[1]))
row1 = cursor1.fetchall()
for r1 in row1:
o['task_name'] = r1[1]
o['task_type'] = r1[2]
o['desc'] = r1[8]
res.append(o)
return HttpResponse(json.dumps({
"status":"1",
"res":res
}))