Commit faf23a43 authored by ZZH's avatar ZZH

fix business meter parse

parent b127e317
...@@ -9,21 +9,17 @@ from unify_api.modules.electric.dao.electric_dao import \ ...@@ -9,21 +9,17 @@ from unify_api.modules.electric.dao.electric_dao import \
get_elec_mtid_sid_by_cid get_elec_mtid_sid_by_cid
from unify_api.utils.common_utils import round_2 from unify_api.utils.common_utils import round_2
from pot_libs.aredis_util.aredis_utils import RedisUtils from pot_libs.aredis_util.aredis_utils import RedisUtils
from pot_libs.common.components.query import Range, Equal, Filter, PageRequest
from pot_libs.es_util.es_query import EsQuery
from pot_libs.es_util.es_utils import EsUtil
from pot_libs.logger import log from pot_libs.logger import log
from pot_libs.mysql_util.mysql_util import MysqlUtil from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api import constants from unify_api import constants
from unify_api.constants import Importance, EVENT_TYPE_MAP, SDU_ALARM_LIST from unify_api.constants import Importance, SDU_ALARM_LIST
from unify_api.modules.alarm_manager.dao.list_static_dao import \ from unify_api.modules.alarm_manager.dao.list_static_dao import \
alarm_aggs_importance, \ alarm_aggs_importance, \
sdu_alarm_importance_dao sdu_alarm_importance_dao
from unify_api.modules.common.dao.common_dao import monitor_point_join, \ from unify_api.modules.common.dao.common_dao import monitor_point_join
monitor_by_cid
from unify_api.modules.common.procedures.common_utils import get_electric_index from unify_api.modules.common.procedures.common_utils import get_electric_index
from unify_api.modules.common.procedures.points import get_points, \ from unify_api.modules.common.procedures.points import proxy_points, \
proxy_points, get_points_num get_points_num
from unify_api.modules.common.procedures.pttl_max import pttl_max, \ from unify_api.modules.common.procedures.pttl_max import pttl_max, \
pttl_max_new15 pttl_max_new15
from unify_api.modules.electric.views.electric import METERDATA_CURRENT_KEY from unify_api.modules.electric.views.electric import METERDATA_CURRENT_KEY
...@@ -31,7 +27,7 @@ from unify_api.modules.home_page.components.count_info_cps import ( ...@@ -31,7 +27,7 @@ from unify_api.modules.home_page.components.count_info_cps import (
MaxResidualCurrent, MaxResidualCurrent,
ElectricInfo, ElectricInfo,
) )
from unify_api.utils.time_format import last30_day_range, convert_es_str from unify_api.utils.time_format import last30_day_range
from unify_api.modules.home_page.dao.count_info_dao import ( from unify_api.modules.home_page.dao.count_info_dao import (
get_inline_by_cid, get_power_factor_kpi, get_pcvf_kpi, get_economic_kpi, get_inline_by_cid, get_power_factor_kpi, get_pcvf_kpi, get_economic_kpi,
get_md_space, get_tc_runtime get_md_space, get_tc_runtime
...@@ -186,104 +182,7 @@ async def electric_use_info(cid): ...@@ -186,104 +182,7 @@ async def electric_use_info(cid):
) )
async def normal_rate_of_location(company_id): async def normal_rate_of_location(cid):
"""
获取温度和漏电流达标率
:param company_id:
:return:
"""
location_map = {}
async with MysqlUtil() as conn:
location_sql = "select `location`.`id`, `group`, `item`, `location`.`type`, `threshold` from location left join alarm_setting on location.id=alarm_setting.location_id where location.cid = %s and `enable`=1 and location.type in %s and `alarm_setting`.type in %s"
locations = await conn.fetchall(
sql=location_sql,
args=(
company_id,
("residual_current", "temperature"),
("overTemp", "overResidualCurrent"),
),
)
for location in locations:
location_map[location["id"]] = location
# todo批量hmget
count_info_map = {
"residual_current": {"total": 0, "normal": 0},
"temperature": {"total": 0, "normal": 0},
}
print(f"len(location_map)={len(location_map)}")
location_ids = list(location_map.keys())
adio_currents = []
if location_ids:
adio_currents = await RedisUtils().hmget("adio_current",
*location_ids)
adio_info_map = {}
for index, item_byte in enumerate(adio_currents):
if item_byte:
item = json.loads(item_byte.decode())
adio_info_map[location_ids[index]] = item
for location_id, location_info in location_map.items():
audio_info = adio_info_map.get(location_id)
count_info_map[location_info["type"]]["total"] += 1
if audio_info and audio_info.get("value"):
time_now = int(time.time())
real_tt = audio_info.get("timestamp", 0)
if (time_now - real_tt) > constants.REAL_EXP_TIME:
# 超过4小时的值不统计在normal里
log.warn(f"adio_current location_id={location_id} has expire!")
continue
print(
"threshold={} location_info['type'] = {} audio_info['value']={}".format(
location_info["threshold"], location_info["type"],
audio_info["value"],
)
)
if (
isinstance(location_info["threshold"], float)
and audio_info["value"] < location_info["threshold"]
):
count_info_map[location_info["type"]]["normal"] += 1
if count_info_map["temperature"]["total"] == 0:
temperature_qr = "100%"
else:
temperature_qr = (
str(
round(
(
count_info_map["temperature"]["normal"]
/ count_info_map["temperature"]["total"]
)
* 100,
)
)
+ "%"
)
if count_info_map["residual_current"]["total"] == 0:
residual_current_qr = "100%"
else:
residual_current_qr = (
str(
round(
(
count_info_map["residual_current"]["normal"]
/ count_info_map["residual_current"]["total"]
)
* 100
)
)
+ "%"
)
return temperature_qr, residual_current_qr
async def normal_rate_of_location_new15(cid):
"""获取温度和漏电流达标率""" """获取温度和漏电流达标率"""
location_map = {} location_map = {}
location_sql = "select l.lid, l.item, l.ad_type type, s.threshold from " \ location_sql = "select l.lid, l.item, l.ad_type type, s.threshold from " \
...@@ -489,28 +388,7 @@ async def get_max_aiao_of_filed(cid, start, end, filed="temperature"): ...@@ -489,28 +388,7 @@ async def get_max_aiao_of_filed(cid, start, end, filed="temperature"):
) )
async def company_power_use_info(company_id, es_time_start, es_time_end): async def company_power_use_info(company_id, start, end):
async with EsUtil() as es:
filters = [
{"term": {"cid": company_id}},
{"range": {
"quarter_time": {"gte": es_time_start, "lt": es_time_end, }}},
]
query_body = {
"query": {"bool": {"filter": filters}},
"size": 0,
"aggs": {"kwh": {"sum": {"field": "kwh"}},
"charge": {"sum": {"field": "charge"}}, },
}
es_result = await es.search_origin(body=query_body,
index=constants.COMPANY_15MIN_POWER)
return {
"charge": es_result["aggregations"]["charge"]["value"],
"kwh": es_result["aggregations"]["kwh"]["value"],
}
async def company_power_use_info_new15(company_id, start, end):
async with MysqlUtil() as conn: async with MysqlUtil() as conn:
sql = f"SELECT sum(kwh) kwh, sum(charge) charge FROM " \ sql = f"SELECT sum(kwh) kwh, sum(charge) charge FROM " \
f"`company_15min_power` where create_time BETWEEN " \ f"`company_15min_power` where create_time BETWEEN " \
...@@ -520,9 +398,8 @@ async def company_power_use_info_new15(company_id, start, end): ...@@ -520,9 +398,8 @@ async def company_power_use_info_new15(company_id, start, end):
async def get_company_charge_price(company_id, es_time_start, es_time_end): async def get_company_charge_price(company_id, es_time_start, es_time_end):
power_use_info = await company_power_use_info_new15(company_id, power_use_info = await company_power_use_info(company_id, es_time_start,
es_time_start, es_time_end)
es_time_end)
if power_use_info["kwh"]: if power_use_info["kwh"]:
unit_price = power_use_info["charge"] / power_use_info["kwh"] unit_price = power_use_info["charge"] / power_use_info["kwh"]
else: else:
...@@ -537,8 +414,8 @@ async def power_charge_price(cid): ...@@ -537,8 +414,8 @@ async def power_charge_price(cid):
yestday = now - timedelta(1) yestday = now - timedelta(1)
yestday_start = datetime(yestday.year, yestday.month, yestday.day, 0, 0, 0) yestday_start = datetime(yestday.year, yestday.month, yestday.day, 0, 0, 0)
yestday_end = yestday_start + timedelta(1) yestday_end = yestday_start + timedelta(1)
yestday_datas = await company_power_use_info_new15(cid, str(yestday_start), yestday_datas = await company_power_use_info(cid, str(yestday_start),
str(yestday_end)) str(yestday_end))
if yestday_datas["kwh"]: if yestday_datas["kwh"]:
yestoday_price = yestday_datas["charge"] / yestday_datas["kwh"] yestoday_price = yestday_datas["charge"] / yestday_datas["kwh"]
else: else:
...@@ -550,9 +427,9 @@ async def power_charge_price(cid): ...@@ -550,9 +427,9 @@ async def power_charge_price(cid):
else: else:
last_month_start = datetime(year=now.year, month=now.month - 1, day=1) last_month_start = datetime(year=now.year, month=now.month - 1, day=1)
last_month_end = datetime(year=now.year, month=now.month, day=1) last_month_end = datetime(year=now.year, month=now.month, day=1)
last_month_datas = await company_power_use_info_new15( last_month_datas = await company_power_use_info(cid, str(last_month_start),
cid, str(last_month_start), str(last_month_end) str(last_month_end)
) )
if last_month_datas["kwh"]: if last_month_datas["kwh"]:
last_month_price = last_month_datas["charge"] / last_month_datas["kwh"] last_month_price = last_month_datas["charge"] / last_month_datas["kwh"]
else: else:
...@@ -633,18 +510,6 @@ async def optimization_count_info(company_id: int): ...@@ -633,18 +510,6 @@ async def optimization_count_info(company_id: int):
inlines = await conn.fetchall(sql, args=(company_id,)) inlines = await conn.fetchall(sql, args=(company_id,))
inline_ids = [inline["inlid"] for inline in inlines] inline_ids = [inline["inlid"] for inline in inlines]
# 获取公司上月用电
# now = datetime.now()
# es_start_time = (
# pendulum.datetime(now.year, now.month, 1)
# .subtract(months=1)
# .strftime("%Y-%m-%dT%H:%M:%S+08:00")
# )
# es_end_time = pendulum.datetime(now.year, now.month, 1).strftime(
# "%Y-%m-%dT%H:%M:%S+08:00")
# power_use_info = await company_power_use_info(company_id, es_start_time,
# es_end_time)
now = datetime.now() now = datetime.now()
start_time = ( start_time = (
pendulum.datetime(now.year, now.month, 1) pendulum.datetime(now.year, now.month, 1)
...@@ -653,8 +518,8 @@ async def optimization_count_info(company_id: int): ...@@ -653,8 +518,8 @@ async def optimization_count_info(company_id: int):
) )
end_time = pendulum.datetime(now.year, now.month, 1).strftime( end_time = pendulum.datetime(now.year, now.month, 1).strftime(
"%Y-%m-%d %H:%M:%S") "%Y-%m-%d %H:%M:%S")
power_use_info = await company_power_use_info_new15(company_id, start_time, power_use_info = await company_power_use_info(company_id, start_time,
end_time) end_time)
month_charge = power_use_info.get("charge") month_charge = power_use_info.get("charge")
month_kwh = power_use_info.get("kwh") month_kwh = power_use_info.get("kwh")
count_info_map = { count_info_map = {
...@@ -964,9 +829,9 @@ async def optimization_count_info_new(company_id: int): ...@@ -964,9 +829,9 @@ async def optimization_count_info_new(company_id: int):
) )
es_end_time = pendulum.datetime(now.year, now.month, 1).strftime( es_end_time = pendulum.datetime(now.year, now.month, 1).strftime(
"%Y-%m-%dT%H:%M:%S+08:00") "%Y-%m-%dT%H:%M:%S+08:00")
power_use_info = await company_power_use_info_new15(company_id, power_use_info = await company_power_use_info(company_id,
es_start_time, es_start_time,
es_end_time) es_end_time)
month_charge = power_use_info["charge"] month_charge = power_use_info["charge"]
count_info_map = { count_info_map = {
"avg_price": power_use_info["charge"] / power_use_info["kwh"] "avg_price": power_use_info["charge"] / power_use_info["kwh"]
......
...@@ -19,12 +19,10 @@ from unify_api.modules.home_page.components.count_info_proxy_cps import \ ...@@ -19,12 +19,10 @@ from unify_api.modules.home_page.components.count_info_proxy_cps import \
CountInfoProxyResp, IycResp, IycmResp, RtrResp, CmResp, ApcResp, AsiResp, \ CountInfoProxyResp, IycResp, IycmResp, RtrResp, CmResp, ApcResp, AsiResp, \
HsiResp, AiiResp HsiResp, AiiResp
from unify_api.modules.home_page.procedures.count_info_pds import ( from unify_api.modules.home_page.procedures.count_info_pds import (
get_max_aiao_of_filed, get_max_aiao_of_filed, normal_rate_of_location,
normal_rate_of_location, normal_rate_of_location_new15,
other_info, other_info_new15, other_info, other_info_new15,
power_count_info, power_count_info,
electric_use_info, electric_use_info,
datetime_to_timestamp,
power_charge_price, power_charge_price,
cal_power_factor, cal_power_factor,
optimization_count_info, optimization_count_info_new optimization_count_info, optimization_count_info_new
...@@ -59,7 +57,7 @@ async def post_count_info(request, body: CountInfoReq) -> CountInfoResp: ...@@ -59,7 +57,7 @@ async def post_count_info(request, body: CountInfoReq) -> CountInfoResp:
# temperature_qr, residual_current_qr = await normal_rate_of_location( # temperature_qr, residual_current_qr = await normal_rate_of_location(
# company_id) # company_id)
temperature_qr, residual_current_qr = await \ temperature_qr, residual_current_qr = await \
normal_rate_of_location_new15(company_id) normal_rate_of_location(company_id)
# 今日报警数和累计安全运行天数 # 今日报警数和累计安全运行天数
# today_alarm_count, safe_run_days, alarm_count = await other_info( # today_alarm_count, safe_run_days, alarm_count = await other_info(
......
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