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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
package dashboard
import (
"path/filepath"
"slices"
"sort"
"strconv"
"ior/internal/statsengine"
common "ior/internal/tui/common"
)
type DirSnapshot struct {
Dir string
Accesses uint64
BytesRead uint64
BytesWritten uint64
AvgLatencyNs float64
MaxLatencyNs uint64
FileCount uint64
}
type fileSortKey uint8
const (
fileSortKeyAccesses fileSortKey = iota
fileSortKeyRead
fileSortKeyWrite
fileSortKeyAvgLatency
fileSortKeyMaxLatency
fileSortKeyPath
)
type fileDirSortKey uint8
const (
fileDirSortKeyAccesses fileDirSortKey = iota
fileDirSortKeyRead
fileDirSortKeyWrite
fileDirSortKeyAvgLatency
fileDirSortKeyMaxLatency
fileDirSortKeyFileCount
fileDirSortKeyDir
)
func renderFiles(snap *statsengine.Snapshot, width, height int) string {
return renderFilesWithSort(snap, width, height, 0, 0, tableSortState[fileSortKey]{})
}
func renderFilesWithOffset(snap *statsengine.Snapshot, width, height, offset, selectedCol int) string {
return renderFilesWithSort(snap, width, height, offset, selectedCol, tableSortState[fileSortKey]{})
}
func renderFilesWithSort(snap *statsengine.Snapshot, width, height, offset, selectedCol int, sortState tableSortState[fileSortKey]) string {
if snap == nil {
return "Files: waiting for stats..."
}
pathWidth := filePathWidth(width)
rows := fileRows(sortedFileSnapshots(snap.Files(), sortState), pathWidth)
if len(rows) == 0 {
return "Files: no data"
}
columns := fileColumns(width)
return renderSelectableTable(
columns,
rows,
height,
offset,
selectedCol,
"enter:filter",
"s/S:sort",
fileSortHint(sortState),
"d:dirs",
"v:mode in dirs",
)
}
func renderFilesDirGrouped(snap *statsengine.Snapshot, width, height, offset, selectedCol int) string {
return renderFilesDirGroupedWithSort(snap, width, height, offset, selectedCol, tableSortState[fileDirSortKey]{})
}
func renderFilesDirGroupedWithSort(snap *statsengine.Snapshot, width, height, offset, selectedCol int, sortState tableSortState[fileDirSortKey]) string {
if snap == nil {
return "Files (dirs): waiting for stats..."
}
pathWidth := dirPathWidth(width)
rows := dirRows(sortedDirSnapshots(aggregateFilesByDir(snap.Files()), sortState), pathWidth)
if len(rows) == 0 {
return "Files (dirs): no data"
}
columns := fileDirColumns(width)
return renderSelectableTable(
columns,
rows,
height,
offset,
selectedCol,
"enter:filter",
"s/S:sort",
fileDirSortHint(sortState),
"d:files",
"v:mode",
"b:metric",
)
}
func fileRows(files []statsengine.FileSnapshot, pathWidth int) [][]string {
rows := make([][]string, 0, len(files))
for _, f := range files {
rows = append(rows, []string{
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 fileColumns(width int) []common.TableColumn {
pathWidth := filePathWidth(width)
return []common.TableColumn{
{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},
}
}
func fileDirColumns(width int) []common.TableColumn {
pathWidth := dirPathWidth(width)
return []common.TableColumn{
{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},
}
}
func sortedFileSnapshots(rows []statsengine.FileSnapshot, sortState tableSortState[fileSortKey]) []statsengine.FileSnapshot {
if len(rows) == 0 {
return nil
}
if !sortState.active {
return rows
}
sorted := slices.Clone(rows)
slices.SortFunc(sorted, func(left, right statsengine.FileSnapshot) int {
cmp := compareFileBySort(left, right, sortState.key)
if cmp == 0 {
cmp = compareFileDefault(left, right)
}
return sortState.apply(cmp)
})
return sorted
}
func sortedDirSnapshots(rows []DirSnapshot, sortState tableSortState[fileDirSortKey]) []DirSnapshot {
if len(rows) == 0 {
return nil
}
if !sortState.active {
return rows
}
sorted := slices.Clone(rows)
slices.SortFunc(sorted, func(left, right DirSnapshot) int {
cmp := compareDirBySort(left, right, sortState.key)
if cmp == 0 {
cmp = compareDirDefault(left, right)
}
return sortState.apply(cmp)
})
return sorted
}
func compareFileBySort(left, right statsengine.FileSnapshot, key fileSortKey) int {
switch key {
case fileSortKeyAccesses:
return compareUint64Desc(left.Accesses, right.Accesses)
case fileSortKeyRead:
return compareUint64Desc(left.BytesRead, right.BytesRead)
case fileSortKeyWrite:
return compareUint64Desc(left.BytesWritten, right.BytesWritten)
case fileSortKeyAvgLatency:
return compareFloat64Desc(left.AvgLatencyNs, right.AvgLatencyNs)
case fileSortKeyMaxLatency:
return compareUint64Desc(left.MaxLatencyNs, right.MaxLatencyNs)
case fileSortKeyPath:
return compareStringAsc(left.Path, right.Path)
default:
return 0
}
}
func compareFileDefault(left, right statsengine.FileSnapshot) int {
if cmp := compareUint64Desc(left.Accesses, right.Accesses); cmp != 0 {
return cmp
}
return compareStringAsc(left.Path, right.Path)
}
func compareDirBySort(left, right DirSnapshot, key fileDirSortKey) int {
switch key {
case fileDirSortKeyAccesses:
return compareUint64Desc(left.Accesses, right.Accesses)
case fileDirSortKeyRead:
return compareUint64Desc(left.BytesRead, right.BytesRead)
case fileDirSortKeyWrite:
return compareUint64Desc(left.BytesWritten, right.BytesWritten)
case fileDirSortKeyAvgLatency:
return compareFloat64Desc(left.AvgLatencyNs, right.AvgLatencyNs)
case fileDirSortKeyMaxLatency:
return compareUint64Desc(left.MaxLatencyNs, right.MaxLatencyNs)
case fileDirSortKeyFileCount:
return compareUint64Desc(left.FileCount, right.FileCount)
case fileDirSortKeyDir:
return compareStringAsc(left.Dir, right.Dir)
default:
return 0
}
}
func compareDirDefault(left, right DirSnapshot) int {
if cmp := compareUint64Desc(left.Accesses, right.Accesses); cmp != 0 {
return cmp
}
return compareStringAsc(left.Dir, right.Dir)
}
func fileSortKeyForColumn(column int) (fileSortKey, bool) {
switch column {
case 0:
return fileSortKeyAccesses, true
case 1:
return fileSortKeyRead, true
case 2:
return fileSortKeyWrite, true
case 3:
return fileSortKeyAvgLatency, true
case 4:
return fileSortKeyMaxLatency, true
case 5:
return fileSortKeyPath, true
default:
return 0, false
}
}
func fileDirSortKeyForColumn(column int) (fileDirSortKey, bool) {
switch column {
case 0:
return fileDirSortKeyAccesses, true
case 1:
return fileDirSortKeyRead, true
case 2:
return fileDirSortKeyWrite, true
case 3:
return fileDirSortKeyAvgLatency, true
case 4:
return fileDirSortKeyMaxLatency, true
case 5:
return fileDirSortKeyFileCount, true
case 6:
return fileDirSortKeyDir, true
default:
return 0, false
}
}
func fileSortHint(sortState tableSortState[fileSortKey]) string {
return "sort: " + fileSortLabel(sortState)
}
func fileSortLabel(sortState tableSortState[fileSortKey]) string {
if !sortState.active {
return "default"
}
switch sortState.key {
case fileSortKeyAccesses:
return sortLabelWithDirection("Accesses", false, sortState.reverse)
case fileSortKeyRead:
return sortLabelWithDirection("Read", false, sortState.reverse)
case fileSortKeyWrite:
return sortLabelWithDirection("Write", false, sortState.reverse)
case fileSortKeyAvgLatency:
return sortLabelWithDirection("Avg Latency", false, sortState.reverse)
case fileSortKeyMaxLatency:
return sortLabelWithDirection("Max Latency", false, sortState.reverse)
case fileSortKeyPath:
return sortLabelWithDirection("Path", true, sortState.reverse)
default:
return "default"
}
}
func fileDirSortHint(sortState tableSortState[fileDirSortKey]) string {
return "sort: " + fileDirSortLabel(sortState)
}
func fileDirSortLabel(sortState tableSortState[fileDirSortKey]) string {
if !sortState.active {
return "default"
}
switch sortState.key {
case fileDirSortKeyAccesses:
return sortLabelWithDirection("Accesses", false, sortState.reverse)
case fileDirSortKeyRead:
return sortLabelWithDirection("Read", false, sortState.reverse)
case fileDirSortKeyWrite:
return sortLabelWithDirection("Write", false, sortState.reverse)
case fileDirSortKeyAvgLatency:
return sortLabelWithDirection("Avg Latency", false, sortState.reverse)
case fileDirSortKeyMaxLatency:
return sortLabelWithDirection("Max Latency", false, sortState.reverse)
case fileDirSortKeyFileCount:
return sortLabelWithDirection("Files", false, sortState.reverse)
case fileDirSortKeyDir:
return sortLabelWithDirection("Directory", true, sortState.reverse)
default:
return "default"
}
}
func findFileOffset(rows []statsengine.FileSnapshot, path string) (int, bool) {
for idx, row := range rows {
if row.Path == path {
return idx, true
}
}
return 0, false
}
func findDirOffset(rows []DirSnapshot, dir string) (int, bool) {
for idx, row := range rows {
if row.Dir == dir {
return idx, true
}
}
return 0, false
}
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) [][]string {
rows := make([][]string, 0, len(dirs))
for _, d := range dirs {
rows = append(rows, []string{
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
}
|