-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
149 lines (113 loc) · 5.4 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from django.shortcuts import render
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from dashboard.models import Switches, Commands, Devices, DataPoints
from dashboard.forms import SwitchForm, CommandForm, DeviceForm
from dashboard.scheduler import Processes
from django.http import JsonResponse, HttpResponseNotFound, HttpResponse
from datetime import datetime
# Create your views here.
@ensure_csrf_cookie
@never_cache
def dashboard(request):
context = {}
context['switches'] = {switch:[device.pk for device in Devices.objects.filter(switch=switch).order_by('name')] for switch in Switches.objects.all().order_by('name')}
context['poll_status'] = {switch.pk:Processes.isProcessRunning(switch.pk) for switch in Switches.objects.all().order_by('name')}
return render(request, "dashboard/index.html", context)
@never_cache
def all_graphs(request):
if request.method == "POST":
start = datetime.strptime(request.POST['start'], "%Y-%m-%d %H:%M")
end = datetime.strptime(request.POST['end'], "%Y-%m-%d %H:%M")
data_points = DataPoints.objects.filter(datetime__gt=start, datetime__lt=end)
graphs = {}
for device in Devices.objects.all():
if data_points.filter(device=device).exists():
graphs[device.pk] = {}
graphs[device.pk]['title'] = f"{device.name} ({device.port})"
graphs[device.pk]['start'] = start.timestamp()*1000
graphs[device.pk]['end'] = end.timestamp()*1000
graphs[device.pk]['in'] = [{'x':data.datetime.timestamp()*1000, 'y':data.bytes / 1000 / data.interval.total_seconds()} for data in data_points.filter(device=device, input=True).order_by('datetime')]
graphs[device.pk]['out'] = [{'x':data.datetime.timestamp()*1000, 'y':data.bytes / 1000 / data.interval.total_seconds()} for data in data_points.filter(device=device, input=False).order_by('datetime')]
return JsonResponse(graphs)
return HttpResponseNotFound("This path does not support GET requests.")
@never_cache
def get_switch_commands(request, pk):
if request.method == "GET":
switch = Switches.objects.get(pk=pk)
response = {'commands': {str(command):command.pk for command in Commands.objects.filter(switch=switch).order_by('priority')}}
return JsonResponse(response)
return HttpResponseNotFound("This path does not support POST requests.")
def delete_switch(request, pk):
if request.method == "GET":
Switches.objects.get(pk=pk).delete()
return HttpResponse('')
return HttpResponseNotFound("This path does not support POST requests.")
def delete_device(request, pk):
if request.method == "GET":
Devices.objects.get(pk=pk).delete()
return HttpResponse('')
return HttpResponseNotFound("This path does not support POST requests.")
def start_switch(request, pk):
if request.method == "GET":
if Processes.startProcess(pk) is True:
response = HttpResponse("This switch has been started.")
else:
response = HttpResponse("This switch is already being polled.")
response.status_code = 409
return response
return HttpResponseNotFound("This path does not support POST requests.")
def stop_switch(request, pk):
if request.method == "GET":
if Processes.stopProcess(pk) is True:
response = HttpResponse("This switch has been stopped.")
else:
response = HttpResponse("This switch is already stopped.")
response.status_code = 409
return response
return HttpResponseNotFound("This path does not support POST requests.")
def delete_device_data(request, pk):
if request.method == "POST":
start = datetime.strptime(request.POST['start'], "%Y-%m-%d %H:%M")
end = datetime.strptime(request.POST['end'], "%Y-%m-%d %H:%M")
device = Devices.objects.get(pk=pk)
DataPoints.objects.filter(device=device, datetime__gt=start, datetime__lt=end).delete()
return HttpResponse("Data points deleted.")
return HttpResponseNotFound("This path does not support GET requests.")
class SwitchCreateView(CreateView):
template_name = "form/index.html"
extra_context = {'title': 'Add Switch'}
model = Switches
form_class = SwitchForm
success_url = "/"
class SwitchUpdateView(UpdateView):
template_name = "form/index.html"
extra_context = {'title': 'Update Switch'}
model = Switches
form_class = SwitchForm
success_url = "/"
class CommandCreateView(CreateView):
template_name = "form/index.html"
extra_context = {'title': 'Add Command'}
model = Commands
form_class = CommandForm
success_url = "/"
class CommandUpdateView(UpdateView):
template_name = "form/index.html"
extra_context = {'title': 'Update Command'}
model = Commands
form_class = CommandForm
success_url = "/"
class DeviceCreateView(CreateView):
template_name = "form/index.html"
extra_context = {'title': 'Add Device'}
model = Devices
form_class = DeviceForm
success_url = "/"
class DeviceUpdateView(UpdateView):
template_name = "form/index.html"
extra_context = {'title': 'Update Device'}
model = Devices
form_class = DeviceForm
success_url = "/"