wg-backend-django/wg_api/views.py
2022-11-22 14:07:36 +07:00

175 lines
4.7 KiB
Python

from django.shortcuts import render
from django.http import HttpResponse
# from django.core import serializer
from rest_framework import viewsets
from rest_framework.permissions import IsAdminUser
from .serializers import wgSerializer
from .models import user_model
from .utils.html_parser import get_logs, get_profiles
import json
from .utils.statistics import count_clients, count_traffic, count_connection, count_percentage, count_broken
# Create your views here.
class wgView(viewsets.ModelViewSet):
# queryset = user_model.objects.all()
queryset = user_model.objects.all()
serializer_class = wgSerializer
# print(">"*100," ",inf_data)
def get(self, request):
return self.inf_connector()
# iserializer = wgSerializer
# queryset = user_model.objects.all()
# serializers_class = wgSerializer
# serializer = wgSerializer
# permission_classes = [IsAdminUser]
def wgViewLogs(request):
param = request.GET.get('param', '')
data = get_logs()
data = json.loads(data)
final_data = []
if param == "d2201":
print("x"*100)
i = 1
for x in data:
if "DTS22_PNUP_01" in str(x["wgName"]):
print(x)
x["data_id"]=i
final_data.append(x)
i = i+1
elif param == "d2202":
print("x"*100)
i = 1
for x in data:
if "DTS22_PNUP_02" in str(x["wgName"]):
print(x)
x["data_id"]=i
final_data.append(x)
i = i+1
elif param == "d2203":
print("x"*100)
i = 1
for x in data:
if "DTS22_PNUP_03" in str(x["wgName"]):
print(x)
x["data_id"]=i
final_data.append(x)
i = i+1
elif param == "d2204":
print("x"*100)
i = 1
for x in data:
if "DTS22_PNUP_04" in str(x["wgName"]):
print(x)
x["data_id"]=i
final_data.append(x)
i = i+1
elif param == "d2205":
print("x"*100)
i = 1
for x in data:
if "DTS22_PNUP_05" in str(x["wgName"]):
print(x)
x["data_id"]=i
final_data.append(x)
i = i+1
elif param == "d2206":
print("x"*100)
i = 1
for x in data:
if "DTS22_TELU" in str(x["wgName"]):
print(x)
x["data_id"]=i
final_data.append(x)
i = i+1
elif param == "GCI":
print("x"*100)
i = 1
for x in data:
if "GCI" in str(x["wgName"]):
x["data_id"]=i
print(x)
final_data.append(x)
i = i+1
elif param == "META":
print("x"*100)
i = 1
for x in data:
if "NIS" in str(x["wgName"]):
print(x["data_id"])
x["data_id"]=i
print(x["data_id"])
final_data.append(x)
i = i+1
else:
final_data = data
final_data = json.dumps(final_data)
return HttpResponse(final_data, content_type='application')
def wgViewClients(request):
param = request.GET.get('param', '')
filter = request.GET.get('filter', '')
data = get_profiles()
data = json.loads(data)
final_data = []
if filter :
print("x"*100)
i = 1
for x in data:
if str(filter).lower() in str(x["name"]).lower():
print(x["data_id"])
x["data_id"]=i
print(x["data_id"])
final_data.append(x)
i = i+1
final_data = json.dumps(final_data)
elif param == "stats":
data_logs = get_logs()
data = json.loads(data_logs)
sum_data = count_clients(data)
connected_data = count_connection(data)
traffic_data = len(data)
percentage_data = count_percentage(data)
broken_data = count_broken(data)
traffic_data = count_traffic(data)
if traffic_data > 1000000000:
traffic_data="{:.2f}".format(traffic_data/1000000000)+" GB"
else:
traffic_data="{:.2f}".format(traffic_data/1000000)+" MB"
data = {}
data["clienttotal"]=sum_data
data["traffictotal"]=traffic_data
data["connectedtotal"]=connected_data
data["percentagetotal"]=percentage_data
data["brokentotal"]=broken_data
final_data = json.dumps(data)
else :
final_data = json.dumps(data)
# print(request.GET.get('param', ''))
return HttpResponse(final_data, content_type='application')