github.com/loafoe/cli@v7.1.0+incompatible/actor/v3action/process_summary.go (about)

     1  package v3action
     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  	InstanceDetails []ProcessInstance
    17  }
    18  
    19  type ProcessSummaries []ProcessSummary
    20  
    21  func (p ProcessSummary) TotalInstanceCount() int {
    22  	return len(p.InstanceDetails)
    23  }
    24  
    25  func (p ProcessSummary) HealthyInstanceCount() int {
    26  	count := 0
    27  	for _, instance := range p.InstanceDetails {
    28  		if instance.State == constant.ProcessInstanceRunning {
    29  			count++
    30  		}
    31  	}
    32  	return count
    33  }
    34  
    35  func (ps ProcessSummaries) Sort() {
    36  	sort.Slice(ps, func(i int, j int) bool {
    37  		var iScore int
    38  		var jScore int
    39  
    40  		switch ps[i].Type {
    41  		case constant.ProcessTypeWeb:
    42  			iScore = 0
    43  		default:
    44  			iScore = 1
    45  		}
    46  
    47  		switch ps[j].Type {
    48  		case constant.ProcessTypeWeb:
    49  			jScore = 0
    50  		default:
    51  			jScore = 1
    52  		}
    53  
    54  		if iScore == 1 && jScore == 1 {
    55  			return ps[i].Type < ps[j].Type
    56  		}
    57  		return iScore < jScore
    58  	})
    59  }
    60  
    61  func (ps ProcessSummaries) String() string {
    62  	ps.Sort()
    63  
    64  	var summaries []string
    65  	for _, p := range ps {
    66  		summaries = append(summaries, fmt.Sprintf("%s:%d/%d", p.Type, p.HealthyInstanceCount(), p.TotalInstanceCount()))
    67  	}
    68  
    69  	return strings.Join(summaries, ", ")
    70  }
    71  
    72  func (actor Actor) getProcessSummariesForApp(appGUID string, withObfuscatedValues bool) (ProcessSummaries, Warnings, error) {
    73  	log.WithFields(log.Fields{
    74  		"appGUID":              appGUID,
    75  		"withObfuscatedValues": withObfuscatedValues,
    76  	}).Info("retieving process information")
    77  
    78  	ccv3Processes, warnings, err := actor.CloudControllerClient.GetApplicationProcesses(appGUID)
    79  	allWarnings := Warnings(warnings)
    80  	if err != nil {
    81  		return nil, allWarnings, err
    82  	}
    83  
    84  	var processSummaries ProcessSummaries
    85  	for _, ccv3Process := range ccv3Processes {
    86  		process := ccv3Process
    87  		if withObfuscatedValues {
    88  			fullProcess, warnings, err := actor.CloudControllerClient.GetApplicationProcessByType(appGUID, ccv3Process.Type)
    89  			allWarnings = append(allWarnings, warnings...)
    90  			if err != nil {
    91  				return nil, allWarnings, err
    92  			}
    93  			process = fullProcess
    94  		}
    95  
    96  		processSummary, warnings, err := actor.getProcessSummary(Process(process))
    97  		allWarnings = append(allWarnings, warnings...)
    98  		if err != nil {
    99  			return nil, allWarnings, err
   100  		}
   101  
   102  		processSummaries = append(processSummaries, processSummary)
   103  	}
   104  
   105  	return processSummaries, allWarnings, nil
   106  }
   107  
   108  func (actor Actor) getProcessSummary(process Process) (ProcessSummary, Warnings, error) {
   109  	instances, warnings, err := actor.CloudControllerClient.GetProcessInstances(process.GUID)
   110  	allWarnings := Warnings(warnings)
   111  	if err != nil {
   112  		return ProcessSummary{}, allWarnings, err
   113  	}
   114  
   115  	processSummary := ProcessSummary{
   116  		Process: process,
   117  	}
   118  	for _, instance := range instances {
   119  		processSummary.InstanceDetails = append(processSummary.InstanceDetails, ProcessInstance(instance))
   120  	}
   121  	return processSummary, allWarnings, nil
   122  }