# up device statistic panel withe role permission
This commit is contained in:
parent
a72be66046
commit
8eb5027b8f
|
@ -84,7 +84,6 @@ class DeviceLogStatisticAPIView(APIView):
|
||||||
# calc daily count, put it in redis as cache.
|
# calc daily count, put it in redis as cache.
|
||||||
if cache.has_key('daily_count'):
|
if cache.has_key('daily_count'):
|
||||||
daily_count = cache.get('daily_count')
|
daily_count = cache.get('daily_count')
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
tz = pytz.timezone("UTC")
|
tz = pytz.timezone("UTC")
|
||||||
today = datetime.now(tz).date()
|
today = datetime.now(tz).date()
|
||||||
|
@ -102,12 +101,21 @@ class DeviceLogStatisticAPIView(APIView):
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
|
|
||||||
class DeviceInfoStatisticAPIView(APIView):
|
class DeviceInfoStatisticAPIView(APIView, RoleMixin):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
online_count = DeviceInfo.objects.filter(online=1).count()
|
user_roles = self.get_role()
|
||||||
offline_count = DeviceInfo.objects.filter(online=0).count()
|
online_devices = DeviceInfo.objects.filter(online=1)
|
||||||
|
offline_devices = DeviceInfo.objects.filter(online=0)
|
||||||
|
if 'staff' in user_roles or 'admin' in user_roles:
|
||||||
|
online_ids = [query.device_id for query in online_devices if query.org == self.request.user.org]
|
||||||
|
offline_ids = [query.device_id for query in offline_devices if query.org == self.request.user.org]
|
||||||
|
online_devices = online_devices.filter(device_id__in=online_ids)
|
||||||
|
offline_devices = offline_devices.filter(device_id__in=offline_ids)
|
||||||
|
online_count = online_devices.count()
|
||||||
|
offline_count = offline_devices.count()
|
||||||
|
|
||||||
data = {'online_count': online_count, 'offline_count': offline_count}
|
data = {'online_count': online_count, 'offline_count': offline_count}
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
|
@ -128,13 +136,6 @@ class DeviceLogHistoryListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
|
||||||
pagination_class = PostPageNumberPagination
|
pagination_class = PostPageNumberPagination
|
||||||
search_fields = ['device_id', 'date']
|
search_fields = ['device_id', 'date']
|
||||||
|
|
||||||
# def get_queryset(self, *args, **kwargs):
|
|
||||||
# queryset_list = DevicePostStatistic.objects.get_queryset().order_by('-date')
|
|
||||||
# device_id = self.request.GET.get('device_id')
|
|
||||||
# if device_id:
|
|
||||||
# queryset_list = queryset_list.filter(device_id__icontains=device_id)
|
|
||||||
# return queryset_list
|
|
||||||
|
|
||||||
def get_queryset(self, *args, **kwargs):
|
def get_queryset(self, *args, **kwargs):
|
||||||
user_roles = self.get_role()
|
user_roles = self.get_role()
|
||||||
queryset_list = DevicePostStatistic.objects.get_queryset().order_by('-date')
|
queryset_list = DevicePostStatistic.objects.get_queryset().order_by('-date')
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from django.core.cache import cache
|
||||||
from counter.models import DeviceInfo
|
from counter.models import DeviceInfo
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,6 +12,10 @@ class RoleMixin(object):
|
||||||
|
|
||||||
class DeviceListMixin(object):
|
class DeviceListMixin(object):
|
||||||
def get_device_list(self):
|
def get_device_list(self):
|
||||||
device_list = DeviceInfo.objects.get_queryset().order_by('-online')
|
if cache.has_key('device_list'):
|
||||||
|
device_list = cache.get('device_list')
|
||||||
|
else:
|
||||||
|
device_list = DeviceInfo.objects.get_queryset().order_by('-online')
|
||||||
|
cache.set('device_list', device_list, 60 * 60)
|
||||||
|
|
||||||
return device_list
|
return device_list
|
||||||
|
|
Loading…
Reference in New Issue