This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
crash_count.go
80 lines (66 loc) · 2.42 KB
/
crash_count.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
package store
import (
"fmt"
"strconv"
"time"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry/hm9000/models"
"github.com/cloudfoundry/storeadapter"
)
func (store *RealStore) crashCountStoreKey(crashCount models.CrashCount) string {
return store.SchemaRoot() + "/apps/crashes/" + store.AppKey(crashCount.AppGuid, crashCount.AppVersion) + "/" + strconv.Itoa(crashCount.InstanceIndex)
}
func (store *RealStore) SaveCrashCounts(crashCounts ...models.CrashCount) error {
t := time.Now()
nodes := make([]storeadapter.StoreNode, len(crashCounts))
for i, crashCount := range crashCounts {
nodes[i] = storeadapter.StoreNode{
Key: store.crashCountStoreKey(crashCount),
Value: crashCount.ToJSON(),
TTL: uint64(store.config.MaximumBackoffDelay().Seconds()) * 2,
}
}
err := store.adapter.SetMulti(nodes)
store.logger.Debug(fmt.Sprintf("Save Duration Crash Counts"), lager.Data{
"Number of Items": fmt.Sprintf("%d", len(crashCounts)),
"Duration": fmt.Sprintf("%.4f seconds", time.Since(t).Seconds()),
})
return err
}
func (store *RealStore) getCrashCounts() (results []models.CrashCount, err error) {
node, err := store.adapter.ListRecursively(store.SchemaRoot() + "/apps/crashes")
if err != nil {
if storeErr, ok := err.(storeadapter.Error); ok && storeErr.Type() == storeadapter.ErrorKeyNotFound {
return results, nil
}
return results, err
}
for _, crashNode := range node.ChildNodes {
crashCounts, err := store.crashCountsForNode(crashNode)
if err != nil {
return []models.CrashCount{}, nil
}
results = append(results, crashCounts...)
}
return results, nil
}
func (store *RealStore) getCrashCountForApp(appGuid string, appVersion string) (results []models.CrashCount, err error) {
node, err := store.adapter.ListRecursively(store.SchemaRoot() + "/apps/crashes/" + store.AppKey(appGuid, appVersion))
if err != nil {
if storeErr, ok := err.(storeadapter.Error); ok && storeErr.Type() == storeadapter.ErrorKeyNotFound {
return []models.CrashCount{}, nil
}
return []models.CrashCount{}, err
}
return store.crashCountsForNode(node)
}
func (store *RealStore) crashCountsForNode(node storeadapter.StoreNode) (results []models.CrashCount, err error) {
for _, crashNode := range node.ChildNodes {
crashCount, err := models.NewCrashCountFromJSON(crashNode.Value)
if err != nil {
return []models.CrashCount{}, err
}
results = append(results, crashCount)
}
return results, nil
}