481 lines
14 KiB
Go
481 lines
14 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.hpds.cc/Component/logging"
|
|
"hpds-iot-web/internal/proto"
|
|
"hpds-iot-web/model"
|
|
"net/http"
|
|
"time"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
type SystemService interface {
|
|
BrandList(ctx context.Context, req proto.BrandRequest) (rsp *proto.BaseResponse, err error)
|
|
BrandInfo(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error)
|
|
AddBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error)
|
|
EditBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error)
|
|
DeleteBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error)
|
|
|
|
NodeList(ctx context.Context, req proto.NodeRequest) (rsp *proto.BaseResponse, err error)
|
|
NodeInfo(ctx context.Context, req proto.NodeItemRequest) (rsp *proto.BaseResponse, err error)
|
|
EditNode(ctx context.Context, req proto.NodeItemRequest) (rsp *proto.BaseResponse, err error)
|
|
NodeState(ctx context.Context, req proto.NodeInfoRequest) (rsp *proto.BaseResponse, err error)
|
|
NodeLastState(ctx context.Context, req proto.NodeInfoRequest) (rsp *proto.BaseResponse, err error)
|
|
}
|
|
|
|
func NewSystemService(engine *xorm.Engine, logger *logging.Logger) SystemService {
|
|
return &repo{
|
|
engine: engine,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (rp *repo) BrandList(ctx context.Context, req proto.BrandRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
data := make([]model.Brand, 0)
|
|
count, err := rp.engine.Where("(? = '' or brand_name like ?)", req.BrandName, "%"+req.BrandName+"%").
|
|
And("status = 1").Limit(int(req.Size), int(((req.Page)-1)*req.Size)).
|
|
FindAndCount(&data)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "成功"
|
|
rsp = FillPaging(count, req.Page, req.Size, data, rsp)
|
|
rsp.Err = err
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
|
|
func (rp *repo) BrandInfo(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
var h bool
|
|
item := new(model.Brand)
|
|
h, err = rp.engine.ID(req.BrandId).Get(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
if !h {
|
|
err = fmt.Errorf("未能找到对应的品牌")
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "获取品牌成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = item
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
func (rp *repo) AddBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
item := &model.Brand{
|
|
BrandName: req.BrandName,
|
|
BrandLogo: req.BrandLogo,
|
|
BrandWeb: req.BrandWeb,
|
|
Status: 1,
|
|
CreateAt: time.Now().Unix(),
|
|
UpdateAt: time.Now().Unix(),
|
|
}
|
|
_, err = rp.engine.Insert(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "新增品牌成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = item
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
func (rp *repo) EditBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
var h bool
|
|
item := new(model.Brand)
|
|
h, err = rp.engine.ID(req.BrandId).Get(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
if !h {
|
|
err = fmt.Errorf("未能找到对应的品牌")
|
|
goto ReturnPoint
|
|
}
|
|
if len(req.BrandName) > 0 {
|
|
item.BrandName = req.BrandName
|
|
}
|
|
if len(req.BrandLogo) > 0 {
|
|
item.BrandLogo = req.BrandLogo
|
|
}
|
|
if len(req.BrandWeb) > 0 {
|
|
item.BrandWeb = req.BrandWeb
|
|
}
|
|
item.UpdateAt = time.Now().Unix()
|
|
_, err = rp.engine.ID(req.BrandId).AllCols().Update(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "修改品牌成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = item
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
func (rp *repo) DeleteBrand(ctx context.Context, req proto.BrandItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
var h bool
|
|
item := new(model.Brand)
|
|
h, err = rp.engine.ID(req.BrandId).Get(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
if !h {
|
|
err = fmt.Errorf("未能找到对应的品牌")
|
|
goto ReturnPoint
|
|
}
|
|
item.Status = 0
|
|
item.UpdateAt = time.Now().Unix()
|
|
_, err = rp.engine.ID(req.BrandId).AllCols().Update(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "删除品牌成功"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
|
|
func (rp *repo) NodeList(ctx context.Context, req proto.NodeRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
data := make([]model.Node, 0)
|
|
var count int64
|
|
count, err = rp.engine.Where("(? = '' or node_name like ?)", req.NodeName, "%"+req.NodeName+"%").
|
|
And("(? = '' or node_guid like ?)", req.NodeGuid, "%"+req.NodeGuid+"%").
|
|
And("(? = 0 or node_type = ?)", req.NodeType, req.NodeType).
|
|
And("node_status = 1").Limit(int(req.Size), int(((req.Page)-1)*req.Size)).
|
|
FindAndCount(&data)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "成功"
|
|
rsp = FillPaging(count, req.Page, req.Size, data, rsp)
|
|
rsp.Err = err
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
|
|
func (rp *repo) NodeInfo(ctx context.Context, req proto.NodeItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
var h bool
|
|
item := new(model.Node)
|
|
h, err = rp.engine.ID(req.NodeId).Get(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
if !h {
|
|
err = fmt.Errorf("未能找到对应的节点")
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "获取节点成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = item
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
|
|
func (rp *repo) EditNode(ctx context.Context, req proto.NodeItemRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
var h bool
|
|
item := new(model.Node)
|
|
h, err = rp.engine.ID(req.NodeId).Get(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
if !h {
|
|
err = fmt.Errorf("未能找到对应的节点")
|
|
goto ReturnPoint
|
|
}
|
|
if len(req.NodeName) > 0 {
|
|
item.NodeName = req.NodeName
|
|
}
|
|
if req.NodeType > 0 {
|
|
item.NodeType = req.NodeType
|
|
}
|
|
if len(req.Platform) > 0 {
|
|
item.Platform = req.Platform
|
|
}
|
|
if len(req.PlatformVersion) > 0 {
|
|
item.PlatformVersion = req.PlatformVersion
|
|
}
|
|
if len(req.CPU) > 0 {
|
|
item.CPU = req.CPU
|
|
}
|
|
if req.MemTotal > 0 {
|
|
item.MemTotal = req.MemTotal
|
|
}
|
|
if req.DiskTotal > 0 {
|
|
item.DiskTotal = req.DiskTotal
|
|
}
|
|
if req.SwapTotal > 0 {
|
|
item.SwapTotal = req.SwapTotal
|
|
}
|
|
if len(req.Arch) > 0 {
|
|
item.Arch = req.Arch
|
|
}
|
|
if len(req.Virtualization) > 0 {
|
|
item.Virtualization = req.Virtualization
|
|
}
|
|
if req.BootTime > 0 {
|
|
item.BootTime = req.BootTime
|
|
}
|
|
if len(req.IP) > 0 {
|
|
item.IP = req.IP
|
|
}
|
|
if len(req.CountryCode) > 0 {
|
|
item.CountryCode = req.CountryCode
|
|
}
|
|
if len(req.Version) > 0 {
|
|
item.Version = req.Version
|
|
}
|
|
item.UpdateAt = time.Now().Unix()
|
|
_, err = rp.engine.ID(req.NodeId).AllCols().Update(item)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "修改节点成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = item
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
|
|
func (rp *repo) NodeState(ctx context.Context, req proto.NodeInfoRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
list := make([]model.NodeState, 0)
|
|
err = rp.engine.Where("node_name = ?", req.NodeGuid).
|
|
And(" uptime > UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -24 HOUR))").
|
|
Find(&list)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
state := new(proto.NodeState)
|
|
state.List = list
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "获取节点状态信息成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = state
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
|
|
func (rp *repo) NodeLastState(ctx context.Context, req proto.NodeInfoRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
list := make([]proto.NodeLastStateItem, 0)
|
|
//err = rp.engine.Where("node_name = ?", req.NodeGuid).
|
|
// And("? + uptime > UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -24 HOUR))", req.Uptime).Desc("uptime").
|
|
// Find(&list)
|
|
err = rp.engine.SQL(`select c.node_id,c.node_name,c.node_guid,c.node_type,c.node_type,c.platform,c.platform_version,c.c_p_u,c.mem_total,
|
|
c.disk_total,c.swap_total,a.c_p_u cpu_used,c.node_status, a.mem_used,a.swap_used, a.disk_used, a.net_in_transfer, a.net_in_speed,
|
|
a.net_out_speed, a.net_out_transfer, a.load1, a.load5, a.load15, a.tcp_conn_count, a.udp_conn_count, a.process_count,
|
|
d.task_name exec_task from node_state a , (select node_name, max(uptime) uptime from node_state group by node_name) b, node c
|
|
left join (select t2.node_id, t2.task_name from task t2, (select node_id, max(start_time) start from task group by node_id) t1 where t2.node_id = t1.node_id and t2.start_time = t1.start and t2.status = 1) d on c.node_id = d.node_id
|
|
where a.node_name = b.node_name and a.uptime = b.uptime and a.node_name = c.node_guid and c.node_status > 0 and (? = '' or a.node_name = ?) `, req.NodeGuid, req.NodeGuid).Find(&list)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
state := new(proto.NodeLastState)
|
|
state.List = list
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "获取节点状态信息成功"
|
|
rsp.Err = ctx.Err()
|
|
rsp.Data = state
|
|
return rsp, err
|
|
}
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|