-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
166 lines (136 loc) · 3.57 KB
/
header.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
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
package component
import (
"context"
"strconv"
"time"
"github.com/kopecmaciej/mongui/config"
"github.com/kopecmaciej/mongui/manager"
"github.com/kopecmaciej/tview"
"github.com/rs/zerolog/log"
)
const (
HeaderComponent manager.Component = "Header"
)
type (
order int
info struct {
label string
value string
}
BaseInfo map[order]info
// Header is a component that displays information about the database
// in the header of the application
Header struct {
*Component
*tview.Table
style *config.HeaderStyle
baseInfo BaseInfo
}
)
// NewHeader creates a new header component
func NewHeader() *Header {
h := Header{
Component: NewComponent(HeaderComponent),
Table: tview.NewTable(),
}
h.SetAfterInitFunc(h.init)
return &h
}
func (h *Header) init() error {
ctx := context.Background()
h.setStyle()
if err := h.setBaseInfo(ctx); err != nil {
return err
}
h.render()
go h.refresh()
return nil
}
func (h *Header) setStyle() {
h.style = &h.app.Styles.Header
h.Table.SetBackgroundColor(h.style.BackgroundColor.Color())
h.Table.SetBorderColor(h.style.BorderColor.Color())
h.Table.SetSelectable(false, false)
h.Table.SetBorder(true)
h.Table.SetBorderPadding(0, 0, 1, 1)
h.Table.SetTitle(" Database Info ")
}
// setBaseInfo sets the base information about the database
// such as status, host, port, database, version, uptime, connections, memory etc.
func (h *Header) setBaseInfo(ctx context.Context) error {
ss, err := h.dao.GetServerStatus(ctx)
if err != nil {
return err
}
port := strconv.Itoa(h.dao.Config.Port)
status := h.style.InactiveSymbol.String()
if ss.Ok == 1 {
status = h.style.ActiveSymbol.String()
}
orElseNil := func(i int32) string {
if i == 0 {
return ""
}
return strconv.Itoa(int(i))
}
h.baseInfo = BaseInfo{
0: {"Status", status},
1: {"Host", h.dao.Config.Host},
2: {"Port", port},
3: {"Database", h.dao.Config.Database},
4: {"Version", ss.Version},
5: {"Uptime", orElseNil(ss.Uptime)},
6: {"Connections", orElseNil(ss.CurrentConns)},
7: {"Available Connections", orElseNil(ss.AvailableConns)},
8: {"Resident Memory", orElseNil(ss.Mem.Resident)},
9: {"Virtual Memory", orElseNil(ss.Mem.Virtual)},
10: {"Is Master", strconv.FormatBool(ss.Repl.IsMaster)},
}
return nil
}
// refresh refreshes the header component every 10 seconds
// to display the most recent information about the database
func (h *Header) refresh() {
for {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := h.setBaseInfo(ctx)
if err != nil {
log.Error().Err(err).Msg("error while refreshing header")
h.baseInfo[0] = info{"Status", h.style.InactiveSymbol.String()}
}
h.app.QueueUpdateDraw(func() {
h.render()
})
time.Sleep(10 * time.Second)
}
}
// render renders the header component
func (h *Header) render() {
b := h.baseInfo
maxInRow := 2
currCol := 0
currRow := 0
for i := 0; i < len(b); i++ {
if i%maxInRow == 0 && i != 0 {
currCol += 2
currRow = 0
}
order := order(i)
h.Table.SetCell(currRow, currCol, h.keyCell(b[order].label))
h.Table.SetCell(currRow, currCol+1, h.valueCell(b[order].value))
currRow++
}
}
func (h *Header) keyCell(text string) *tview.TableCell {
cell := tview.NewTableCell(text + ":")
cell.SetBackgroundColor(h.style.BackgroundColor.Color())
cell.SetTextColor(h.style.KeyColor.Color())
return cell
}
func (h *Header) valueCell(text string) *tview.TableCell {
cell := tview.NewTableCell(text)
cell.SetBackgroundColor(h.style.BackgroundColor.Color())
cell.SetTextColor(h.style.ValueColor.Color())
return cell
}