Commit 313d2526 authored by lcn's avatar lcn

修复Bug

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