github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/process_summary.go (about)

     1  package v7action
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  // ProcessSummary represents a process with instance details.
    13  type ProcessSummary struct {
    14  	Process
    15  
    16  	Sidecars []Sidecar
    17  
    18  	InstanceDetails []ProcessInstance
    19  }
    20  
    21  type ProcessSummaries []ProcessSummary
    22  
    23  func (p ProcessSummary) TotalInstanceCount() int {
    24  	return len(p.InstanceDetails)
    25  }
    26  
    27  func (p ProcessSummary) HealthyInstanceCount() int {
    28  	count := 0
    29  	for _, instance := range p.InstanceDetails {
    30  		if instance.State == constant.ProcessInstanceRunning {
    31  			count++
    32  		}
    33  	}
    34  	return count
    35  }
    36  
    37  func (ps ProcessSummaries) Sort() {
    38  	sort.Slice(ps, func(i int, j int) bool {
    39  		var iScore int
    40  		var jScore int
    41  
    42  		switch ps[i].Type {
    43  		case constant.ProcessTypeWeb:
    44  			iScore = 0
    45  		default:
    46  			iScore = 1
    47  		}
    48  
    49  		switch ps[j].Type {
    50  		case constant.ProcessTypeWeb:
    51  			jScore = 0
    52  		default:
    53  			jScore = 1
    54  		}
    55  
    56  		if iScore == 1 && jScore == 1 {
    57  			return ps[i].Type < ps[j].Type
    58  		}
    59  		return iScore < jScore
    60  	})
    61  }
    62  
    63  func (ps ProcessSummaries) String() string {
    64  	ps.Sort()
    65  
    66  	var summaries []string
    67  	for _, p := range ps {
    68  		summaries = append(summaries, fmt.Sprintf("%s:%d/%d", p.Type, p.HealthyInstanceCount(), p.TotalInstanceCount()))
    69  	}
    70  
    71  	return strings.Join(summaries, ", ")
    72  }
    73  
    74  func (actor Actor) getProcessSummariesForApp(appGUID string, withObfuscatedValues bool) (ProcessSummaries, Warnings, error) {
    75  	log.WithFields(log.Fields{
    76  		"appGUID":              appGUID,
    77  		"withObfuscatedValues": withObfuscatedValues,
    78  	}).Info("retrieving process information")
    79  
    80  	ccv3Processes, warnings, err := actor.CloudControllerClient.GetApplicationProcesses(appGUID)
    81  	allWarnings := Warnings(warnings)
    82  	if err != nil {
    83  		return nil, allWarnings, err
    84  	}
    85  
    86  	var processSummaries ProcessSummaries
    87  	for _, ccv3Process := range ccv3Processes {
    88  		process := Process(ccv3Process)
    89  		if withObfuscatedValues {
    90  			fullProcess, warnings, err := actor.GetProcess(ccv3Process.GUID)
    91  			allWarnings = append(allWarnings, warnings...)
    92  			if err != nil {
    93  				return nil, allWarnings, err
    94  			}
    95  			process = fullProcess
    96  		}
    97  
    98  		processSummary, warnings, err := actor.getProcessSummary(process)
    99  		allWarnings = append(allWarnings, warnings...)
   100  		if err != nil {
   101  			return nil, allWarnings, err
   102  		}
   103  
   104  		processSummaries = append(processSummaries, processSummary)
   105  	}
   106  	processSummaries.Sort()
   107  
   108  	return processSummaries, allWarnings, nil
   109  }
   110  
   111  func (actor Actor) getProcessSummary(process Process) (ProcessSummary, Warnings, error) {
   112  	sidecars, warnings, err := actor.CloudControllerClient.GetProcessSidecars(process.GUID)
   113  	allWarnings := Warnings(warnings)
   114  	if err != nil {
   115  		return ProcessSummary{}, allWarnings, err
   116  	}
   117  
   118  	instances, warnings, err := actor.CloudControllerClient.GetProcessInstances(process.GUID)
   119  	allWarnings = append(allWarnings, Warnings(warnings)...)
   120  	if err != nil {
   121  		return ProcessSummary{}, allWarnings, err
   122  	}
   123  
   124  	processSummary := ProcessSummary{
   125  		Process: process,
   126  	}
   127  	for _, sidecar := range sidecars {
   128  		processSummary.Sidecars = append(processSummary.Sidecars, Sidecar(sidecar))
   129  	}
   130  	for _, instance := range instances {
   131  		processSummary.InstanceDetails = append(processSummary.InstanceDetails, ProcessInstance(instance))
   132  	}
   133  
   134  	return processSummary, allWarnings, nil
   135  }