Skip to content

Commit 31ad79f

Browse files
[feat]: [CI-12621]: Use Map as source of truth for cache metrics (#24)
* [feat]: [CI-12621]: Use Map as source of truth for cache metrics * Update errored to error
1 parent cd942a3 commit 31ad79f

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

cachemetrics.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111

1212
type (
1313
LayerStatus struct {
14-
Status string
15-
Time float64 // Time in seconds; only set for DONE layers
14+
Status string `json:"status"`
15+
Time float64 `json:"time"` // Time in seconds; only set for DONE layers
1616
}
1717

1818
CacheMetrics struct {
1919
TotalLayers int `json:"total_layers"`
2020
Done int `json:"done"`
2121
Cached int `json:"cached"`
22-
Errored int `json:"errored"`
22+
Error int `json:"error"`
2323
Canceled int `json:"canceled"`
2424
Layers map[int]LayerStatus `json:"layers"`
2525
}
@@ -29,7 +29,7 @@ func parseCacheMetrics(ch <-chan string) (CacheMetrics, error) {
2929
var cacheMetrics CacheMetrics
3030
cacheMetrics.Layers = make(map[int]LayerStatus) // Initialize the map
3131

32-
re := regexp.MustCompile(`#(\d+) (DONE|CACHED|ERRORED|CANCELED)(?: ([0-9.]+)s)?`)
32+
re := regexp.MustCompile(`#(\d+) (DONE|CACHED|ERROR|CANCELED)(?: ([0-9.]+)s)?`)
3333

3434
for line := range ch {
3535
matches := re.FindAllStringSubmatch(line, -1)
@@ -44,26 +44,30 @@ func parseCacheMetrics(ch <-chan string) (CacheMetrics, error) {
4444
status := match[2]
4545
layerStatus := LayerStatus{Status: status}
4646

47-
switch status {
48-
case "DONE":
49-
cacheMetrics.Done++
50-
if len(match) == 4 && match[3] != "" {
51-
if duration, err := strconv.ParseFloat(match[3], 64); err == nil {
52-
layerStatus.Time = duration
53-
}
47+
if status == "DONE" && len(match) == 4 && match[3] != "" {
48+
if duration, err := strconv.ParseFloat(match[3], 64); err == nil {
49+
layerStatus.Time = duration
5450
}
55-
case "CACHED":
56-
cacheMetrics.Cached++
57-
case "ERRORED":
58-
cacheMetrics.Errored++
59-
case "CANCELED":
60-
cacheMetrics.Canceled++
6151
}
6252
cacheMetrics.Layers[layerIndex] = layerStatus
6353
}
6454
}
6555

66-
cacheMetrics.TotalLayers = cacheMetrics.Done + cacheMetrics.Cached + cacheMetrics.Errored + cacheMetrics.Canceled
56+
// Count the number of each status in the Layers map
57+
for _, layerStatus := range cacheMetrics.Layers {
58+
switch layerStatus.Status {
59+
case "DONE":
60+
cacheMetrics.Done++
61+
case "CACHED":
62+
cacheMetrics.Cached++
63+
case "ERROR":
64+
cacheMetrics.Error++
65+
case "CANCELED":
66+
cacheMetrics.Canceled++
67+
}
68+
}
69+
70+
cacheMetrics.TotalLayers = cacheMetrics.Done + cacheMetrics.Cached + cacheMetrics.Error + cacheMetrics.Canceled
6771

6872
return cacheMetrics, nil
6973
}

cachemetrics_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ func TestParseCacheMetrics(t *testing.T) {
2020
{
2121
name: "valid metrics with multiple on same line",
2222
input: []string{
23-
"#1 DONE 0.5s #2 CACHED #3 ERRORED #4 CANCELED",
23+
"#1 DONE 0.5s #1 DONE 0.6s #2 CACHED #3 ERROR #4 CANCELED",
2424
"#5 DONE 1.0s #6 CACHED",
2525
},
2626
expected: CacheMetrics{
2727
Layers: map[int]LayerStatus{
28-
1: {Status: "DONE", Time: 0.5},
28+
1: {Status: "DONE", Time: 0.6},
2929
2: {Status: "CACHED"},
30-
3: {Status: "ERRORED"},
30+
3: {Status: "ERROR"},
3131
4: {Status: "CANCELED"},
3232
5: {Status: "DONE", Time: 1.0},
3333
6: {Status: "CACHED"},
3434
},
3535
Done: 2,
3636
Cached: 2,
37-
Errored: 1,
37+
Error: 1,
3838
Canceled: 1,
3939
TotalLayers: 6,
4040
},
@@ -68,13 +68,13 @@ func TestParseCacheMetrics(t *testing.T) {
6868
1: {Status: "DONE", Time: 1.2},
6969
2: {Status: "DONE", Time: 0.8},
7070
3: {Status: "CACHED"},
71-
4: {Status: "ERRORED"},
71+
4: {Status: "ERROR"},
7272
5: {Status: "CANCELED"},
7373
6: {Status: "DONE"},
7474
},
7575
Done: 3,
7676
Cached: 1,
77-
Errored: 1,
77+
Error: 1,
7878
Canceled: 1,
7979
TotalLayers: 6,
8080
},

0 commit comments

Comments
 (0)