summaryrefslogtreecommitdiff
path: root/internal/tui/eventstream/render.go
blob: 3fd2d2651fbb740c1a5942384b6dd1a1257be80c (plain)
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
package eventstream

import (
	"fmt"
	"ior/internal/tui/common"
	"strconv"
	"strings"
)

type columnLayout struct {
	gap     int
	latency int
	comm    int
	pidTid  int
	syscall int
	ret     int
	bytes   int
	file    int
}

func RenderStreamTable(width int, paused bool, totalCount, filteredCount, bufferLen, bufferCap int, filter Filter, events []StreamEvent) string {
	if width <= 0 {
		width = 100
	}

	lines := make([]string, 0, len(events)+3)
	lines = append(lines, renderStatusLine(paused, totalCount, filteredCount, bufferLen, bufferCap))
	lines = append(lines, renderFilterLine(filter))
	lines = append(lines, renderColumnHeader(width))
	for _, ev := range events {
		lines = append(lines, renderEventRow(ev, width))
	}

	return common.PanelStyle.Width(width).Render(strings.Join(lines, "\n"))
}

func renderStatusLine(paused bool, totalCount, filteredCount, bufferLen, bufferCap int) string {
	state := common.HighlightStyle.Render("LIVE")
	if paused {
		state = common.ErrorStyle.Render("PAUSED")
	}
	buffer := strconv.Itoa(bufferLen)
	if bufferCap > 0 {
		buffer = fmt.Sprintf("%d/%d", bufferLen, bufferCap)
	}
	return fmt.Sprintf("%s | total:%d filtered:%d buffer:%s", state, totalCount, filteredCount, buffer)
}

func renderFilterLine(filter Filter) string {
	summary := filter.Summary()
	if summary == "all" {
		summary = common.HighlightStyle.Render(summary)
	}
	return common.HeaderStyle.Render("Filter:") + " " + summary
}

func renderColumnHeader(width int) string {
	cols := computeColumnLayout(width)
	header := fmt.Sprintf("%-*s %-*s %-*s %-*s %-*s %-*s %-*s %s",
		cols.gap, "Gap",
		cols.latency, "Latency",
		cols.comm, "Comm",
		cols.pidTid, "PID.TID",
		cols.syscall, "Syscall",
		cols.ret, "Ret",
		cols.bytes, "Bytes",
		"File",
	)
	return common.HelpBarStyle.Render(header)
}

func renderEventRow(ev StreamEvent, width int) string {
	cols := computeColumnLayout(width)
	pidTid := fmt.Sprintf("%d.%d", ev.PID, ev.TID)
	row := fmt.Sprintf("%-*s %-*s %-*s %-*s %-*s %-*d %-*d %s",
		cols.gap, formatDurationNs(ev.GapNs),
		cols.latency, formatDurationNs(ev.DurationNs),
		cols.comm, truncateMiddle(ev.Comm, cols.comm),
		cols.pidTid, truncateMiddle(pidTid, cols.pidTid),
		cols.syscall, truncateMiddle(ev.Syscall, cols.syscall),
		cols.ret, ev.RetVal,
		cols.bytes, ev.Bytes,
		truncateMiddle(ev.FileName, cols.file),
	)
	if ev.IsError {
		return common.ErrorStyle.Render(row)
	}
	return row
}

func computeColumnLayout(width int) columnLayout {
	if width <= 0 {
		width = 100
	}

	gap := 8
	latency := 9
	comm := 14
	pidTid := 12
	syscall := 11
	ret := 6
	bytes := 10
	fixed := gap + latency + comm + pidTid + syscall + ret + bytes + 7
	file := width - fixed
	if file >= 18 {
		return columnLayout{gap: gap, latency: latency, comm: comm, pidTid: pidTid, syscall: syscall, ret: ret, bytes: bytes, file: file}
	}

	if width < 90 {
		comm = 10
		syscall = 9
	} else {
		comm = 12
		syscall = 10
	}
	fixed = gap + latency + comm + pidTid + syscall + ret + bytes + 7
	file = width - fixed
	if file < 8 {
		file = 8
	}
	return columnLayout{gap: gap, latency: latency, comm: comm, pidTid: pidTid, syscall: syscall, ret: ret, bytes: bytes, file: file}
}

func formatDurationNs(v uint64) string {
	if v < 1000 {
		return fmt.Sprintf("%dns", v)
	}
	us := float64(v) / 1000
	if us < 1000 {
		return fmt.Sprintf("%.1fus", us)
	}
	ms := us / 1000
	if ms < 1000 {
		return fmt.Sprintf("%.1fms", ms)
	}
	s := ms / 1000
	return fmt.Sprintf("%.2fs", s)
}

func truncateMiddle(path string, limit int) string {
	if limit <= 0 {
		return ""
	}
	if len(path) <= limit {
		return path
	}
	if limit <= 3 {
		return path[:limit]
	}

	head := (limit - 3) / 2
	tail := limit - 3 - head
	if tail <= 0 {
		return path[:limit]
	}
	return path[:head] + "..." + path[len(path)-tail:]
}