github.com/polarismesh/polaris@v1.17.8/common/model/client.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package model 19 20 import ( 21 "time" 22 23 "github.com/golang/protobuf/ptypes/wrappers" 24 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 25 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 26 27 commontime "github.com/polarismesh/polaris/common/time" 28 ) 29 30 // Client 客户端上报信息表 31 type Client struct { 32 proto *apiservice.Client 33 valid bool 34 modifyTime time.Time 35 } 36 37 func NewClient(req *apiservice.Client) *Client { 38 return &Client{ 39 proto: req, 40 } 41 } 42 43 func (c *Client) Proto() *apiservice.Client { 44 return c.proto 45 } 46 47 func (c *Client) SetValid(v bool) { 48 c.valid = v 49 } 50 51 func (c *Client) Valid() bool { 52 return c.valid 53 } 54 55 func (c *Client) ModifyTime() time.Time { 56 return c.modifyTime 57 } 58 59 // ClientStore 对应store层(database)的对象 60 type ClientStore struct { 61 ID string 62 Host string 63 Type string 64 Version string 65 Region string 66 Zone string 67 Campus string 68 Stat ClientStatStore 69 Flag int 70 CreateTime int64 71 ModifyTime int64 72 } 73 74 type ClientStatStore struct { 75 Target string 76 Port uint32 77 Protocol string 78 Path string 79 } 80 81 // Store2Instance store的数据转换为组合了api的数据结构 82 func Store2Client(is *ClientStore) *Client { 83 ins := &Client{ 84 proto: &apiservice.Client{ 85 Id: &wrappers.StringValue{Value: is.ID}, 86 Host: &wrappers.StringValue{Value: is.Host}, 87 Version: &wrappers.StringValue{Value: is.Version}, 88 Type: apiservice.Client_ClientType(apiservice.Client_ClientType_value[is.Type]), 89 Location: &apimodel.Location{ 90 Campus: &wrappers.StringValue{Value: is.Campus}, 91 Zone: &wrappers.StringValue{Value: is.Zone}, 92 Region: &wrappers.StringValue{Value: is.Region}, 93 }, 94 Ctime: &wrappers.StringValue{Value: commontime.Int64Time2String(is.CreateTime)}, 95 Mtime: &wrappers.StringValue{Value: commontime.Int64Time2String(is.ModifyTime)}, 96 }, 97 valid: flag2valid(is.Flag), 98 modifyTime: time.Unix(is.ModifyTime, 0), 99 } 100 statInfo := Store2ClientStat(&is.Stat) 101 if nil != statInfo { 102 ins.proto.Stat = append(ins.proto.Stat, statInfo) 103 } 104 return ins 105 } 106 107 func Store2ClientStat(clientStatStore *ClientStatStore) *apiservice.StatInfo { 108 if len(clientStatStore.Target) == 0 && clientStatStore.Port == 0 && len(clientStatStore.Path) == 0 && 109 len(clientStatStore.Protocol) == 0 { 110 return nil 111 } 112 statInfo := &apiservice.StatInfo{} 113 statInfo.Path = &wrappers.StringValue{Value: clientStatStore.Path} 114 statInfo.Protocol = &wrappers.StringValue{Value: clientStatStore.Protocol} 115 statInfo.Port = &wrappers.UInt32Value{Value: clientStatStore.Port} 116 statInfo.Target = &wrappers.StringValue{Value: clientStatStore.Target} 117 return statInfo 118 }