修复bug

main
周平 1 year ago
parent 5c524390b9
commit 9fe7ff8382

@ -1,7 +1,7 @@
from website.handler import BaseHandler from website.handler import BaseHandler
from sqlalchemy import text from sqlalchemy import text
from typing import Any from typing import Any
from website.db_mysql import get_session, get_async_session, to_json from website.db_mysql import get_session, get_async_session, to_json, to_json_list
import json import json
@ -103,11 +103,10 @@ async def get_enterprise_model_and_device_count(
# if res_device: # if res_device:
# device_count = res_device["device_count"] # device_count = res_device["device_count"]
sql = """
SELECT
(SELECT base_models FROM enterprise_busi_model WHERE {where_clause}) as base_models, sql_device_count = "SELECT COUNT(*) AS device_count FROM enterprise_device WHERE {where_clause} "
(SELECT COUNT(*) FROM enterprise_device WHERE {where_clause}) AS device_count sql_base_model = "SELECT base_models FROM enterprise_busi_model WHERE {where_clause} "
"""
where_clause = "" where_clause = ""
params = {} params = {}
@ -118,12 +117,20 @@ async def get_enterprise_model_and_device_count(
where_clause = "entity_suid = :entity_suid" where_clause = "entity_suid = :entity_suid"
params["entity_suid"] = entity_suid params["entity_suid"] = entity_suid
sql = sql.format(where_clause=where_clause) sql_device_count = sql_device_count.format(where_clause=where_clause)
result = await session.execute(text(sql), params) result_device_count = await session.execute(text(sql_device_count), params)
# row = result.fetchone() device_count = to_json(result_device_count)["device_count"]
row = to_json(result)
base_models, device_count = row["base_models"], row["device_count"] sql_base_model = sql_base_model.format(where_clause=where_clause)
if base_models: result_base_model = await session.execute(text(sql_base_model), params)
model_count = len(json.loads(base_models)) base_models = to_json_list(result_base_model)
base_model_ids = []
for item in base_models:
base_models = json.loads(item["base_models"])
for base_model in base_models:
if base_model["id"] not in base_model_ids:
base_model_ids.append(base_model["id"])
model_count = len(base_model_ids)
return model_count, device_count return model_count, device_count

@ -204,15 +204,16 @@ class EnterpriseBusiModelNodeRepository(object):
link_node_ids = [int(node_id) for node_id in data['node_ids'].split(',')] link_node_ids = [int(node_id) for node_id in data['node_ids'].split(',')]
with get_session() as session: with get_session() as session:
for node_id in link_node_ids: for node_id in link_node_ids:
logging.info("node_id: %s")
node_db = DB_Node.EnterpriseNodeRepository() node_db = DB_Node.EnterpriseNodeRepository()
node = node_db.get_node_by_id(node_id) node = node_db.get_entity_suid_by_node_id(node_id)
node_suid = node["suid"] node_suid = node["suid"]
entity_suid = node["entity_suid"] entity_suid = node["entity_suid"]
model_node = EnterpriseBusiModelNode( model_node = EnterpriseBusiModelNode(
suid=shortuuid.ShortUUID().random(10), suid=shortuuid.ShortUUID().random(10),
entity_suid=entity_suid, entity_suid=entity_suid,
busi_model_id=data['busi_model_id'], busi_model_id=data['busimodel_id'],
busi_model_suid=data['busi_model_suid'], busi_model_suid=data['busimodel_suid'],
node_id=node_id, node_id=node_id,
node_suid=node_suid, node_suid=node_suid,
) )

@ -176,7 +176,7 @@ class EnterpriseNodeRepository(object):
def get_entity_suid_by_node_id(self, node_id: int) -> Union[dict, None]: def get_entity_suid_by_node_id(self, node_id: int) -> Union[dict, None]:
with get_session() as session: with get_session() as session:
res = session.execute( res = session.execute(
text("select entity_suid from enterprise_node where id=:id"), text("select suid, entity_suid from enterprise_node where id=:id"),
{"id": node_id}, {"id": node_id},
) )
entity = to_json(res) entity = to_json(res)

Loading…
Cancel
Save