Exemplo n.º 1
0
 def get(self, request, team_id, region_name, app_id, *args, **kwargs):
     is_outer = request.GET.get("is_outer", False)
     if is_outer == "true":
         is_outer = True
     data = []
     start = request.GET.get("start")
     end = request.GET.get("end")
     step = request.GET.get("step", 60)
     if not start or not end:
         raise ServiceHandleException(msg="params error", msg_show="缺少query参数")
     services_relation = group_service_relation_repo.get_services_by_group(self.app.ID)
     service_ids = services_relation.values_list('service_id', flat=True)
     if service_ids:
         services = service_repo.get_services_by_service_ids(service_ids).exclude(service_source="third_party")
         for service in services:
             is_outer_service = True
             has_plugin = False
             service_abled_plugins = app_plugin_service.get_service_abled_plugin(service)
             for plugin in service_abled_plugins:
                 if plugin.category == PluginCategoryConstants.PERFORMANCE_ANALYSIS:
                     has_plugin = True
             if is_outer:
                 is_outer_service = False
                 tenant_service_ports = port_service.get_service_ports(service)
                 for service_port in tenant_service_ports:
                     if service_port.is_outer_service:
                         is_outer_service = True
                         break
             if has_plugin and is_outer_service:
                 dt = {
                     "service_id": service.service_id,
                     "service_cname": service.service_cname,
                     "service_alias": service.service_alias,
                     "monitors": []
                 }
                 for k, v in list(monitor_query_range_items.items()):
                     monitor = {"monitor_item": k}
                     body = {}
                     try:
                         res, body = region_api.get_query_range_data(self.region_name, self.team.tenant_name,
                                                                     v % (service.service_id, start, end, step))
                     except Exception as e:
                         logger.debug(e)
                     if body.get("data"):
                         if body["data"]["result"]:
                             result_list = []
                             for result in body["data"]["result"]:
                                 result["value"] = [str(value) for value in result["value"]]
                                 result_list.append(result)
                             body["data"]["result"] = result_list
                             monitor.update(body)
                             dt["monitors"].append(monitor)
                 data.append(dt)
     serializers = ComponentMonitorSerializers(data=data, many=True)
     serializers.is_valid(raise_exception=True)
     return Response(serializers.data, status=200)
Exemplo n.º 2
0
class TeamAppsMonitorQueryView(TeamAppAPIView):
    @swagger_auto_schema(
        operation_description="应用下组件实时监控",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用id", type=openapi.TYPE_INTEGER),
            openapi.Parameter(
                "is_outer", openapi.IN_QUERY, description="是否只获取对外组件监控", type=openapi.TYPE_STRING, enum=["false", "true"]),
        ],
        responses={200: ComponentMonitorSerializers(many=True)},
        tags=['openapi-apps'],
    )
    def get(self, request, team_id, region_name, app_id, *args, **kwargs):
        is_outer = request.GET.get("is_outer", False)
        if is_outer == "true":
            is_outer = True
        data = []
        services_relation = group_service_relation_repo.get_services_by_group(self.app.ID)
        service_ids = services_relation.values_list('service_id', flat=True)
        if service_ids:
            services = service_repo.get_services_by_service_ids(service_ids).exclude(service_source="third_party")
            for service in services:
                is_outer_service = True
                has_plugin = False
                service_abled_plugins = app_plugin_service.get_service_abled_plugin(service)
                for plugin in service_abled_plugins:
                    if plugin.category == PluginCategoryConstants.PERFORMANCE_ANALYSIS:
                        has_plugin = True
                if is_outer:
                    is_outer_service = False
                    tenant_service_ports = port_service.get_service_ports(service)
                    for service_port in tenant_service_ports:
                        if service_port.is_outer_service:
                            is_outer_service = True
                            break
                if has_plugin and is_outer_service:
                    dt = {
                        "service_id": service.service_id,
                        "service_cname": service.service_cname,
                        "service_alias": service.service_alias,
                        "monitors": []
                    }
                    for k, v in list(monitor_query_items.items()):
                        monitor = {"monitor_item": k}
                        res, body = region_api.get_query_data(self.region_name, self.team.tenant_name, v % service.service_id)
                        if body.get("data"):
                            if body["data"]["result"]:
                                result_list = []
                                for result in body["data"]["result"]:
                                    result["value"] = [str(value) for value in result["value"]]
                                    result_list.append(result)
                                body["data"]["result"] = result_list
                                monitor.update(body)
                                dt["monitors"].append(monitor)
                    data.append(dt)
        serializers = ComponentMonitorSerializers(data=data, many=True)
        serializers.is_valid(raise_exception=True)
        return Response(serializers.data, status=200)
Exemplo n.º 3
0
class TeamAppsMonitorQueryRangeView(TeamAppAPIView):
    @swagger_auto_schema(
        operation_description="应用下组件历史监控",
        manual_parameters=[
            openapi.Parameter("team_id",
                              openapi.IN_PATH,
                              description="团队ID、名称",
                              type=openapi.TYPE_STRING),
            openapi.Parameter("region_name",
                              openapi.IN_PATH,
                              description="数据中心名称",
                              type=openapi.TYPE_STRING),
            openapi.Parameter("app_id",
                              openapi.IN_PATH,
                              description="应用组id",
                              type=openapi.TYPE_INTEGER),
            openapi.Parameter("start",
                              openapi.IN_PATH,
                              description="起始时间戳",
                              type=openapi.TYPE_NUMBER),
            openapi.Parameter("end",
                              openapi.IN_PATH,
                              description="结束时间戳",
                              type=openapi.TYPE_NUMBER),
            openapi.Parameter("step",
                              openapi.IN_PATH,
                              description="步长(默认60)",
                              type=openapi.TYPE_NUMBER),
            openapi.Parameter("is_outer",
                              openapi.IN_QUERY,
                              description="是否只获取对外组件监控",
                              type=openapi.TYPE_STRING,
                              enum=["false", "true"]),
        ],
        responses={200: ComponentMonitorSerializers(many=True)},
        tags=['openapi-apps'],
    )
    def get(self, request, team_id, region_name, app_id, *args, **kwargs):
        is_outer = request.GET.get("is_outer", False)
        if is_outer == "true":
            is_outer = True
        data = []
        start = request.GET.get("start")
        end = request.GET.get("end")
        step = request.GET.get("step", 60)
        if not start or not end:
            raise ServiceHandleException(msg="params error",
                                         msg_show=u"缺少query参数")
        services_relation = group_service_relation_repo.get_services_by_group(
            self.app.ID)
        service_ids = services_relation.values_list('service_id', flat=True)
        if service_ids:
            services = service_repo.get_services_by_service_ids(
                service_ids).exclude(service_source="third_party")
            for service in services:
                is_outer_service = True
                has_plugin = False
                service_abled_plugins = app_plugin_service.get_service_abled_plugin(
                    service)
                for plugin in service_abled_plugins:
                    if plugin.category == PluginCategoryConstants.PERFORMANCE_ANALYSIS:
                        has_plugin = True
                if is_outer:
                    is_outer_service = False
                    tenant_service_ports = port_service.get_service_ports(
                        service)
                    for service_port in tenant_service_ports:
                        if service_port.is_outer_service:
                            is_outer_service = True
                            break
                if has_plugin and is_outer_service:
                    dt = {
                        "service_id": service.service_id,
                        "service_cname": service.service_cname,
                        "service_alias": service.service_alias,
                        "monitors": []
                    }
                    for k, v in monitor_query_range_items.items():
                        monitor = {"monitor_item": k}
                        body = {}
                        try:
                            res, body = region_api.get_query_range_data(
                                self.region_name, self.team.tenant_name,
                                v % (service.service_id, start, end, step))
                        except Exception as e:
                            logger.debug(e)
                        if body.get("data"):
                            if body["data"]["result"]:
                                result_list = []
                                for result in body["data"]["result"]:
                                    result["value"] = [
                                        str(value) for value in result["value"]
                                    ]
                                    result_list.append(result)
                                body["data"]["result"] = result_list
                                monitor.update(body)
                                dt["monitors"].append(monitor)
                    data.append(dt)
        serializers = ComponentMonitorSerializers(data=data, many=True)
        serializers.is_valid(raise_exception=True)
        return Response(serializers.data, status=200)