Files
EdgeAPI/internal/db/models/node_model_ext.go

99 lines
2.3 KiB
Go
Raw Normal View History

2020-07-22 22:17:53 +08:00
package models
2020-09-13 20:37:28 +08:00
import (
"encoding/json"
2020-10-25 18:26:46 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2021-09-13 16:47:40 +08:00
"sort"
2020-09-13 20:37:28 +08:00
"time"
)
2021-07-31 22:23:11 +08:00
// DecodeInstallStatus 安装状态
2020-09-13 20:37:28 +08:00
func (this *Node) DecodeInstallStatus() (*NodeInstallStatus, error) {
if len(this.InstallStatus) == 0 {
2020-09-13 20:37:28 +08:00
return NewNodeInstallStatus(), nil
}
status := &NodeInstallStatus{}
err := json.Unmarshal([]byte(this.InstallStatus), status)
if err != nil {
return NewNodeInstallStatus(), err
}
// 如果N秒钟没有更新状态则认为不在运行
if status.IsRunning && status.UpdatedAt < time.Now().Unix()-10 {
status.IsRunning = false
status.IsFinished = true
status.Error = "timeout"
}
return status, nil
}
2021-07-31 22:23:11 +08:00
// DecodeStatus 节点状态
2020-10-25 18:26:46 +08:00
func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
if len(this.Status) == 0 {
2020-10-25 18:26:46 +08:00
return nil, nil
}
status := &nodeconfigs.NodeStatus{}
err := json.Unmarshal([]byte(this.Status), status)
if err != nil {
return nil, err
}
return status, nil
}
2021-07-31 22:23:11 +08:00
// DNSRouteCodes 所有的DNS线路
func (this *Node) DNSRouteCodes() map[int64][]string {
2020-11-16 13:03:20 +08:00
routes := map[int64][]string{} // domainId => routes
if len(this.DnsRoutes) == 0 {
2021-07-31 22:23:11 +08:00
return routes
2020-11-14 09:41:58 +08:00
}
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
if err != nil {
2021-07-31 22:23:11 +08:00
// 忽略错误
return routes
2020-11-14 09:41:58 +08:00
}
2021-07-31 22:23:11 +08:00
return routes
2020-11-14 09:41:58 +08:00
}
2021-07-31 22:23:11 +08:00
// DNSRouteCodesForDomainId DNS线路
2020-11-16 13:03:20 +08:00
func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error) {
routes := map[int64][]string{} // domainId => routes
if len(this.DnsRoutes) == 0 {
2020-11-16 13:03:20 +08:00
return nil, nil
}
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
if err != nil {
2020-11-16 13:03:20 +08:00
return nil, err
}
2020-11-16 13:03:20 +08:00
domainRoutes, _ := routes[dnsDomainId]
2021-09-13 16:47:40 +08:00
if len(domainRoutes) > 0 {
sort.Strings(domainRoutes)
}
2020-11-16 13:03:20 +08:00
return domainRoutes, nil
}
2021-07-31 22:23:11 +08:00
// DecodeConnectedAPINodeIds 连接的API
func (this *Node) DecodeConnectedAPINodeIds() ([]int64, error) {
apiNodeIds := []int64{}
if IsNotNull(this.ConnectedAPINodes) {
err := json.Unmarshal([]byte(this.ConnectedAPINodes), &apiNodeIds)
if err != nil {
return nil, err
}
}
return apiNodeIds, nil
}
2021-07-31 22:23:11 +08:00
// DecodeSecondaryClusterIds 从集群IDs
func (this *Node) DecodeSecondaryClusterIds() []int64 {
if len(this.SecondaryClusterIds) == 0 {
return []int64{}
}
var result = []int64{}
// 不需要处理错误
_ = json.Unmarshal([]byte(this.SecondaryClusterIds), &result)
return result
}