Commit dd334441 authored by lcn's avatar lcn

修复Bug

parent d85b4ef3
......@@ -27,11 +27,11 @@ class AutoDic(dict):
class EnergyStoreOptimize(object):
def __init__(self, inlid):
self._inlid = inlid
# self._r_cache = redis.Redis(host="172.18.1.253", db=1)
async def calc_inline(self, ess_system):
rlt = {'rlt_flag': True}
inl_info = await self._get_inline_info()
......@@ -47,27 +47,31 @@ class EnergyStoreOptimize(object):
max_dt.int_timestamp)
time_str_d = PricePolicyHelper.quarter_chars_2_time_str(
pp_info_d['quarters'])
# construct inline_var
inline_var = {'inline_capacity': inl_info['tc_runtime'],
'capacity_price': pp_info_d['price_tc'],
'max_demand_price': pp_info_d['price_md']}
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
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
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
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
# contruct df_curve
df_curve = await self._get_kw_curve(max_dt)
# handle return
if len(df_curve) == 0:
rlt['rlt_flag'] = False
......@@ -86,7 +90,7 @@ class EnergyStoreOptimize(object):
eot.economic_evaluate)
rlt['opt_curve'] = self.convert_opt_curve(eot.opt_curve)
return rlt
def convert_economic_evaluate(self, economic_evaluate):
invest_income_table = economic_evaluate['invest_income_table']
table = []
......@@ -102,7 +106,7 @@ class EnergyStoreOptimize(object):
table.append(tmp_d)
economic_evaluate['invest_income_table'] = table
return economic_evaluate
def convert_opt_curve(self, opt_curve):
rlt = []
for idx, row in opt_curve.iterrows():
......@@ -113,7 +117,7 @@ class EnergyStoreOptimize(object):
tmp_d['load_bat_curve'] = row['load_bat_curve']
rlt.append(tmp_d)
return rlt
async def _contruct_section(self, p_char, pp_info_d, time_str_d, max_dt):
""" contruct section_x for inline_var."""
section = {'price': pp_info_d['price_' + p_char]}
......@@ -122,7 +126,7 @@ class EnergyStoreOptimize(object):
time_range_str = ';'.join(time_str_d[p_char])
section['time_range'] = time_range_str
return section
async def _get_inline_info(self):
""" get inline_vc, tc_runtime, cid from redis.
:return: a dict
......@@ -137,7 +141,7 @@ class EnergyStoreOptimize(object):
'tc_runtime': info['tc_runtime'],
'cid': info['cid']}
return rlt
async def _get_company_price_policy(self, cid):
# pp_json = await RedisUtils(db=1).hget(PRICE_POLICY_HASHNAME, str(cid))
# pp_json = self._r_cache.hget(PRICE_POLICY_HASHNAME, str(cid))
......@@ -151,157 +155,77 @@ class EnergyStoreOptimize(object):
return result
# pp = json.loads(pp_json)
# return pp
async def _build_max_kwh_day(self):
""" build es query sentance for find max kwh day."""
dt = pendulum.now()
dt_half_year_ago = dt.subtract(months=6)
q = {
"size": 1,
"query": {
"bool": {
"must": [
{"term": {
"inlid": {
"value": self._inlid
}
}},
{"range": {
"day": {
"gte": str(dt_half_year_ago),
"lt": str(dt)
}
}}
]
}
},
"sort": [
{
"kwh": {
"order": "desc"
}
}
]
}
return q
start_time = dt_half_year_ago.format("YYYY-MM-DD HH:mm:ss")
end_time = dt.format("YYYY-MM-DD HH:mm:ss")
sql = f"""
select create_time from inline_1day_power
where inlid = %s and create_time >= %s and create_time < %s
order by kwh desc limit 1
"""
async with MysqlUtil() as conn:
result = await conn.fetchone(sql, args=(self._inlid, start_time,
end_time))
return result.get("create_time") if result else None
async def _find_kwh_max_day(self):
""" find the max kwh day in latest 6 months.
:return: a dt object, or None if no doc
"""
rlt = None
q = await self._build_max_kwh_day()
async with EsUtil() as es:
search_rlt = await es.search_origin(
body=q,
index=INLINE_1DAY_POWER_ESINDEX)
# 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):
create_time = await self._build_max_kwh_day()
if not create_time:
return create_time
create_time = create_time.strftime("%Y-%m-%d %H:%M:%S")
return pendulum.parse(create_time, tz='Asia/Shanghai')
async def _build_aggs_kwh(self, p_char, start_dt):
end_dt = start_dt.add(days=1)
q = {
"size": 0,
"query": {
"bool": {
"must": [
{"term": {
"inlid": {
"value": self._inlid
}
}},
{"term": {
"spfv": {
"value": p_char
}
}},
{"range": {
"quarter_time": {
"gte": str(start_dt),
"lt": str(end_dt)
}
}}
]
}
},
"aggs": {
"kwh": {
"sum": {
"field": "kwh"
}
}
}
}
return q
start_time = start_dt.format("YYYY-MM-DD HH:mm:ss")
end_time = end_dt.format("YYYY-MM-DD HH:mm:ss")
sql = f"""
select sum(kwh) kwh from inline_15min_power
where inlid = %s and spfv = %s and create_time >= %s and
create_time < %s
order by kwh desc limit 1
"""
async with MysqlUtil() as conn:
result = await conn.fetchone(sql, args=(self._inlid, p_char,
start_time,
end_time))
return result.get("kwh") if result else 0
async def _get_total_kwh_of_one_pchar(self, p_char, start_dt):
q = 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):
return await self._build_aggs_kwh(p_char, start_dt)
async def _build_kw_curve(self, start_dt):
end_dt = start_dt.add(days=1)
q = {
"size": 100,
"_source": ["quarter_time", "p"],
"query": {
"bool": {
"must": [
{"term": {
"inlid": {
"value": self._inlid
}
}},
{"range": {
"quarter_time": {
"gte": str(start_dt),
"lt": str(end_dt)
}
}}
]
}
},
"sort": [
{
"quarter_time": {
"order": "asc"
}
}
]
}
return q
start_time = start_dt.format("YYYY-MM-DD HH:mm:ss")
end_time = end_dt.format("YYYY-MM-DD HH:mm:ss")
sql = f"""
select create_time,p from inline_15min_power
where inlid = %s and create_time >= %s and
create_time < %s
order by create_time asc limit 100
"""
async with MysqlUtil() as conn:
results = await conn.fetchall(sql, args=(self._inlid,
start_time,
end_time))
return results or []
async def _get_kw_curve(self, start_dt):
q = 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']
hits_list = await self._build_kw_curve(start_dt)
kw_list = []
for item in hits_list:
src_d = item['_source']
qrt_str = src_d['quarter_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
src_d = item
src_d['quarter_time'] = item.get("create_time")
kw_list.append(src_d)
df = pd.DataFrame(kw_list)
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