Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
U
unify_api2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chaonan
unify_api2
Commits
dd334441
Commit
dd334441
authored
Apr 19, 2023
by
lcn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复Bug
parent
d85b4ef3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
150 deletions
+74
-150
energy_store_optimize.py
.../modules/energy_optimize/service/energy_store_optimize.py
+74
-150
No files found.
unify_api/modules/energy_optimize/service/energy_store_optimize.py
View file @
dd334441
...
...
@@ -53,16 +53,20 @@ class EnergyStoreOptimize(object):
'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
...
...
@@ -156,152 +160,72 @@ class EnergyStoreOptimize(object):
""" 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
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'
)
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
)
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'
]
return
await
self
.
_build_aggs_kwh
(
p_char
,
start_dt
)
def
_build_kw_curve
(
self
,
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment