Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(library): update Duration() return values #227

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 17 additions & 7 deletions library/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,31 @@ type Build struct {
// Duration calculates and returns the total amount of
// time the build ran for in a human-readable format.
func (b *Build) Duration() string {
// check if the build doesn't have a started or finished timestamp
if b.GetStarted() == 0 || b.GetFinished() == 0 {
// return zero value for time.Duration (0s)
return new(time.Duration).String()
// check if the build doesn't have a started timestamp
if b.GetStarted() == 0 {
// nolint: goconst // ignore making a constant
return "..."
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved
}

// capture finished unix timestamp from the build
finished := time.Unix(b.GetFinished(), 0)
// capture started unix timestamp from the build
started := time.Unix(b.GetStarted(), 0)

// check if the build doesn't have a finished timestamp
if b.GetFinished() == 0 {
// return the duration in a human-readable form by
// subtracting the build started time from the
// current time rounded to the nearest second
return time.Since(started).Round(time.Second).String()
}

// capture finished unix timestamp from the build
finished := time.Unix(b.GetFinished(), 0)

// calculate the duration by subtracting the build
// started time from the build finished time
duration := finished.Sub(started)

// return duration in a human-readable form
// return the duration in a human-readable form
return duration.String()
}

Expand Down
11 changes: 10 additions & 1 deletion library/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
"fmt"
"reflect"
"testing"
"time"

"github.com/go-vela/types/raw"
)

func TestLibrary_Build_Duration(t *testing.T) {
// setup types
unfinished := testBuild()
unfinished.SetFinished(0)

// setup tests
tests := []struct {
build *Build
Expand All @@ -22,9 +27,13 @@ func TestLibrary_Build_Duration(t *testing.T) {
build: testBuild(),
want: "1s",
},
{
build: unfinished,
want: time.Since(time.Unix(unfinished.GetStarted(), 0)).Round(time.Second).String(),
},
{
build: new(Build),
want: "0s",
want: "...",
},
}

Expand Down
23 changes: 16 additions & 7 deletions library/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,30 @@ type Service struct {
// Duration calculates and returns the total amount of
// time the service ran for in a human-readable format.
func (s *Service) Duration() string {
// check if the service doesn't have a started or finished timestamp
if s.GetStarted() == 0 || s.GetFinished() == 0 {
// return zero value for time.Duration (0s)
return new(time.Duration).String()
// check if the service doesn't have a started timestamp
if s.GetStarted() == 0 {
return "..."
}

// capture finished unix timestamp from the service
finished := time.Unix(s.GetFinished(), 0)
// capture started unix timestamp from the service
started := time.Unix(s.GetStarted(), 0)

// check if the service doesn't have a finished timestamp
if s.GetFinished() == 0 {
// return the duration in a human-readable form by
// subtracting the service started time from the
// current time rounded to the nearest second
return time.Since(started).Round(time.Second).String()
}

// capture finished unix timestamp from the service
finished := time.Unix(s.GetFinished(), 0)

// calculate the duration by subtracting the service
// started time from the service finished time
duration := finished.Sub(started)

// return duration in a human-readable form
// return the duration in a human-readable form
return duration.String()
}

Expand Down
11 changes: 10 additions & 1 deletion library/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
"fmt"
"reflect"
"testing"
"time"

"github.com/go-vela/types/pipeline"
)

func TestLibrary_Service_Duration(t *testing.T) {
// setup types
unfinished := testService()
unfinished.SetFinished(0)

// setup tests
tests := []struct {
service *Service
Expand All @@ -22,9 +27,13 @@ func TestLibrary_Service_Duration(t *testing.T) {
service: testService(),
want: "1s",
},
{
service: unfinished,
want: time.Since(time.Unix(unfinished.GetStarted(), 0)).Round(time.Second).String(),
},
{
service: new(Service),
want: "0s",
want: "...",
},
}

Expand Down
23 changes: 16 additions & 7 deletions library/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,30 @@ type Step struct {
// Duration calculates and returns the total amount of
// time the step ran for in a human-readable format.
func (s *Step) Duration() string {
// check if the step doesn't have a started or finished timestamp
if s.GetStarted() == 0 || s.GetFinished() == 0 {
// return zero value for time.Duration (0s)
return new(time.Duration).String()
// check if the step doesn't have a started timestamp
if s.GetStarted() == 0 {
return "..."
}

// capture finished unix timestamp from the step
finished := time.Unix(s.GetFinished(), 0)
// capture started unix timestamp from the step
started := time.Unix(s.GetStarted(), 0)

// check if the step doesn't have a finished timestamp
if s.GetFinished() == 0 {
// return the duration in a human-readable form by
// subtracting the step started time from the
// current time rounded to the nearest second
return time.Since(started).Round(time.Second).String()
}

// capture finished unix timestamp from the step
finished := time.Unix(s.GetFinished(), 0)

// calculate the duration by subtracting the step
// started time from the step finished time
duration := finished.Sub(started)

// return duration in a human-readable form
// return the duration in a human-readable form
return duration.String()
}

Expand Down
11 changes: 10 additions & 1 deletion library/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
"fmt"
"reflect"
"testing"
"time"

"github.com/go-vela/types/pipeline"
)

func TestLibrary_Step_Duration(t *testing.T) {
// setup types
unfinished := testStep()
unfinished.SetFinished(0)

// setup tests
tests := []struct {
step *Step
Expand All @@ -22,9 +27,13 @@ func TestLibrary_Step_Duration(t *testing.T) {
step: testStep(),
want: "1s",
},
{
step: unfinished,
want: time.Since(time.Unix(unfinished.GetStarted(), 0)).Round(time.Second).String(),
},
{
step: new(Step),
want: "0s",
want: "...",
},
}

Expand Down