github.com/wrgl/wrgl@v0.14.0/pkg/widgets/prof/stat_diff_cells.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright © 2022 Wrangle Ltd
     3  
     4  package widgetsprof
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/rivo/tview"
    10  	diffprof "github.com/wrgl/wrgl/pkg/diff/prof"
    11  	"github.com/wrgl/wrgl/pkg/widgets"
    12  )
    13  
    14  type StatDiffCells interface {
    15  	Name() string
    16  	NumRows() int
    17  	NumColumns() int
    18  	DecorateCells(row int, cells []*widgets.TableCell)
    19  }
    20  
    21  type uint16StatDiffCells struct {
    22  	*diffprof.Uint16Stat
    23  }
    24  
    25  func (s *uint16StatDiffCells) Name() string {
    26  	return s.Uint16Stat.Name
    27  }
    28  
    29  func (s *uint16StatDiffCells) NumRows() int {
    30  	return 1
    31  }
    32  
    33  func (s *uint16StatDiffCells) NumColumns() int {
    34  	if s.Old == s.New {
    35  		return 1
    36  	}
    37  	return 2
    38  }
    39  
    40  func (s *uint16StatDiffCells) DecorateCells(row int, cells []*widgets.TableCell) {
    41  	if s.Old == s.New {
    42  		cells[0].SetText(fmt.Sprintf("%d", s.Old)).SetStyle(cellStyle)
    43  	} else {
    44  		if s.New != 0 {
    45  			cells[0].SetText(fmt.Sprintf("%d", s.New)).SetStyle(addedStyle)
    46  		}
    47  		if s.Old != 0 {
    48  			cells[1].SetText(fmt.Sprintf("%d", s.Old)).SetStyle(removedStyle)
    49  		}
    50  	}
    51  }
    52  
    53  type uint32StatDiffCells struct {
    54  	*diffprof.Uint32Stat
    55  }
    56  
    57  func (s *uint32StatDiffCells) Name() string {
    58  	return s.Uint32Stat.Name
    59  }
    60  
    61  func (s *uint32StatDiffCells) NumRows() int {
    62  	return 1
    63  }
    64  
    65  func (s *uint32StatDiffCells) NumColumns() int {
    66  	if s.Old == s.New {
    67  		return 1
    68  	}
    69  	return 2
    70  }
    71  
    72  func (s *uint32StatDiffCells) DecorateCells(row int, cells []*widgets.TableCell) {
    73  	if s.Old == s.New {
    74  		cells[0].SetText(fmt.Sprintf("%d", s.Old)).SetStyle(cellStyle)
    75  	} else {
    76  		if s.New != 0 {
    77  			cells[0].SetText(fmt.Sprintf("%d", s.New)).SetStyle(addedStyle)
    78  		}
    79  		if s.Old != 0 {
    80  			cells[1].SetText(fmt.Sprintf("%d", s.Old)).SetStyle(removedStyle)
    81  		}
    82  	}
    83  }
    84  
    85  type float64StatDiffCells struct {
    86  	*diffprof.Float64Stat
    87  }
    88  
    89  func (s *float64StatDiffCells) Name() string {
    90  	return s.Float64Stat.Name
    91  }
    92  
    93  func (s *float64StatDiffCells) NumRows() int {
    94  	return 1
    95  }
    96  
    97  func (s *float64StatDiffCells) NumColumns() int {
    98  	if s.Old == s.New {
    99  		return 1
   100  	}
   101  	return 2
   102  }
   103  
   104  func (s *float64StatDiffCells) DecorateCells(row int, cells []*widgets.TableCell) {
   105  	if s.Old == s.New {
   106  		cells[0].SetText(fmt.Sprintf("%d", s.Old)).SetStyle(cellStyle)
   107  	} else {
   108  		if s.New != nil {
   109  			cells[0].SetText(floatString(*s.New)).SetStyle(addedStyle)
   110  		}
   111  		if s.Old != nil {
   112  			cells[1].SetText(floatString(*s.Old)).SetStyle(removedStyle)
   113  		}
   114  	}
   115  }
   116  
   117  type topValuesStatDiffCells struct {
   118  	*diffprof.TopValuesStat
   119  }
   120  
   121  func (s *topValuesStatDiffCells) Name() string {
   122  	return s.TopValuesStat.Name
   123  }
   124  
   125  func (s *topValuesStatDiffCells) NumRows() int {
   126  	return len(s.TopValuesStat.Values)
   127  }
   128  
   129  func (s *topValuesStatDiffCells) NumColumns() int {
   130  	return 3
   131  }
   132  
   133  func (s *topValuesStatDiffCells) DecorateCells(row int, cells []*widgets.TableCell) {
   134  	v := s.Values[row]
   135  	cells[0].SetText(v.Value).
   136  		SetStyle(statValueStyle)
   137  	if v.NewCount == v.OldCount && v.NewPct == v.OldPct {
   138  		cells[1].SetText(fmt.Sprintf("%d %3d%%", v.NewCount, v.NewPct)).
   139  			SetAlign(tview.AlignRight).
   140  			SetStyle(cellStyle)
   141  	} else {
   142  		if v.NewCount != 0 {
   143  			cells[1].SetText(fmt.Sprintf("%d %3d%%", v.NewCount, v.NewPct)).
   144  				SetAlign(tview.AlignRight).
   145  				SetStyle(addedStyle)
   146  		}
   147  		if v.OldCount != 0 {
   148  			cells[2].SetText(fmt.Sprintf("%d %3d%%", v.OldCount, v.OldPct)).
   149  				SetAlign(tview.AlignRight).
   150  				SetStyle(removedStyle)
   151  		}
   152  	}
   153  }
   154  
   155  type percentilesStatDiffCells struct {
   156  	*diffprof.PercentilesStat
   157  }
   158  
   159  func (s *percentilesStatDiffCells) Name() string {
   160  	return s.PercentilesStat.Name
   161  }
   162  
   163  func (s *percentilesStatDiffCells) NumRows() int {
   164  	return len(s.PercentilesStat.Values)
   165  }
   166  
   167  func (s *percentilesStatDiffCells) NumColumns() int {
   168  	return 3
   169  }
   170  
   171  func (s *percentilesStatDiffCells) DecorateCells(row int, cells []*widgets.TableCell) {
   172  	v := s.Values[row]
   173  	cells[0].SetText(fmt.Sprintf("%d", (row+1)*100/(len(s.Values)+1))).
   174  		SetStyle(statValueStyle)
   175  	if v.New == v.Old {
   176  		cells[1].SetText(floatString(v.New)).
   177  			SetAlign(tview.AlignRight).
   178  			SetStyle(cellStyle)
   179  	} else {
   180  		if v.New != 0 {
   181  			cells[1].SetText(floatString(v.New)).
   182  				SetAlign(tview.AlignRight).
   183  				SetStyle(addedStyle)
   184  		}
   185  		if v.Old != 0 {
   186  			cells[2].SetText(floatString(v.Old)).
   187  				SetAlign(tview.AlignRight).
   188  				SetStyle(removedStyle)
   189  		}
   190  	}
   191  }