Files
EdgeAPI/internal/db/models/node_model_ext.go
2021-01-17 16:48:00 +08:00

81 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"time"
)
// 安装状态
func (this *Node) DecodeInstallStatus() (*NodeInstallStatus, error) {
if len(this.InstallStatus) == 0 || this.InstallStatus == "null" {
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
}
// 节点状态
func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
if len(this.Status) == 0 || this.Status == "null" {
return nil, nil
}
status := &nodeconfigs.NodeStatus{}
err := json.Unmarshal([]byte(this.Status), status)
if err != nil {
return nil, err
}
return status, nil
}
// 所有的DNS线路
func (this *Node) DNSRouteCodes() (map[int64][]string, error) {
routes := map[int64][]string{} // domainId => routes
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
return routes, nil
}
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
if err != nil {
return map[int64][]string{}, err
}
return routes, nil
}
// DNS线路
func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error) {
routes := map[int64][]string{} // domainId => routes
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
return nil, nil
}
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
if err != nil {
return nil, err
}
domainRoutes, _ := routes[dnsDomainId]
return domainRoutes, nil
}
// 连接的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
}