Skip to content

Commit c5065c6

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

File tree

4 files changed

+92
-50
lines changed

4 files changed

+92
-50
lines changed

cmd/display.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ const (
1212
defaultFramesPerSecond = 5 // frames per second
1313
)
1414

15+
type PopupState int
16+
17+
const (
18+
PopupHidden PopupState = iota
19+
PopupColumns
20+
PopupPanels
21+
PopupTimeRange
22+
)
23+
1524
var (
1625
app *tview.Application
1726
pages *tview.Pages
1827
mainView *tview.Flex
1928
playPauseButton *tview.Button
2029

21-
durationText = tview.NewTextView()
22-
sizeText = tview.NewTextView()
30+
durationText = tview.NewTextView()
31+
sizeText = tview.NewTextView()
32+
countTextView = tview.NewTextView()
2333

2434
showCount = 1
2535
framesPerSecond = defaultFramesPerSecond
26-
showPopup bool
36+
popup PopupState
2737
paused = false
2838
errAdvancedDisplay error
2939
focus = ""
@@ -33,13 +43,13 @@ func getPages() *tview.Pages {
3343
if capture == Metric {
3444
pages = tview.NewPages().AddPage("main", getMetricMain(), true, true)
3545

36-
if showPopup {
46+
if popup == PopupPanels {
3747
pages = pages.AddPage("modal", getMetricsModal(), true, true)
3848
}
3949
} else {
4050
pages = tview.NewPages().AddPage("main", getFlowMain(), true, true)
4151

42-
if showPopup {
52+
if popup == PopupColumns {
4353
pages = pages.AddPage("modal", getColumnsModal(), true, true)
4454
}
4555
}
@@ -70,9 +80,7 @@ func getInfoRow() tview.Primitive {
7080
infoRow.AddItem(tview.NewTextView().SetText(getLogLevelText()), 0, 1, false)
7181
}
7282
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-
}
83+
infoRow.AddItem(sizeText.SetText(getSizeText()).SetTextAlign(tview.AlignCenter), 0, 1, false)
7684
if logLevel == "debug" {
7785
fpsText := tview.NewTextView().SetText(getFPSText()).SetTextAlign(tview.AlignCenter)
7886
infoRow.
@@ -92,8 +100,8 @@ func getInfoRow() tview.Primitive {
92100
return infoRow
93101
}
94102

95-
func getCountRow() tview.Primitive {
96-
countTextView := tview.NewTextView().SetText(getShowCountText())
103+
func getCountRow(useSpacer bool) *tview.Flex {
104+
countTextView.SetText(getShowCountText())
97105
countRow := tview.NewFlex().SetDirection(tview.FlexColumn).
98106
AddItem(countTextView, 0, 1, false).
99107
AddItem(tview.NewButton("-").SetSelectedFunc(func() {
@@ -117,7 +125,9 @@ func getCountRow() tview.Primitive {
117125
updateScreen()
118126
}), 5, 0, false).
119127
AddItem(tview.NewTextView(), 0, 2, false)
120-
countRow.AddItem(tview.NewTextView(), 16, 0, false)
128+
if useSpacer {
129+
countRow.AddItem(tview.NewTextView(), 16, 0, false)
130+
}
121131
return countRow
122132
}
123133

@@ -145,7 +155,10 @@ func getDurationText() string {
145155
}
146156

147157
func getSizeText() string {
148-
return fmt.Sprintf("Capture size: %s", sizestr.ToString(totalBytes))
158+
if capture != Metric {
159+
return fmt.Sprintf("Capture size: %s", sizestr.ToString(totalBytes))
160+
}
161+
return ""
149162
}
150163

151164
func updateStatusTexts() {
@@ -183,7 +196,7 @@ func pause(pause bool) {
183196

184197
func updateScreen() {
185198
if app != nil {
186-
showPopup = false
199+
popup = PopupHidden
187200
app.SetRoot(getPages(), true)
188201
}
189202
}

cmd/flow_display.go

Lines changed: 2 additions & 2 deletions
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)
@@ -186,7 +186,7 @@ func getFlowTop() tview.Primitive {
186186
// add cycles and custom columns modal button
187187
columnsRow.AddItem(cyclesCol, 0, 1, false)
188188
columnsRow.AddItem(tview.NewButton(" Manage columns ").SetSelectedFunc(func() {
189-
showPopup = true
189+
popup = PopupColumns
190190
app.SetRoot(getPages(), true)
191191
}), 16, 0, false)
192192
flexView.AddItem(columnsRow, 2, 0, false)

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: 49 additions & 32 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 {
@@ -28,9 +27,10 @@ type Graph struct {
2827
}
2928

3029
var (
31-
legendView *tview.Flex
32-
graphsContainer = tview.NewFlex().SetDirection(tview.FlexRow)
33-
panelsTextView = tview.NewTextView()
30+
legendView *tview.Flex
31+
timeRangeDropdown = tview.NewDropDown()
32+
graphsContainer = tview.NewFlex().SetDirection(tview.FlexRow)
33+
panelsTextView = tview.NewTextView()
3434

3535
selectedPanels = []string{}
3636
graphs = []Graph{}
@@ -64,7 +64,7 @@ var (
6464
)
6565

6666
func createMetricDisplay() {
67-
showCount = defaultMetricShowCount
67+
updateShowMetricCount()
6868
updateGraphs(false)
6969

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

102114
// panels row containing cycles and custom panels picker
103115
panelsRow := tview.NewFlex().SetDirection(tview.FlexColumn)
104116

105-
// display
117+
// panels
106118
panelsRow.AddItem(panelsTextView, 0, 1, false)
107119
if len(selectedPanels) == 0 {
108120
panelsRow.
@@ -126,17 +138,24 @@ func getMetricTop() tview.Primitive {
126138
}), 10, 0, false)
127139
}
128140
panelsRow.AddItem(tview.NewTextView(), 0, 2, false)
129-
updatePanels(false)
141+
if !paused {
142+
updatePanels(false)
143+
}
130144

131-
// add cycles and custom columns modal button
145+
// add panels modal button
132146
panelsRow.AddItem(tview.NewButton(" Manage panels ").SetSelectedFunc(func() {
133-
showPopup = true
147+
popup = PopupPanels
134148
app.SetRoot(getPages(), true)
135149
}), 16, 0, false)
136150
topView.AddItem(panelsRow, 1, 0, false)
137151
return topView
138152
}
139153

154+
func updateShowMetricCount() {
155+
showCount = metricCounts[durations[selectedDuration]]
156+
countTextView.SetText(getShowCountText())
157+
}
158+
140159
func getGraphs() tview.Primitive {
141160
graphsContainer.Clear()
142161

@@ -318,35 +337,33 @@ func updatePanels(query bool) {
318337
}
319338

320339
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-
})
340+
for index := range graphs {
341+
if graphs[index].Plot != nil {
342+
graphs[index].Plot.SetXAxisLabelFunc(func(i int) string {
343+
return graphs[index].Query.Range.Start.Add(time.Duration(i) * graphs[index].Query.Range.Step).Format(HHMMSS24h)
344+
})
327345

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-
}
346+
if len(graphs[index].Labels) > 0 {
347+
graphs[index].Plot.SetFocusFunc(func() {
348+
mainView.AddItem(getLegends(graphs[index].Query.PromQL, graphs[index].Labels, graphs[index].Legends, graphs[index].Data), len(graphs[index].Legends)+3, 0, false)
349+
})
350+
graphs[index].Plot.SetBlurFunc(func() {
351+
if legendView != nil {
352+
mainView.RemoveItem(legendView)
353+
}
354+
})
355+
}
338356

339-
if graphs[index].Data != nil {
340-
graphs[index].Plot.SetData(graphs[index].Data)
341-
}
357+
if graphs[index].Data != nil {
358+
graphs[index].Plot.SetData(graphs[index].Data)
342359
}
343360
}
344361
}
345362
}
346363

347364
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 {
365+
// Skip if paused, query / matrix are invalid or when graph array changed in between
366+
if paused || query == nil || matrix == nil || index >= len(graphs) || graphs[index].Query.PromQL != query.PromQL {
350367
return
351368
}
352369

0 commit comments

Comments
 (0)