Skip to content

Commit

Permalink
feat(service): add worker information (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp committed Apr 10, 2020
1 parent 80de769 commit 87de204
Show file tree
Hide file tree
Showing 4 changed files with 372 additions and 132 deletions.
72 changes: 48 additions & 24 deletions database/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ var (

// Service is the database representation of a service in a build.
type Service struct {
ID sql.NullInt64 `sql:"id"`
BuildID sql.NullInt64 `sql:"build_id"`
RepoID sql.NullInt64 `sql:"repo_id"`
Number sql.NullInt32 `sql:"number"`
Name sql.NullString `sql:"name"`
Image sql.NullString `sql:"image"`
Status sql.NullString `sql:"status"`
Error sql.NullString `sql:"error"`
ExitCode sql.NullInt32 `sql:"exit_code"`
Created sql.NullInt64 `sql:"created"`
Started sql.NullInt64 `sql:"started"`
Finished sql.NullInt64 `sql:"finished"`
ID sql.NullInt64 `sql:"id"`
BuildID sql.NullInt64 `sql:"build_id"`
RepoID sql.NullInt64 `sql:"repo_id"`
Number sql.NullInt32 `sql:"number"`
Name sql.NullString `sql:"name"`
Image sql.NullString `sql:"image"`
Status sql.NullString `sql:"status"`
Error sql.NullString `sql:"error"`
ExitCode sql.NullInt32 `sql:"exit_code"`
Created sql.NullInt64 `sql:"created"`
Started sql.NullInt64 `sql:"started"`
Finished sql.NullInt64 `sql:"finished"`
Host sql.NullString `sql:"host"`
Runtime sql.NullString `sql:"runtime"`
Distribution sql.NullString `sql:"distribution"`
}

// Nullify ensures the valid flag for
Expand Down Expand Up @@ -120,6 +123,21 @@ func (s *Service) Nullify() *Service {
s.Finished.Valid = false
}

// check if the Host field should be false
if len(s.Host.String) == 0 {
s.Host.Valid = false
}

// check if the Runtime field should be false
if len(s.Runtime.String) == 0 {
s.Runtime.Valid = false
}

// check if the Distribution field should be false
if len(s.Distribution.String) == 0 {
s.Distribution.Valid = false
}

return s
}

Expand All @@ -140,6 +158,9 @@ func (s *Service) ToLibrary() *library.Service {
service.SetCreated(s.Created.Int64)
service.SetStarted(s.Started.Int64)
service.SetFinished(s.Finished.Int64)
service.SetHost(s.Host.String)
service.SetRuntime(s.Runtime.String)
service.SetDistribution(s.Distribution.String)

return service
}
Expand Down Expand Up @@ -179,18 +200,21 @@ func (s *Service) Validate() error {
// to a database Service type.
func ServiceFromLibrary(s *library.Service) *Service {
service := &Service{
ID: sql.NullInt64{Int64: s.GetID(), Valid: true},
BuildID: sql.NullInt64{Int64: s.GetBuildID(), Valid: true},
RepoID: sql.NullInt64{Int64: s.GetRepoID(), Valid: true},
Number: sql.NullInt32{Int32: int32(s.GetNumber()), Valid: true},
Name: sql.NullString{String: s.GetName(), Valid: true},
Image: sql.NullString{String: s.GetImage(), Valid: true},
Status: sql.NullString{String: s.GetStatus(), Valid: true},
Error: sql.NullString{String: s.GetError(), Valid: true},
ExitCode: sql.NullInt32{Int32: int32(s.GetExitCode()), Valid: true},
Created: sql.NullInt64{Int64: s.GetCreated(), Valid: true},
Started: sql.NullInt64{Int64: s.GetStarted(), Valid: true},
Finished: sql.NullInt64{Int64: s.GetFinished(), Valid: true},
ID: sql.NullInt64{Int64: s.GetID(), Valid: true},
BuildID: sql.NullInt64{Int64: s.GetBuildID(), Valid: true},
RepoID: sql.NullInt64{Int64: s.GetRepoID(), Valid: true},
Number: sql.NullInt32{Int32: int32(s.GetNumber()), Valid: true},
Name: sql.NullString{String: s.GetName(), Valid: true},
Image: sql.NullString{String: s.GetImage(), Valid: true},
Status: sql.NullString{String: s.GetStatus(), Valid: true},
Error: sql.NullString{String: s.GetError(), Valid: true},
ExitCode: sql.NullInt32{Int32: int32(s.GetExitCode()), Valid: true},
Created: sql.NullInt64{Int64: s.GetCreated(), Valid: true},
Started: sql.NullInt64{Int64: s.GetStarted(), Valid: true},
Finished: sql.NullInt64{Int64: s.GetFinished(), Valid: true},
Host: sql.NullString{String: s.GetHost(), Valid: true},
Runtime: sql.NullString{String: s.GetRuntime(), Valid: true},
Distribution: sql.NullString{String: s.GetDistribution(), Valid: true},
}

return service.Nullify()
Expand Down
162 changes: 90 additions & 72 deletions database/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,38 @@ import (
func TestDatabase_Service_Nullify(t *testing.T) {
// setup types
s := &Service{
ID: sql.NullInt64{Int64: 0, Valid: true},
BuildID: sql.NullInt64{Int64: 0, Valid: true},
RepoID: sql.NullInt64{Int64: 0, Valid: true},
Number: sql.NullInt32{Int32: 0, Valid: true},
Name: sql.NullString{String: "", Valid: true},
Image: sql.NullString{String: "", Valid: true},
Status: sql.NullString{String: "", Valid: true},
Error: sql.NullString{String: "", Valid: true},
ExitCode: sql.NullInt32{Int32: 0, Valid: true},
Created: sql.NullInt64{Int64: 0, Valid: true},
Started: sql.NullInt64{Int64: 0, Valid: true},
Finished: sql.NullInt64{Int64: 0, Valid: true},
ID: sql.NullInt64{Int64: 0, Valid: true},
BuildID: sql.NullInt64{Int64: 0, Valid: true},
RepoID: sql.NullInt64{Int64: 0, Valid: true},
Number: sql.NullInt32{Int32: 0, Valid: true},
Name: sql.NullString{String: "", Valid: true},
Image: sql.NullString{String: "", Valid: true},
Status: sql.NullString{String: "", Valid: true},
Error: sql.NullString{String: "", Valid: true},
ExitCode: sql.NullInt32{Int32: 0, Valid: true},
Created: sql.NullInt64{Int64: 0, Valid: true},
Started: sql.NullInt64{Int64: 0, Valid: true},
Finished: sql.NullInt64{Int64: 0, Valid: true},
Host: sql.NullString{String: "", Valid: true},
Runtime: sql.NullString{String: "", Valid: true},
Distribution: sql.NullString{String: "", Valid: true},
}
want := &Service{
ID: sql.NullInt64{Int64: 0, Valid: false},
BuildID: sql.NullInt64{Int64: 0, Valid: false},
RepoID: sql.NullInt64{Int64: 0, Valid: false},
Number: sql.NullInt32{Int32: 0, Valid: false},
Name: sql.NullString{String: "", Valid: false},
Image: sql.NullString{String: "", Valid: false},
Status: sql.NullString{String: "", Valid: false},
Error: sql.NullString{String: "", Valid: false},
ExitCode: sql.NullInt32{Int32: 0, Valid: false},
Created: sql.NullInt64{Int64: 0, Valid: false},
Started: sql.NullInt64{Int64: 0, Valid: false},
Finished: sql.NullInt64{Int64: 0, Valid: false},
ID: sql.NullInt64{Int64: 0, Valid: false},
BuildID: sql.NullInt64{Int64: 0, Valid: false},
RepoID: sql.NullInt64{Int64: 0, Valid: false},
Number: sql.NullInt32{Int32: 0, Valid: false},
Name: sql.NullString{String: "", Valid: false},
Image: sql.NullString{String: "", Valid: false},
Status: sql.NullString{String: "", Valid: false},
Error: sql.NullString{String: "", Valid: false},
ExitCode: sql.NullInt32{Int32: 0, Valid: false},
Created: sql.NullInt64{Int64: 0, Valid: false},
Started: sql.NullInt64{Int64: 0, Valid: false},
Finished: sql.NullInt64{Int64: 0, Valid: false},
Host: sql.NullString{String: "", Valid: false},
Runtime: sql.NullString{String: "", Valid: false},
Distribution: sql.NullString{String: "", Valid: false},
}

// run test
Expand Down Expand Up @@ -70,32 +76,38 @@ func TestDatabase_Service_ToLibrary(t *testing.T) {
num64 := int64(num)
str := "foo"
want := &library.Service{
ID: &num64,
BuildID: &num64,
RepoID: &num64,
Number: &num,
Name: &str,
Image: &str,
Status: &str,
Error: &str,
ExitCode: &num,
Created: &num64,
Started: &num64,
Finished: &num64,
ID: &num64,
BuildID: &num64,
RepoID: &num64,
Number: &num,
Name: &str,
Image: &str,
Status: &str,
Error: &str,
ExitCode: &num,
Created: &num64,
Started: &num64,
Finished: &num64,
Host: &str,
Runtime: &str,
Distribution: &str,
}
s := &Service{
ID: sql.NullInt64{Int64: num64, Valid: true},
BuildID: sql.NullInt64{Int64: num64, Valid: true},
RepoID: sql.NullInt64{Int64: num64, Valid: true},
Number: sqlNum,
Name: sql.NullString{String: str, Valid: true},
Image: sql.NullString{String: str, Valid: true},
Status: sql.NullString{String: str, Valid: true},
Error: sql.NullString{String: str, Valid: true},
ExitCode: sqlNum,
Created: sql.NullInt64{Int64: num64, Valid: true},
Started: sql.NullInt64{Int64: num64, Valid: true},
Finished: sql.NullInt64{Int64: num64, Valid: true},
ID: sql.NullInt64{Int64: num64, Valid: true},
BuildID: sql.NullInt64{Int64: num64, Valid: true},
RepoID: sql.NullInt64{Int64: num64, Valid: true},
Number: sqlNum,
Name: sql.NullString{String: str, Valid: true},
Image: sql.NullString{String: str, Valid: true},
Status: sql.NullString{String: str, Valid: true},
Error: sql.NullString{String: str, Valid: true},
ExitCode: sqlNum,
Created: sql.NullInt64{Int64: num64, Valid: true},
Started: sql.NullInt64{Int64: num64, Valid: true},
Finished: sql.NullInt64{Int64: num64, Valid: true},
Host: sql.NullString{String: str, Valid: true},
Runtime: sql.NullString{String: str, Valid: true},
Distribution: sql.NullString{String: str, Valid: true},
}

// run test
Expand Down Expand Up @@ -217,32 +229,38 @@ func TestDatabase_ServiceFromLibrary(t *testing.T) {
num64 := int64(num)
str := "foo"
want := &Service{
ID: sql.NullInt64{Int64: num64, Valid: true},
BuildID: sql.NullInt64{Int64: num64, Valid: true},
RepoID: sql.NullInt64{Int64: num64, Valid: true},
Number: sqlNum,
Name: sql.NullString{String: str, Valid: true},
Image: sql.NullString{String: str, Valid: true},
Status: sql.NullString{String: str, Valid: true},
Error: sql.NullString{String: str, Valid: true},
ExitCode: sqlNum,
Created: sql.NullInt64{Int64: num64, Valid: true},
Started: sql.NullInt64{Int64: num64, Valid: true},
Finished: sql.NullInt64{Int64: num64, Valid: true},
ID: sql.NullInt64{Int64: num64, Valid: true},
BuildID: sql.NullInt64{Int64: num64, Valid: true},
RepoID: sql.NullInt64{Int64: num64, Valid: true},
Number: sqlNum,
Name: sql.NullString{String: str, Valid: true},
Image: sql.NullString{String: str, Valid: true},
Status: sql.NullString{String: str, Valid: true},
Error: sql.NullString{String: str, Valid: true},
ExitCode: sqlNum,
Created: sql.NullInt64{Int64: num64, Valid: true},
Started: sql.NullInt64{Int64: num64, Valid: true},
Finished: sql.NullInt64{Int64: num64, Valid: true},
Host: sql.NullString{String: str, Valid: true},
Runtime: sql.NullString{String: str, Valid: true},
Distribution: sql.NullString{String: str, Valid: true},
}
s := &library.Service{
ID: &num64,
BuildID: &num64,
RepoID: &num64,
Number: &num,
Name: &str,
Image: &str,
Status: &str,
Error: &str,
ExitCode: &num,
Created: &num64,
Started: &num64,
Finished: &num64,
ID: &num64,
BuildID: &num64,
RepoID: &num64,
Number: &num,
Name: &str,
Image: &str,
Status: &str,
Error: &str,
ExitCode: &num,
Created: &num64,
Started: &num64,
Finished: &num64,
Host: &str,
Runtime: &str,
Distribution: &str,
}

// run test
Expand Down
Loading

0 comments on commit 87de204

Please sign in to comment.