summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/files.go
blob: 80e30372d2afb410c84072ac2f71c60df5414b9b (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dashboard

import (
	"fmt"
	"ior/internal/statsengine"
	"path/filepath"
	"sort"
	"strconv"

	"github.com/charmbracelet/bubbles/table"
)

type DirSnapshot struct {
	Dir string

	Accesses     uint64
	BytesRead    uint64
	BytesWritten uint64

	AvgLatencyNs float64
	MaxLatencyNs uint64
	FileCount    uint64
}

func renderFiles(snap *statsengine.Snapshot, width, height int) string {
	return renderFilesWithOffset(snap, width, height, 0)
}

func renderFilesWithOffset(snap *statsengine.Snapshot, width, height, offset int) string {
	if snap == nil {
		return "Files: waiting for stats..."
	}

	pathWidth := filePathWidth(width)
	rows := fileRows(snap.Files(), pathWidth)
	if len(rows) == 0 {
		return "Files: no data"
	}

	columns := []table.Column{
		{Title: "Accesses", Width: 8},
		{Title: "Read", Width: 9},
		{Title: "Write", Width: 9},
		{Title: "Avg Latency", Width: 11},
		{Title: "Max Latency", Width: 11},
		{Title: "Path", Width: pathWidth},
	}

	tbl := table.New(
		table.WithColumns(columns),
		table.WithRows(rows),
		table.WithFocused(true),
	)
	tbl.SetHeight(syscallTableHeight(height))
	tbl.SetWidth(tableWidth(width))
	cursor := clampOffset(offset, len(rows))
	tbl.SetCursor(cursor)
	return tbl.View() + fmt.Sprintf("\nRow %d/%d [d:dirs]", cursor+1, len(rows))
}

func renderFilesDirGrouped(snap *statsengine.Snapshot, width, height, offset int) string {
	if snap == nil {
		return "Files (dirs): waiting for stats..."
	}

	pathWidth := dirPathWidth(width)
	rows := dirRows(aggregateFilesByDir(snap.Files()), pathWidth)
	if len(rows) == 0 {
		return "Files (dirs): no data"
	}

	columns := []table.Column{
		{Title: "Accesses", Width: 8},
		{Title: "Read", Width: 9},
		{Title: "Write", Width: 9},
		{Title: "Avg Latency", Width: 11},
		{Title: "Max Latency", Width: 11},
		{Title: "Files", Width: 5},
		{Title: "Directory", Width: pathWidth},
	}

	tbl := table.New(
		table.WithColumns(columns),
		table.WithRows(rows),
		table.WithFocused(true),
	)
	tbl.SetHeight(syscallTableHeight(height))
	tbl.SetWidth(tableWidth(width))
	cursor := clampOffset(offset, len(rows))
	tbl.SetCursor(cursor)
	return tbl.View() + fmt.Sprintf("\nRow %d/%d [d:files]", cursor+1, len(rows))
}

func fileRows(files []statsengine.FileSnapshot, pathWidth int) []table.Row {
	rows := make([]table.Row, 0, len(files))
	for _, f := range files {
		rows = append(rows, table.Row{
			strconv.FormatUint(f.Accesses, 10),
			formatBytes(float64(f.BytesRead)),
			formatBytes(float64(f.BytesWritten)),
			formatDurationNs(f.AvgLatencyNs),
			formatDurationUintNs(f.MaxLatencyNs),
			truncatePathMiddle(f.Path, pathWidth),
		})
	}
	return rows
}

func filePathWidth(width int) int {
	if width <= 0 {
		return 24
	}
	// Keep fixed metrics visible and let path consume the remaining space.
	// Fixed columns sum to 48 chars; reserve extra for separators/padding.
	w := width - 58
	if w < 14 {
		return 14
	}
	return w
}

func dirPathWidth(width int) int {
	if width <= 0 {
		return 24
	}
	// Directory view adds a 5-char Files column (+1 spacing), so reserve 6 more.
	w := width - 64
	if w < 14 {
		return 14
	}
	return w
}

func truncatePathMiddle(path string, limit int) string {
	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:]
}

func aggregateFilesByDir(files []statsengine.FileSnapshot) []DirSnapshot {
	if len(files) == 0 {
		return nil
	}

	dirs := make(map[string]DirSnapshot, len(files))
	weightedLatency := make(map[string]float64, len(files))
	for _, f := range files {
		dir := filepath.Dir(f.Path)
		s := dirs[dir]
		s.Dir = dir
		s.Accesses += f.Accesses
		s.BytesRead += f.BytesRead
		s.BytesWritten += f.BytesWritten
		if f.MaxLatencyNs > s.MaxLatencyNs {
			s.MaxLatencyNs = f.MaxLatencyNs
		}
		s.FileCount++
		weightedLatency[dir] += f.AvgLatencyNs * float64(f.Accesses)
		dirs[dir] = s
	}

	out := make([]DirSnapshot, 0, len(dirs))
	for dir, s := range dirs {
		if s.Accesses > 0 {
			s.AvgLatencyNs = weightedLatency[dir] / float64(s.Accesses)
		}
		out = append(out, s)
	}

	sort.Slice(out, func(i, j int) bool {
		if out[i].Accesses != out[j].Accesses {
			return out[i].Accesses > out[j].Accesses
		}
		return out[i].Dir < out[j].Dir
	})
	return out
}

func dirRows(dirs []DirSnapshot, pathWidth int) []table.Row {
	rows := make([]table.Row, 0, len(dirs))
	for _, d := range dirs {
		rows = append(rows, table.Row{
			strconv.FormatUint(d.Accesses, 10),
			formatBytes(float64(d.BytesRead)),
			formatBytes(float64(d.BytesWritten)),
			formatDurationNs(d.AvgLatencyNs),
			formatDurationUintNs(d.MaxLatencyNs),
			strconv.FormatUint(d.FileCount, 10),
			truncatePathMiddle(d.Dir, pathWidth),
		})
	}
	return rows
}