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

[Lens] Add suffix formatter #82852

Merged
merged 9 commits into from
Nov 12, 2020
Merged

Conversation

flash1293
Copy link
Contributor

Fixes #76714

This PR adds a new "suffix" formatter which works similar to the range formatter. It's taking a nested serialized field format object and uses it to format the value itself, then adds the suffix in the back. As a param it takes a unit which can be passed to the time_scale function as well.

Screenshot 2020-11-06 at 17 00 34

This PR doesn't handle the label change - I imagine this to be part of the PR piecing all the parts together (UI to enable this, setting the label, setting the outer formatter, writing the time_scale expression function)

Testing

To test, replace the buildColumn function in x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/metrics.tsx by this:

    buildColumn: ({ suggestedPriority, field, previousColumn }) => ({
      label: ofName(field.displayName),
      dataType: 'number',
      operationType: type,
      suggestedPriority,
      sourceField: field.name,
      isBucketed: false,
      scale: 'ratio',
      params:
        // TODO just for testing
        previousColumn && previousColumn.dataType === 'number'
          ? previousColumn.params
          : {
              parentFormat: {
                id: 'suffix',
                params: {
                  unit: 'h',
                },
              },
            },
    }),

Then configure a chart using average/min/max/median operations.

Related changes

In x-pack/plugins/lens/public/indexpattern_datasource/format_column.ts, I had to make sure the id of the outer format is used correctly. This wasn't an issue for range because it only replaced params on an already existing outer format.

@flash1293 flash1293 added Feature:Lens release_note:skip Skip the PR/issue when compiling release notes v7.11.0 v8.0.0 labels Nov 6, 2020
@flash1293 flash1293 marked this pull request as ready for review November 6, 2020 19:11
@flash1293 flash1293 requested a review from a team November 6, 2020 19:11
@flash1293 flash1293 added the Team:Visualizations Visualization editors, elastic-charts and infrastructure label Nov 6, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app (Team:KibanaApp)

Copy link
Contributor

@wylieconlon wylieconlon left a comment

Choose a reason for hiding this comment

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

It looks like the field-specific default formatter is not being respected in all cases. Steps to reproduce:

  1. Apply the code change you suggested
  2. Drag a numeric field into the workspace
  3. Hover over the tooltip

Screen Shot 2020-11-06 at 2 27 20 PM

Other than this issue the behavior LGTM

@flash1293
Copy link
Contributor Author

@elasticmachine merge upstream

@flash1293
Copy link
Contributor Author

@wylieconlon Thanks for the review, the nested params were not passed along the right way. I fixed it by explicitly handling the separate cases.

format_column definitely needs unit tests, I will add them on a separate PR if that's OK.

Copy link
Contributor

@dej611 dej611 left a comment

Choose a reason for hiding this comment

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

Not sure I proposed an improvement over the current one, but I got lost parsing all the branchings in the format_column body

Comment on lines 122 to 139
return withParams(col, {
...col.meta.params,
params: {
...innerParams,
...parentFormatParams,
},
});
} else {
// original format is not nested, wrapping it insifr parentFormatId/parentFormatParams
return withParams(col, {
...col.meta.params,
id: parentFormatId,
params: {
id: col.meta.params?.id,
params: innerParams,
...parentFormatParams,
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this bit a bit hard to wrap my head around.
I would propose something alternative:

const isNested = isNestedFormat(col.meta.params);
const innerParams = isNested ? {id: col.meta.params?.id, params: col.meta.params?.params } : col.meta.params?.params;

const formatId = isNested ? col.meta.params.id : parentFormatId;

return withParams(col, {
    ...col.meta.params,
    id: formatId,
    params: {
      ...innerParams,
      ...parentFormatParams
    }
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dej611 I refactored the code like this, however there is a bug in there - the first ternary condition calculating the innerParams is flipped (it should be the nested params if the original formatter was nested, not the other way around)

@mbondyra
Copy link
Contributor

Functionality works great and code looks good to me. The only thing that doesn't work so well is the test case provided (try switching from count of records or cardinality to average - the suffix won't be applied). The right solution would be in this case to add suffix in both cases:

buildColumn: ({ field, previousColumn }) => ({
      label: ofName(field.displayName),
      dataType: 'number',
      operationType: type,
      sourceField: field.name,
      isBucketed: false,
      scale: 'ratio',
      params:
        // TODO just for testing
        previousColumn && previousColumn.dataType === 'number'
          ?{
             ...previousColumn.params,
              parentFormat: {
                id: 'suffix',
                params: {
                  unit: 'h',
                },
              },
            } 
          : {
              parentFormat: {
                id: 'suffix',
                params: {
                  unit: 'h',
                },
              },
            },
    }),

It doesn't influence the working code though so PR approved :)

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
lens 432 433 +1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
lens 911.0KB 913.3KB +2.3KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
lens 50.3KB 50.5KB +151.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@wylieconlon wylieconlon left a comment

Choose a reason for hiding this comment

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

Haven't tested it or seriously reviewed again, but I don't want to block it any more.

@wylieconlon wylieconlon dismissed their stale review November 11, 2020 19:33

Changes were made

@flash1293 flash1293 merged commit f1f2672 into elastic:master Nov 12, 2020
flash1293 added a commit to flash1293/kibana that referenced this pull request Nov 12, 2020
gmmorris added a commit to gmmorris/kibana that referenced this pull request Nov 12, 2020
* master:
  [Ingest Manager] Lift up registry/{stream,extract} functions (elastic#83239)
  [Reporting] Move "common" types and constants to allow cross-plugin integration (elastic#83198)
  [Lens] Add suffix formatter (elastic#82852)
  [App Search] Version documentation links (elastic#83245)
  Use saved object references for dashboard drilldowns (elastic#82602)
  Btsymbala/registered av (elastic#81910)
  [APM] Errors table for service overview (elastic#83065)
flash1293 added a commit that referenced this pull request Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Lens release_note:skip Skip the PR/issue when compiling release notes Team:Visualizations Visualization editors, elastic-charts and infrastructure v7.11.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Lens] Support a formatter or format option which append "per second" "per minute" "per hour"
6 participants