Example #1
0
 def get_object_list(self, request, model, serializer, **kwargs):
     try:
         page_size = kwargs.get('page_size', None)
         page = YzyWebPagination()
         if page_size:
             page.page_size = page_size
             kwargs.pop('page_size')
         contain = kwargs.get('contain', None)
         logger.debug("table:%s, query conditions:%s", model._meta.db_table,
                      kwargs)
         # 默认模糊查询条件是and,如n果需要使用or,则在外层组合好,通过contain字段传递进来
         if contain:
             logger.debug("get contain query filter:%s", contain)
             kwargs.pop('contain')
             query_set = model.objects.filter(**kwargs).filter(contain)
         else:
             query_set = model.objects.filter(**kwargs)
         instance = page.paginate_queryset(queryset=query_set,
                                           request=request,
                                           view=self)
         ser = serializer(instance=instance,
                          many=True,
                          context={'request': request})
         return page.get_paginated_response(ser.data)
     except Exception as e:
         logger.error("get all data from table:%s error:%s",
                      model._meta.db_table, e)
         raise e
Example #2
0
    def get(self, request, *args, **kwargs):
        page = YzyWebPagination()
        group_uuid = request.GET.get("uuid", "")
        group_type = request.GET.get("type")
        _filter = request.GET.get("filter")
        query_set = YzyTerminal.objects
        if group_uuid:
            group_info = terminal_mgr.get_group_info(group_uuid)
            if not group_info:
                raise Http404()
            group_name = group_info.name
            query_set = query_set.filter(group_uuid=group_uuid).order_by("terminal_id")
        else:
            query_set = query_set.filter(Q(group_uuid__isnull=True) | Q(group_uuid='')).order_by("terminal_id")
            group_name = "未分组"

        if _filter:
            query_set = query_set.filter(Q(name__contains=_filter) | Q(ip__contains=_filter))
        terminals = page.paginate_queryset(queryset=query_set, request=request, view=self)
        ser = YzyTerminalSerializer(instance=terminals, many=True, context={'request': request})
        group_dict = {"name": group_name, "uuid": group_uuid, "open_num": 0, "close_num": 0}
        # import pdb; pdb.set_trace()
        all_terminals = query_set.all()
        for i in all_terminals:
            if str(i.status) == "0":
                group_dict["close_num"] += 1
            else:
                group_dict["open_num"] += 1

        # ret = terminal_mgr.get_all_terminal(request, group_type, group_uuid, _filter, page)
        # return JSONResponse(ret)
        return page.get_paginated_response(ser.data, {"group_info": group_dict})
Example #3
0
 def get(self, request, *args, **kwargs):
     page = YzyWebPagination()
     roles = self.get_object_list(request, page)
     ser = YzyRoleSerializer(instance=roles,
                             many=True,
                             context={'request': request})
     return page.get_paginated_response(ser.data)
Example #4
0
    def get_desktop_info(self, request, uuid):
        logger.debug("start get instance info")
        resp = get_error_result("Success")
        try:
            node = YzyNodes.objects.filter(deleted=False, uuid=uuid).first()
            if not node:
                logger.error("get desktop info error: ParameterError")
                ret = get_error_result("ParameterError")
                return JSONResponse(ret)
            resp['data'] = {}
            instances = YzyInstances.objects.filter(deleted=False,
                                                    host=node).all()
            power_instance = instances.filter(
                status=constants.STATUS_ACTIVE).count()
            error_instance = instances.exclude(message='').count()
            resp['data']['node_name'] = node.name
            resp['data']['power_instance'] = power_instance
            resp['data']['error_instance'] = error_instance
            resp['data']['total_instance'] = len(instances)
            resp['data']['shutdown_instance'] = len(instances) - power_instance

            page = YzyWebPagination()
            desktops = page.paginate_queryset(queryset=instances,
                                              request=request,
                                              view=self)
            ser = YzyWarningInfoDesktopSerializer(instance=desktops,
                                                  many=True,
                                                  context={'request': request})
            resp['data']['instances'] = ser.data
            logger.info("get instance info success: %s", desktops)
            return JSONResponse(resp)
        except Exception as e:
            logger.error("get instance info error:%s", e, exc_info=True)
            ret = get_error_result("OtherError")
            return JSONResponse(ret)
Example #5
0
 def get(self, request, *args, **kwargs):
     try:
         page = YzyWebPagination()
         search_type = request.GET.get("search_type", "")
         time_frame = request.GET.get("time_frame", "")
         status = request.GET.get("status", "")
         query_set = YzyTask.objects.filter(deleted=False).all()
         if search_type == "current":
             query_set = query_set[:5]
         if status:
             query_set = query_set.filter(status=status).all()
         if time_frame:
             date = self.to_date(time_frame)
             tz = timezone.get_default_timezone()
             utc_date = date.astimezone(tz)
             query_set = query_set.filter(created_at__gte=utc_date).all()
         operates = page.paginate_queryset(queryset=query_set,
                                           request=request,
                                           view=self)
         ser = YzyTaskSerializer(instance=operates,
                                 many=True,
                                 context={'request': request})
         return page.get_paginated_response(ser.data)
     except Exception as e:
         logger.error("get task info error: %s", e)
         return JsonResponse(get_error_result("OtherError"))
Example #6
0
    def get(self, request, *args, **kwargs):
        page = YzyWebPagination()
        group_uuid = request.GET.get("uuid", "")
        group_type = request.GET.get("type")
        _filter = request.GET.get("filter")
        status = request.GET.get("status", '-1')
        query_set = YzyVoiTerminal.objects
        if group_uuid:
            group_info = voi_terminal_mgr.get_group_info(group_uuid)
            if not group_info:
                raise Http404()
            group_name = group_info.name
            query_set = query_set.filter(group_uuid=group_uuid)
        else:
            query_set = query_set.filter(
                Q(group_uuid__isnull=True)
                | Q(group_uuid='')).order_by("terminal_id")
            group_name = "未分组"

        if status != '-1':
            query_set = query_set.filter(status=status)
        if _filter:
            query_set = query_set.filter(
                Q(name__contains=_filter) | Q(ip__contains=_filter))
        terminals = page.paginate_queryset(queryset=query_set,
                                           request=request,
                                           view=self)
        ser = YzyVoiTerminalSerializer(instance=terminals,
                                       many=True,
                                       context={'request': request})
        group_dict = {
            "name": group_name,
            "uuid": group_uuid,
            "close_num": 0,
            "uefi_num": 0,
            "linux_num": 0,
            "windows_num": 0,
            "u_linux_num": 0
        }
        # import pdb; pdb.set_trace()
        all_terminals = query_set.all()
        for i in all_terminals:
            if i.status == 0:
                group_dict["close_num"] += 1
            elif i.status == 1:
                group_dict["uefi_num"] += 1
            elif i.status == 2:
                group_dict["linux_num"] += 1
            elif i.status == 3:
                group_dict["windows_num"] += 1
            elif i.status == 5:
                group_dict["u_linux_num"] += 1

        return page.get_paginated_response(ser.data,
                                           {"group_info": group_dict})
Example #7
0
 def get(self, request, *args, **kwargs):
     try:
         page = YzyWebPagination()
         query_set = YzyOperationLog.objects.filter(
             deleted=False).order_by('-id')
         nodes = page.paginate_queryset(queryset=query_set,
                                        request=request,
                                        view=self)
         ser = OperationLogSerializer(instance=nodes,
                                      many=True,
                                      context={'request': request})
         return page.get_paginated_response(ser.data)
     except Exception as e:
         logger.error("get operation log error:%s", e)
         return HttpResponseServerError()
Example #8
0
    def get(self, request, *args, **kwargs):
        option = request.GET.get('option', '')
        date = request.GET.get('date', '')
        page = YzyWebPagination()
        query_set = YzyWarningLog.objects.filter(deleted=False)
        if option:
            query_set = query_set.filter(option=option)
        if date:
            filter_date = datetime.datetime.strptime(date, "%Y-%m-%d")
            tz = timezone.get_default_timezone()
            start = filter_date.replace(tzinfo=tz)
            end = start + datetime.timedelta(days=1)
            query_set = query_set.filter(created_at__range=[start, end])

        warning_logs = page.paginate_queryset(queryset=query_set,
                                              request=request,
                                              view=self)
        ser = YzyWarningLogSerializer(instance=warning_logs,
                                      many=True,
                                      context={'request': request})
        return page.get_paginated_response(ser.data)
Example #9
0
    def get(self, request, *args, **kwargs):
        user_id = request.GET.get("user_id", "")
        date = request.GET.get("date", "")
        page = YzyWebPagination()
        query_set = education_model.YzyOperationLog.objects.filter(
            deleted=False)
        if user_id:
            query_set = query_set.filter(user_id=user_id)
        if date:
            filter_date = datetime.datetime.strptime(date, "%Y-%m-%d")
            tz = timezone.get_default_timezone()
            start = filter_date.replace(tzinfo=tz)
            end = start + datetime.timedelta(days=1)
            query_set = query_set.filter(created_at__range=[start, end])

        operates = page.paginate_queryset(queryset=query_set,
                                          request=request,
                                          view=self)
        ser = education_serializer.OperationLogSerializer(
            instance=operates, many=True, context={'request': request})
        return page.get_paginated_response(ser.data)
Example #10
0
    def get_node_info(self, request):
        logger.debug("start get node info")
        resp = get_error_result("Success")
        try:
            nodes = YzyNodes.objects.filter(deleted=False).all()
            power_nodes = nodes.exclude(
                status=constants.STATUS_SHUTDOWN).count()
            resp['data'] = {}
            resp['data']['curr_time'] = time.strftime('%Y/%m/%d %H:%M',
                                                      time.localtime())
            resp['data']['node_total'] = len(nodes)
            resp['data']['power_node'] = power_nodes
            resp['data']['shutdown_node'] = len(nodes) - power_nodes

            instances = YzyInstances.objects.filter(deleted=False).all()
            # voi_instances = YzyVoiTerminal.objects.filter(deleted=False).all()
            power_desktop = instances.filter(
                status=constants.STATUS_ACTIVE).count()
            error_desktop = instances.exclude(message='').count()
            resp['data']['desktop_total'] = len(instances)
            resp['data']['power_desktop'] = power_desktop
            resp['data']['shutdown_desktop'] = len(instances) - power_desktop
            resp['data']['error_desktop'] = error_desktop

            page = YzyWebPagination()
            controller_nodes = page.paginate_queryset(queryset=nodes,
                                                      request=request,
                                                      view=self)
            ser = YzyWarningInfoSerializer(instance=controller_nodes,
                                           many=True,
                                           context={'request': request})
            resp['data']['nodes'] = ser.data
            logger.info("get node info success: %s", nodes)
            return JSONResponse(resp)
        except Exception as e:
            logger.error("get node info error:%s", e, exc_info=True)
            ret = get_error_result("OtherError")
            return JSONResponse(ret)