This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
/
eureka_page_service.go
133 lines (119 loc) · 3.83 KB
/
eureka_page_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package service
import (
"github.com/choerodon/go-register-server/pkg/api/entity"
"github.com/choerodon/go-register-server/pkg/api/metrics"
"github.com/choerodon/go-register-server/pkg/api/repository"
"github.com/choerodon/go-register-server/pkg/embed"
"github.com/emicklei/go-restful"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"html/template"
"net"
"runtime"
"strconv"
"time"
)
type EurekaPageService interface {
HomePage(req *restful.Request, resp *restful.Response)
}
type EurekaPageServiceImpl struct {
appRepo *repository.ApplicationRepository
}
func NewEurekaPageServiceImpl(appRepo *repository.ApplicationRepository) *EurekaPageServiceImpl {
return &EurekaPageServiceImpl{appRepo: appRepo}
}
func (es *EurekaPageServiceImpl) HomePage(req *restful.Request, resp *restful.Response) {
metrics.RequestCount.With(prometheus.Labels{"path": req.Request.RequestURI}).Inc()
t := template.Must(template.ParseFiles("templates/eureka.html"))
register, eurekaInstances := getEurekaApplicationInfos(es.appRepo.GetApplicationResources().Applications.ApplicationList)
err := t.Execute(resp.ResponseWriter, &entity.EurekaPage{
GeneralInfo: getGeneralInfo(),
InstanceInfo: getInstanceInfo(),
CurrentTime: time.Now(),
AvailableRegisters: register,
EurekaInstances: eurekaInstances,
})
if err != nil {
glog.Fatalf("Error Get Home Page: %s", err.Error())
}
}
func instanceHtml(instances []*entity.Instance) template.HTML {
html := ""
for i := 0; i < len(instances); i++ {
html = html + "<a href=\"" + instances[i].StatusPageUrl +
"\" target=\"_blank\">" + instances[i].InstanceId + "</a> "
}
return template.HTML(html)
}
func getCpuNum() int {
return runtime.NumCPU()
}
func getMemUsage() string {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return strconv.Itoa(bToMb(m.Sys)) + "MiB"
}
func bToMb(b uint64) int {
return int(b) / 1024 / 1024
}
func getIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func getGeneralInfo() map[string]interface{} {
info := make(map[string]interface{})
info["NumOfCpu"] = getCpuNum()
info["UsedMemory"] = getMemUsage()
info["NamespaceOfRegisterServer"] = embed.Env.RegisterServerNamespace
info["NamespacesOfListeningOn"] = embed.Env.RegisterServiceNamespace
return info
}
func getInstanceInfo() map[string]interface{} {
info := make(map[string]interface{})
info["IpAddr"] = getIP()
return info
}
func getEurekaApplicationInfos(list []*entity.Application) ([]*entity.Instance, []*entity.EurekaInstance) {
infos := make([]*entity.EurekaInstance, 0)
var register []*entity.Instance
for _, value := range list {
available, inAvailable := covertApplicationToEurekaInfo(value)
availableSize := len(available)
inAvailableSize := len(inAvailable)
infos = append(infos, &entity.EurekaInstance{
Name: value.Name,
AMIs: strconv.Itoa(availableSize) + "/" + strconv.Itoa(availableSize+inAvailableSize),
AvailabilityZones: availableSize,
Available: available,
InAvailable: inAvailable,
AvailableHtml: instanceHtml(available),
InAvailableHtml: instanceHtml(inAvailable),
})
if value.Name == entity.RegisterServerName {
register = available
}
}
return register, infos
}
func covertApplicationToEurekaInfo(application *entity.Application) ([]*entity.Instance, []*entity.Instance) {
available := make([]*entity.Instance, 0)
inAvailable := make([]*entity.Instance, 0)
for _, value := range application.Instances {
if value.Status == "UP" {
available = append(available, value)
} else {
inAvailable = append(inAvailable, value)
}
}
return available, inAvailable
}