mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-02-07 17:05:37 +08:00
优化消息发送相关代码/删除监控相关代码
This commit is contained in:
@@ -34,7 +34,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
// EnableMessageMedia 启用条目
|
||||
func (this *MessageMediaDAO) EnableMessageMedia(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -43,7 +43,7 @@ func (this *MessageMediaDAO) EnableMessageMedia(tx *dbs.Tx, id int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
// DisableMessageMedia 禁用条目
|
||||
func (this *MessageMediaDAO) DisableMessageMedia(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -52,7 +52,7 @@ func (this *MessageMediaDAO) DisableMessageMedia(tx *dbs.Tx, id int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
// FindEnabledMessageMedia 查找启用中的条目
|
||||
func (this *MessageMediaDAO) FindEnabledMessageMedia(tx *dbs.Tx, id int64) (*MessageMedia, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -83,19 +83,19 @@ func (this *MessageMediaDAO) FindAllEnabledMessageMedias(tx *dbs.Tx) (result []*
|
||||
return
|
||||
}
|
||||
|
||||
// 设置当前所有可用的媒介
|
||||
// UpdateMessageMedias 设置当前所有可用的媒介
|
||||
func (this *MessageMediaDAO) UpdateMessageMedias(tx *dbs.Tx, mediaMaps []maps.Map) error {
|
||||
// 新的媒介信息
|
||||
mediaTypes := []string{}
|
||||
var mediaTypes = []string{}
|
||||
for index, m := range mediaMaps {
|
||||
order := len(mediaMaps) - index
|
||||
mediaType := m.GetString("type")
|
||||
var order = len(mediaMaps) - index
|
||||
var mediaType = m.GetString("code")
|
||||
mediaTypes = append(mediaTypes, mediaType)
|
||||
|
||||
name := m.GetString("name")
|
||||
description := m.GetString("description")
|
||||
userDescription := m.GetString("userDescription")
|
||||
isOn := m.GetBool("isOn")
|
||||
var name = m.GetString("name")
|
||||
var description = m.GetString("description")
|
||||
var userDescription = m.GetString("user")
|
||||
var isOn = m.GetBool("isOn")
|
||||
|
||||
mediaId, err := this.Query(tx).
|
||||
ResultPk().
|
||||
@@ -128,7 +128,7 @@ func (this *MessageMediaDAO) UpdateMessageMedias(tx *dbs.Tx, mediaMaps []maps.Ma
|
||||
return err
|
||||
}
|
||||
for _, one := range ones {
|
||||
mediaType := one.(*MessageMedia).Type
|
||||
var mediaType = one.(*MessageMedia).Type
|
||||
if !lists.ContainsString(mediaTypes, mediaType) {
|
||||
err := this.Query(tx).
|
||||
Pk(one.(*MessageMedia).Id).
|
||||
@@ -142,7 +142,7 @@ func (this *MessageMediaDAO) UpdateMessageMedias(tx *dbs.Tx, mediaMaps []maps.Ma
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据类型查找媒介
|
||||
// FindEnabledMediaWithType 根据类型查找媒介
|
||||
func (this *MessageMediaDAO) FindEnabledMediaWithType(tx *dbs.Tx, mediaType string) (*MessageMedia, error) {
|
||||
one, err := this.Query(tx).
|
||||
Attr("type", mediaType).
|
||||
|
||||
@@ -210,7 +210,7 @@ func (this *MessageTaskDAO) CreateMessageTasks(tx *dbs.Tx, role nodeconfigs.Node
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
allRecipientIds := []int64{}
|
||||
var allRecipientIds = []int64{}
|
||||
for _, receiver := range receivers {
|
||||
if receiver.RecipientId > 0 {
|
||||
allRecipientIds = append(allRecipientIds, int64(receiver.RecipientId))
|
||||
@@ -223,7 +223,7 @@ func (this *MessageTaskDAO) CreateMessageTasks(tx *dbs.Tx, role nodeconfigs.Node
|
||||
}
|
||||
}
|
||||
|
||||
sentMap := map[int64]bool{} // recipientId => bool 用来检查是否已经发送,防止重复发送给某个接收人
|
||||
var sentMap = map[int64]bool{} // recipientId => bool 用来检查是否已经发送,防止重复发送给某个接收人
|
||||
for _, recipientId := range allRecipientIds {
|
||||
_, ok := sentMap[recipientId]
|
||||
if ok {
|
||||
|
||||
@@ -1,215 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const (
|
||||
MonitorNodeStateEnabled = 1 // 已启用
|
||||
MonitorNodeStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type MonitorNodeDAO dbs.DAO
|
||||
|
||||
func NewMonitorNodeDAO() *MonitorNodeDAO {
|
||||
return dbs.NewDAO(&MonitorNodeDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeMonitorNodes",
|
||||
Model: new(MonitorNode),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*MonitorNodeDAO)
|
||||
}
|
||||
|
||||
var SharedMonitorNodeDAO *MonitorNodeDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedMonitorNodeDAO = NewMonitorNodeDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableMonitorNode 启用条目
|
||||
func (this *MonitorNodeDAO) EnableMonitorNode(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", MonitorNodeStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableMonitorNode 禁用条目
|
||||
func (this *MonitorNodeDAO) DisableMonitorNode(tx *dbs.Tx, nodeId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("state", MonitorNodeStateDisabled).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除运行日志
|
||||
return SharedNodeLogDAO.DeleteNodeLogs(tx, nodeconfigs.NodeRoleMonitor, nodeId)
|
||||
}
|
||||
|
||||
// FindEnabledMonitorNode 查找启用中的条目
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNode(tx *dbs.Tx, id int64) (*MonitorNode, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", MonitorNodeStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*MonitorNode), err
|
||||
}
|
||||
|
||||
// FindMonitorNodeName 根据主键查找名称
|
||||
func (this *MonitorNodeDAO) FindMonitorNodeName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindAllEnabledMonitorNodes 列出所有可用监控节点
|
||||
func (this *MonitorNodeDAO) FindAllEnabledMonitorNodes(tx *dbs.Tx) (result []*MonitorNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CountAllEnabledMonitorNodes 计算监控节点数量
|
||||
func (this *MonitorNodeDAO) CountAllEnabledMonitorNodes(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// ListEnabledMonitorNodes 列出单页的监控节点
|
||||
func (this *MonitorNodeDAO) ListEnabledMonitorNodes(tx *dbs.Tx, offset int64, size int64) (result []*MonitorNode, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Desc("order").
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// CreateMonitorNode 创建监控节点
|
||||
func (this *MonitorNodeDAO) CreateMonitorNode(tx *dbs.Tx, name string, description string, isOn bool) (nodeId int64, err error) {
|
||||
uniqueId, err := this.GenUniqueId(tx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
secret := rands.String(32)
|
||||
err = NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleMonitor)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var op = NewMonitorNodeOperator()
|
||||
op.IsOn = isOn
|
||||
op.UniqueId = uniqueId
|
||||
op.Secret = secret
|
||||
op.Name = name
|
||||
op.Description = description
|
||||
op.State = NodeStateEnabled
|
||||
err = this.Save(tx, op)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// UpdateMonitorNode 修改监控节点
|
||||
func (this *MonitorNodeDAO) UpdateMonitorNode(tx *dbs.Tx, nodeId int64, name string, description string, isOn bool) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
|
||||
var op = NewMonitorNodeOperator()
|
||||
op.Id = nodeId
|
||||
op.Name = name
|
||||
op.Description = description
|
||||
op.IsOn = isOn
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledMonitorNodeWithUniqueId 根据唯一ID获取节点信息
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNodeWithUniqueId(tx *dbs.Tx, uniqueId string) (*MonitorNode, error) {
|
||||
result, err := this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
Attr("state", MonitorNodeStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*MonitorNode), err
|
||||
}
|
||||
|
||||
// FindEnabledMonitorNodeIdWithUniqueId 根据唯一ID获取节点ID
|
||||
func (this *MonitorNodeDAO) FindEnabledMonitorNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
Attr("state", MonitorNodeStateEnabled).
|
||||
ResultPk().
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// GenUniqueId 生成唯一ID
|
||||
func (this *MonitorNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
|
||||
for {
|
||||
uniqueId := rands.HexString(32)
|
||||
ok, err := this.Query(tx).
|
||||
Attr("uniqueId", uniqueId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
return uniqueId, nil
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateNodeStatus 更改节点状态
|
||||
func (this *MonitorNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
|
||||
if statusJSON == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("status", string(statusJSON)).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// CountAllLowerVersionNodes 计算所有节点中低于某个版本的节点数量
|
||||
func (this *MonitorNodeDAO) CountAllLowerVersionNodes(tx *dbs.Tx, version string) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(MonitorNodeStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Where("status IS NOT NULL").
|
||||
Where("(JSON_EXTRACT(status, '$.buildVersionCode') IS NULL OR JSON_EXTRACT(status, '$.buildVersionCode')<:version)").
|
||||
Param("version", utils.VersionToLong(version)).
|
||||
Count()
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
@@ -1,38 +0,0 @@
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// MonitorNode 监控节点
|
||||
type MonitorNode struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn bool `field:"isOn"` // 是否启用
|
||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||
Secret string `field:"secret"` // 密钥
|
||||
Name string `field:"name"` // 名称
|
||||
Description string `field:"description"` // 描述
|
||||
Order uint32 `field:"order"` // 排序
|
||||
State uint8 `field:"state"` // 状态
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
Weight uint32 `field:"weight"` // 权重
|
||||
Status dbs.JSON `field:"status"` // 运行状态
|
||||
}
|
||||
|
||||
type MonitorNodeOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
UniqueId interface{} // 唯一ID
|
||||
Secret interface{} // 密钥
|
||||
Name interface{} // 名称
|
||||
Description interface{} // 描述
|
||||
Order interface{} // 排序
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
AdminId interface{} // 管理员ID
|
||||
Weight interface{} // 权重
|
||||
Status interface{} // 运行状态
|
||||
}
|
||||
|
||||
func NewMonitorNodeOperator() *MonitorNodeOperator {
|
||||
return &MonitorNodeOperator{}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package models
|
||||
Reference in New Issue
Block a user