catalog_real_tree.py 2.8 KB
# coding=utf-8
#author:        4N
#createtime:    2021/3/9
#email:         nheweijun@sina.com


from ..models import ServiceCatalog,Service,ServiceEngine
from app.util.component.ApiTemplate import ApiTemplate
import requests
from app.util.component.StructurePrint import StructurePrint
class Api(ApiTemplate):
    api_name = "目录树"
    def process(self):

        # 返回结果
        res = {}
        try:
            # 业务逻辑

            catalogs = ServiceCatalog.query.all()


            # 获取全部影像服务
            image_engines = ServiceEngine.query.filter_by(type="ImageServer").all()
            image_services = []
            for ie in image_engines:
                try:
                    url = "{}/API/Service/List".format(ie.url)
                    response:requests.Response = requests.post(url,{"page_size":1000})
                    if not response.json().get("result"):
                        raise Exception("获取影像服务List失败!")
                    else:
                        image_services.extend(response.json()["data"]["list"])
                except:
                    StructurePrint().print("{}访问失败".format(ie.url))
            tree_origin = []
            for cata in catalogs:

                catalog_guids = [c.guid for c in ServiceCatalog.query.filter(ServiceCatalog.path.like("%" + cata.guid + "%")).all()]
                service_count = Service.query.filter(Service.catalog_guid.in_(catalog_guids)).count()

                image_service_count = len([igs for igs in image_services if igs["catalog_guid"] in catalog_guids])

                cata_json ={}

                cata_json["description"] = cata.description
                cata_json["guid"] = cata.guid
                cata_json["name"] = cata.name
                cata_json["path"] = cata.path
                cata_json["pguid"] = cata.pguid
                cata_json["sort"] = cata.sort
                cata_json["service_count"]=service_count+image_service_count
                cata_json["children"] = []
                tree_origin.append(cata_json)

            for cata in tree_origin:
                cata_pguid = cata["pguid"]
                if not cata_pguid=="0":
                    for c in tree_origin:
                        if c["guid"].__eq__(cata_pguid):
                            c["children"].append(cata)

            res["data"] = [cata for cata in tree_origin if cata["pguid"].__eq__("0")]
            res["result"] = True
        except Exception as e:
            raise e
        return res

    api_doc={
    
    "tags":["服务目录接口"],
    "parameters":[
    ],
    "responses":{
        200:{
            "schema":{
                "properties":{
                }
            }
            }
        }
    }