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

Implemented number field formatter for durations #6499

Merged
merged 1 commit into from
Mar 14, 2016

Conversation

bevacqua
Copy link
Contributor

This PR fixes #5130.

Numeric fields in elasticsearch can represent a duration. This PR adds a flexible Duration formatter that allows the user to decide how to display durations.

  • The input format can be customized depending on whether the field represents seconds, minutes, months, etc. (defaults to seconds)
  • The output format can be customized depending on how the user wants to render the duration ("humanized", seconds, minutes, etc) (defaults to humanize)
  • When the output format is not humanized the user can also specify the decimal precision for the displayed value (defaults to 2)

screen shot 2016-03-09 at 17 22 42

]
}];

fixtures.forEach((fixture, i) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this pattern of dynamically generating test cases used for testing the other field formatters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think other test files keep all assertions in a single test case

Copy link
Contributor

Choose a reason for hiding this comment

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

One big test case isn't ideal either, but we should aim to be more explicit with our tests rather than generating them automatically. It's more boilerplate up front, but it pays off in test clarity and expressiveness down the line. As it stands now, I've read over this loop within a loop a few times, and I'm still not 100% confident I know what it's testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like this was a common enough pattern to avoid code repetition. I agree that the fixtures and inputs loop could be a bit clearer, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Avoiding repetition is good, but clarity and expressiveness should be stronger concerns within tests. With some changes (namely not automating the test execution), you can still avoid duplicating the test case itself for common inputs/outputs and have expressive and concise tests.

One possible example of the same test cases: https://gist.github.com/epixa/aed0d611e41ac366073d

That all said, I'm pretty squarely in the don't-automate-test-creation camp, but there are others on the team that are more comfortable with it, so don't feel obligated to switch to the other extreme.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be fair, your code is much easier to read. I'll use a version of that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Rad. Be warned: my code wouldn't actually run or anything useful like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know, haha.

import moment from 'moment';
import IndexPatternsFieldFormatProvider from 'ui/index_patterns/_field_format/FieldFormat';
import durationTemplate from 'ui/stringify/editors/duration.html';
export default function DurationFormatProvider(Private) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a space between the imports and export? It just makes it a little easier to grok at a glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above, virtually none of the files I saw do this. Thanks

@epixa
Copy link
Contributor

epixa commented Mar 10, 2016

Looking good. Go ahead and assign it back to me whenever you've addressed the feedback in either code or in comments.

@epixa epixa assigned bevacqua and unassigned epixa Mar 10, 2016
@epixa epixa removed the review label Mar 10, 2016
@bevacqua bevacqua assigned epixa and unassigned bevacqua Mar 10, 2016
@epixa epixa added the review label Mar 10, 2016

class Duration extends FieldFormat {
constructor(params) {
super(params);
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to specify a constructor if it's only calling the parent constructor.

@epixa
Copy link
Contributor

epixa commented Mar 10, 2016

Added a few things, but this looks almost ready.

@epixa epixa assigned bevacqua and unassigned epixa Mar 10, 2016
@bevacqua bevacqua assigned epixa and unassigned bevacqua Mar 10, 2016
@bevacqua
Copy link
Contributor Author

That should do it.

@epixa
Copy link
Contributor

epixa commented Mar 10, 2016

Assuming tests go green, LGTM.

At this point I'd assign it back to you and you'd grab a second person to review it. We can sort it out on irc though.

@epixa epixa assigned bevacqua and unassigned epixa Mar 10, 2016
@rashidkpc rashidkpc assigned rashidkpc and unassigned bevacqua Mar 11, 2016
@rashidkpc
Copy link
Contributor

LGTM

rashidkpc pushed a commit that referenced this pull request Mar 14, 2016
Implemented number field formatter for durations
@rashidkpc rashidkpc merged commit 73beb4b into elastic:master Mar 14, 2016
@bevacqua
Copy link
Contributor Author

🎆

@bevacqua bevacqua deleted the feature/duration-formatter branch March 14, 2016 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Format number as time duration
3 participants