Commit 15ba9fa7 authored by ZZH's avatar ZZH

remove es 2023-6-2

parent 4af81566
import json import json
import pendulum import pendulum
from pot_libs.common.components.query import PageRequest, Filter, Equal, Range import io
from pot_libs.es_util.es_query import EsQuery import pandas as pd
from pot_libs.es_util.es_utils import EsUtil from pot_libs.common.components.query import PageRequest
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 pot_libs.sanic_api import summary, examples from pot_libs.sanic_api import summary, examples
from pot_libs.utils.exc_util import ParamException from pot_libs.utils.exc_util import ParamException
from unify_api.constants import COMPANY_15MIN_POWER, POINT_15MIN_POWER, PRODUCT from unify_api.constants import PRODUCT
from unify_api.modules.common.components.common_cps import LevelResp from unify_api.modules.common.components.common_cps import LevelResp
from unify_api.modules.common.dao.common_dao import company_by_cids from unify_api.modules.common.dao.common_dao import company_by_cids
from unify_api.modules.common.procedures.cids import get_cids, get_proxy_cids from unify_api.modules.common.procedures.cids import get_proxy_cids
from unify_api.modules.common.procedures.points import points_by_storeys from unify_api.modules.common.procedures.points import points_by_storeys
from unify_api.modules.elec_charge.components.elec_charge_cps import \ from unify_api.modules.elec_charge.components.elec_charge_cps import \
power_overview_example, PricePolicyReq, PricePolicyResp, \ power_overview_example, PricePolicyReq, PricePolicyResp, \
...@@ -26,15 +26,12 @@ from unify_api.modules.elec_charge.service.elec_charge_service import \ ...@@ -26,15 +26,12 @@ from unify_api.modules.elec_charge.service.elec_charge_service import \
kwh_points_service, kwh_card_level_service, load_info_service kwh_points_service, kwh_card_level_service, load_info_service
from unify_api.modules.users.procedures.jwt_user import jwt_user from unify_api.modules.users.procedures.jwt_user import jwt_user
from unify_api.utils.common_utils import round_2, round_4, NumListHelper from unify_api.utils.common_utils import round_2, round_4, NumListHelper
from unify_api.utils.es_query_body import agg_statistics
from unify_api.utils.request_util import filed_value_from_list from unify_api.utils.request_util import filed_value_from_list
from unify_api.utils.time_format import convert_es_str, last_time_str, \ from unify_api.utils.time_format import last_time_str, today_month_date, \
today_month_date, srv_time srv_time
import io
import pandas as pd
async def power_overview_new15(start, end, point_id, cid): async def power_overview(start, end, point_id, cid):
if point_id == -1: # 选的全部 if point_id == -1: # 选的全部
sql = f"SELECT spfv, sum(p), sum(kwh), sum(charge) " \ sql = f"SELECT spfv, sum(p), sum(kwh), sum(charge) " \
f"FROM company_15min_power where create_time " \ f"FROM company_15min_power where create_time " \
...@@ -68,51 +65,6 @@ async def power_overview_new15(start, end, point_id, cid): ...@@ -68,51 +65,6 @@ async def power_overview_new15(start, end, point_id, cid):
return pv1, pv2 return pv1, pv2
async def power_overview(date_start, date_end, point_id, cid):
"""抽离电量电费信息,供调用"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
# 2. 获取query_body # es时间查询字段:quarter_time
query_range = Range(field="quarter_time", start=start_es,
end=end_es)
if point_id == -1: # 选的全部
equal = Equal(field="cid", value=cid)
index = COMPANY_15MIN_POWER
else:
equal = Equal(field="pid", value=point_id)
index = POINT_15MIN_POWER
query_filter = Filter(equals=[equal], ranges=[query_range], in_groups=[],
keywords=[])
page_request = PageRequest(
page_size=10, page_num=1, filter=query_filter, sort=None)
query_body = EsQuery.agg_group_by(page_request, "spfv.keyword", "sum",
("kwh", "charge", "p"))
log.info(query_body)
# 3. 查询es
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=index)
if not es_re:
return PowerViewRes(charge=Spvf(), power=Spvf())
es_re = es_re["aggregations"]["spfvs"]["buckets"]
# 4. 构造返回
pv1 = Spvf() # 电量对象
pv2 = Spvf() # 电费对象
for info in es_re:
if info.get("key") == "s":
pv1.s = info.get("kwh")["value"]
pv2.s = info.get("charge")["value"]
elif info.get("key") == "p":
pv1.p = info.get("kwh")["value"]
pv2.p = info.get("charge")["value"]
elif info.get("key") == "f":
pv1.f = info.get("kwh")["value"]
pv2.f = info.get("charge")["value"]
elif info.get("key") == "v":
pv1.v = info.get("kwh")["value"]
pv2.v = info.get("charge")["value"]
return pv1, pv2
@summary('电量电费信息') @summary('电量电费信息')
@examples(power_overview_example) @examples(power_overview_example)
async def post_power_overview(req, body: PageRequest) -> PowerViewRes: async def post_power_overview(req, body: PageRequest) -> PowerViewRes:
...@@ -125,8 +77,8 @@ async def post_power_overview(req, body: PageRequest) -> PowerViewRes: ...@@ -125,8 +77,8 @@ async def post_power_overview(req, body: PageRequest) -> PowerViewRes:
date_end = body.filter.ranges[0].end date_end = body.filter.ranges[0].end
if date_start == date_end: if date_start == date_end:
return PowerViewRes(power=Spvf(), charge=Spvf()) return PowerViewRes(power=Spvf(), charge=Spvf())
pv1, pv2 = await power_overview_new15(start=date_start, end=date_end,
point_id=point_id, cid=cid) pv1, pv2 = await power_overview(date_start, date_end, point_id, cid)
return PowerViewRes(power=pv1, charge=pv2) return PowerViewRes(power=pv1, charge=pv2)
...@@ -173,7 +125,7 @@ async def post_price_policy(req, body: PricePolicyReq) -> PricePolicyResp: ...@@ -173,7 +125,7 @@ async def post_price_policy(req, body: PricePolicyReq) -> PricePolicyResp:
return PricePolicyResp(price_info=res_list) return PricePolicyResp(price_info=res_list)
async def aver_elec_price_new15(start, end, point_id, cid, date_type): async def avg_ele_price(start, end, point_id, cid, date_type):
if point_id == -1: # 选的全部 if point_id == -1: # 选的全部
table_name = "company_15min_power" table_name = "company_15min_power"
name = "cid" name = "cid"
...@@ -203,43 +155,6 @@ async def aver_elec_price_new15(start, end, point_id, cid, date_type): ...@@ -203,43 +155,6 @@ async def aver_elec_price_new15(start, end, point_id, cid, date_type):
return this_ck, last_ck return this_ck, last_ck
async def aver_elec_price(start, end, point_id, cid, date_type):
"""平均电价和增长率, 抽离功能"""
if point_id == -1: # 选的全部
matchs = {"cid": cid}
index = COMPANY_15MIN_POWER
else:
matchs = {"pid": point_id}
index = POINT_15MIN_POWER
# 2.构造query_body
query_this = agg_statistics(matchs=matchs,
aggs_key=["kwh", "charge"],
start=start, end=end, date_key="quarter_time")
# 3.返回数据
async with EsUtil() as es:
this_re = await es.search_origin(body=query_this,
index=index)
# 本周期电量电费
this_ck = ChargeKwh()
this_ck.charge = this_re["aggregations"]["charge"]["value"]
this_ck.kwh = this_re["aggregations"]["kwh"]["value"]
if date_type == "range":
return this_ck, None
# 上周期电量电费
start_last, end_last = last_time_str(start, end, date_type)
query_last = agg_statistics(matchs=matchs,
aggs_key=["kwh", "charge"],
start=start_last, end=end_last,
date_key="quarter_time")
async with EsUtil() as es:
last_re = await es.search_origin(body=query_last,
index=index)
last_ck = ChargeKwh()
last_ck.charge = last_re["aggregations"]["charge"]["value"]
last_ck.kwh = last_re["aggregations"]["kwh"]["value"]
return this_ck, last_ck
@summary('平均电价和增长率') @summary('平均电价和增长率')
async def post_aver_elec_price(req, body: AverPriceReq) -> AverPriceResp: async def post_aver_elec_price(req, body: AverPriceReq) -> AverPriceResp:
"""平均电价, 增长率""" """平均电价, 增长率"""
...@@ -251,7 +166,7 @@ async def post_aver_elec_price(req, body: AverPriceReq) -> AverPriceResp: ...@@ -251,7 +166,7 @@ async def post_aver_elec_price(req, body: AverPriceReq) -> AverPriceResp:
date_type = body.date_type date_type = body.date_type
if start == end: if start == end:
return AverPriceResp(this_power=ChargeKwh(), last_power=ChargeKwh()) return AverPriceResp(this_power=ChargeKwh(), last_power=ChargeKwh())
this_ck, last_ck = await aver_elec_price_new15(start, end, point_id, cid, this_ck, last_ck = await avg_ele_price(start, end, point_id, cid,
date_type) date_type)
return AverPriceResp(this_power=this_ck, last_power=last_ck) return AverPriceResp(this_power=this_ck, last_power=last_ck)
...@@ -262,20 +177,20 @@ async def post_index_charge(req, body: IndexChargeReq) -> IndexChargeResp: ...@@ -262,20 +177,20 @@ async def post_index_charge(req, body: IndexChargeReq) -> IndexChargeResp:
point_id = body.point_id point_id = body.point_id
today_start, today_end, month_start, month_end = today_month_date() today_start, today_end, month_start, month_end = today_month_date()
# 1. 今日电量电费spvf # 1. 今日电量电费spvf
kwh_t, charge_t = await power_overview_new15(today_start, today_end, kwh_t, charge_t = await power_overview(today_start, today_end, point_id,
point_id, cid) cid)
today_spvf = PowerViewRes(power=kwh_t, charge=charge_t) today_spvf = PowerViewRes(power=kwh_t, charge=charge_t)
# 2. 本月电量电费spvf # 2. 本月电量电费spvf
kwh_m, charge_m = await power_overview_new15(month_start, month_end, kwh_m, charge_m = await power_overview(month_start, month_end, point_id,
point_id, cid) cid)
month_spvf = PowerViewRes(power=kwh_m, charge=charge_m) month_spvf = PowerViewRes(power=kwh_m, charge=charge_m)
# 3. 今日平均电价和增长率 # 3. 今日平均电价和增长率
this_ck_t, last_ck_t = await aver_elec_price_new15(today_start, today_end, this_ck_t, last_ck_t = await avg_ele_price(today_start, today_end,
point_id, cid, point_id, cid,
date_type="day") date_type="day")
today_power = AverPriceResp(this_power=this_ck_t, last_power=last_ck_t) today_power = AverPriceResp(this_power=this_ck_t, last_power=last_ck_t)
# 4. 本月平均电价和增长率 # 4. 本月平均电价和增长率
this_ck_m, last_ck_m = await aver_elec_price_new15(month_start, month_end, this_ck_m, last_ck_m = await avg_ele_price(month_start, month_end,
point_id, cid, point_id, cid,
date_type="month") date_type="month")
month_power = AverPriceResp(this_power=this_ck_m, last_power=last_ck_m) month_power = AverPriceResp(this_power=this_ck_m, last_power=last_ck_m)
......
from pot_libs.sanic_api import summary from pot_libs.sanic_api import summary
from unify_api.modules.load_analysis.components.load_forecast_cps import ( from unify_api.modules.load_analysis.components.load_forecast_cps import (
ForecastReq, ForecastReq, ForecastResp,
ForecastResp,
) )
from unify_api.modules.load_analysis.service.load_forecast_service import \ from unify_api.modules.load_analysis.service.load_forecast_service import \
load_forecast_service, load_forecast_service_new15 load_forecast_srv
from unify_api.utils.time_format import time_pick_transf_new from unify_api.utils.time_format import time_pick_transf_new
...@@ -17,9 +16,7 @@ async def post_load_forecast(req, body: ForecastReq) -> ForecastResp: ...@@ -17,9 +16,7 @@ async def post_load_forecast(req, body: ForecastReq) -> ForecastResp:
end = body.end end = body.end
# 管理版本多个工厂的情况, 兼容能力最强的参数cids, 保留旧有的cid: # 管理版本多个工厂的情况, 兼容能力最强的参数cids, 保留旧有的cid:
cids = body.cids cids = body.cids
# return await load_forecast_service(cid, cids, start, end)
terms = cids if cids else [cid] terms = cids if cids else [cid]
# 获取时间差 # 获取时间差
interval, slots = time_pick_transf_new(start, end) interval, slots = time_pick_transf_new(start, end)
return await load_forecast_service_new15(terms, start, end, return await load_forecast_srv(terms, start, end, interval, slots)
interval, slots)
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