github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/info.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package component
    18  
    19  import (
    20  	"github.com/rivo/tview"
    21  	"k8s.io/client-go/rest"
    22  	"sigs.k8s.io/controller-runtime/pkg/client"
    23  
    24  	"github.com/oam-dev/kubevela/references/cli/top/config"
    25  	"github.com/oam-dev/kubevela/references/cli/top/model"
    26  )
    27  
    28  // InfoBoard a component which display system info
    29  type InfoBoard struct {
    30  	*tview.Table
    31  	style *config.ThemeConfig
    32  }
    33  
    34  // NewInfo return an info component instance
    35  func NewInfo(config *config.ThemeConfig) *InfoBoard {
    36  	c := &InfoBoard{
    37  		Table: tview.NewTable(),
    38  		style: config,
    39  	}
    40  	return c
    41  }
    42  
    43  // Init info component init
    44  func (board *InfoBoard) Init(c client.Client, restConf *rest.Config) {
    45  	board.layout()
    46  	board.UpdateInfo(c, restConf)
    47  }
    48  
    49  func (board *InfoBoard) layout() {
    50  	titleColor := board.style.Info.Title.Color()
    51  	row := 0
    52  	board.SetCell(row, 0, board.sectionCell("Context").SetTextColor(titleColor))
    53  	board.SetCell(row, 2, infoCell("|").SetTextColor(titleColor))
    54  	row++
    55  
    56  	board.SetCell(row, 0, board.sectionCell("K8S Version").SetTextColor(titleColor))
    57  	board.SetCell(row, 2, infoCell("|").SetTextColor(titleColor))
    58  	row++
    59  
    60  	board.SetCell(row, 0, board.sectionCell("VelaCLI Version").SetTextColor(titleColor))
    61  	board.SetCell(row, 2, infoCell("|").SetTextColor(titleColor))
    62  	row++
    63  
    64  	board.SetCell(row, 0, board.sectionCell("VelaCore Version").SetTextColor(titleColor))
    65  	board.SetCell(row, 2, infoCell("|").SetTextColor(titleColor))
    66  	row++
    67  
    68  	board.SetCell(row, 0, board.sectionCell("Cluster Num").SetTextColor(titleColor))
    69  	board.SetCell(row, 2, infoCell("|").SetTextColor(titleColor))
    70  	row++
    71  
    72  	board.SetCell(row, 0, board.sectionCell("Running App Num").SetTextColor(titleColor))
    73  	board.SetCell(row, 2, infoCell("|").SetTextColor(titleColor))
    74  
    75  	row = 0
    76  	board.SetCell(row, 3, infoCell("").SetTextColor(titleColor))
    77  	board.SetCell(row, 4, infoCell("VelaCore").SetTextColor(titleColor))
    78  	board.SetCell(row, 5, infoCell("Gateway").SetTextColor(titleColor))
    79  	board.SetCell(row, 6, infoCell("|").SetTextColor(titleColor))
    80  	row++
    81  
    82  	board.SetCell(row, 3, infoCell("").SetTextColor(titleColor))
    83  	board.SetCell(row, 4, infoCell("").SetTextColor(titleColor))
    84  	board.SetCell(row, 5, infoCell("").SetTextColor(titleColor))
    85  	board.SetCell(row, 6, infoCell("|").SetTextColor(titleColor))
    86  	row++
    87  
    88  	board.SetCell(row, 3, board.sectionCell("CPU Requests").SetTextColor(titleColor))
    89  	board.SetCell(row, 6, infoCell("|").SetTextColor(titleColor))
    90  	row++
    91  
    92  	board.SetCell(row, 3, board.sectionCell("CPU Limits").SetTextColor(titleColor))
    93  	board.SetCell(row, 6, infoCell("|").SetTextColor(titleColor))
    94  	row++
    95  
    96  	board.SetCell(row, 3, board.sectionCell("MEM Requests").SetTextColor(titleColor))
    97  	board.SetCell(row, 6, infoCell("|").SetTextColor(titleColor))
    98  	row++
    99  
   100  	board.SetCell(row, 3, board.sectionCell("MEM Limits").SetTextColor(titleColor))
   101  	board.SetCell(row, 6, infoCell("|").SetTextColor(titleColor))
   102  }
   103  
   104  // UpdateInfo update the info of system info board
   105  func (board *InfoBoard) UpdateInfo(c client.Client, restConf *rest.Config) {
   106  	textColor := board.style.Info.Text.Color()
   107  	row := 0
   108  	info := model.NewInfo()
   109  	board.SetCell(row, 1, infoCell(info.CurrentContext()).SetTextColor(textColor))
   110  	row++
   111  
   112  	k8s := model.K8SVersion(restConf)
   113  	board.SetCell(row, 1, infoCell(k8s).SetTextColor(textColor))
   114  	row++
   115  
   116  	velaCLI := model.VelaCLIVersion()
   117  	board.SetCell(row, 1, infoCell(velaCLI).SetTextColor(textColor))
   118  	row++
   119  
   120  	velaCore := model.VelaCoreVersion()
   121  	board.SetCell(row, 1, infoCell(velaCore).SetTextColor(textColor))
   122  	row++
   123  
   124  	clusterNum := info.ClusterNum()
   125  	board.SetCell(row, 1, infoCell(clusterNum).SetTextColor(textColor))
   126  	row++
   127  
   128  	appNum := model.ApplicationRunningNum(restConf)
   129  	board.SetCell(row, 1, infoCell(appNum).SetTextColor(textColor))
   130  
   131  	velaCoreCPULimit, velaCoreMEMLimit, velaCoreCPURequest, velaCoreMEMRequest := model.VelaCoreRatio(c, restConf)
   132  	gatewayCPULimit, gatewayMEMLimit, gatewayCPURequest, gatewayMEMRequest := model.CLusterGatewayRatio(c, restConf)
   133  
   134  	row = 2
   135  
   136  	board.SetCell(row, 4, infoCell(velaCoreCPURequest).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   137  	board.SetCell(row, 5, infoCell(gatewayCPURequest).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   138  	row++
   139  
   140  	board.SetCell(row, 4, infoCell(velaCoreCPULimit).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   141  	board.SetCell(row, 5, infoCell(gatewayCPULimit).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   142  	row++
   143  
   144  	board.SetCell(row, 4, infoCell(velaCoreMEMRequest).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   145  	board.SetCell(row, 5, infoCell(gatewayMEMRequest).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   146  	row++
   147  
   148  	board.SetCell(row, 4, infoCell(velaCoreMEMLimit).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   149  	board.SetCell(row, 5, infoCell(gatewayMEMLimit).SetTextColor(textColor).SetAlign(tview.AlignCenter))
   150  }
   151  
   152  func (board *InfoBoard) sectionCell(t string) *tview.TableCell {
   153  	c := tview.NewTableCell(t + ":")
   154  	c.SetAlign(tview.AlignLeft)
   155  	return c
   156  }
   157  
   158  func infoCell(t string) *tview.TableCell {
   159  	c := tview.NewTableCell(t)
   160  	return c
   161  }