Commit 2bd87ca5 authored by ZZH's avatar ZZH

longgang data 2023-7-19

parent 4100febc
......@@ -29,7 +29,7 @@ class CompanyResponse(Model, DbErr):
@dataclass
class CmReq(Model):
cid: int = Int("公司id").eg(66)
cid: int = Opt(Int("公司id").eg(66))
@dataclass
......
......@@ -103,19 +103,19 @@ async def item_by_mitd_dao(mtids):
return datas
async def monitor_point_storey_join_in(cid, page_num, page_size):
async def load_compy_storey_points(cid, pg_num, pg_size):
"""monitor和point和storey联合查询, 分页"""
sql = "SELECT monitor.cid,c.address,point.name,point.create_time, " \
"monitor.sid, srm.room_name, srm.storey_name, monitor.longitude, " \
"point.mtid,monitor.latitude,point.pid FROM monitor " \
"inner join point on monitor.mtid = point.mtid inner join " \
"storey_room_map srm on point.pid = srm.point_id left join " \
"company c on c.cid = monitor.cid WHERE monitor.cid in %s " \
"and monitor.demolished = 0 order by point.pid limit %s, %s"
async with MysqlUtil() as conn:
monitor_point_storey_list = await conn.fetchall(sql, args=(
cid, (page_num - 1) * page_size, page_size))
return monitor_point_storey_list
sql = "SELECT monitor.cid, c.address, point.name, point.create_time, " \
"monitor.sid, monitor.meter_no, srm.room_name, srm.storey_name, " \
"monitor.longitude, point.mtid, monitor.latitude, point.pid " \
"FROM monitor " \
"inner join point on monitor.mtid = point.mtid " \
"inner join storey_room_map srm on point.pid = srm.point_id " \
"left join company c on c.cid = monitor.cid " \
"WHERE monitor.cid in %s and monitor.demolished = 0 " \
"order by point.pid limit %s, %s"
async with MysqlUtil() as conn:
return await conn.fetchall(sql, (cid, (pg_num - 1) * pg_size, pg_size))
async def meter_param_by_mid(mtid):
......
from dataclasses import dataclass
from pot_libs.sanic_api import Model
from pot_libs.sanic_api.column import Float, Str, List, Int, Dict, Opt
from pot_libs.common.components.fields import Cid
@dataclass
class BasicInfoReq(Model):
# cid: int = Int("工厂id").eg(78)
# cid: int = Opt(Int("工厂id").eg(78))
page_size: int = Opt(Int("页面大小").eg(10))
page_num: int = Opt(Int("当前页面").eg(1))
......@@ -46,3 +44,61 @@ class SupplementReq(Model):
start: str = Str("开始时间").eg("2021-12-29 00:00:00")
end: str = Str("开始时间").eg("2021-12-29 06:00:00")
type: str = Str("类型").eg("appliance or electric")
@dataclass
class RiskCount(Model):
security_user: int = Opt(Int("安全用户").eg(10))
risk_user: int = Opt(Int("风险用户").eg(2))
@dataclass
class ContentName(Model):
ele_overload: int = Opt(Int("用电超载").eg(10))
high_power_app: int = Opt(Int("大功率电器").eg(2))
illegal_ele_app: int = Opt(Int("违规电器").eg(2))
power_quality: int = Opt(Int("电能质量").eg(2))
ele_car_battery: int = Opt(Int("电能质量").eg(2))
@dataclass
class HomeDataResp(Model):
risk_distribution: RiskCount = Opt(RiskCount)
content_distribution: ContentName = Opt(ContentName)
electric_use_score: float = Opt(Float("用电安全指数").eg(90.3))
total_tenant: int = Opt(Int("接入住户").eg(20))
online_rate: float = Opt(Float("在线率").eg(0.8))
safe_day: float = Opt(Float("平均安全运行").eg(11.1))
total_power: float = Opt(Float("累计监测用电").eg(96000))
total_alarm: int = Opt(Int("累计报警次数").eg(5))
@dataclass
class HomeLstAlarmReq(Model):
cid: Opt(Cid)
importance: list = Opt(List("报警等级,默认:[1, 2, 3]").eg([1, 2, 3]))
page_size: int = Opt(Int("每页记录数").eg(10))
page_num: int = Opt(Int("当前页码").eg(1))
start: str = Opt(Str("开始时间").eg("2021-02-01 00:00:00"))
end: str = Opt(Str("结束时间").eg("2021-02-28 23:59:59"))
@dataclass
class HomeAlarmStatsReq(Model):
cid: Opt(Cid)
importance: list = Opt(List("报警等级,默认:[1, 2, 3]").eg([1, 2, 3]))
page_size: int = Opt(Int("每页记录数").eg(10))
page_num: int = Opt(Int("当前页码").eg(1))
start: str = Opt(Str("开始时间").eg("2021-02-01 00:00:00"))
end: str = Opt(Str("结束时间").eg("2021-02-28 23:59:59"))
@dataclass
class HomeAlarmStatsResp(Model):
ele_overload: dict = Dict("线路过载").eg(
{"slots": ["00-01", "00-02", "00-03"], "value": [1, 2, 3]})
illegal_ele_app: dict = Dict("违规电器").eg(
{"slots": ["00-01", "00-02", "00-03"], "value": [1, 2, 3]})
power_quality: dict = Dict("电能质量").eg(
{"slots": ["00-01", "00-02", "00-03"], "value": [1, 2, 3]})
from pot_libs.mysql_util.mysql_util import MysqlUtil
from pot_libs.es_util.es_utils import EsUtil
from unify_api.constants import POINT_1MIN_EVENT, SDU_ALARM_LIST
from unify_api.constants import SDU_ALARM_LIST
import pendulum
from unify_api.utils.time_format import CST
async def get_user_product_auth(user_id):
sql = "SELECT * from user_product_auth where user_id=%s and product=4"
async with MysqlUtil() as conn:
user_info = await conn.fetchone(sql, args=(user_id,))
return user_info
return await conn.fetchone(sql, args=(user_id,))
async def get_basic_info_by_mtid(mtid, cid):
......@@ -16,8 +16,7 @@ async def get_basic_info_by_mtid(mtid, cid):
"on p.pid=s.point_id LEFT JOIN monitor m on m.mtid=p.mtid " \
"where m.mtid=%s and m.demolished=0 and s.cid =%s"
async with MysqlUtil() as conn:
info = await conn.fetchone(sql, args=(mtid, cid))
return info
return await conn.fetchone(sql, args=(mtid, cid))
async def monitor_point_company(cids):
......@@ -26,45 +25,27 @@ async def monitor_point_company(cids):
"on c.cid=m.cid INNER JOIN point p on m.mtid=p.mtid " \
"INNER JOIN storey_room_map s on s.point_id=p.pid where c.cid in %s"
async with MysqlUtil() as conn:
datas = await conn.fetchall(sql, args=(cids, ))
return datas
return await conn.fetchall(sql, args=(cids,))
async def result_longgang_by_cid(cids, page_num, page_size, importance):
alarm_list = SDU_ALARM_LIST
async def load_lg_sdu_events(cid, pg_num, pg_size, importance):
cond_lst = [f"cid={cid}", f"event_type in {tuple(SDU_ALARM_LIST)}"]
if len(importance) > 0:
importance = str(tuple(importance)).replace(',)', ')')
cond_lst.append(f"importance in {importance}")
query_body = {
"from": (page_num - 1) * page_size,
"size": page_size,
"query": {
"bool": {
"must": [
{
"terms": {
"cid": cids
}
},
{
"terms": {
"type.keyword": alarm_list
}
},
{
"terms": {
"importance": importance
}
}
]
}
},
"sort": [
{
"datetime": {
"order": "desc"
}
}
]
}
async with EsUtil() as es:
es_re = await es.search_origin(body=query_body, index=POINT_1MIN_EVENT)
return es_re
time_format = "%Y-%m-%d %H:%M:%S"
end_date = str(pendulum.now(tz=CST).strftime(time_format))
cond_lst.append(f"event_datetime < '{end_date}'")
cond_str = " AND ".join(cond_lst)
async with MysqlUtil() as conn:
sql = f"select count(*) cnt from point_1min_event WHERE {cond_str};"
total = await conn.fetchone(sql)
total_count = total.get("cnt", 0)
if total_count <= 0:
return 0, []
sql = f"select * from point_1min_event WHERE {cond_str} " \
f"order by event_datetime desc limit %s,%s;"
data = await conn.fetchall(sql, args=((pg_num - 1) * pg_size, pg_size))
return total_count, data
import random
from pot_libs.sanic_api import summary
from unify_api.modules.shidianu.components.open_data_cps import (
BasicInfoReq, BasicInfoResp, StbDataReq, StbDataResp, SupplementReq
BasicInfoReq, BasicInfoResp, StbDataReq, StbDataResp, SupplementReq,
HomeDataResp, HomeLstAlarmReq, HomeAlarmStatsReq, HomeAlarmStatsResp
)
from unify_api.modules.alarm_manager.components.list_alarm import \
ListAlarmResponse
from unify_api.modules.shidianu.service.open_data_service import \
basic_info_longgang_service, stb_data_longgang_service, \
from unify_api.modules.shidianu.service.open_data_service import (
basic_info_longgang_service, stb_data_longgang_service,
supplement_data_service, result_longgang_service
)
from pot_libs.settings import SETTING
from unify_api.utils.time_format import last30_day_range
from unify_api.modules.common.dao.common_dao import monitor_by_cid
from unify_api.modules.home_page.service.count_info_service import safe_run_sdu
from unify_api.modules.common.procedures.power_cps import power_use_count
from unify_api.modules.common.procedures.alarm_cps import alarm_count_sdu_new
from unify_api.modules.shidianu.service.open_data_service import get_power
from unify_api.modules.common.components.select_company_cps import CmReq
from pot_libs.common.components.responses import success_res
from unify_api.modules.alarm_manager.service.alarm_static_service import (
sdu_alarm_statistics_service
)
from unify_api.modules.home_page.procedures.count_info_pds import (
electric_use_info_sdu
)
from unify_api.modules.common.dao.common_dao import storey_by_cid
from unify_api.modules.alarm_manager.service.list_alarm_service import \
new_list_alarm_service
# 数据对外开放接口
@summary("获取装置列表")
async def post_basic_info_longgang2(req, body: BasicInfoReq) -> BasicInfoResp:
async def post_basic_info_longgang(req, body: BasicInfoReq) -> BasicInfoResp:
user_id = req.ctx.user_id
# user_id = 10086
page_size = body.page_size or 10
......@@ -20,7 +41,7 @@ async def post_basic_info_longgang2(req, body: BasicInfoReq) -> BasicInfoResp:
@summary("查询数据")
async def post_stb_data_longgang2(req, body: StbDataReq) -> StbDataResp:
async def post_stb_data_longgang(req, body: StbDataReq) -> StbDataResp:
user_id = req.ctx.user_id
# user_id = 10086
# cid = body.cid
......@@ -29,27 +50,25 @@ async def post_stb_data_longgang2(req, body: StbDataReq) -> StbDataResp:
@summary("获取告警结果")
async def post_alarm_result_longgang2(req, body: BasicInfoReq) -> \
async def post_alarm_result_longgang(req, body: BasicInfoReq) -> \
ListAlarmResponse:
user_id = req.ctx.user_id
# user_id = 10086
page_size = body.page_size or 10
page_num = body.page_num or 1
pg_size = body.page_size or 10
pg_num = body.page_num or 1
importance = [2, 3]
return await result_longgang_service(user_id, importance, page_size,
page_num)
return await result_longgang_service(user_id, importance, pg_size, pg_num)
@summary("获取分析结果")
async def post_analyse_result_longgang2(req, body: BasicInfoReq) -> \
async def post_analyse_result_longgang(req, body: BasicInfoReq) -> \
ListAlarmResponse:
user_id = req.ctx.user_id
# user_id = 10086
page_size = body.page_size or 10
page_num = body.page_num or 1
pg_size = body.page_size or 10
pg_num = body.page_num or 1
importance = [1]
return await result_longgang_service(user_id, importance, page_size,
page_num)
return await result_longgang_service(user_id, importance, pg_size, pg_num)
@summary("补充数据")
......@@ -61,3 +80,96 @@ async def post_supplement_data(req, body: SupplementReq) -> StbDataResp:
end = body.end
type = body.type
return await supplement_data_service(user_id, cid, start, end, type)
@summary("首页信息")
async def post_home_page_data(req, body: CmReq) -> HomeDataResp:
user_id = req.ctx.user_id
cid = 223
cids = [223]
is_auth = await get_power(user_id, cids)
if not is_auth and not SETTING.debug_mode:
return success_res(code=4001, msg="您没有权限访问")
start, end = last30_day_range()
product = 4
# 安全和报警统计
res = await sdu_alarm_statistics_service([cid], start, end, product)
# 安全指数
alarm_res = await electric_use_info_sdu(cid)
electric_use_score = round(alarm_res.electric_use_score)
# 1. 接入住户,从monitor表取,解决拆除逻辑
monitor_list = await monitor_by_cid(cid)
total_tenant = len(monitor_list)
# 2. 安全运行天数: 以天计,当工厂某天I级、II级报警总数小于总户数*5%时,即为安全运行,
# 展示自接入累加安全运行天数
safe_day = await safe_run_sdu(cid, total_tenant)
# 3. 在线率
online_rate = 88 + random.choice([1, 1.5, 2, 2.5, 3, 3.5, 4])
# 4.累计用电
total_power = await power_use_count(cids)
# 5. 累计报警
total_alarm = await alarm_count_sdu_new(cids)
return HomeDataResp(
risk_distribution=res.risk_distribution,
content_distribution=res.content_distribution,
electric_use_score=electric_use_score,
total_tenant=total_tenant,
online_rate=online_rate,
safe_day=safe_day,
total_power=total_power,
total_alarm=total_alarm,
)
@summary("首页-最近报警")
async def post_home_page_lst_alarm(req, body: HomeLstAlarmReq) -> \
ListAlarmResponse:
user_id = req.ctx.user_id
# cid = body.cid
cid = 223
is_auth = await get_power(user_id, [cid])
if not is_auth and not SETTING.debug_mode:
return success_res(code=4001, msg="您没有权限访问")
importance = body.importance
page_size = body.page_size
page_num = body.page_num
start = body.start
end = body.end
product = 4
point_ids = None
storeys = await storey_by_cid(cid)
storey_ids = [item["storey_id"] for item in storeys]
return await new_list_alarm_service(cid, storey_ids, page_num, page_size,
start, end, product, importance,
point_ids)
@summary("首页-运行趋势")
async def post_home_page_alarm_stats(req, body: HomeAlarmStatsReq) -> \
HomeAlarmStatsResp:
user_id = req.ctx.user_id
# cid = body.cid
cid = 223
is_auth = await get_power(user_id, [cid])
if not is_auth and not SETTING.debug_mode:
return success_res(code=4001, msg="您没有权限访问")
# 1. 获取参数
start = body.start
end = body.end
product = 4
sr = await sdu_alarm_statistics_service([cid], start, end, product)
return HomeAlarmStatsResp(
ele_overload=sr.ele_overload,
illegal_ele_app=sr.illegal_ele_app,
power_quality=sr.power_quality
)
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