# Buggy code
def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=0):
    """
    @summary: 获取 CMDB 上业务的 IP 列表，以及 agent 状态等信息
    @param request:
    @param bk_biz_id: 业务 CMDB ID
    @param bk_supplier_account: 业务开发商账号
    @param bk_supplier_id: 业务开发商ID
    @params fields: list 查询字段，默认只返回 bk_host_innerip、bk_host_name、bk_host_id, 可以查询主机的任意字段，也可以查询
                set、module、cloud、agent等信息
    @return:
    """
    fields = json.loads(request.GET.get("fields", "[]"))
    client = get_client_by_request(request)
    condition = [
        {
            "bk_obj_id": "host",
            "fields": [],
        }
    ]
    if "set" in fields:
        condition.append(
            {
                "bk_obj_id": "set",
                "fields": [],
            }
        )
    if "module" in fields:
        condition.append(
            {
                "bk_obj_id": "module",
                "fields": [],
            }
        )
    kwargs = {
        "bk_biz_id": bk_biz_id,
        "bk_supplier_account": bk_supplier_account,
        "condition": condition,
    }
    host_result = client.cc.search_host(kwargs)
    if not host_result["result"]:
        message = handle_api_error(
            _("配置平台(CMDB)"), "cc.search_host", kwargs, host_result["message"]
        )
        result = {
            "result": False,
            "code": ERROR_CODES.API_CMDB_ERROR,
            "message": message,
        }
        return JsonResponse(result)

    host_info = host_result["data"]["info"]
    data = []
    default_fields = ["bk_host_innerip", "bk_host_name", "bk_host_id"]
    fields = list(set(default_fields + fields))
    for host in host_info:
        host_detail = {
            field: host["host"][field] for field in fields if field in host["host"]
        }
        if "set" in fields:
            host_detail["set"] = host["set"]
        if "module" in fields:
            host_detail["module"] = host["module"]
        if "cloud" in fields or "agent" in fields:
            host_detail["cloud"] = host["host"]["bk_cloud_id"]
        data.append(host_detail)

    if "agent" in fields:
        agent_kwargs = {
            "bk_biz_id": bk_biz_id,
            "bk_supplier_id": bk_supplier_id,
            "hosts": [
                {"bk_cloud_id": host["cloud"][0]["id"], "ip": host["bk_host_innerip"]}
                for host in data
            ],
        }
        agent_result = client.gse.get_agent_status(agent_kwargs)
        if not agent_result["result"]:
            message = handle_api_error(
                _("管控平台(GSE)"),
                "gse.get_agent_status",
                agent_kwargs,
                agent_result["message"],
            )
            result = {
                "result": False,
                "code": ERROR_CODES.API_GSE_ERROR,
                "message": message,
            }
            return JsonResponse(result)

        agent_data = agent_result["data"]
        for host in data:
            # agent在线状态，0为不在线，1为在线，-1为未知
            agent_info = agent_data.get(
                "%s:%s" % (host["cloud"][0]["id"], host["bk_host_innerip"]), {}
            )
            host["agent"] = agent_info.get("bk_agent_alive", -1)

    result = {"result": True, "code": NO_ERROR, "data": data}
    return JsonResponse(result)


# Traceback (ImportError)
------STARTING: Migrate Database------
Traceback (most recent call last):
File "manage.py", line 27, in <module>
execute_from_command_line(sys.argv)
File "/cache/.bk/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/cache/.bk/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 328, in execute
django.setup()
File "/cache/.bk/env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/cache/.bk/env/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/cache/.bk/env/lib/python2.7/site-packages/django/apps/config.py", line 112, in create
mod = import_module(mod_path)
File "/cache/.bk/env/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/data/app/code/pipeline/apps.py", line 18, in <module>
from rediscluster import StrictRedisCluster
File "/cache/.bk/env/lib/python2.7/site-packages/rediscluster/__init__.py", line 7, in <module>
from .client import StrictRedisCluster, RedisCluster
File "/cache/.bk/env/lib/python2.7/site-packages/rediscluster/client.py", line 10, in <module>
from .connection import (
File "/cache/.bk/env/lib/python2.7/site-packages/rediscluster/connection.py", line 11, in <module>
from .nodemanager import NodeManager
File "/cache/.bk/env/lib/python2.7/site-packages/rediscluster/nodemanager.py", line 12, in <module>
from redis._compat import b, unicode, bytes, long, basestring
ImportError: cannot import name b
------FAILURE: Migrate Database------

# Fixed code
def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=0):
    """
    @summary: 获取 CMDB 上业务的 IP 列表，以及 agent 状态等信息
    @param request:
    @param bk_biz_id: 业务 CMDB ID
    @param bk_supplier_account: 业务开发商账号
    @param bk_supplier_id: 业务开发商ID
    @params fields: list 查询字段，默认只返回 bk_host_innerip、bk_host_name、bk_host_id, 可以查询主机的任意字段，也可以查询
                set、module、cloud、agent等信息
    @return:
    """
    fields = json.loads(request.GET.get("fields", "[]"))
    client = get_client_by_user(request.user.username)
    condition = [
        {
            "bk_obj_id": "host",
            "fields": [],
        }
    ]
    if "set" in fields:
        condition.append(
            {
                "bk_obj_id": "set",
                "fields": [],
            }
        )
    if "module" in fields:
        condition.append(
            {
                "bk_obj_id": "module",
                "fields": [],
            }
        )
    kwargs = {
        "bk_biz_id": bk_biz_id,
        "bk_supplier_account": bk_supplier_account,
        "condition": condition,
    }
    host_result = client.cc.search_host(kwargs)
    if not host_result["result"]:
        message = handle_api_error(
            _("配置平台(CMDB)"), "cc.search_host", kwargs, host_result["message"]
        )
        result = {
            "result": False,
            "code": ERROR_CODES.API_CMDB_ERROR,
            "message": message,
        }
        return JsonResponse(result)

    host_info = host_result["data"]["info"]
    data = []
    default_fields = ["bk_host_innerip", "bk_host_name", "bk_host_id"]
    fields = list(set(default_fields + fields))
    for host in host_info:
        host_detail = {
            field: host["host"][field] for field in fields if field in host["host"]
        }
        if "set" in fields:
            host_detail["set"] = host["set"]
        if "module" in fields:
            host_detail["module"] = host["module"]
        if "cloud" in fields or "agent" in fields:
            host_detail["cloud"] = host["host"]["bk_cloud_id"]
        data.append(host_detail)

    if "agent" in fields:
        agent_kwargs = {
            "bk_biz_id": bk_biz_id,
            "bk_supplier_id": bk_supplier_id,
            "hosts": [
                {"bk_cloud_id": host["cloud"][0]["id"], "ip": host["bk_host_innerip"]}
                for host in data
            ],
        }
        agent_result = client.gse.get_agent_status(agent_kwargs)
        if not agent_result["result"]:
            message = handle_api_error(
                _("管控平台(GSE)"),
                "gse.get_agent_status",
                agent_kwargs,
                agent_result["message"],
            )
            result = {
                "result": False,
                "code": ERROR_CODES.API_GSE_ERROR,
                "message": message,
            }
            return JsonResponse(result)

        agent_data = agent_result["data"]
        for host in data:
            # agent在线状态，0为不在线，1为在线，-1为未知
            agent_info = agent_data.get(
                "%s:%s" % (host["cloud"][0]["id"], host["bk_host_innerip"]), {}
            )
            host["agent"] = agent_info.get("bk_agent_alive", -1)

    result = {"result": True, "code": NO_ERROR, "data": data}
    return JsonResponse(result)