-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.go
More file actions
184 lines (147 loc) · 3.23 KB
/
Copy pathinfo.go
File metadata and controls
184 lines (147 loc) · 3.23 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/thekhanj/avail/common"
"golang.org/x/term"
)
type SiteStatusList []*SiteStatus
func (this *SiteStatusList) Apply(opts ...SiteStatusOption) {
for _, s := range *this {
s.Apply(opts...)
}
}
func (this SiteStatusList) String() string {
lines := make([]string, len(this))
for i, s := range this {
lines[i] = s.String()
}
return strings.Join(lines, "\n")
}
func (this *SiteStatusList) MaxTitleLength(opts ...SiteStatusOption) int {
max := 0
for _, s := range *this {
if len(s.title) > max {
max = len(s.title)
}
}
return max
}
var _ fmt.Stringer = (*SiteStatusList)(nil)
type SiteStatusOption = func(*SiteStatus)
func SiteStatusWithTitleLength(titleLength int) SiteStatusOption {
return func(s *SiteStatus) {
s.titleLength = titleLength
}
}
type SiteStatus struct {
title string
titleLength int
Latency int64
Health bool
}
func (this *SiteStatus) Apply(opts ...SiteStatusOption) {
for _, o := range opts {
o(this)
}
}
func (this SiteStatus) String() string {
isTTY := term.IsTerminal(int(os.Stdout.Fd()))
healthColor := ""
latencyColor := ""
titleColor := ""
colorReset := ""
if isTTY {
titleColor = ""
latencyColor = "\x1b[36m"
if this.Health {
healthColor = "\x1b[1m\x1b[32m"
} else {
healthColor = "\x1b[1m\x1b[31m"
}
colorReset = "\x1b[0m"
}
health := "OK"
if !this.Health {
health = "FAILED"
}
titleLength := this.titleLength
if titleLength == 0 {
titleLength = 20
}
return fmt.Sprintf(
"%s%-"+strconv.Itoa(titleLength+1)+"s%s %s%s%s (latency: %s%d ms%s)",
titleColor, this.title+":", colorReset,
healthColor, health, colorReset,
latencyColor, this.Latency, colorReset,
)
}
var _ fmt.Stringer = (*SiteStatus)(nil)
func NewInfo(pid int) *Info {
return &Info{pid}
}
type Info struct {
pid int
}
func (this *Info) Titles() ([]string, error) {
pings := filepath.Join(common.GetPidVarDir(this.pid))
titles := make([]string, 0)
entries, err := os.ReadDir(pings)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
titles = append(titles, entry.Name())
}
}
return titles, nil
}
func (this *Info) SitesStatus(titles []string) (SiteStatusList, error) {
ret := make(SiteStatusList, len(titles))
for i, title := range titles {
s, err := this.SiteStatus(title)
if err != nil {
return nil, err
}
ret[i] = &s
}
titleLength := ret.MaxTitleLength()
ret.Apply(SiteStatusWithTitleLength(titleLength))
return ret, nil
}
func (this *Info) SiteStatus(title string) (SiteStatus, error) {
ret := SiteStatus{title: title}
dir := filepath.Join(common.GetPidVarDir(this.pid), title)
latencyFile := filepath.Join(dir, "latency")
b, err := os.ReadFile(latencyFile)
if err != nil {
return ret, err
}
latency, err := strconv.ParseInt(
strings.TrimSpace(string(b)),
10, 64,
)
if err != nil {
return ret, err
}
healthFile := filepath.Join(dir, "health")
b, err = os.ReadFile(healthFile)
if err != nil {
return ret, err
}
health, err := strconv.Atoi(strings.TrimSpace(string(b)))
if err != nil {
return ret, err
}
ret.Latency = latency
if health == 0 {
ret.Health = false
} else {
ret.Health = true
}
return ret, nil
}