github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/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  	)
    34  	allWarnings = append(allWarnings, warnings...)
    35  
    36  	if err != nil {
    37  		return nil, allWarnings, err
    38  	}
    39  
    40  	var events []Event
    41  	for _, ccEvent := range ccEvents {
    42  		events = append(events, Event{
    43  			GUID:        ccEvent.GUID,
    44  			Time:        ccEvent.CreatedAt,
    45  			Type:        ccEvent.Type,
    46  			ActorName:   ccEvent.ActorName,
    47  			Description: generateDescription(ccEvent.Data),
    48  		})
    49  
    50  	}
    51  
    52  	return events, allWarnings, nil
    53  }
    54  
    55  var knownMetadataKeys = []string{
    56  	"index",
    57  	"reason",
    58  	"cell_id",
    59  	"instance",
    60  	"exit_description",
    61  	"exit_status",
    62  	"recursive",
    63  	"disk_quota",
    64  	"instances",
    65  	"memory",
    66  	"state",
    67  	"command",
    68  	"environment_json",
    69  }
    70  
    71  func generateDescription(data map[string]interface{}) string {
    72  	mappedData := generic.NewMap(data)
    73  	if mappedData.Has("request") {
    74  		mappedData = generic.NewMap(mappedData.Get("request"))
    75  	}
    76  	return formatDescription(mappedData, knownMetadataKeys)
    77  }
    78  
    79  func formatDescription(metadata generic.Map, keys []string) string {
    80  	parts := []string{}
    81  	for _, key := range keys {
    82  		value := metadata.Get(key)
    83  		if value != nil {
    84  			parts = append(parts, fmt.Sprintf("%s: %s", key, formatDescriptionPart(value)))
    85  		}
    86  	}
    87  	return strings.Join(parts, ", ")
    88  }
    89  
    90  func formatDescriptionPart(val interface{}) string {
    91  	switch val := val.(type) {
    92  	case string:
    93  		return val
    94  	case float64:
    95  		return strconv.FormatFloat(val, byte('f'), -1, 64)
    96  	case bool:
    97  		if val {
    98  			return "true"
    99  		}
   100  		return "false"
   101  	default:
   102  		return fmt.Sprintf("%s", val)
   103  	}
   104  }