Commit dd334441 authored by lcn's avatar lcn

修复Bug

parent d85b4ef3
...@@ -53,16 +53,20 @@ class EnergyStoreOptimize(object): ...@@ -53,16 +53,20 @@ class EnergyStoreOptimize(object):
'capacity_price': pp_info_d['price_tc'], 'capacity_price': pp_info_d['price_tc'],
'max_demand_price': pp_info_d['price_md']} 'max_demand_price': pp_info_d['price_md']}
if pp_info_d['price_s']: if pp_info_d['price_s']:
sct = await self._contruct_section('s', pp_info_d, time_str_d, max_dt) sct = await self._contruct_section('s', pp_info_d, time_str_d,
max_dt)
inline_var['section_s'] = sct inline_var['section_s'] = sct
if pp_info_d['price_p']: if pp_info_d['price_p']:
sct = await self._contruct_section('p', pp_info_d, time_str_d, max_dt) sct = await self._contruct_section('p', pp_info_d, time_str_d,
max_dt)
inline_var['section_p'] = sct inline_var['section_p'] = sct
if pp_info_d['price_f']: if pp_info_d['price_f']:
sct = await self._contruct_section('f', pp_info_d, time_str_d, max_dt) sct = await self._contruct_section('f', pp_info_d, time_str_d,
max_dt)
inline_var['section_f'] = sct inline_var['section_f'] = sct
if pp_info_d['price_v']: if pp_info_d['price_v']:
sct = await self._contruct_section('v', pp_info_d, time_str_d, max_dt) sct = await self._contruct_section('v', pp_info_d, time_str_d,
max_dt)
inline_var['section_v'] = sct inline_var['section_v'] = sct
# contruct df_curve # contruct df_curve
...@@ -156,152 +160,72 @@ class EnergyStoreOptimize(object): ...@@ -156,152 +160,72 @@ class EnergyStoreOptimize(object):
""" build es query sentance for find max kwh day.""" """ build es query sentance for find max kwh day."""
dt = pendulum.now() dt = pendulum.now()
dt_half_year_ago = dt.subtract(months=6) dt_half_year_ago = dt.subtract(months=6)
q = { start_time = dt_half_year_ago.format("YYYY-MM-DD HH:mm:ss")
"size": 1, end_time = dt.format("YYYY-MM-DD HH:mm:ss")
"query": { sql = f"""
"bool": { select create_time from inline_1day_power
"must": [ where inlid = %s and create_time >= %s and create_time < %s
{"term": { order by kwh desc limit 1
"inlid": { """
"value": self._inlid async with MysqlUtil() as conn:
} result = await conn.fetchone(sql, args=(self._inlid, start_time,
}}, end_time))
{"range": {
"day": { return result.get("create_time") if result else None
"gte": str(dt_half_year_ago),
"lt": str(dt)
}
}}
]
}
},
"sort": [
{
"kwh": {
"order": "desc"
}
}
]
}
return q
async def _find_kwh_max_day(self): async def _find_kwh_max_day(self):
""" find the max kwh day in latest 6 months. """ find the max kwh day in latest 6 months.
:return: a dt object, or None if no doc :return: a dt object, or None if no doc
""" """
rlt = None rlt = None
q = await self._build_max_kwh_day() create_time = await self._build_max_kwh_day()
async with EsUtil() as es: if not create_time:
search_rlt = await es.search_origin( return create_time
body=q, create_time = create_time.strftime("%Y-%m-%d %H:%M:%S")
index=INLINE_1DAY_POWER_ESINDEX) return pendulum.parse(create_time, tz='Asia/Shanghai')
# search_rlt = self._es.search(INLINE_1DAY_POWER_ESINDEX, q)
hits_list = search_rlt['hits']['hits']
try:
max_day_doc = hits_list[0]['_source']
except IndexError:
pass
else:
day_str = max_day_doc['day']
rlt = pendulum.from_format(day_str, 'YYYY-MM-DDTHH:mm:ssZ',
tz='Asia/Shanghai')
return rlt
def _build_aggs_kwh(self, p_char, start_dt): async def _build_aggs_kwh(self, p_char, start_dt):
end_dt = start_dt.add(days=1) end_dt = start_dt.add(days=1)
q = { start_time = start_dt.format("YYYY-MM-DD HH:mm:ss")
"size": 0, end_time = end_dt.format("YYYY-MM-DD HH:mm:ss")
"query": { sql = f"""
"bool": { select sum(kwh) kwh from inline_15min_power
"must": [ where inlid = %s and spfv = %s and create_time >= %s and
{"term": { create_time < %s
"inlid": { order by kwh desc limit 1
"value": self._inlid """
} async with MysqlUtil() as conn:
}}, result = await conn.fetchone(sql, args=(self._inlid, p_char,
{"term": { start_time,
"spfv": { end_time))
"value": p_char
} return result.get("kwh") if result else 0
}},
{"range": {
"quarter_time": {
"gte": str(start_dt),
"lt": str(end_dt)
}
}}
]
}
},
"aggs": {
"kwh": {
"sum": {
"field": "kwh"
}
}
}
}
return q
async def _get_total_kwh_of_one_pchar(self, p_char, start_dt): async def _get_total_kwh_of_one_pchar(self, p_char, start_dt):
q = self._build_aggs_kwh(p_char, start_dt) return await self._build_aggs_kwh(p_char, start_dt)
async with EsUtil() as es:
search_rlt = await es.search_origin(body=q, index=INLINE_15MIN_POWER_ESINDEX)
# search_rlt = self._es.search(INLINE_15MIN_POWER_ESINDEX, q)
aggs_rlt = search_rlt['aggregations']
return aggs_rlt['kwh']['value']
def _build_kw_curve(self, start_dt): async def _build_kw_curve(self, start_dt):
end_dt = start_dt.add(days=1) end_dt = start_dt.add(days=1)
q = { start_time = start_dt.format("YYYY-MM-DD HH:mm:ss")
"size": 100, end_time = end_dt.format("YYYY-MM-DD HH:mm:ss")
"_source": ["quarter_time", "p"], sql = f"""
"query": { select create_time,p from inline_15min_power
"bool": { where inlid = %s and create_time >= %s and
"must": [ create_time < %s
{"term": { order by create_time asc limit 100
"inlid": { """
"value": self._inlid async with MysqlUtil() as conn:
} results = await conn.fetchall(sql, args=(self._inlid,
}}, start_time,
{"range": { end_time))
"quarter_time": { return results or []
"gte": str(start_dt),
"lt": str(end_dt)
}
}}
]
}
},
"sort": [
{
"quarter_time": {
"order": "asc"
}
}
]
}
return q
async def _get_kw_curve(self, start_dt): async def _get_kw_curve(self, start_dt):
q = self._build_kw_curve(start_dt) hits_list = await self._build_kw_curve(start_dt)
async with EsUtil() as es:
search_rlt = await es.search_origin(
body=q,
index=INLINE_15MIN_POWER_ESINDEX)
# search_rlt = self._es.search(INLINE_15MIN_POWER_ESINDEX, q)
# hits_list is already sorted by quarter_time asc
hits_list = search_rlt['hits']['hits']
kw_list = [] kw_list = []
for item in hits_list: for item in hits_list:
src_d = item['_source'] src_d = item
qrt_str = src_d['quarter_time'] src_d['quarter_time'] = item.get("create_time")
dt = pendulum.from_format(qrt_str, 'YYYY-MM-DDTHH:mm:ssZ',
tz='Asia/Shanghai')
qrt_dt = datetime.datetime(year=dt.year, month=dt.month,
day=dt.day, hour=dt.hour,
minute=dt.minute, second=dt.second)
src_d['quarter_time'] = qrt_dt
kw_list.append(src_d) kw_list.append(src_d)
df = pd.DataFrame(kw_list) df = pd.DataFrame(kw_list)
return df return df
......
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