Commit 81c24bd9 authored by lcn's avatar lcn

修改tdengine写法

parent 4bb601d6
...@@ -438,3 +438,11 @@ DOWNLOAD_ORDER = "filedata/zhiweiu/order" ...@@ -438,3 +438,11 @@ DOWNLOAD_ORDER = "filedata/zhiweiu/order"
SMS_LOGIN_TEMPLATE = "SMS_222195119" SMS_LOGIN_TEMPLATE = "SMS_222195119"
SMS_SIGN_NAME = "清科优能" SMS_SIGN_NAME = "清科优能"
TDENGINE_CHILD_SUBFIX_LIST = {
"db_appliance": "app",
"db_soe": "soe",
"db_scope": "scp",
"db_adio": "adi",
"db_electric": "ele",
}
\ No newline at end of file
...@@ -36,7 +36,7 @@ async def storey_by_cid(cid): ...@@ -36,7 +36,7 @@ async def storey_by_cid(cid):
async def query_points_by_storey(storeys): async def query_points_by_storey(storeys):
"""根据storey_id查询point_id和room_name""" """根据storey_id查询point_id和room_name"""
sql = "SELECT s.storey_id,s.storey_name,s.point_id,s.room_name,m.mtid," \ sql = "SELECT s.storey_id,s.storey_name,s.point_id,s.room_name,m.mtid," \
"p.ctnum from storey_room_map s LEFT JOIN point p " \ "p.ctnum,p.cid from storey_room_map s LEFT JOIN point p " \
"on p.pid=s.point_id LEFT JOIN monitor m on m.mtid=p.mtid " \ "on p.pid=s.point_id LEFT JOIN monitor m on m.mtid=p.mtid " \
"where s.storey_id in %s and m.demolished=0 " \ "where s.storey_id in %s and m.demolished=0 " \
"order by s.storey_id, s.room_name" "order by s.storey_id, s.room_name"
......
...@@ -3,7 +3,7 @@ from pot_libs.mysql_util.mysql_util import MysqlUtil ...@@ -3,7 +3,7 @@ from pot_libs.mysql_util.mysql_util import MysqlUtil
async def monitor_point_join_by_points(points): async def monitor_point_join_by_points(points):
"""monitor和point关联""" """monitor和point关联"""
sql = "SELECT m.mtid,p.ctnum,m.name, m.m_type, p.pid " \ sql = "SELECT m.mtid,p.ctnum,m.name, m.m_type, p.pid,p.cid " \
"FROM monitor m inner join point p on m.mtid = p.mtid " \ "FROM monitor m inner join point p on m.mtid = p.mtid " \
"WHERE p.pid in %s and m.demolished = 0 order by field(p.pid,{})".\ "WHERE p.pid in %s and m.demolished = 0 order by field(p.pid,{})".\
format(str(points).replace("[", "").replace("]", "")) format(str(points).replace("[", "").replace("]", ""))
......
...@@ -227,20 +227,20 @@ async def qual_current_data(point_mid): ...@@ -227,20 +227,20 @@ async def qual_current_data(point_mid):
return ret_dic return ret_dic
async def elec_current_data_new15(mtids): async def elec_current_data_new15(mtids, cid):
res_map = {} res_map = {}
url = f"{SETTING.stb_url}db_electric" url = f"{SETTING.stb_url}db_electric"
sql = f"" sql = f"""
for mtid in mtids: select tbname,last_row(*) from electric_stb
table_name = f"mt{mtid}_ele" where cpyid={cid}
if len(sql) > 0: group by tbname
sql += " union all " """
sql += f"select last_row(*) from {table_name}"
is_succ, results = await get_td_engine_data(url, sql) is_succ, results = await get_td_engine_data(url, sql)
if is_succ: if is_succ:
head = [re.findall(r'last_row\((.*)\)', i)[0] if "(" in i else i head = [re.findall(r'last_row\((.*)\)', i)[0] if "(" in i else i
for i in results["head"]] for i in results["head"]]
for res in results["data"]: for res in results["data"]:
data = dict(zip(head, res)) data = dict(zip(head, res))
res_map[data["mtid"]] = data if data["mtid"] in mtids:
res_map[data["mtid"]] = data
return res_map return res_map
...@@ -38,11 +38,12 @@ async def elec_current_storeys_service(storeys): ...@@ -38,11 +38,12 @@ async def elec_current_storeys_service(storeys):
point_list = await points_by_storeys(storeys) point_list = await points_by_storeys(storeys)
# mtids # mtids
mtids = [i.get("mtid") for i in point_list] mtids = [i.get("mtid") for i in point_list]
cid = point_list[0]['cid'] if len(point_list) > 0 else 0
# 2.获取mid, ctnum # 2.获取mid, ctnum
# point_mid = await batch_get_wiring_type(points) # point_mid = await batch_get_wiring_type(points)
# # 3. 获取redis数据 # # 3. 获取redis数据
# res = await elec_current_data(point_mid) # res = await elec_current_data(point_mid)
res = await elec_current_data_new15(mtids) res = await elec_current_data_new15(mtids, cid)
# 4. 返回数据 # 4. 返回数据
elec_data = {} elec_data = {}
for info in point_list: for info in point_list:
...@@ -128,7 +129,8 @@ async def qual_current_storeys_service(storeys): ...@@ -128,7 +129,8 @@ async def qual_current_storeys_service(storeys):
# # 3. 获取redis数据 # # 3. 获取redis数据
# res = await qual_current_data(point_mid) # res = await qual_current_data(point_mid)
mtids = [point["mtid"] for point in point_list if point["mtid"]] mtids = [point["mtid"] for point in point_list if point["mtid"]]
res = await elec_current_data_new15(mtids) cid = point_list[0]['cid'] if len(point_list) > 0 else 0
res = await elec_current_data_new15(mtids, cid)
# 4. 返回数据 # 4. 返回数据
qual_data = {} qual_data = {}
for info in point_list: for info in point_list:
...@@ -210,7 +212,8 @@ async def elec_card_level_service(point_list): ...@@ -210,7 +212,8 @@ async def elec_card_level_service(point_list):
# # 3. 获取redis数据 # # 3. 获取redis数据
# res_redis = await elec_current_data(point_mid) # res_redis = await elec_current_data(point_mid)
mtids = [monitor["mtid"] for monitor in monitor_point_list if monitor["mtid"]] mtids = [monitor["mtid"] for monitor in monitor_point_list if monitor["mtid"]]
results = await elec_current_data_new15(mtids) cid = monitor_point_list[0]['cid'] if len(monitor_point_list) > 0 else 0
results = await elec_current_data_new15(mtids, cid)
# 4. 返回数据 # 4. 返回数据
ret_data = { ret_data = {
"inline": [], "inline": [],
...@@ -303,7 +306,8 @@ async def qual_current_level_service(point_list): ...@@ -303,7 +306,8 @@ async def qual_current_level_service(point_list):
# res_redis = await qual_current_data(point_mid) # res_redis = await qual_current_data(point_mid)
mtids = [monitor["mtid"] for monitor in monitor_point_list if mtids = [monitor["mtid"] for monitor in monitor_point_list if
monitor["mtid"]] monitor["mtid"]]
res = await elec_current_data_new15(mtids) cid = monitor_point_list[0]['cid'] if len(monitor_point_list) > 0 else 0
res = await elec_current_data_new15(mtids, cid)
# 4. 返回数据 # 4. 返回数据
ret_data = { ret_data = {
"inline": [], "inline": [],
......
...@@ -550,13 +550,11 @@ async def current_load_new15(cid): ...@@ -550,13 +550,11 @@ async def current_load_new15(cid):
from unify_api.modules.common.service.td_engine_service import \ from unify_api.modules.common.service.td_engine_service import \
get_td_engine_data get_td_engine_data
url = f"{SETTING.stb_url}db_electric" url = f"{SETTING.stb_url}db_electric"
monitors = await monitor_by_cid(cid) sql = f"""
sql = f"" select last_row(*) from electric_stb
for monitor in monitors: where cpyid={cid}
table_name = f"mt{monitor.get('mtid')}_ele" group by tbname
if len(sql) > 0: """
sql += " union all "
sql += f"select last_row(*) from {table_name}"
is_succ, results = await get_td_engine_data(url, sql) is_succ, results = await get_td_engine_data(url, sql)
now_tt = int(time.time()) now_tt = int(time.time())
if is_succ: if is_succ:
......
...@@ -189,13 +189,11 @@ async def health_ctl_rate_service_new15(cid): ...@@ -189,13 +189,11 @@ async def health_ctl_rate_service_new15(cid):
real_tt = now_ts real_tt = now_ts
total = 0 total = 0
url = f"{SETTING.stb_url}db_electric" url = f"{SETTING.stb_url}db_electric"
monitors = await monitor_by_cid(cid) sql = f"""
sql = f"" select last_row(*) from electric_stb
for monitor in monitors: where cpyid={cid}
table_name = f"mt{monitor.get('mtid')}_ele" group by tbname
if len(sql) > 0: """
sql += " union all "
sql += f"select last_row(*) from {table_name}"
is_succ, results = await get_td_engine_data(url, sql) is_succ, results = await get_td_engine_data(url, sql)
time_str = time_format.get_datetime_str(real_tt) time_str = time_format.get_datetime_str(real_tt)
if is_succ: if is_succ:
......
...@@ -106,14 +106,11 @@ async def stb_data_longgang_service(user_id, type): ...@@ -106,14 +106,11 @@ async def stb_data_longgang_service(user_id, type):
token = get_token() token = get_token()
stb_url = f"{SETTING.stb_url}{db_name[type]['super']}" stb_url = f"{SETTING.stb_url}{db_name[type]['super']}"
sql = f"""
monitor_datas = await points_monitor_by_cid(cids) select last_row(*) from electric_stb
sql = f"" where cpyid = {cids[0]}
for monitor_data in monitor_datas: group by tbname
table_name = f"mt{monitor_data.get('mtid')}_{db_name[type]['suffix']}" """
if len(sql) > 0:
sql += " union all "
sql += f"select last_row(*) from {table_name}"
resp_str, status = await AioHttpUtils().post_data( resp_str, status = await AioHttpUtils().post_data(
stb_url, data=sql, timeout=50, stb_url, data=sql, timeout=50,
headers={"Authorization": f"Basic {token}"} headers={"Authorization": f"Basic {token}"}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment