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
5be6d7c4
Commit
5be6d7c4
authored
Jun 25, 2023
by
ZZH
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change real time electric load from redis 2023-6-25
parent
27de7a5d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
185 deletions
+86
-185
electric_service.py
unify_api/modules/electric/service/electric_service.py
+57
-38
electric.py
unify_api/modules/electric/views/electric.py
+29
-147
No files found.
unify_api/modules/electric/service/electric_service.py
View file @
5be6d7c4
import
json
import
pandas
as
pd
import
pendulum
from
pot_libs.settings
import
SETTING
from
pot_libs.logger
import
log
from
pot_libs.utils.exc_util
import
BusinessException
from
pot_libs.aredis_util.aredis_utils
import
RedisUtils
from
unify_api.constants
import
(
POINT_LEVEL_MAP
,
U_THRESHOLD
,
COSTTL_THRESHOLD
,
LF_THRESHOLD
,
THDU_THRESHOLD
,
BL_THRESHOLD
,
THDI_THRESHOLD
)
from
unify_api.modules.common.procedures.points
import
points_by_storeys
,
\
get_meter_by_point
from
unify_api.modules.common.service.td_engine_service
import
\
get_td_engine_data
from
unify_api.modules.electric.dao.electric_dao
import
\
monitor_point_join_by_points
,
get_electric_datas_dao
from
unify_api.modules.electric.procedures.electric_pds
import
\
...
...
@@ -18,13 +20,13 @@ from unify_api.utils.common_utils import round_2, round_4, multiplication_two
from
unify_api.modules.electric.procedures.electric_util
import
\
get_wiring_type
,
load_point_ctnum
from
datetime
import
datetime
from
unify_api.constants
import
REAL_EXP_TIME
from
unify_api.modules.common.procedures.location_temp_rcurrent
import
\
location_stats_statics
from
unify_api.modules.electric.components.electric
import
(
ElecIndexResponse
,
ElecIndex
,
EscResp
,
QcsResp
,
EclResp
,
QclResp
,
)
from
unify_api.utils.taos_new
import
parse_td_columns
from
unify_api.utils.time_format
import
get_15min_ago
from
unify_api.utils.time_format
import
CST
,
YMD_Hms
,
timestamp2dts
async
def
elec_current_storeys_service
(
storeys
):
...
...
@@ -33,7 +35,7 @@ async def elec_current_storeys_service(storeys):
point_list
=
await
points_by_storeys
(
storeys
)
# mtids
mtids
=
[
i
.
get
(
"mtid"
)
for
i
in
point_list
]
cid
=
point_list
[
0
][
'cid'
]
if
len
(
point_list
)
>
0
else
0
cid
=
point_list
[
0
][
"cid"
]
if
len
(
point_list
)
>
0
else
0
# 2.获取mid, ctnum
# point_mid = await batch_get_wiring_type(points)
# # 3. 获取redis数据
...
...
@@ -70,8 +72,8 @@ async def elec_current_storeys_service(storeys):
"freq"
:
round_2
(
res
[
mtid
]
.
get
(
"freq"
)),
"costtl"
:
round_2
(
res
[
mtid
]
.
get
(
"costtl"
)),
"lf"
:
round_2
(
res
[
mtid
]
.
get
(
"lf"
)),
"sdu_i"
:
res
[
mtid
]
.
get
(
'sdu_i'
),
"sdu_u"
:
res
[
mtid
]
.
get
(
'sdu_u'
),
"sdu_i"
:
res
[
mtid
]
.
get
(
"sdu_i"
),
"sdu_u"
:
res
[
mtid
]
.
get
(
"sdu_u"
),
}
# redis数据过期,或者没有数据
else
:
...
...
@@ -124,7 +126,7 @@ async def qual_current_storeys_service(storeys):
# # 3. 获取redis数据
# res = await qual_current_data(point_mid)
mtids
=
[
point
[
"mtid"
]
for
point
in
point_list
if
point
[
"mtid"
]]
cid
=
point_list
[
0
][
'cid'
]
if
len
(
point_list
)
>
0
else
0
cid
=
point_list
[
0
][
"cid"
]
if
len
(
point_list
)
>
0
else
0
res
=
await
elec_current_data
(
mtids
,
cid
)
# 4. 返回数据
qual_data
=
{}
...
...
@@ -208,7 +210,7 @@ async def elec_card_level_service(point_list):
# res_redis = await elec_current_data(point_mid)
mtids
=
[
monitor
[
"mtid"
]
for
monitor
in
monitor_point_list
if
monitor
[
"mtid"
]]
cid
=
monitor_point_list
[
0
][
'cid'
]
if
len
(
monitor_point_list
)
>
0
else
0
cid
=
monitor_point_list
[
0
][
"cid"
]
if
len
(
monitor_point_list
)
>
0
else
0
results
=
await
elec_current_data
(
mtids
,
cid
)
# 4. 返回数据
ret_data
=
{
...
...
@@ -302,7 +304,7 @@ async def qual_current_level_service(point_list):
# res_redis = await qual_current_data(point_mid)
mtids
=
[
monitor
[
"mtid"
]
for
monitor
in
monitor_point_list
if
monitor
[
"mtid"
]]
cid
=
monitor_point_list
[
0
][
'cid'
]
if
len
(
monitor_point_list
)
>
0
else
0
cid
=
monitor_point_list
[
0
][
"cid"
]
if
len
(
monitor_point_list
)
>
0
else
0
res
=
await
elec_current_data
(
mtids
,
cid
)
# 4. 返回数据
ret_data
=
{
...
...
@@ -583,32 +585,49 @@ async def elec_index_service(cid, point_id, start, end):
async
def
elec_current_service
(
point_id
):
# 获取mtid
meter_info
=
await
get_meter_by_point
(
point_id
)
if
not
meter_info
:
raise
BusinessException
(
message
=
"没有该监测点的monitor信息,请联系运维人员!"
)
mtid
=
meter_info
[
"mtid"
]
last_15min_time
=
get_15min_ago
()
# 获取子表中的实时数据
url
=
f
"{SETTING.stb_url}db_electric?tz=Asia/Shanghai"
sql
=
f
"select last_row(*) from mt{mtid}_ele where ts>='{last_15min_time}'"
is_succ
,
results
=
await
get_td_engine_data
(
url
,
sql
)
if
not
is_succ
or
not
results
or
results
.
get
(
"code"
)
>
0
:
return
''
,
{}
head
=
parse_td_columns
(
results
)
if
not
results
[
"data"
]:
results
[
"data"
]
=
[
''
for
i
in
range
(
len
(
head
))]
res
=
dict
(
zip
(
head
,
results
[
"data"
][
0
]))
p_info
=
await
get_meter_by_point
(
point_id
)
if
not
p_info
or
not
p_info
[
"mtid"
]:
msg
=
f
"没有监测点:{point_id} monitor信息,请联系运维人员!"
raise
BusinessException
(
message
=
msg
)
# 识电U只有一项有数据,返回具体的项
ctnum
=
res
.
get
(
"ctnum"
)
or
3
res
=
get_sdu_i_and_u
(
res
,
ctnum
)
now_ts
=
pendulum
.
now
(
tz
=
CST
)
.
int_timestamp
d_ele_rt
,
ts
=
None
,
now_ts
try
:
mtid
=
p_info
[
"mtid"
]
key
=
f
"real_time:electric:{SETTING.mysql_db}:{mtid}"
key_hr
=
f
"real_time:electric_hr:{SETTING.mysql_db}:{mtid}"
rt_ele
,
rt_ele_hr
=
await
RedisUtils
()
.
mget
([
key
,
key_hr
])
if
rt_ele
and
rt_ele_hr
:
rt_ele
,
rt_ele_hr
=
json
.
loads
(
rt_ele
),
json
.
loads
(
rt_ele_hr
)
if
now_ts
-
rt_ele
[
"ts"
]
<=
REAL_EXP_TIME
:
if
now_ts
-
rt_ele_hr
[
"ts"
]
<=
REAL_EXP_TIME
:
for
k
in
rt_ele_hr
.
keys
():
if
k
not
in
rt_ele
.
keys
():
rt_ele
[
k
]
=
rt_ele_hr
[
k
]
if
res
.
get
(
"ts"
):
time_str
=
str
(
res
[
"ts"
])[
0
:
19
]
else
:
time_str
=
time_format
.
get_datetime_str
(
0
)
return
time_str
,
res
d_ele_rt
,
ts
=
rt_ele
,
rt_ele
[
"ts"
]
elif
rt_ele
:
rt_ele
=
json
.
loads
(
rt_ele
)
if
now_ts
-
rt_ele
[
"ts"
]
<=
REAL_EXP_TIME
:
d_ele_rt
,
ts
=
rt_ele
,
rt_ele
[
"ts"
]
elif
rt_ele_hr
:
rt_ele_hr
=
json
.
loads
(
rt_ele_hr
)
if
now_ts
-
rt_ele_hr
[
"ts"
]
<=
REAL_EXP_TIME
:
d_ele_rt
,
ts
=
rt_ele_hr
,
rt_ele_hr
[
"ts"
]
except
Exception
as
e
:
log
.
error
(
f
"parse real time electric error, pid:{point_id}"
)
log
.
exception
(
e
)
time_str
=
timestamp2dts
(
ts
,
YMD_Hms
)
if
d_ele_rt
is
None
:
return
time_str
,
None
# 识电U只有一项有数据,返回具体的项
ctnum
=
d_ele_rt
.
get
(
"ctnum"
)
or
3
return
time_str
,
get_sdu_i_and_u
(
d_ele_rt
,
ctnum
)
def
get_sdu_i_and_u
(
res
,
ctnum
):
...
...
@@ -617,15 +636,15 @@ def get_sdu_i_and_u(res, ctnum):
'''
res
[
"sdu_i"
]
=
None
res
[
"sdu_u"
]
=
None
meter_
n
=
res
.
get
(
'meter_sn'
,
''
)
.
lower
()
if
meter_
n
==
'a'
:
meter_
sn
=
res
.
get
(
"meter_sn"
,
""
)
.
lower
()
if
meter_
sn
==
"a"
:
res
[
"sdu_i"
]
=
"ia"
res
[
"sdu_u"
]
=
"ua"
if
ctnum
==
3
else
"uab"
if
meter_
n
==
'b'
:
if
meter_
sn
==
"b"
:
res
[
"sdu_i"
]
=
"ib"
if
ctnum
==
3
:
res
[
"sdu_u"
]
=
"ub"
if
meter_
n
==
'c'
:
if
meter_
sn
==
"c"
:
res
[
"sdu_i"
]
=
"ic"
res
[
"sdu_u"
]
=
"uc"
if
ctnum
==
3
else
"ucb"
return
res
unify_api/modules/electric/views/electric.py
View file @
5be6d7c4
This diff is collapsed.
Click to expand it.
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