github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/event.go (about)

     1  package v7action
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/util/generic"
    11  )
    12  
    13  type Event struct {
    14  	GUID        string
    15  	Time        time.Time
    16  	Type        string
    17  	ActorName   string
    18  	Description string
    19  }
    20  
    21  func (actor Actor) GetRecentEventsByApplicationNameAndSpace(appName string, spaceGUID string) ([]Event, Warnings, error) {
    22  	var allWarnings Warnings
    23  
    24  	app, appWarnings, appErr := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    25  	allWarnings = append(allWarnings, appWarnings...)
    26  	if appErr != nil {
    27  		return nil, allWarnings, appErr
    28  	}
    29  
    30  	ccEvents, warnings, err := actor.CloudControllerClient.GetEvents(
    31  		ccv3.Query{Key: ccv3.TargetGUIDFilter, Values: []string{app.GUID}},
    32  		ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.CreatedAtDescendingOrder}},
    33  		ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
    34  	)
    35  	allWarnings = append(allWarnings, warnings...)
    36  
    37  	if err != nil {
    38  		return nil, allWarnings, err
    39  	}
    40  
    41  	var events []Event
    42  	for _, ccEvent := range ccEvents {
    43  		events = append(events, Event{
    44  			GUID:        ccEvent.GUID,
    45  			Time:        ccEvent.CreatedAt,
    46  			Type:        ccEvent.Type,
    47  			ActorName:   ccEvent.ActorName,
    48  			Description: generateDescription(ccEvent.Data),
    49  		})
    50  
    51  	}
    52  
    53  	return events, allWarnings, nil
    54  }
    55  
    56  var knownMetadataKeys = []string{
    57  	"index",
    58  	"reason",
    59  	"cell_id",
    60  	"instance",
    61  	"exit_description",
    62  	"exit_status",
    63  	"recursive",
    64  	"disk_quota",
    65  	"instances",
    66  	"memory",
    67  	"state",
    68  	"command",
    69  	"environment_json",
    70  }
    71  
    72  func generateDescription(data map[string]interface{}) string {
    73  	mappedData := generic.NewMap(data)
    74  	if mappedData.Has("request") {
    75  		mappedData = generic.NewMap(mappedData.Get("request"))
    76  	}
    77  	return formatDescription(mappedData, knownMetadataKeys)
    78  }
    79  
    80  func formatDescription(metadata generic.Map, keys []string) string {
    81  	parts := []string{}
    82  	for _, key := range keys {
    83  		value := metadata.Get(key)
    84  		if value != nil {
    85  			parts = append(parts, fmt.Sprintf("%s: %s", key, formatDescriptionPart(value)))
    86  		}
    87  	}
    88  	return strings.Join(parts, ", ")
    89  }
    90  
    91  func formatDescriptionPart(val interface{}) string {
    92  	switch val := val.(type) {
    93  	case string:
    94  		return val
    95  	case float64:
    96  		return strconv.FormatFloat(val, byte('f'), -1, 64)
    97  	case bool:
    98  		if val {
    99  			return "true"
   100  		}
   101  		return "false"
   102  	default:
   103  		return fmt.Sprintf("%s", val)
   104  	}
   105  }