Commit 313d2526 authored by lcn's avatar lcn

修复Bug

parent 995fd2d3
......@@ -19,7 +19,7 @@ async def point_day_power_dao(cid, start, end):
ORDER BY pp.create_time
"""
async with MysqlUtil() as conn:
data = await conn.fetchall(sql, args=(cid, ))
data = await conn.fetchall(sql, args=(cid,))
return data
......@@ -31,7 +31,7 @@ async def get_total_kwh_dao(cid, start, end):
and pp.create_time<='{end}' and m.demolished=0
"""
async with MysqlUtil() as conn:
total_kwh = await conn.fetchone(sql, args=(cid, ))
total_kwh = await conn.fetchone(sql, args=(cid,))
return total_kwh
......@@ -40,7 +40,7 @@ async def get_kwh_charge(table_name, name, value, start, end):
f"FROM {table_name} where create_time>='{start}' and " \
f"create_time<='{end}' and {name} = %s"
async with MysqlUtil() as conn:
datas = await conn.fetchone(sql, args=(value, ))
datas = await conn.fetchone(sql, args=(value,))
return datas
......@@ -73,7 +73,7 @@ async def query_charge_aggs(date_start, date_end, cid_list):
"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
......@@ -138,132 +138,46 @@ async def power_charge_p_aggs(date_start, date_end, cid_list, interval):
"""
date_histogram,
"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
"bool": {
"must": [
{
"terms": {
"cid": cid_list
}
},
{
"range": {
"quarter_time": {
"gte": start_es,
"lte": end_es
}
}
}
]
}
},
"aggs": {
"quarter_time": {
"date_histogram": {
"field": "quarter_time",
"interval": interval,
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
},
"aggs": {
"kwh": {
"sum": {
"field": "kwh"
}
},
"charge": {
"sum": {
"field": "charge"
}
},
"p": {
"sum": {
"field": "p"
}
}
}
}
}
}
log.info(query_body)
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=index)
return es_re["aggregations"]["quarter_time"]["buckets"]
if interval == "hour":
time_fmt = "%%Y-%%m-%%d %%H"
elif interval == "day":
time_fmt = "%%Y-%%m-%%d"
else:
time_fmt = "%%Y-%%m-%%d %%H:%%i"
sql = f"""
select date_format(create_time,"{time_fmt}") as create_time,sum(kwh)
kwh,sum(charge) charge,sum(p) p
from company_15min_power
where cid in %s and create_time >= %s and create_time <= %s
group by date_format(create_time,"{time_fmt}")
"""
async with MysqlUtil() as conn:
results = await conn.fetchall(sql,
args=(cid_list, date_start, date_end))
return results or []
async def power_charge_p_cid_aggs(date_start, date_end, cid_list, interval):
"""
excel下载, 按照cid,date_histogram两次聚合,求电量电费
"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
"bool": {
"must": [
{
"terms": {
"cid": cid_list
}
},
{
"range": {
"quarter_time": {
"gte": start_es,
"lte": end_es
}
}
}
]
}
},
"aggs": {
"cids": {
"terms": {
"field": "cid",
"size": 1000
},
"aggs": {
"quarter_time": {
"date_histogram": {
"field": "quarter_time",
"interval": interval,
"time_zone": "+08:00",
"format": "yyyy-MM-dd HH:mm:ss"
},
"aggs": {
"kwh": {
"sum": {
"field": "kwh"
}
},
"charge": {
"sum": {
"field": "charge"
}
},
"p": {
"sum": {
"field": "p"
}
}
}
}
}
}
}
}
log.info(query_body)
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=index)
return es_re["aggregations"]["cids"]["buckets"]
if interval == "hour":
time_fmt = "%%Y-%%m-%%d %%H"
elif interval == "day":
time_fmt = "%%Y-%%m-%%d"
else:
time_fmt = "%%Y-%%m-%%d %%H:%%i"
sql = f"""
select cid,date_format(create_time,"{time_fmt}") as create_time,
sum(kwh) kwh,sum(charge) charge,sum(p) p
from company_15min_power
where cid in %s and create_time >= %s and create_time <= %s
group by cid,date_format(create_time,"{time_fmt}")
"""
async with MysqlUtil() as conn:
results = await conn.fetchall(sql,
args=(cid_list, date_start, date_end))
return results or []
async def query_charge_aggs_points(date_start, date_end, point_list):
......@@ -293,7 +207,7 @@ async def query_charge_aggs_points(date_start, date_end, point_list):
"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
......@@ -348,7 +262,7 @@ async def query_charge_aggs_points_new15(start, end, point_list):
f"where pid in %s and create_time BETWEEN '{start}' and '{end}' " \
f"GROUP BY pid"
async with MysqlUtil() as conn:
datas = await conn.fetchall(sql, args=(point_list, ))
datas = await conn.fetchall(sql, args=(point_list,))
return datas
......@@ -356,7 +270,7 @@ async def histogram_aggs_points(date_start, date_end, point_list, interval):
"""date_histogram"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
......@@ -418,7 +332,7 @@ async def power_charge_p_point_aggs(date_start, date_end, pid_list, interval):
"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
......@@ -495,7 +409,6 @@ async def point_aggs_kwh(point_list, start=None, end=None):
return data
async def point_aggs_kwh_new15(point_list, start=None, end=None):
"""1.5版本根据pid,求电量电费"""
if start and end:
......@@ -506,7 +419,7 @@ async def point_aggs_kwh_new15(point_list, start=None, end=None):
sql = "SELECT sum(kwh) kwh,sum(charge) charge FROM point_15min_power" \
" where pid in %s"
async with MysqlUtil() as conn:
data = await conn.fetchone(sql, args=(point_list, ))
data = await conn.fetchone(sql, args=(point_list,))
return data
......@@ -516,7 +429,7 @@ async def extended_bounds_agg(date_start, date_end, cid_list, interval):
"""
start_es = convert_es_str(date_start)
end_es = convert_es_str(date_end)
query_body = {
"size": 0,
"query": {
......
......@@ -113,11 +113,11 @@ def by_slots(slots, es_re_dic):
for slot in slots:
if slot in es_re_dic:
# 1.每个时间点,电量信息
kwh_value = round_2(es_re_dic[slot].get("kwh").get("value"))
kwh_value = round_2(es_re_dic[slot].get("kwh"))
# 2.每个时间点,电费信息
charge_value = round_2(es_re_dic[slot].get("charge").get("value"))
charge_value = round_2(es_re_dic[slot].get("charge"))
# 3. 功率
p_value = round_2(es_re_dic[slot].get("p").get("value"))
p_value = round_2(es_re_dic[slot].get("p"))
# 4. 电价
try:
price_value = round_2(charge_value / kwh_value)
......@@ -141,16 +141,18 @@ async def power_charge_p_proxy(cid_list, start, end, date_type, interval):
# 1. 96个点数据
slots_96 = SLOTS_15MIN
es_re_96 = await power_charge_p_aggs(start, end, cid_list, "15m")
es_re_96_dic = es_process(es_re_96, fmat="HH:mm") # 为了es结果和slots对应
es_re_96_dic = es_process(es_re_96, fmat="HH:mm",
time_key="create_time") # 为了es结果和slots对应
kwh_24, charge_24, p_24, price_24 = by_slots(slots_96, es_re_96_dic)
# 96个点,工厂电量电费
# 2. 24个点数据
slots_24 = SLOTS[date_type]
es_re_24 = await power_charge_p_aggs(start, end, cid_list, "hour")
es_re_24_dic = es_process(es_re_24, fmat="HH:mm") # 为了es结果和slots对应
es_re_24_dic = es_process(es_re_24, fmat="HH:mm",
time_key="create_time") # 为了es结果和slots对应
kwh_24, charge_24, p_24, price_24 = by_slots(slots_24, es_re_24_dic)
# elif date_type == "month":
# intervel, slots = time_pick_transf(start, end)
# es_re = es_process(es_re, fmat="MM-DD") # 为了es结果和slots对应
......@@ -305,7 +305,7 @@ def start_end_date():
def time_str_to_str(date_str, format="HH:mm"):
"""YYYY-MM-DD HH:mm:ss格式转换为format格式"""
start_f = my_pendulum.from_format(date_str, 'YYYY-MM-DD HH:mm:ss')
start_f = my_pendulum.parse(date_str)
start_f = start_f.format(format)
return start_f
......
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