99
1010 "github.com/gdamore/tcell/v2"
1111 "github.com/navidys/tvxwidgets"
12+ pmod "github.com/prometheus/common/model"
1213 "github.com/rivo/tview"
1314)
1415
3334
3435 selectedPanels = []string {}
3536 graphs = []Graph {}
37+ focussedGraph = - 1
3638 colors = []tcell.Color {
3739 tcell .ColorWhite ,
3840 tcell .ColorPeru ,
4547 tcell .ColorAquaMarine ,
4648 tcell .ColorDarkSeaGreen ,
4749 tcell .ColorOrange ,
48- tcell .ColorBisque ,
50+ tcell .ColorYellowGreen ,
4951 tcell .ColorTeal ,
5052 tcell .ColorPurple ,
5153 tcell .ColorMintCream ,
5456 tcell .ColorSalmon ,
5557 tcell .ColorMidnightBlue ,
5658 tcell .ColorDeepSkyBlue ,
57- tcell .ColorFloralWhite ,
59+ tcell .ColorYellow ,
5860 tcell .ColorMediumSeaGreen ,
5961 tcell .ColorBlanchedAlmond ,
6062 tcell .ColorDarkKhaki ,
6365)
6466
6567func createMetricDisplay () {
66- framesPerSecond = 1
6768 updateShowMetricCount ()
6869 updateGraphs (false )
6970
@@ -162,6 +163,9 @@ func getGraphs() tview.Primitive {
162163 var flex * tview.Flex
163164 for index := range graphs {
164165 plot := getPlot (toMetricName (graphs [index ].Query .PromQL , 0 ))
166+ if focussedGraph == index {
167+ plot .SetBorderColor (tcell .ColorBlue )
168+ }
165169 graphs [index ].Plot = plot
166170
167171 if index % 2 == 0 {
@@ -200,7 +204,9 @@ func getLegends(title string, labels []string, legends []map[string]string, data
200204 for j , label := range labels {
201205 table .SetCell (i + 1 , j + 1 , tview .NewTableCell (ellipsizeAndPad (legends [i ][label ], 50 )))
202206 }
203- table .SetCell (i + 1 , len (labels )+ 1 , tview .NewTableCell (ellipsizeAndPad (fmt .Sprintf ("%.2f" , data [i ][len (data [i ])- 1 ]), 50 )))
207+ if len (data ) > i && len (data [i ])- 1 >= 0 {
208+ table .SetCell (i + 1 , len (labels )+ 1 , tview .NewTableCell (ellipsizeAndPad (fmt .Sprintf ("%.2f" , data [i ][len (data [i ])- 1 ]), 50 )))
209+ }
204210 }
205211
206212 legendView .AddItem (table , 0 , 1 , false )
@@ -306,6 +312,7 @@ func getPlot(title string) *tvxwidgets.Plot {
306312
307313func updateGraphs (query bool ) {
308314 graphs = []Graph {}
315+ focussedGraph = - 1
309316
310317 if len (selectedPanels ) > 0 {
311318 for _ , p := range selectedPanels {
@@ -344,14 +351,22 @@ func updatePlots() {
344351 })
345352
346353 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- })
350354 graphs [index ].Plot .SetBlurFunc (func () {
355+ focussedGraph = - 1
351356 if legendView != nil {
352357 mainView .RemoveItem (legendView )
358+ getGraphs ()
353359 }
354360 })
361+ graphs [index ].Plot .SetFocusFunc (func () {
362+ focussedGraph = index
363+ getGraphs ()
364+ mainView .AddItem (getLegends (graphs [index ].Query .PromQL , graphs [index ].Labels , graphs [index ].Legends , graphs [index ].Data ), len (graphs [index ].Legends )+ 3 , 0 , false )
365+ })
366+ } else {
367+ graphs [index ].Plot .SetFocusFunc (func () {
368+ getGraphs ()
369+ })
355370 }
356371
357372 if graphs [index ].Data != nil {
@@ -371,13 +386,20 @@ func appendMetrics(query *Query, matrix *Matrix, index int) {
371386 graphs [index ].Query = * query
372387
373388 // then update data
389+ updateData (matrix , index )
390+ }
391+
392+ func updateData (matrix * Matrix , index int ) {
374393 if len (* matrix ) > 0 {
375394 if len (* matrix ) > len (colors ) {
376395 * matrix = (* matrix )[:len (colors )] // avoid color indexing crash
377396 }
378397 labels := []string {}
379398 legends := make ([]map [string ]string , len (* matrix ))
380399 data := make ([][]float64 , len (* matrix ))
400+ timestamps := []pmod.Time {}
401+ maxValuesLen := 0
402+
381403 for i , s := range * matrix {
382404 legends [i ] = map [string ]string {}
383405 for k , v := range s .Metric {
@@ -386,11 +408,41 @@ func appendMetrics(query *Query, matrix *Matrix, index int) {
386408 }
387409 legends [i ][string (k )] = string (v )
388410 }
411+ if len (s .Values ) > maxValuesLen {
412+ maxValuesLen = len (s .Values )
413+ for _ , v := range s .Values {
414+ // keep the timestamps to compare all series on the same basis
415+ timestamps = append (timestamps , v .Timestamp )
416+ }
417+ }
418+ }
419+
420+ if maxValuesLen > 0 {
421+ for i , s := range * matrix {
422+ start := 0
423+ if maxValuesLen - len (s .Values ) > 0 {
424+ for j , t := range timestamps {
425+ // set start index to the closest matching timestamp
426+ if t .Equal (s .Values [0 ].Timestamp ) || t .After (s .Values [0 ].Timestamp ) {
427+ start = j
428+ break
429+ }
430+ }
389431
390- data [i ] = make ([]float64 , len (s .Values ))
391- for j , v := range s .Values {
392- data [i ][j ] = float64 (v .Value )
432+ if start == 0 {
433+ break // no matching timestamp found, skip this series
434+ }
435+ }
436+
437+ data [i ] = make ([]float64 , maxValuesLen )
438+ for j , v := range s .Values {
439+ if start + j < len (data [i ]) {
440+ data [i ][start + j ] = float64 (v .Value )
441+ }
442+ }
393443 }
444+ } else {
445+ graphs [index ].Data = [][]float64 {}
394446 }
395447
396448 sort .Strings (labels )
0 commit comments