Commit 2d16cd9d authored by lcn's avatar lcn

bug修复

parent ad6721cd
import asyncio
import hashlib
import json import json
import time import time
import aioredis import aioredis
from pot_libs.aredis_util.aredis_utils import RedisClient, RedisUtils
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
from unify_api import constants from unify_api import constants
from unify_api.constants import POINT_LEVEL_MAP from unify_api.constants import POINT_LEVEL_MAP
...@@ -9,7 +12,8 @@ from unify_api.modules.adio.components.adio import AdioIndex ...@@ -9,7 +12,8 @@ from unify_api.modules.adio.components.adio import AdioIndex
from unify_api.modules.adio.components.adio_card_cps import AcResp from unify_api.modules.adio.components.adio_card_cps import AcResp
from unify_api.modules.adio.dao.adio_card_dao import \ from unify_api.modules.adio.dao.adio_card_dao import \
monitor_location_join_by_locations, alarm_setting_by_locations monitor_location_join_by_locations, alarm_setting_by_locations
from unify_api.modules.adio.dao.adio_dao import get_location_15min_dao from unify_api.modules.adio.dao.adio_dao import get_location_15min_dao, \
get_location_dao
from unify_api.modules.common.procedures.list_point_pds import \ from unify_api.modules.common.procedures.list_point_pds import \
monitor_map_point_location monitor_map_point_location
from unify_api.utils.common_utils import round_2 from unify_api.utils.common_utils import round_2
...@@ -111,6 +115,37 @@ async def post_adio_card_service(location_list, cid): ...@@ -111,6 +115,37 @@ async def post_adio_card_service(location_list, cid):
) )
async def adio_index_service(location_group, start, end):
redis_data, redis_key = await get_location_15min_redis(location_group,
start, end)
if redis_data:
datas = redis_data.get("data")
else:
# # load location表信息
location_info = await get_location_dao(location_group)
tasks = [get_location_15min_service(location_info, lid, start, end) for
lid
in location_group]
adio_index_result = await asyncio.gather(*tasks)
datas = list(adio_index_result)
# set to redis
await RedisUtils().setex(redis_key, 60 * 7,
json.dumps({"data": datas}))
adio_indexes = []
for data in datas:
adio_index = AdioIndex(**data)
adio_indexes.append(adio_index)
return adio_indexes
async def get_location_15min_redis(location_group, start, end):
redis_key_data = json.dumps(
{"group": location_group, "start": start, "end": end})
redis_key = hashlib.md5(redis_key_data.encode()).hexdigest()
redis_data = await RedisUtils().get(redis_key)
return json.loads(redis_data) if redis_data else {}, redis_key
async def get_location_15min_service(location_info, lid, start, end): async def get_location_15min_service(location_info, lid, start, end):
value_max, value_min, value_avg = [], [], [] value_max, value_min, value_avg = [], [], []
value_max_time, value_min_time = [], [] value_max_time, value_min_time = [], []
...@@ -136,13 +171,13 @@ async def get_location_15min_service(location_info, lid, start, end): ...@@ -136,13 +171,13 @@ async def get_location_15min_service(location_info, lid, start, end):
value_avg_list = [m for m in value_avg] value_avg_list = [m for m in value_avg]
value_avg_data = sum(value_avg_list) / len(value_avg_list) \ value_avg_data = sum(value_avg_list) / len(value_avg_list) \
if value_avg_list else 0 if value_avg_list else 0
adio_index = AdioIndex( adio_index = dict({
type=location_info[lid]["type"], "type": location_info[lid]["type"],
item=location_info[lid]["item"], "item": location_info[lid]["item"],
max=round_2(value_max_max), "max": round_2(value_max_max),
max_time=str(value_max_time_data) if value_max_time_data else "", "max_time": str(value_max_time_data) if value_max_time_data else "",
min=round_2(value_min_min), "min": round_2(value_min_min),
min_time=str(value_min_time_data) if value_min_time_data else "", "min_time": str(value_min_time_data) if value_min_time_data else "",
avg=round_2(value_avg_data), "avg": round_2(value_avg_data),
) })
return adio_index return adio_index
...@@ -13,7 +13,7 @@ from pot_libs.sanic_api import summary, description, examples ...@@ -13,7 +13,7 @@ from pot_libs.sanic_api import summary, description, examples
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
from pot_libs.logger import log from pot_libs.logger import log
from unify_api.modules.adio.service.adio_card import get_location_15min_service from unify_api.modules.adio.service.adio_card import adio_index_service
from unify_api.utils import time_format from unify_api.utils import time_format
from unify_api.utils.time_format import CST, YMD_Hms, timestamp2dts from unify_api.utils.time_format import CST, YMD_Hms, timestamp2dts
from unify_api import constants from unify_api import constants
...@@ -247,9 +247,6 @@ async def post_adio_index(req, body: PageRequest) -> AdioIndexResponse: ...@@ -247,9 +247,6 @@ async def post_adio_index(req, body: PageRequest) -> AdioIndexResponse:
log.warning("para exception, in_groups is NULL, no location_id") log.warning("para exception, in_groups is NULL, no location_id")
return AdioIndexResponse(adio_indexes=[]) return AdioIndexResponse(adio_indexes=[])
# # load location表信息
location_info = await get_location_dao(location_group)
# 获取时间 # 获取时间
try: try:
start = body.filter.ranges[0].start start = body.filter.ranges[0].start
...@@ -257,8 +254,5 @@ async def post_adio_index(req, body: PageRequest) -> AdioIndexResponse: ...@@ -257,8 +254,5 @@ async def post_adio_index(req, body: PageRequest) -> AdioIndexResponse:
except: except:
log.error("para error, ranges is NULL") log.error("para error, ranges is NULL")
return AdioIndexResponse(adio_indexes=[]) return AdioIndexResponse(adio_indexes=[])
tasks = [get_location_15min_service(location_info, lid, start, end) for lid adio_indexes = await adio_index_service(location_group, start, end)
in location_group]
adio_index_result = await asyncio.gather(*tasks)
adio_indexes = list(adio_index_result)
return AdioIndexResponse(adio_indexes=adio_indexes) return AdioIndexResponse(adio_indexes=adio_indexes)
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