You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

502 lines
17 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: utf-8 -*-
import json
import logging
from website import errors, consts
from website.db.enterprise_busi_model import enterprise_busi_model as DB_BusiModel
from website.db.enterprise_busi_model import (
enterprise_busi_model_node_device as DB_BusiModelNodeDevice,
)
from website.db.enterprise_device import enterprise_device as DB_Device
from website.db.enterprise_entity import enterprise_entity as DB_Entity
from website.db.enterprise_node import enterprise_node as DB_Node
from website.db.enterprise_node import enterprise_node_alert as DB_NodeAlert
from website.handler import APIHandler, authenticated, operation_log
from website.util import shortuuid
class AddHandler(APIHandler):
"""
- 描述:添加企业节点
- 请求方式post
- 请求参数:
>- entity_suid, int, 企业id, short uuid
>- name, string, 名称
>- parent, int, 上级节点id
>- addr, string, 地址
>- lola, string, 经纬度longitude and latitude
>- contact, string, 负责人
>- phone, string, 联系方式
>- comment, string, 简介
- 返回值:无
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_add_str, "添加企业节点")
def post(self):
entity_id = self.get_int_argument("entity_id")
entity_suid = self.get_escaped_argument("entity_suid", "")
name = self.get_escaped_argument("name", "")
parent = self.get_int_argument("parent", 0)
addr = self.get_escaped_argument("addr", "")
lola = self.get_escaped_argument("lola", "")
contact = self.get_escaped_argument("contact", "")
phone = self.get_escaped_argument("phone", "")
comment = self.get_escaped_argument("comment", "")
if not entity_id or not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_entity = DB_Entity.EnterpriseEntityRepository()
entity_suid = db_entity.get_entity_suid(entity_id)
db_node = DB_Node.EnterpriseNodeRepository()
db_node.insert_node(
{
"suid": shortuuid.ShortUUID().random(length=10),
"entity_id": entity_id,
"entity_suid": entity_suid,
"name": name,
"parent": parent,
"addr": addr,
"lola": lola,
"contact": contact,
"phone": phone,
"comment": comment,
}
)
self.finish()
class EditHandler(APIHandler):
"""
- 描述:更新企业节点
- 请求方式post
- 请求参数:
>- node_id, int, 节点id
>- name, string, 名称
>- parent, int, 上级节点id
>- addr, string, 地址
>- lola, string, 经纬度longitude and latitude
>- contact, string, 负责人
>- phone, string, 联系方式
>- comment, string, 简介
- 返回值:无
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_edit_str, "更新企业节点")
def post(self):
node_id = self.get_int_argument("node_id", 0)
name = self.get_escaped_argument("name", "")
parent = self.get_int_argument("parent", 0)
addr = self.get_escaped_argument("addr", "")
lola = self.get_escaped_argument("lola", "")
contact = self.get_escaped_argument("contact", "")
phone = self.get_escaped_argument("phone", "")
comment = self.get_escaped_argument("comment", "")
if not node_id or not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
db_node.update_node(
{
"id": node_id,
"name": name,
"parent": parent,
"addr": addr,
"lola": lola,
"contact": contact,
"phone": phone,
"comment": comment,
}
)
self.finish()
class TreeHandler(APIHandler):
"""
- 描述:企业节点树
- 请求方式post
- 请求参数:
>- entity_id, int, 企业id
>- name, string, 搜索内容
- 返回值:
```
{
"data":[
{
"id": 123,
"name": "xxx",
"children": [
{
"id": 123,
"name": "xxx"
"children": [
{
"id": 123,
"name": "xxx"
},
]
},
...
]
},
...
]
}
```
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_list_str, "企业节点树")
def post(self):
entity_id = self.get_escaped_argument("entity_id", "")
name = self.get_escaped_argument("name", "")
# logging.info(f"name is {name}")
if not entity_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
nodes = db_node.select_tree(entity_id, name)
self.finish({"data": nodes})
class InfoHandler(APIHandler):
"""
- 描述:企业节点信息
- 请求方式post
- 请求参数:
>- node_id, int, 节点id
- 返回值:
```
{
"name": "xxx",
"parent": "xxx",
"addr": "xxx",
"lola": "xxx",
"contact": "xxx",
"phone": "xxx",
"comment": "xxx",
}
```
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_list_str, "企业节点信息")
def post(self):
node_id = self.get_int_argument("node_id")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
node = db_node.select_node(node_id)
node = node and node or {}
self.finish(node)
class DeleteHandler(APIHandler):
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_list_str, "删除节点")
def post(self):
node_id = self.get_int_argument("node_id")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
db_node.delete_node(node_id)
self.finish()
class BusimodelHandler(APIHandler):
"""
- 描述:企业节点,业务模型部署
- 请求方式post
- 请求参数:
> - pageNo
> - pageSize
> - node_id, int, 节点id
- 返回值:
```
{
"count": 123,
"data": [
{
"busi_model_id": 123, # 业务模型id
"busi_model_name": "xxx", # 业务模型name
"deployed": 0, # 是否部署
"create_time": "xxx" # 创建时间
},
...
]
}
```
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_list_str, "业务模型部署列表")
def post(self):
node_id = self.get_int_argument("node_id")
pageNo = self.get_int_argument("pageNo", 1)
pageSize = self.get_int_argument("pageSize", 20)
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "企业节点不能为空")
db_model_node = DB_BusiModel.EnterpriseBusiModelNodeRepository()
busi_models = db_model_node.get_busi_model_by_node_id(node_id, pageNo, pageSize)
count = busi_models["count"]
models = busi_models["data"]
db_mode_node_device = (
DB_BusiModelNodeDevice.EnterpriseBusiModelNodeDeviceRepository()
)
for busi_model in models:
deployed = db_mode_node_device.check_deployed(
node_id, busi_model["busi_model_id"]
)
busi_model["deployed"] = deployed
self.finish({"count": count, "data": models})
class BusimodelInfoHandler(APIHandler):
"""
- 描述:企业节点,业务模型部署 -> 业务模型信息
- 请求方式post
- 请求参数:
> - node_id, int, 节点id
> - busi_model_id, int业务模型id
- 返回值:
```
{
"busi_model_name": "xx", # 业务模型名称
"busi_model_comment": "xxx", # 简介
"base_models": "xxx,xx,xx,xx" # 关联基础模型
"devices": [
{
"device_id": 123,
"device_name": "xxx"
},
...
]
}
```
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_list_str, "业务模型部署 -> 业务模型信息")
def post(self):
node_id = self.get_int_argument("node_id")
busi_model_id = self.get_int_argument("busi_model_id")
if not node_id or not busi_model_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_busi_model = DB_BusiModel.EnterpriseBusiModelRepository()
busi_model = db_busi_model.get_busi_model_by_id(busi_model_id)
busi_model_name = busi_model.name
busi_model_comment = busi_model.comment
base_models = json.loads(busi_model.base_models)
base_model_names = ",".join([base_model["name"] for base_model in base_models])
db_mode_node_device = (
DB_BusiModelNodeDevice.EnterpriseBusiModelNodeDeviceRepository()
)
device_ids = db_mode_node_device.get_device_ids(node_id, busi_model_id)
device_ids = list(set([item["device_id"] for item in device_ids]))
db_device = DB_Device.EnterpriseDeviceRepository()
devices = db_device.get_devices(device_ids)
devices_return = [
{"device_id": item["id"], "device_name": item["name"]} for item in devices
]
self.finish(
{
"busi_model_name": busi_model_name,
"busi_model_comment": busi_model_comment,
"base_models": base_model_names,
"devices": devices_return,
}
)
class BusimodelDeployHandler(APIHandler):
"""
- 描述:企业节点,节点信息 -> 业务模型部署 -> 业务模型配置
- 请求方式post
- 请求参数:
> - node_id, int, 节点id
> - busi_model_id, int业务模型id
> - device_ids, string, 设备id, 多个id逗号分割
- 返回值:无
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_add_str, "业务模型部署 -> 业务模型配置")
def post(self):
node_id = self.get_int_argument("node_id")
busi_model_id = self.get_int_argument("busi_model_id")
device_ids = self.get_escaped_argument("device_ids", "")
if not node_id or not busi_model_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
if not device_ids:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备不能为空")
db_node = DB_Node.EnterpriseNodeRepository()
node = db_node.get_node_by_id(node_id)
if not node:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "节点不存在")
node_suid = node["suid"]
db_busi_model = DB_BusiModel.EnterpriseBusiModelRepository()
busi_model = db_busi_model.get_busi_model_by_id(busi_model_id)
busi_model_suid = busi_model.suid
entity_suid_row = db_node.get_entity_suid_by_node_id(node_id)
if not entity_suid_row:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "企业节点不存在")
db_device = DB_Device.EnterpriseDeviceRepository()
records = []
for device_id in device_ids.split(","):
device = db_device.get_device(int(device_id))
if not device:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备不存在")
records.append(
{
"suid": shortuuid.ShortUUID().random(length=10),
"entity_suid": entity_suid_row["entity_suid"],
"node_id": node_id,
"node_suid": node_suid,
"busi_model_id": busi_model_id,
"busi_model_suid": busi_model_suid,
"device_id": int(device_id),
"device_suid": device["suid"],
}
)
db_busi_model_node_device = (
DB_BusiModelNodeDevice.EnterpriseBusiModelNodeDeviceRepository()
)
db_busi_model_node_device.delete_by_busi_model_and_node(node_id, busi_model_id)
db_busi_model_node_device.batch_insert_record(records)
self.finish()
class AlertHandler(APIHandler):
"""
- 描述:企业节点,节点信息 -> 设备列表 -> 告警设置信息
- 请求方式post
- 请求参数:
> - node_id, int, 节点id
- 返回值:
```
{
"is_sms": 1, # 是否sms
"sms_to": "xxxx", # 短信联系人
"is_email": 1, # 是否email
"email_to": "xxx", # 邮件联系人
"freq": "T", # T/每次D/每天W/每周M/每月
}
```
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_list_str, "节点信息 -> 设备列表 -> 告警设置信息")
def post(self):
node_id = self.get_int_argument("node_id")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
node = db_node.get_node_by_id(node_id)
if not node:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "节点不存在")
node_suid = node["suid"]
row = db_node.get_entity_suid_by_node_id(node_id)
entity_suid = row["entity_suid"]
db_alert = DB_NodeAlert.EnterpriseNodeAlertRepository()
data = db_alert.get_one(entity_suid, node_suid)
res = {
"is_sms": 0,
"sms_to": "",
"is_email": 0,
"email_to": "",
"freq": "T",
}
if data:
res.update({
"is_sms": data["is_sms"],
"sms_to": data["sms_to"],
"is_email": data["is_email"],
"email_to": data["email_to"],
"freq": data["freq"],
})
self.finish(res)
class AlertConfigHandler(APIHandler):
"""
- 描述:企业节点,节点信息 -> 设备列表 -> 告警设置,更新配置
- 请求方式post
- 请求参数:
> - node_id, int, 节点id
> - is_sms, int, 是否sms, 1/是0/否
> - sms_to, string, 短信联系人
> - is_email, int, 是否email1/是0/否
> - email_to, string, 邮件联系人
> - freq, string, 频次, T/每次D/每天W/每周M/每月
- 返回值:无
"""
@authenticated
@operation_log("企业管理", "节点设置", consts.op_type_edit_str, "节点信息 -> 设备列表 -> 告警设置,更新配置")
def post(self):
node_id = self.get_int_argument("node_id")
is_sms = self.get_int_argument("is_sms", 0)
sms_to = self.get_escaped_argument("sms_to", "")
is_email = self.get_int_argument("is_email", 0)
email_to = self.get_escaped_argument("email_to", "")
freq = self.get_escaped_argument("freq", "T")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
if freq not in ("T", "D", "W", "M"):
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
node = db_node.get_node_by_id(node_id)
if not node:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "节点不存在")
node_suid = node["suid"]
row = db_node.get_entity_suid_by_node_id(node_id)
entity_suid = row["entity_suid"]
insert_data = {
"entity_suid": entity_suid,
"node_id": node_id,
"node_suid": node_suid,
"is_sms": is_sms,
"sms_to": sms_to,
"is_email": is_email,
"email_to": email_to,
"freq": freq,
}
db_alert = DB_NodeAlert.EnterpriseNodeAlertRepository()
db_alert.update_or_inert(insert_data)
self.finish()