github.com/wrgl/wrgl@v0.14.0/pkg/widgets/prof/stat_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 "math" 9 10 "github.com/rivo/tview" 11 "github.com/wrgl/wrgl/pkg/objects" 12 "github.com/wrgl/wrgl/pkg/widgets" 13 ) 14 15 type StatCells interface { 16 Name() string 17 NumRows(colProf *objects.ColumnProfile) int 18 NumColumns() int 19 DecorateCells(row int, tblProf *objects.TableProfile, colProf *objects.ColumnProfile, cells []*widgets.TableCell) 20 } 21 22 type singleStatCells struct { 23 name string 24 cellText func(colProf *objects.ColumnProfile) string 25 } 26 27 func (c *singleStatCells) Name() string { 28 return c.name 29 } 30 31 func (c *singleStatCells) NumRows(colProf *objects.ColumnProfile) int { 32 if c.cellText(colProf) == "" { 33 return 0 34 } 35 return 1 36 } 37 38 func (c *singleStatCells) NumColumns() int { 39 return 1 40 } 41 42 func (c *singleStatCells) DecorateCells(row int, tblProf *objects.TableProfile, colProf *objects.ColumnProfile, cells []*widgets.TableCell) { 43 cells[0].SetText(c.cellText(colProf)).SetStyle(cellStyle) 44 } 45 46 func newSingleStatCells(name string, cellText func(colProf *objects.ColumnProfile) string) *singleStatCells { 47 return &singleStatCells{ 48 name: name, 49 cellText: cellText, 50 } 51 } 52 53 type topValuesCells struct { 54 name string 55 values func(colProf *objects.ColumnProfile) objects.ValueCounts 56 } 57 58 func (c *topValuesCells) Name() string { 59 return c.name 60 } 61 62 func (c *topValuesCells) NumRows(colProf *objects.ColumnProfile) int { 63 v := c.values(colProf) 64 if v == nil { 65 return 0 66 } 67 return v.Len() 68 } 69 70 func (c *topValuesCells) NumColumns() int { 71 return 3 72 } 73 74 func (c *topValuesCells) DecorateCells(row int, tblProf *objects.TableProfile, colProf *objects.ColumnProfile, cells []*widgets.TableCell) { 75 values := c.values(colProf) 76 v := values[row] 77 cells[0].SetText(v.Value). 78 SetStyle(statValueStyle) 79 cells[1].SetText(fmt.Sprintf("%d", v.Count)). 80 SetStyle(cellStyle) 81 pct := byte(math.Round(float64(v.Count) / float64(tblProf.RowsCount) * 100)) 82 cells[2].SetText(fmt.Sprintf("%3d%%", pct)). 83 SetStyle(pctStyle) 84 } 85 86 func newTopValuesCells(name string, values func(colProf *objects.ColumnProfile) objects.ValueCounts) *topValuesCells { 87 return &topValuesCells{ 88 name: name, 89 values: values, 90 } 91 } 92 93 type percentilesCells struct { 94 name string 95 values func(colProf *objects.ColumnProfile) []float64 96 } 97 98 func (c *percentilesCells) Name() string { 99 return c.name 100 } 101 102 func (c *percentilesCells) NumRows(colProf *objects.ColumnProfile) int { 103 v := c.values(colProf) 104 if v == nil { 105 return 0 106 } 107 return len(v) 108 } 109 110 func (c *percentilesCells) NumColumns() int { 111 return 2 112 } 113 114 func (c *percentilesCells) DecorateCells(row int, tblProf *objects.TableProfile, colProf *objects.ColumnProfile, cells []*widgets.TableCell) { 115 values := c.values(colProf) 116 v := values[row] 117 cells[0].SetText(fmt.Sprintf("%d", (row+1)*100/(len(values)+1))). 118 SetAlign(tview.AlignRight). 119 SetStyle(statValueStyle) 120 cells[1].SetText(floatString(v)). 121 SetStyle(cellStyle) 122 } 123 124 func newPercentilesCells(name string, values func(colProf *objects.ColumnProfile) []float64) *percentilesCells { 125 return &percentilesCells{ 126 name: name, 127 values: values, 128 } 129 }