Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions cachemetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (

type (
LayerStatus struct {
Status string
Time float64 // Time in seconds; only set for DONE layers
Status string `json:"status"`
Time float64 `json:"time"` // Time in seconds; only set for DONE layers
}

CacheMetrics struct {
TotalLayers int `json:"total_layers"`
Done int `json:"done"`
Cached int `json:"cached"`
Errored int `json:"errored"`
Error int `json:"error"`
Canceled int `json:"canceled"`
Layers map[int]LayerStatus `json:"layers"`
}
Expand All @@ -29,7 +29,7 @@ func parseCacheMetrics(ch <-chan string) (CacheMetrics, error) {
var cacheMetrics CacheMetrics
cacheMetrics.Layers = make(map[int]LayerStatus) // Initialize the map

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

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

switch status {
case "DONE":
cacheMetrics.Done++
if len(match) == 4 && match[3] != "" {
if duration, err := strconv.ParseFloat(match[3], 64); err == nil {
layerStatus.Time = duration
}
if status == "DONE" && len(match) == 4 && match[3] != "" {
if duration, err := strconv.ParseFloat(match[3], 64); err == nil {
layerStatus.Time = duration
}
case "CACHED":
cacheMetrics.Cached++
case "ERRORED":
cacheMetrics.Errored++
case "CANCELED":
cacheMetrics.Canceled++
}
cacheMetrics.Layers[layerIndex] = layerStatus
}
}

cacheMetrics.TotalLayers = cacheMetrics.Done + cacheMetrics.Cached + cacheMetrics.Errored + cacheMetrics.Canceled
// Count the number of each status in the Layers map
for _, layerStatus := range cacheMetrics.Layers {
switch layerStatus.Status {
case "DONE":
cacheMetrics.Done++
case "CACHED":
cacheMetrics.Cached++
case "ERROR":
cacheMetrics.Error++
case "CANCELED":
cacheMetrics.Canceled++
}
}

cacheMetrics.TotalLayers = cacheMetrics.Done + cacheMetrics.Cached + cacheMetrics.Error + cacheMetrics.Canceled

return cacheMetrics, nil
}
Expand Down
12 changes: 6 additions & 6 deletions cachemetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ func TestParseCacheMetrics(t *testing.T) {
{
name: "valid metrics with multiple on same line",
input: []string{
"#1 DONE 0.5s #2 CACHED #3 ERRORED #4 CANCELED",
"#1 DONE 0.5s #1 DONE 0.6s #2 CACHED #3 ERROR #4 CANCELED",
"#5 DONE 1.0s #6 CACHED",
},
expected: CacheMetrics{
Layers: map[int]LayerStatus{
1: {Status: "DONE", Time: 0.5},
1: {Status: "DONE", Time: 0.6},
2: {Status: "CACHED"},
3: {Status: "ERRORED"},
3: {Status: "ERROR"},
4: {Status: "CANCELED"},
5: {Status: "DONE", Time: 1.0},
6: {Status: "CACHED"},
},
Done: 2,
Cached: 2,
Errored: 1,
Error: 1,
Canceled: 1,
TotalLayers: 6,
},
Expand Down Expand Up @@ -68,13 +68,13 @@ func TestParseCacheMetrics(t *testing.T) {
1: {Status: "DONE", Time: 1.2},
2: {Status: "DONE", Time: 0.8},
3: {Status: "CACHED"},
4: {Status: "ERRORED"},
4: {Status: "ERROR"},
5: {Status: "CANCELED"},
6: {Status: "DONE"},
},
Done: 3,
Cached: 1,
Errored: 1,
Error: 1,
Canceled: 1,
TotalLayers: 6,
},
Expand Down