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