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

feat(library): add Duration() function to resources #222

Merged
merged 6 commits into from
Dec 29, 2021

Conversation

jbrockopp
Copy link
Contributor

@jbrockopp jbrockopp commented Dec 23, 2021

Currently, we have logic to calculate the duration (amount of time a resource was running) for various resources.

The following resources have this functionality:

This change introduces a Duration() method on each of these resources that will replace the above linked functions:

types/library/build.go

Lines 51 to 70 in 16db0db

// 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()
}
// 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)
// 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 duration.String()
}

types/library/service.go

Lines 36 to 55 in 16db0db

// 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()
}
// 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)
// 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 duration.String()
}

types/library/step.go

Lines 38 to 57 in 16db0db

// 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 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()
}
// 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)
// 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 duration.String()
}

@jbrockopp jbrockopp added the feature Indicates a new feature label Dec 23, 2021
@jbrockopp jbrockopp self-assigned this Dec 23, 2021
@codecov
Copy link

codecov bot commented Dec 23, 2021

Codecov Report

Merging #222 (b3cbd9e) into master (34d807f) will increase coverage by 0.27%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #222      +/-   ##
==========================================
+ Coverage   96.74%   97.01%   +0.27%     
==========================================
  Files          53       53              
  Lines        5645     5690      +45     
==========================================
+ Hits         5461     5520      +59     
+ Misses        137      125      -12     
+ Partials       47       45       -2     
Impacted Files Coverage Δ
library/build.go 100.00% <100.00%> (ø)
library/service.go 85.98% <100.00%> (+2.97%) ⬆️
library/step.go 100.00% <100.00%> (+1.98%) ⬆️

@jbrockopp jbrockopp marked this pull request as ready for review December 28, 2021 14:06
@jbrockopp jbrockopp requested a review from a team as a code owner December 28, 2021 14:06
Copy link
Contributor

@ecrupper ecrupper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍 , one small typo that I spotted.

I think this is a lot cleaner than how it was previously set up with the duration helper functions. Thanks!

library/step.go Outdated Show resolved Hide resolved
@wass3r wass3r merged commit 3cfb59b into master Dec 29, 2021
@wass3r wass3r deleted the feature/library/duration branch December 29, 2021 12:45
@wass3r
Copy link
Collaborator

wass3r commented Dec 30, 2021

an afterthought, should a build in progress (ie. started is set but finished is not set yet) return 0s for the duration? or the time since started?

@jbrockopp
Copy link
Contributor Author

jbrockopp commented Dec 30, 2021

@wass3r initially, I thought the same thing and actually had written code to accomplish that 👍

However, when I started writing tests, I stumbled upon a reason why it may not be great for that to be the case 😅

The started timestamp we use in our test objects is 1563474078:

b.SetStarted(1563474078)

s.SetStarted(1563474078)

s.SetStarted(1563474078)

That UNIX timestamp actually refers to a time on Thursday, July 18, 2019 👍

After removing the finished timestamp from a test object, the Duration() function returned something like 2y5m10d.

In most scenarios, the functionality you described for durations would work 😃

The typical case for a resource to have a started timestamp but not a finished timestamp is due to it still running.

However, it's possible for a resource to get stuck/hung meaning it never gets a finished timestamp.

I decided to use 0s if the started or finished timestamp aren't set to help prevent misleading information.

@wass3r
Copy link
Collaborator

wass3r commented Dec 30, 2021

Isn't it still misleading though? A running build (not hung) will show its duration as 0s if the callee is to use the returned value as is.

I agree that hung builds are more of an outlier than running builds, so I think it's ok if the duration says something abnormally large.

@jbrockopp
Copy link
Contributor Author

Isn't it still misleading though?

Yes, it is misleading either way 🤷

Would you rather have these functions return ... like we do in the CLI?

I agree that hung builds are more of an outlier than running builds, ...

Agreed 👍

so I think it's ok if the duration says something abnormally large.

Unfortunately, I don't agree 😞

@wass3r
Copy link
Collaborator

wass3r commented Dec 31, 2021

I guess we disagree.

I find it much more bothersome for a healthy running build to say it's been running for 0s (when it hasn't) than a rare hung build to tell me it has been running for 6 months. If used in the CLI (for example), I would not use the value as is. I think folks would prefer either the current running time, or something indeterminate like ....

@jbrockopp
Copy link
Contributor Author

Would you like me to open up a PR that changes the behavior to ... then?

The only downside I see to that is ... isn't actually a valid duration 😞

If someone were to use the Duration() functions with time.ParseDuration() it would always return an error:

https://go.dev/play/p/_sIxzHTyc9n

On a positive note, the user would still get 0s as the return value since the default for time.Duration{} is 0s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants