As a part of f4991cb1 ComposeEntry struct was removed from store package.
This change made sense because this struct is connected more with API than
with store - store uses its own Compose struct. In addition, converters
between Compose and ComposeEntry were added. Unfortunately, ComposeEntry
contains ImageSize which was not stored in Compose but retrieved from store
using GetImage method. This made those converters dependent on the store,
which was messy.
To solve this issue this commit adds image struct into Compose struct.
The content of image struct is generated on the worker side - when the worker
sets the compose status to FINISHED, it also sends Image struct with detailed
information about the result.
89 lines
3 KiB
Go
89 lines
3 KiB
Go
package weldr
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"github.com/osbuild/osbuild-composer/internal/store"
|
|
"log"
|
|
"sort"
|
|
)
|
|
|
|
type ComposeEntry struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Blueprint string `json:"blueprint"`
|
|
Version string `json:"version"`
|
|
ComposeType string `json:"compose_type"`
|
|
ImageSize int64 `json:"image_size"`
|
|
QueueStatus string `json:"queue_status"`
|
|
JobCreated float64 `json:"job_created"`
|
|
JobStarted float64 `json:"job_started,omitempty"`
|
|
JobFinished float64 `json:"job_finished,omitempty"`
|
|
Uploads []UploadResponse `json:"uploads,omitempty"`
|
|
}
|
|
|
|
func composeToComposeEntry(id uuid.UUID, compose store.Compose, includeUploads bool) *ComposeEntry {
|
|
var composeEntry ComposeEntry
|
|
|
|
composeEntry.ID = id
|
|
composeEntry.Blueprint = compose.Blueprint.Name
|
|
composeEntry.Version = compose.Blueprint.Version
|
|
composeEntry.ComposeType = compose.OutputType
|
|
composeEntry.QueueStatus = compose.QueueStatus
|
|
|
|
if includeUploads {
|
|
composeEntry.Uploads = TargetsToUploadResponses(compose.Targets)
|
|
}
|
|
|
|
switch compose.QueueStatus {
|
|
case "WAITING":
|
|
composeEntry.JobCreated = float64(compose.JobCreated.UnixNano()) / 1000000000
|
|
|
|
case "RUNNING":
|
|
composeEntry.JobCreated = float64(compose.JobCreated.UnixNano()) / 1000000000
|
|
composeEntry.JobStarted = float64(compose.JobStarted.UnixNano()) / 1000000000
|
|
|
|
case "FINISHED":
|
|
if compose.Image != nil {
|
|
composeEntry.ImageSize = compose.Image.Size
|
|
} else {
|
|
log.Printf("finished compose with id %s has nil image\n", id.String())
|
|
composeEntry.ImageSize = 0
|
|
}
|
|
|
|
composeEntry.JobCreated = float64(compose.JobCreated.UnixNano()) / 1000000000
|
|
composeEntry.JobStarted = float64(compose.JobStarted.UnixNano()) / 1000000000
|
|
composeEntry.JobFinished = float64(compose.JobFinished.UnixNano()) / 1000000000
|
|
|
|
case "FAILED":
|
|
composeEntry.JobCreated = float64(compose.JobCreated.UnixNano()) / 1000000000
|
|
composeEntry.JobStarted = float64(compose.JobStarted.UnixNano()) / 1000000000
|
|
composeEntry.JobFinished = float64(compose.JobFinished.UnixNano()) / 1000000000
|
|
default:
|
|
panic("invalid compose state")
|
|
}
|
|
|
|
return &composeEntry
|
|
}
|
|
|
|
func composesToComposeEntries(composes map[uuid.UUID]store.Compose, uuids []uuid.UUID, includeUploads bool) []*ComposeEntry {
|
|
var composeEntries []*ComposeEntry
|
|
if uuids == nil {
|
|
composeEntries = make([]*ComposeEntry, 0, len(composes))
|
|
for id, compose := range composes {
|
|
composeEntries = append(composeEntries, composeToComposeEntry(id, compose, includeUploads))
|
|
}
|
|
} else {
|
|
composeEntries = make([]*ComposeEntry, 0, len(uuids))
|
|
for _, id := range uuids {
|
|
if compose, exists := composes[id]; exists {
|
|
composeEntries = append(composeEntries, composeToComposeEntry(id, compose, includeUploads))
|
|
}
|
|
}
|
|
}
|
|
|
|
// make this function output more predictable
|
|
sort.Slice(composeEntries, func(i, j int) bool {
|
|
return composeEntries[i].ID.String() < composeEntries[j].ID.String()
|
|
})
|
|
|
|
return composeEntries
|
|
}
|