forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
110 lines (99 loc) · 2.49 KB
/
events.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
105
106
107
108
109
110
package resources
import (
"fmt"
"strconv"
"strings"
"time"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/util/generic"
)
type EventResource interface {
ToFields() models.EventFields
}
type EventResourceNewV2 struct {
Resource
Entity struct {
Timestamp time.Time
Type string
Actor string `json:"actor"`
ActorName string `json:"actor_name"`
Metadata map[string]interface{}
}
}
type EventResourceOldV2 struct {
Resource
Entity struct {
Timestamp time.Time
ExitDescription string `json:"exit_description"`
ExitStatus int `json:"exit_status"`
InstanceIndex int `json:"instance_index"`
}
}
func (resource EventResourceNewV2) ToFields() models.EventFields {
metadata := generic.NewMap(resource.Entity.Metadata)
if metadata.Has("request") {
metadata = generic.NewMap(metadata.Get("request"))
}
return models.EventFields{
GUID: resource.Metadata.GUID,
Name: resource.Entity.Type,
Timestamp: resource.Entity.Timestamp,
Description: formatDescription(metadata, knownMetadataKeys),
Actor: resource.Entity.Actor,
ActorName: resource.Entity.ActorName,
}
}
func (resource EventResourceOldV2) ToFields() models.EventFields {
return models.EventFields{
GUID: resource.Metadata.GUID,
Name: T("app crashed"),
Timestamp: resource.Entity.Timestamp,
Description: fmt.Sprintf(T("instance: {{.InstanceIndex}}, reason: {{.ExitDescription}}, exit_status: {{.ExitStatus}}",
map[string]interface{}{
"InstanceIndex": resource.Entity.InstanceIndex,
"ExitDescription": resource.Entity.ExitDescription,
"ExitStatus": strconv.Itoa(resource.Entity.ExitStatus),
})),
}
}
var knownMetadataKeys = []string{
"index",
"reason",
"cell_id",
"instance",
"exit_description",
"exit_status",
"recursive",
"disk_quota",
"instances",
"memory",
"state",
"command",
"environment_json",
}
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)
}
}