Skip to content

Commit 09a4bc5

Browse files
committed
add comments
1 parent 0463f66 commit 09a4bc5

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

tool/tctl/common/top/box.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ import (
2222
"github.com/charmbracelet/lipgloss"
2323
)
2424

25+
// boxedView wraps the provided content in a rounded border,
26+
// with the title embedded in the top. For example, if the
27+
// content was \t\t\tHello and the title was Some Heading the
28+
// returned content would be:
29+
//
30+
// ╭Some Heading────────╮
31+
// │ │
32+
// │ Hello │
33+
// ╰────────────────────╯
2534
func boxedView(title string, content string, width int) string {
2635
rounderBorder := lipgloss.RoundedBorder()
2736

tool/tctl/common/top/model.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
"github.com/gravitational/teleport/api/constants"
3535
)
3636

37+
// topModel is a [tea.Model] implementation which
38+
// displays various tabs and content displayed by
39+
// the tctl top command.
3740
type topModel struct {
3841
width int
3942
height int
@@ -52,6 +55,9 @@ func newTopModel(refreshInterval time.Duration, clt *roundtrip.Client) *topModel
5255
}
5356
}
5457

58+
// refresh pulls metrics from Teleport and builds
59+
// a [Report] according to the configured refresh
60+
// interval.
5561
func (m *topModel) refresh() tea.Cmd {
5662
return func() tea.Msg {
5763
if m.report != nil {
@@ -75,6 +81,8 @@ func (m *topModel) Init() tea.Cmd {
7581
return m.refresh()
7682
}
7783

84+
// Update processes messages in order to updated the
85+
// view based on user input and new metrics data.
7886
func (m *topModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7987
switch msg := msg.(type) {
8088
case tea.WindowSizeMsg:
@@ -105,6 +113,8 @@ func (m *topModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
105113
return m, nil
106114
}
107115

116+
// View formats the metrics and draws them to
117+
// the screen.
108118
func (m *topModel) View() string {
109119
availableHeight := m.height
110120
header := headerView(m.selected, m.width)
@@ -125,6 +135,9 @@ func (m *topModel) View() string {
125135
)
126136
}
127137

138+
// headerView generates the tab bar displayed at
139+
// the top of the screen. The selectedTab will be
140+
// rendered a different color to indicate as such.
128141
func headerView(selectedTab int, width int) string {
129142
tabs := tabView(selectedTab)
130143

@@ -138,6 +151,8 @@ func headerView(selectedTab int, width int) string {
138151
return tabs + lipgloss.NewStyle().Render(filler) + "\n" + strings.Repeat("‾", width)
139152
}
140153

154+
// footerView generates the help text displayed at the
155+
// bottom of the screen.
141156
func (m *topModel) footerView() string {
142157
underscore := lipgloss.NewStyle().Underline(true).Render(" ")
143158
underline := strings.Repeat(underscore, m.width)
@@ -173,6 +188,8 @@ func (m *topModel) footerView() string {
173188
statusBarStyle.Render(right)
174189
}
175190

191+
// contentView generates the appropriate content
192+
// based on which tab is selected.
176193
func (m *topModel) contentView() string {
177194
if m.report == nil {
178195
return ""
@@ -192,6 +209,7 @@ func (m *topModel) contentView() string {
192209
}
193210
}
194211

212+
// renderCommon generates the view for the cluster stats tab.
195213
func renderCommon(report *Report, width int) string {
196214
columnWidth := width / 2
197215

@@ -294,6 +312,7 @@ func renderCommon(report *Report, width int) string {
294312
)
295313
}
296314

315+
// renderBackend generates the view for the backend stats tab.
297316
func renderBackend(report *Report, height, width int) string {
298317
latencyWidth := width / 3
299318
requestsWidth := width * 2 / 3
@@ -326,6 +345,7 @@ func renderBackend(report *Report, height, width int) string {
326345
)
327346
}
328347

348+
// renderCache generates the view for the cache stats tab.
329349
func renderCache(report *Report, height, width int) string {
330350
latencyWidth := width / 3
331351
requestsWidth := width * 2 / 3
@@ -359,6 +379,7 @@ func renderCache(report *Report, height, width int) string {
359379
)
360380
}
361381

382+
// renderWatcher generates the view for the watcher stats tab.
362383
func renderWatcher(report *Report, height, width int) string {
363384
graphWidth := width * 40 / 100
364385
graphHeight := height / 3
@@ -412,6 +433,7 @@ func renderWatcher(report *Report, height, width int) string {
412433
)
413434
}
414435

436+
// tabView renders the tabbed content in the header.
415437
func tabView(selectedTab int) string {
416438
output := lipgloss.NewStyle().
417439
Underline(true).

tool/tctl/common/top/report.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ type Percentile struct {
289289
Value time.Duration
290290
}
291291

292+
// Percentiles returns an iterator of the percentiles
293+
// of the buckets within the historgram.
292294
func (h Histogram) Percentiles() iter.Seq[Percentile] {
293295
return func(yield func(Percentile) bool) {
294296
if h.Count == 0 {

tool/tctl/common/top/table.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type column struct {
2727
content []string
2828
}
2929

30+
// tableView renders two columns in a table like view
31+
// that has no headings. Content lengths of the columns
32+
// is required to match.
3033
func tableView(width int, first, second column) string {
3134
if len(first.content) != len(second.content) {
3235
panic("column content must have equal heights")
@@ -55,6 +58,8 @@ func tableView(width int, first, second column) string {
5558
return style.Render(lipgloss.JoinVertical(lipgloss.Left, rows...))
5659
}
5760

61+
// percentileTableView renders a dynamic table like view
62+
// displaying the percentiles of the provided histogram.
5863
func percentileTableView(width int, hist Histogram) string {
5964
firstColumn := column{
6065
width: width / 2,
@@ -78,6 +83,8 @@ func percentileTableView(width int, hist Histogram) string {
7883
return tableView(width, firstColumn, secondColumn)
7984
}
8085

86+
// requestsTableView renders a table like view
87+
// displaying information about backend request stats.
8188
func requestsTableView(height, width int, stats *BackendStats) string {
8289
style := lipgloss.NewStyle().
8390
Width(width).
@@ -123,6 +130,8 @@ func requestsTableView(height, width int, stats *BackendStats) string {
123130
return style.Render(lipgloss.JoinVertical(lipgloss.Left, rows...))
124131
}
125132

133+
// eventsTableView renders a table like view
134+
// displaying information about watcher event stats.
126135
func eventsTableView(height, width int, stats *WatcherStats) string {
127136
style := lipgloss.NewStyle().
128137
Width(width).

0 commit comments

Comments
 (0)