Skip to content

Commit

Permalink
upload: store upload method in database
Browse files Browse the repository at this point in the history
Add a new column called "method" to the upload table.
It stores if the output was uploaded via S3, docker image registry or filecopy.

This allows to create statistics depending the upload method like bandwidth per
method.
  • Loading branch information
fho committed Nov 15, 2018
1 parent d394f0a commit 441e59e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
7 changes: 6 additions & 1 deletion command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func resultAddBuildResult(bud *buildUserData, r *build.Result) {

func resultAddUploadResult(appName string, ar baur.BuildOutput, r *scheduler.Result) {
var arType storage.ArtifactType
var uploadMethod storage.UploadMethod

resultLock.Lock()
defer resultLock.Unlock()
Expand All @@ -147,10 +148,13 @@ func resultAddUploadResult(appName string, ar baur.BuildOutput, r *scheduler.Res
switch r.Job.Type() {
case scheduler.JobDocker:
arType = storage.DockerArtifact
uploadMethod = storage.DockerRegistry
case scheduler.JobFileCopy:
fallthrough
arType = storage.FileArtifact
uploadMethod = storage.FileCopy
case scheduler.JobS3:
arType = storage.FileArtifact
uploadMethod = storage.S3
default:
panic(fmt.Sprintf("unknown job type %v", r.Job.Type()))
}
Expand All @@ -171,6 +175,7 @@ func resultAddUploadResult(appName string, ar baur.BuildOutput, r *scheduler.Res
Type: arType,
Upload: storage.Upload{
URI: r.URL,
Method: uploadMethod,
UploadDuration: r.Duration,
},
Digest: artDigest.String(),
Expand Down
6 changes: 4 additions & 2 deletions command/ls_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ func lsOutputs(cmd *cobra.Command, args []string) {
row = []interface{}{o.Upload.URI}
} else {
row = []interface{}{
o.Type,
o.Upload.URI,
o.Digest,
bytesToMib(int(o.SizeBytes)),
o.Upload.UploadDuration,
o.Type,
o.Upload.Method,
}
}

Expand All @@ -96,11 +97,12 @@ func getLsOutputsFormatter(isQuiet, isCsv bool) format.Formatter {
}

headers = []string{
"Type",
"URI",
"Digest",
"Size (MiB)",
"Upload Duration",
"Output Type",
"Method",
}

return table.New(headers, os.Stdout)
Expand Down
4 changes: 3 additions & 1 deletion command/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func showBuild(buildID int) {
mustWriteRow(formatter, []interface{}{underline("Outputs:")})
}
for i, o := range build.Outputs {
mustWriteRow(formatter, []interface{}{"", "Type:", highlight(o.Type)})
mustWriteRow(formatter, []interface{}{"", "URI:", highlight(o.Upload.URI)})
mustWriteRow(formatter, []interface{}{"", "Digest:", highlight(o.Digest)})
mustWriteRow(formatter, []interface{}{
Expand All @@ -179,6 +178,9 @@ func showBuild(buildID int) {
"Upload Duration:",
highlight(fmt.Sprintf("%.2f s", o.Upload.UploadDuration.Seconds())),
})
mustWriteRow(formatter, []interface{}{"", "Type:", highlight(o.Type)})
mustWriteRow(formatter, []interface{}{"", "Upload Method:", highlight(o.Upload.Method)})

if i+1 < len(build.Outputs) {
mustWriteRow(formatter, []interface{}{})
}
Expand Down
1 change: 1 addition & 0 deletions storage/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CREATE TABLE upload (
id SERIAL PRIMARY KEY,
build_output_id INTEGER REFERENCES build_output (id) ON DELETE CASCADE,
uri TEXT NOT NULL,
method TEXT NOT NULL,
upload_duration_ns BIGINT NOT NULL
);
Expand Down
10 changes: 5 additions & 5 deletions storage/postgres/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func insertOutputsIfNotExist(tx *sql.Tx, outputs []*storage.Output) ([]int, erro

func insertInputBuilds(tx *sql.Tx, buildID int, inputIDs []int) error {
const stmt1 = `
INSERT into input_build
INSERT into input_build
(build_id, input_id)
VALUES
`
Expand Down Expand Up @@ -240,7 +240,7 @@ func insertAppIfNotExist(tx *sql.Tx, app *storage.Application) error {
func insertUploads(tx *sql.Tx, buildOutputIDs []int, outputs []*storage.Output) error {
const stmt = `
INSERT into upload
(build_output_id, uri, upload_duration_ns)
(build_output_id, uri, method, upload_duration_ns)
VALUES
`

Expand All @@ -257,9 +257,9 @@ func insertUploads(tx *sql.Tx, buildOutputIDs []int, outputs []*storage.Output)
}

for i, out := range outputs {
stmtVals += fmt.Sprintf("($%d, $%d, $%d)", argCNT, argCNT+1, argCNT+2)
argCNT += 3
queryArgs = append(queryArgs, buildOutputIDs[i], out.Upload.URI, out.Upload.UploadDuration)
stmtVals += fmt.Sprintf("($%d, $%d,$%d, $%d)", argCNT, argCNT+1, argCNT+2, argCNT+3)
argCNT += 4
queryArgs = append(queryArgs, buildOutputIDs[i], out.Upload.URI, out.Upload.Method, out.Upload.UploadDuration)

if i < len(outputs)-1 {
stmtVals += ", "
Expand Down
3 changes: 2 additions & 1 deletion storage/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *Client) Close() {
func (c *Client) GetBuildOutputs(buildID int) ([]*storage.Output, error) {
const stmt = `SELECT
output.name, output.digest, output.type, output.size_bytes,
upload.id, upload.uri, upload.upload_duration_ns
upload.id, upload.uri, upload.method, upload.upload_duration_ns
FROM output
JOIN build_output ON output.id = build_output.output_id
JOIN upload ON upload.build_output_id = build_output.id
Expand All @@ -64,6 +64,7 @@ func (c *Client) GetBuildOutputs(buildID int) ([]*storage.Output, error) {
&output.SizeBytes,
&output.Upload.ID,
&output.Upload.URI,
&output.Upload.Method,
&output.Upload.UploadDuration,
)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ const (
FileArtifact ArtifactType = "file"
)

// UploadMethod describes the used upload mechanism
type UploadMethod string

// Description of UploadMethod Values
const (
S3 UploadMethod = "s3"
DockerRegistry UploadMethod = "docker"
FileCopy UploadMethod = "filecopy"
)

// ErrNotExist indicates that a record does not exist
var ErrNotExist = errors.New("does not exist")

Expand Down Expand Up @@ -60,6 +70,7 @@ type Upload struct {
ID int
UploadDuration time.Duration
URI string
Method UploadMethod
}

// Output represents a build output
Expand Down

0 comments on commit 441e59e

Please sign in to comment.