Commit e8ab94b6 authored by ZZH's avatar ZZH

fix longgang data 2023-7-19

parent 634e0d8d
...@@ -16,6 +16,7 @@ from pot_libs.aiohttp_util.aiohttp_utils import AioHttpUtils ...@@ -16,6 +16,7 @@ from pot_libs.aiohttp_util.aiohttp_utils import AioHttpUtils
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
from pot_libs.utils.pendulum_wrapper import my_pendulum from pot_libs.utils.pendulum_wrapper import my_pendulum
from pot_libs.aredis_util.aredis_utils import RedisUtils from pot_libs.aredis_util.aredis_utils import RedisUtils
from unify_api.utils.taos_new import get_td_engine_data, parse_td_columns
from unify_api.utils.time_format import ( from unify_api.utils.time_format import (
CST, convert_dt_to_timestr, convert_to_dt CST, convert_dt_to_timestr, convert_to_dt
) )
...@@ -76,25 +77,15 @@ async def stb_data_longgang_service(user_id, d_type): ...@@ -76,25 +77,15 @@ async def stb_data_longgang_service(user_id, d_type):
if not await get_power(user_id, cids): if not await get_power(user_id, cids):
return success_res(code=4001, msg="您没有权限访问") return success_res(code=4001, msg="您没有权限访问")
td_token = get_token()
db_name = topic2db[d_type] db_name = topic2db[d_type]
stb_url = f"{SETTING.stb_url}{db_name}?tz=Asia/Shanghai" stb_url = f"{SETTING.stb_url}{db_name}?tz=Asia/Shanghai"
sql = f""" sql = f"select last_row(*) from {db_name}.{d_type}_stb " \
select last_row(*) from electric_stb f"where cpyid={cids[0]} group by tbname;"
where cpyid = {cids[0]} is_succ, results = await get_td_engine_data(stb_url, sql)
group by tbname if not is_succ:
"""
resp_str, status = await AioHttpUtils().post_data(
stb_url, data=sql, timeout=50,
headers={"Authorization": f"Basic {td_token}"}
)
if not resp_str or status != 200:
return success_res(code=4003, msg="未查找到数据") return success_res(code=4003, msg="未查找到数据")
results = json.loads(resp_str) head = parse_td_columns(results)
head = [re.findall(r'last_row\((.*)\)', i)[0] if "(" in i else i for i in
results["head"]]
datas = [dict(zip(head, r)) for r in results["data"]] datas = [dict(zip(head, r)) for r in results["data"]]
if d_type == "electric": if d_type == "electric":
[data.pop("ts_received") for data in datas] [data.pop("ts_received") for data in datas]
...@@ -164,27 +155,20 @@ async def supplement_data_service(user_id, cid, start, end, d_type): ...@@ -164,27 +155,20 @@ async def supplement_data_service(user_id, cid, start, end, d_type):
except Exception as e: except Exception as e:
success_res(code=4004, msg="开始时间或者结束时间错误") success_res(code=4004, msg="开始时间或者结束时间错误")
token = get_token()
db_name = topic2db[d_type] db_name = topic2db[d_type]
stb_url = f"{SETTING.stb_url}{db_name}?tz=Asia/Shanghai" stb_url = f"{SETTING.stb_url}{db_name}?tz=Asia/Shanghai"
if d_type == "electric": if d_type == "electric":
sql = f"select * from {d_type}_stb where cpyid={cid} and " \ sql = f"select * from {d_type}_stb where cpyid={cid} and " \
f"ts >= '{start}' and ts <= '{end}'" f"ts >= '{start}' and ts <= '{end}'"
else: else:
sql = f"select * from {d_type}_stb where cpyid={cid} and " \ sql = f"select * from {d_type}_stb where cpyid={cid} and " \
f"ts_origin >= '{start}' and ts_origin <= '{end}'" f"ts_origin >= '{start}' and ts_origin <= '{end}'"
resp_str, status = await AioHttpUtils().post_data( is_succ, results = await get_td_engine_data(stb_url, sql)
stb_url, data=sql, timeout=50, if not is_succ:
headers={"Authorization": f"Basic {token}"}
)
if not resp_str or status != 200:
return success_res(code=4003, msg="未查找到数据") return success_res(code=4003, msg="未查找到数据")
results = json.loads(resp_str) head = parse_td_columns(results)
datas = [] datas = [dict(zip(head, r)) for r in results["data"]]
for res in results["data"]:
datas.append(dict(zip(results["head"], res)))
if d_type == "electric": if d_type == "electric":
datas = [data.pop("ts_received") for data in datas] datas = [data.pop("ts_received") for data in datas]
else: else:
......
...@@ -20,7 +20,6 @@ async def get_td_engine_data(url, sql): ...@@ -20,7 +20,6 @@ async def get_td_engine_data(url, sql):
headers={"Authorization": f"Basic {token}"} headers={"Authorization": f"Basic {token}"}
) )
results = json.loads(resp_str) results = json.loads(resp_str)
log.info(f"resp_str:{resp_str},status:{status}")
if results["code"] != 0: if results["code"] != 0:
return False, None return False, None
return True, results return True, results
......
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