@@ -14,8 +14,7 @@ import (
1414
1515const (
1616 // 24h hh:mm:ss: 14:23:20
17- HHMMSS24h = "15:04:05"
18- defaultMetricShowCount = 150
17+ HHMMSS24h = "15:04:05"
1918)
2019
2120type Graph struct {
@@ -28,9 +27,10 @@ type Graph struct {
2827}
2928
3029var (
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 {}
6464)
6565
6666func createMetricDisplay () {
67- showCount = defaultMetricShowCount
67+ updateShowMetricCount ()
6868 updateGraphs (false )
6969
7070 app = tview .NewApplication ().
@@ -97,12 +97,24 @@ func createMetricDisplay() {
9797func 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+
140159func getGraphs () tview.Primitive {
141160 graphsContainer .Clear ()
142161
@@ -318,35 +337,33 @@ func updatePanels(query bool) {
318337}
319338
320339func 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
347364func 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