Skip to content

Commit 26adbd4

Browse files
committed
add time range dropdown
1 parent 4c09f66 commit 26adbd4

File tree

4 files changed

+73
-41
lines changed

4 files changed

+73
-41
lines changed

cmd/display.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ var (
1818
mainView *tview.Flex
1919
playPauseButton *tview.Button
2020

21-
durationText = tview.NewTextView()
22-
sizeText = tview.NewTextView()
21+
durationText = tview.NewTextView()
22+
sizeText = tview.NewTextView()
23+
countTextView = tview.NewTextView()
2324

2425
showCount = 1
2526
framesPerSecond = defaultFramesPerSecond
@@ -70,9 +71,7 @@ func getInfoRow() tview.Primitive {
7071
infoRow.AddItem(tview.NewTextView().SetText(getLogLevelText()), 0, 1, false)
7172
}
7273
infoRow.AddItem(durationText.SetText(getDurationText()).SetTextAlign(tview.AlignCenter), 0, 1, false)
73-
if capture != Metric {
74-
infoRow.AddItem(sizeText.SetText(getSizeText()).SetTextAlign(tview.AlignCenter), 0, 1, false)
75-
}
74+
infoRow.AddItem(sizeText.SetText(getSizeText()).SetTextAlign(tview.AlignCenter), 0, 1, false)
7675
if logLevel == "debug" {
7776
fpsText := tview.NewTextView().SetText(getFPSText()).SetTextAlign(tview.AlignCenter)
7877
infoRow.
@@ -92,8 +91,8 @@ func getInfoRow() tview.Primitive {
9291
return infoRow
9392
}
9493

95-
func getCountRow() tview.Primitive {
96-
countTextView := tview.NewTextView().SetText(getShowCountText())
94+
func getCountRow(useSpacer bool) *tview.Flex {
95+
countTextView.SetText(getShowCountText())
9796
countRow := tview.NewFlex().SetDirection(tview.FlexColumn).
9897
AddItem(countTextView, 0, 1, false).
9998
AddItem(tview.NewButton("-").SetSelectedFunc(func() {
@@ -117,7 +116,9 @@ func getCountRow() tview.Primitive {
117116
updateScreen()
118117
}), 5, 0, false).
119118
AddItem(tview.NewTextView(), 0, 2, false)
120-
countRow.AddItem(tview.NewTextView(), 16, 0, false)
119+
if useSpacer {
120+
countRow.AddItem(tview.NewTextView(), 16, 0, false)
121+
}
121122
return countRow
122123
}
123124

@@ -145,7 +146,10 @@ func getDurationText() string {
145146
}
146147

147148
func getSizeText() string {
148-
return fmt.Sprintf("Capture size: %s", sizestr.ToString(totalBytes))
149+
if capture != Metric {
150+
return fmt.Sprintf("Capture size: %s", sizestr.ToString(totalBytes))
151+
}
152+
return ""
149153
}
150154

151155
func updateStatusTexts() {

cmd/flow_display.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func getFlowTop() tview.Primitive {
130130
flexView.AddItem(getInfoRow(), 1, 0, false)
131131

132132
// flows count
133-
flexView.AddItem(getCountRow(), 1, 0, false)
133+
flexView.AddItem(getCountRow(true), 1, 0, false)
134134

135135
// columns row containing cycles (display, enrichment) and custom columns picker
136136
columnsRow := tview.NewFlex().SetDirection(tview.FlexColumn)

cmd/metric_capture.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ var metricCmd = &cobra.Command{
1717
}
1818

1919
var (
20-
client *api.Client
20+
client *api.Client
21+
selectedDuration = 0
22+
durations = []string{"5m", "10m", "30m", "1h", "6h"}
23+
metricCounts = map[string]int{
24+
"5m": 60,
25+
"10m": 70,
26+
"30m": 80,
27+
"1h": 90,
28+
"6h": 100,
29+
}
2130
)
2231

2332
func runMetricCapture(c *cobra.Command, _ []string) {
@@ -86,10 +95,13 @@ func queryGraph(ctx context.Context, client api.Client, index int) {
8695
func queryProm(ctx context.Context, client api.Client, promQL string) (*Query, *Matrix) {
8796
now := currentTime()
8897

89-
ran := 5 * time.Minute
98+
ran, err := time.ParseDuration(durations[selectedDuration])
99+
if err != nil {
100+
log.Fatal(err)
101+
}
90102
end := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location())
91103
start := end.Add(-ran)
92-
step := ran / time.Duration(showCount)
104+
step := time.Duration(ran.Nanoseconds() / int64(showCount))
93105

94106
// update query with start / end / step
95107
query := Query{

cmd/metric_display.go

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414

1515
const (
1616
// 24h hh:mm:ss: 14:23:20
17-
HHMMSS24h = "15:04:05"
18-
defaultMetricShowCount = 150
17+
HHMMSS24h = "15:04:05"
1918
)
2019

2120
type Graph struct {
@@ -64,7 +63,7 @@ var (
6463
)
6564

6665
func createMetricDisplay() {
67-
showCount = defaultMetricShowCount
66+
updateShowMetricCount()
6867
updateGraphs(false)
6968

7069
app = tview.NewApplication().
@@ -97,12 +96,24 @@ func createMetricDisplay() {
9796
func getMetricTop() tview.Primitive {
9897
topView := tview.NewFlex().SetDirection(tview.FlexRow)
9998
topView.AddItem(getInfoRow(), 1, 0, false)
100-
topView.AddItem(getCountRow(), 1, 0, false)
99+
countRow := getCountRow(false)
100+
// add time range dropdown
101+
countRow.AddItem(tview.NewDropDown().
102+
SetLabel("Time range ").
103+
SetOptions(durations, nil).
104+
SetCurrentOption(selectedDuration).
105+
SetSelectedFunc(func(_ string, index int) {
106+
selectedDuration = index
107+
updateShowMetricCount()
108+
updateGraphs(true)
109+
}).
110+
SetFieldWidth(5), 16, 0, false)
111+
topView.AddItem(countRow, 1, 0, false)
101112

102113
// panels row containing cycles and custom panels picker
103114
panelsRow := tview.NewFlex().SetDirection(tview.FlexColumn)
104115

105-
// display
116+
// panels
106117
panelsRow.AddItem(panelsTextView, 0, 1, false)
107118
if len(selectedPanels) == 0 {
108119
panelsRow.
@@ -126,9 +137,11 @@ func getMetricTop() tview.Primitive {
126137
}), 10, 0, false)
127138
}
128139
panelsRow.AddItem(tview.NewTextView(), 0, 2, false)
129-
updatePanels(false)
140+
if !paused {
141+
updatePanels(false)
142+
}
130143

131-
// add cycles and custom columns modal button
144+
// add panels modal button
132145
panelsRow.AddItem(tview.NewButton(" Manage panels ").SetSelectedFunc(func() {
133146
showPopup = true
134147
app.SetRoot(getPages(), true)
@@ -137,6 +150,11 @@ func getMetricTop() tview.Primitive {
137150
return topView
138151
}
139152

153+
func updateShowMetricCount() {
154+
showCount = metricCounts[durations[selectedDuration]]
155+
countTextView.SetText(getShowCountText())
156+
}
157+
140158
func getGraphs() tview.Primitive {
141159
graphsContainer.Clear()
142160

@@ -318,35 +336,33 @@ func updatePanels(query bool) {
318336
}
319337

320338
func updatePlots() {
321-
if !paused {
322-
for index := range graphs {
323-
if graphs[index].Plot != nil {
324-
graphs[index].Plot.SetXAxisLabelFunc(func(i int) string {
325-
return graphs[index].Query.Range.Start.Add(time.Duration(i) * graphs[index].Query.Range.Step).Format(HHMMSS24h)
326-
})
339+
for index := range graphs {
340+
if graphs[index].Plot != nil {
341+
graphs[index].Plot.SetXAxisLabelFunc(func(i int) string {
342+
return graphs[index].Query.Range.Start.Add(time.Duration(i) * graphs[index].Query.Range.Step).Format(HHMMSS24h)
343+
})
327344

328-
if len(graphs[index].Labels) > 0 {
329-
graphs[index].Plot.SetFocusFunc(func() {
330-
mainView.AddItem(getLegends(graphs[index].Query.PromQL, graphs[index].Labels, graphs[index].Legends, graphs[index].Data), len(graphs[index].Legends)+3, 0, false)
331-
})
332-
graphs[index].Plot.SetBlurFunc(func() {
333-
if legendView != nil {
334-
mainView.RemoveItem(legendView)
335-
}
336-
})
337-
}
345+
if len(graphs[index].Labels) > 0 {
346+
graphs[index].Plot.SetFocusFunc(func() {
347+
mainView.AddItem(getLegends(graphs[index].Query.PromQL, graphs[index].Labels, graphs[index].Legends, graphs[index].Data), len(graphs[index].Legends)+3, 0, false)
348+
})
349+
graphs[index].Plot.SetBlurFunc(func() {
350+
if legendView != nil {
351+
mainView.RemoveItem(legendView)
352+
}
353+
})
354+
}
338355

339-
if graphs[index].Data != nil {
340-
graphs[index].Plot.SetData(graphs[index].Data)
341-
}
356+
if graphs[index].Data != nil {
357+
graphs[index].Plot.SetData(graphs[index].Data)
342358
}
343359
}
344360
}
345361
}
346362

347363
func appendMetrics(query *Query, matrix *Matrix, index int) {
348-
// Skip if query / matrix are invalid or when graph array changed in between
349-
if query == nil || matrix == nil || index >= len(graphs) || graphs[index].Query.PromQL != query.PromQL {
364+
// Skip if paused, query / matrix are invalid or when graph array changed in between
365+
if paused || query == nil || matrix == nil || index >= len(graphs) || graphs[index].Query.PromQL != query.PromQL {
350366
return
351367
}
352368

0 commit comments

Comments
 (0)