-
Notifications
You must be signed in to change notification settings - Fork 4k
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(core): duration.toHumanString() #6691
Conversation
Add a function for `Duration` to render itself to a human readable string. This can be used in dashboards or other situations where Durations need to be represented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are in there, could we also clean up -
aws-cdk/packages/@aws-cdk/core/package.json
Lines 60 to 70 in bed5357
"docs-public-apis:@aws-cdk/core.Duration.days", | |
"docs-public-apis:@aws-cdk/core.Duration.hours", | |
"docs-public-apis:@aws-cdk/core.Duration.millis", | |
"docs-public-apis:@aws-cdk/core.Duration.minutes", | |
"docs-public-apis:@aws-cdk/core.Duration.seconds", | |
"docs-public-apis:@aws-cdk/core.Duration.toDays", | |
"docs-public-apis:@aws-cdk/core.Duration.toHours", | |
"docs-public-apis:@aws-cdk/core.Duration.toISOString", | |
"docs-public-apis:@aws-cdk/core.Duration.toMilliseconds", | |
"docs-public-apis:@aws-cdk/core.Duration.toMinutes", | |
"docs-public-apis:@aws-cdk/core.Duration.toSeconds", |
*/ | ||
public toHumanString(): string { | ||
if (this.amount === 0) { return fmtUnit(0, this.unit); } | ||
if (Token.isUnresolved(this.amount)) { return `<token> ${this.unit.label}`; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not do ${this.amount} {this.unit}
instead? The token should get resolved, yes?
It might not be the best output and we might end up something like '458321 seconds' but at least it's something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, nevermind. I realized we can't do string operations on numeric tokens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to error instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think that's reasonable? I feel it's more reasonable to render what we got, which is " minutes", and let the human decide whether that's good enough for them or not.
If not, they can always add the check themselves.
// 2 significant parts, that's totally enough for humans | ||
return parts.slice(0, 2).join(' '); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be a configurable option, but we can totally do that later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was thinking the same.
test.equal(Duration.seconds(3666).toHumanString(), '1 hour 1 minute'); | ||
|
||
test.equal(Duration.millis(3000).toHumanString(), '3 seconds'); | ||
test.equal(Duration.millis(3666).toHumanString(), '3 seconds 666 millis'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it do the right thing for Duration.millis(3.6).toHumanString()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not. You crazy person.
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Added 'do-not-merge' label in case you want to address the linter exclusions I mentioned, while you're in there.
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Add a function for
Duration
to render itself to a human readablestring.
This can be used in dashboards or other situations where Durations need
to be represented.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license