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

223 lines
5.4 KiB
Go
Raw Permalink Normal View History

2020-07-22 22:17:53 +08:00
package models
2020-09-13 20:37:28 +08:00
import (
"encoding/json"
2024-07-27 14:15:25 +08:00
"sort"
"time"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
2020-10-25 18:26:46 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2022-05-18 21:02:53 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
2023-05-17 18:42:21 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
2020-09-13 20:37:28 +08:00
)
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
}
2023-05-17 18:42:21 +08:00
var status = &NodeInstallStatus{}
2022-03-22 19:30:30 +08:00
err := json.Unmarshal(this.InstallStatus, status)
2020-09-13 20:37:28 +08:00
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
}
2023-05-17 18:42:21 +08:00
var status = &nodeconfigs.NodeStatus{}
2022-03-22 19:30:30 +08:00
err := json.Unmarshal(this.Status, status)
2020-10-25 18:26:46 +08:00
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 {
2023-05-17 18:42:21 +08:00
var 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
}
2022-03-22 19:30:30 +08:00
err := json.Unmarshal(this.DnsRoutes, &routes)
2020-11-14 09:41:58 +08:00
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) {
2023-05-17 18:42:21 +08:00
var routes = map[int64][]string{} // domainId => routes
if len(this.DnsRoutes) == 0 {
2020-11-16 13:03:20 +08:00
return nil, nil
}
2022-03-22 19:30:30 +08:00
err := json.Unmarshal(this.DnsRoutes, &routes)
if err != nil {
2020-11-16 13:03:20 +08:00
return nil, err
}
2023-08-08 16:46:17 +08:00
var 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) {
2023-05-17 18:42:21 +08:00
var apiNodeIds = []int64{}
if IsNotNull(this.ConnectedAPINodes) {
2022-03-22 19:30:30 +08:00
err := json.Unmarshal(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{}
// 不需要处理错误
2022-03-22 19:30:30 +08:00
_ = json.Unmarshal(this.SecondaryClusterIds, &result)
2021-07-31 22:23:11 +08:00
return result
}
2022-04-04 12:08:08 +08:00
2022-05-18 21:02:53 +08:00
// AllClusterIds 获取所属集群IDs
2022-04-04 12:08:08 +08:00
func (this *Node) AllClusterIds() []int64 {
var result = []int64{}
if this.ClusterId > 0 {
result = append(result, int64(this.ClusterId))
}
result = append(result, this.DecodeSecondaryClusterIds()...)
return result
}
2022-05-18 21:02:53 +08:00
// DecodeDDoSProtection 解析DDoS Protection设置
func (this *Node) DecodeDDoSProtection() *ddosconfigs.ProtectionConfig {
if IsNull(this.DdosProtection) {
return nil
}
var result = &ddosconfigs.ProtectionConfig{}
err := json.Unmarshal(this.DdosProtection, &result)
if err != nil {
// ignore err
}
return result
}
// HasDDoSProtection 检查是否有DDOS设置
func (this *Node) HasDDoSProtection() bool {
var config = this.DecodeDDoSProtection()
if config != nil {
return !config.IsPriorEmpty()
}
return false
}
// DecodeMaxCacheDiskCapacity 解析硬盘容量
2022-05-18 21:02:53 +08:00
func (this *Node) DecodeMaxCacheDiskCapacity() *shared.SizeCapacity {
if this.MaxCacheDiskCapacity.IsNull() {
return nil
}
// ignore error
capacity, _ := shared.DecodeSizeCapacityJSON(this.MaxCacheDiskCapacity)
return capacity
}
// DecodeMaxCacheMemoryCapacity 解析内存容量
2022-05-18 21:02:53 +08:00
func (this *Node) DecodeMaxCacheMemoryCapacity() *shared.SizeCapacity {
if this.MaxCacheMemoryCapacity.IsNull() {
return nil
}
// ignore error
capacity, _ := shared.DecodeSizeCapacityJSON(this.MaxCacheMemoryCapacity)
return capacity
}
// DecodeDNSResolver 解析DNS解析主机配置
func (this *Node) DecodeDNSResolver() *nodeconfigs.DNSResolverConfig {
if this.DnsResolver.IsNull() {
return nil
}
var resolverConfig = nodeconfigs.DefaultDNSResolverConfig()
err := json.Unmarshal(this.DnsResolver, resolverConfig)
if err != nil {
// ignore error
}
return resolverConfig
}
2022-08-25 20:37:10 +08:00
// DecodeLnAddrs 解析Ln地址
2022-08-25 20:37:10 +08:00
func (this *Node) DecodeLnAddrs() []string {
if IsNull(this.LnAddrs) {
return nil
}
var result = []string{}
err := json.Unmarshal(this.LnAddrs, &result)
if err != nil {
// ignore error
}
return result
}
// DecodeCacheDiskSubDirs 解析缓存目录
func (this *Node) DecodeCacheDiskSubDirs() []*serverconfigs.CacheDir {
if IsNull(this.CacheDiskSubDirs) {
return nil
}
var result = []*serverconfigs.CacheDir{}
err := json.Unmarshal(this.CacheDiskSubDirs, &result)
if err != nil {
remotelogs.Error("Node.DecodeCacheDiskSubDirs", err.Error())
}
return result
}
// DecodeAPINodeAddrs 解析API节点地址
func (this *Node) DecodeAPINodeAddrs() []*serverconfigs.NetworkAddressConfig {
var result = []*serverconfigs.NetworkAddressConfig{}
if IsNull(this.ApiNodeAddrs) {
return result
}
err := json.Unmarshal(this.ApiNodeAddrs, &result)
if err != nil {
remotelogs.Error("Node.DecodeAPINodeAddrs", err.Error())
}
return result
}
2023-05-17 18:42:21 +08:00
// CheckIsOffline 检查是否已经离线
func (this *Node) CheckIsOffline() bool {
return len(this.OfflineDay) > 0 && this.OfflineDay < timeutil.Format("Ymd")
}