github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/application_view.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 view
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/gdamore/tcell/v2"
    24  
    25  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    26  	"github.com/oam-dev/kubevela/references/cli/top/component"
    27  	"github.com/oam-dev/kubevela/references/cli/top/model"
    28  )
    29  
    30  // ApplicationView is the application view, this view display info of application of KubeVela
    31  type ApplicationView struct {
    32  	*CommonResourceView
    33  	ctx context.Context
    34  }
    35  
    36  // Name return application view name
    37  func (v *ApplicationView) Name() string {
    38  	return "Application"
    39  }
    40  
    41  // Init the application view
    42  func (v *ApplicationView) Init() {
    43  	v.CommonResourceView.Init()
    44  	v.SetTitle(fmt.Sprintf("[ %s ]", v.Title())).SetTitleColor(v.app.config.Theme.Table.Title.Color())
    45  	v.bindKeys()
    46  }
    47  
    48  // Start the application view
    49  func (v *ApplicationView) Start() {
    50  	v.Clear()
    51  	v.Update(func() {})
    52  	v.CommonResourceView.AutoRefresh(v.Update)
    53  }
    54  
    55  // Stop the application view
    56  func (v *ApplicationView) Stop() {
    57  	v.CommonResourceView.Stop()
    58  }
    59  
    60  // Hint return key action menu hints of the application view
    61  func (v *ApplicationView) Hint() []model.MenuHint {
    62  	return v.Actions().Hint()
    63  }
    64  
    65  // InitView return a new application view
    66  func (v *ApplicationView) InitView(ctx context.Context, app *App) {
    67  	v.ctx = ctx
    68  	if v.CommonResourceView == nil {
    69  		v.CommonResourceView = NewCommonView(app)
    70  	}
    71  }
    72  
    73  // Refresh the view content
    74  func (v *ApplicationView) Refresh(_ *tcell.EventKey) *tcell.EventKey {
    75  	v.CommonResourceView.Refresh(true, v.Update)
    76  	return nil
    77  }
    78  
    79  // Update refresh the content of body of view
    80  func (v *ApplicationView) Update(timeoutCancel func()) {
    81  	v.BuildHeader()
    82  	v.BuildBody()
    83  	timeoutCancel()
    84  }
    85  
    86  // BuildHeader render the header of table
    87  func (v *ApplicationView) BuildHeader() {
    88  	header := []string{"Name", "Namespace", "Phase", "WorkflowMode", "Workflow", "Service", "CreateTime"}
    89  	v.CommonResourceView.BuildHeader(header)
    90  }
    91  
    92  // BuildBody render the body of table
    93  func (v *ApplicationView) BuildBody() {
    94  	apps, err := model.ListApplications(v.ctx, v.app.client)
    95  	if err != nil {
    96  		return
    97  	}
    98  	appInfos := apps.ToTableBody()
    99  	v.CommonResourceView.BuildBody(appInfos)
   100  	rowNum := len(appInfos)
   101  	v.ColorizeStatusText(rowNum)
   102  }
   103  
   104  // ColorizeStatusText colorize the status column text
   105  func (v *ApplicationView) ColorizeStatusText(rowNum int) {
   106  	for i := 0; i < rowNum; i++ {
   107  		status := v.Table.GetCell(i+1, 2).Text
   108  		highlightColor := v.app.config.Theme.Table.Body.String()
   109  
   110  		switch common.ApplicationPhase(status) {
   111  		case common.ApplicationStarting:
   112  			highlightColor = v.app.config.Theme.Status.Starting.String()
   113  		case common.ApplicationRendering, common.ApplicationPolicyGenerating, common.ApplicationRunningWorkflow, common.ApplicationWorkflowSuspending:
   114  			highlightColor = v.app.config.Theme.Status.Waiting.String()
   115  		case common.ApplicationUnhealthy, common.ApplicationWorkflowTerminated, common.ApplicationWorkflowFailed, common.ApplicationDeleting:
   116  			highlightColor = v.app.config.Theme.Status.Failed.String()
   117  		case common.ApplicationRunning:
   118  			highlightColor = v.app.config.Theme.Status.Healthy.String()
   119  		default:
   120  		}
   121  		v.Table.GetCell(i+1, 2).SetText(fmt.Sprintf("[%s::]%s", highlightColor, status))
   122  	}
   123  }
   124  
   125  // Title return table title of application view
   126  func (v *ApplicationView) Title() string {
   127  	namespace := v.ctx.Value(&model.CtxKeyNamespace).(string)
   128  	if namespace == "" {
   129  		namespace = "all"
   130  	}
   131  	return fmt.Sprintf("Application"+" (%s)", namespace)
   132  }
   133  
   134  func (v *ApplicationView) bindKeys() {
   135  	v.Actions().Delete([]tcell.Key{tcell.KeyEnter})
   136  	v.Actions().Add(model.KeyActions{
   137  		tcell.KeyESC:   model.KeyAction{Description: "Exit", Action: v.app.Exist, Visible: true, Shared: true},
   138  		tcell.KeyEnter: model.KeyAction{Description: "Managed Resource", Action: v.managedResourceView, Visible: true, Shared: true},
   139  		component.KeyN: model.KeyAction{Description: "Select Namespace", Action: v.namespaceView, Visible: true, Shared: true},
   140  		component.KeyY: model.KeyAction{Description: "Yaml", Action: v.yamlView, Visible: true, Shared: true},
   141  		component.KeyR: model.KeyAction{Description: "Refresh", Action: v.Refresh, Visible: true, Shared: true},
   142  		component.KeyT: model.KeyAction{Description: "Topology", Action: v.topologyView, Visible: true, Shared: true},
   143  	})
   144  }
   145  
   146  func (v *ApplicationView) managedResourceView(event *tcell.EventKey) *tcell.EventKey {
   147  	row, _ := v.GetSelection()
   148  	if row == 0 {
   149  		return event
   150  	}
   151  
   152  	name, namespace := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text
   153  	ctx := context.WithValue(v.ctx, &model.CtxKeyAppName, name)
   154  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, namespace)
   155  	v.app.command.run(ctx, "resource")
   156  	return event
   157  }
   158  
   159  func (v *ApplicationView) namespaceView(event *tcell.EventKey) *tcell.EventKey {
   160  	v.app.content.Clear()
   161  	v.app.command.run(v.ctx, "ns")
   162  	return event
   163  }
   164  
   165  func (v *ApplicationView) yamlView(event *tcell.EventKey) *tcell.EventKey {
   166  	row, _ := v.GetSelection()
   167  	if row == 0 {
   168  		return event
   169  	}
   170  	name, namespace := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text
   171  	gvr := model.GVR{
   172  		GV: "core.oam.dev/v1beta1",
   173  		R: model.Resource{
   174  			Kind:      "Application",
   175  			Name:      name,
   176  			Namespace: namespace,
   177  		},
   178  	}
   179  	ctx := context.WithValue(v.app.ctx, &model.CtxKeyGVR, &gvr)
   180  	v.app.command.run(ctx, "yaml")
   181  	return nil
   182  }
   183  
   184  func (v *ApplicationView) topologyView(event *tcell.EventKey) *tcell.EventKey {
   185  	row, _ := v.GetSelection()
   186  	if row == 0 {
   187  		return event
   188  	}
   189  	name, namespace := v.GetCell(row, 0).Text, v.GetCell(row, 1).Text
   190  
   191  	ctx := context.WithValue(context.Background(), &model.CtxKeyAppName, name)
   192  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, namespace)
   193  
   194  	v.app.command.run(ctx, "topology")
   195  	return nil
   196  }