This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cacheattempts.go
67 lines (58 loc) · 1.93 KB
/
cacheattempts.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
package executor
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"time"
)
type CacheAttempts struct {
cacheRetrievalAttempts map[string]*api.CacheRetrievalAttempt
}
func NewCacheAttempts() *CacheAttempts {
return &CacheAttempts{
cacheRetrievalAttempts: make(map[string]*api.CacheRetrievalAttempt),
}
}
func (ca *CacheAttempts) Failed(key string, error string) {
ca.cacheRetrievalAttempts[key] = &api.CacheRetrievalAttempt{Error: error}
}
func (ca *CacheAttempts) Hit(key string, size uint64, downloadedIn, extractedIn time.Duration) {
ca.cacheRetrievalAttempts[key] = &api.CacheRetrievalAttempt{
Result: &api.CacheRetrievalAttempt_Hit_{
Hit: &api.CacheRetrievalAttempt_Hit{
SizeBytes: size,
DownloadedInNanos: uint64(downloadedIn.Nanoseconds()),
ExtractedInNanos: uint64(extractedIn.Nanoseconds()),
},
},
}
}
func (ca *CacheAttempts) PopulatedIn(key string, populatedIn time.Duration) {
ca.cacheRetrievalAttempts[key] = &api.CacheRetrievalAttempt{
Result: &api.CacheRetrievalAttempt_Miss_{
Miss: &api.CacheRetrievalAttempt_Miss{
PopulatedInNanos: uint64(populatedIn.Nanoseconds()),
},
},
}
}
func (ca *CacheAttempts) Miss(key string, size uint64, archivedIn, uploadedIn time.Duration) {
if attempt, ok := ca.cacheRetrievalAttempts[key]; ok {
if miss, ok := attempt.Result.(*api.CacheRetrievalAttempt_Miss_); ok {
miss.Miss.SizeBytes = size
miss.Miss.ArchivedInNanos = uint64(archivedIn.Nanoseconds())
miss.Miss.UploadedInNanos = uint64(uploadedIn.Nanoseconds())
return
}
}
ca.cacheRetrievalAttempts[key] = &api.CacheRetrievalAttempt{
Result: &api.CacheRetrievalAttempt_Miss_{
Miss: &api.CacheRetrievalAttempt_Miss{
SizeBytes: size,
ArchivedInNanos: uint64(archivedIn.Nanoseconds()),
UploadedInNanos: uint64(uploadedIn.Nanoseconds()),
},
},
}
}
func (ca *CacheAttempts) ToProto() map[string]*api.CacheRetrievalAttempt {
return ca.cacheRetrievalAttempts
}