-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[MetricVis] Add auto scale functionality to metric unified renderer #124811
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
Merged
VladLasitsa
merged 13 commits into
elastic:main
from
VladLasitsa:add_auto_size_to_metric_expression
Feb 11, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ae5b9f
Add AutoSize component for metric
VladLasitsa 056476a
Fix Checks
VladLasitsa baa0903
moved auto scale from class to hoc component
VladLasitsa 391337c
Remove unnecessary code
VladLasitsa 95d1759
Fix checks
VladLasitsa f51272a
Refactoring
VladLasitsa 0a806c8
Update shapshots
VladLasitsa 8f6d00a
Update snapshots
VladLasitsa 7250587
Merge branch 'main' into add_auto_size_to_metric_expression
kibanamachine 4a7ff18
Fix styles and change file name from auto_scale to with_auto_scale
VladLasitsa 50ecc63
Merge branch 'main' into add_auto_size_to_metric_expression
kibanamachine a5cb229
Fix some remarks
VladLasitsa 7fce828
Fix some remarks
VladLasitsa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...ression_metric/common/expression_functions/__snapshots__/metric_vis_function.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...pressions/expression_metric/public/components/__snapshots__/with_auto_scale.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/plugins/chart_expressions/expression_metric/public/components/with_auto_scale.styles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { css } from '@emotion/react'; | ||
| import { euiThemeVars } from '@kbn/ui-theme'; | ||
|
|
||
| export const autoScaleWrapperStyle = css({ | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| maxWidth: '100%', | ||
| maxHeight: '100%', | ||
| overflow: 'hidden', | ||
| lineHeight: euiThemeVars.euiLineHeight, | ||
| }); |
51 changes: 51 additions & 0 deletions
51
src/plugins/chart_expressions/expression_metric/public/components/with_auto_scale.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { computeScale, withAutoScale } from './with_auto_scale'; | ||
| import { mount } from 'enzyme'; | ||
|
|
||
| const mockElement = (clientWidth = 100, clientHeight = 200) => ({ | ||
| clientHeight, | ||
| clientWidth, | ||
| }); | ||
|
|
||
| describe('AutoScale', () => { | ||
| describe('computeScale', () => { | ||
| it('is 1 if any element is null', () => { | ||
| expect(computeScale(null, null)).toBe(1); | ||
| expect(computeScale(mockElement(), null)).toBe(1); | ||
| expect(computeScale(null, mockElement())).toBe(1); | ||
| }); | ||
|
|
||
| it('is never over 1', () => { | ||
| expect(computeScale(mockElement(2000, 2000), mockElement(1000, 1000))).toBe(1); | ||
| }); | ||
|
|
||
| it('is never under 0.3 in default case', () => { | ||
| expect(computeScale(mockElement(2000, 1000), mockElement(1000, 10000))).toBe(0.3); | ||
| }); | ||
|
|
||
| it('is never under specified min scale if specified', () => { | ||
| expect(computeScale(mockElement(2000, 1000), mockElement(1000, 10000), 0.1)).toBe(0.1); | ||
| }); | ||
|
|
||
| it('is the lesser of the x or y scale', () => { | ||
| expect(computeScale(mockElement(2000, 2000), mockElement(3000, 5000))).toBe(0.4); | ||
| expect(computeScale(mockElement(2000, 3000), mockElement(4000, 3200))).toBe(0.5); | ||
| }); | ||
| }); | ||
|
|
||
| describe('withAutoScale', () => { | ||
| it('renders', () => { | ||
| const Component = () => <h1>Hoi!</h1>; | ||
| const WrappedComponent = withAutoScale(Component); | ||
| expect(mount(<WrappedComponent />)).toMatchSnapshot(); | ||
| }); | ||
| }); | ||
| }); |
91 changes: 91 additions & 0 deletions
91
src/plugins/chart_expressions/expression_metric/public/components/with_auto_scale.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import React, { useRef, useEffect, useState, ComponentType, useMemo } from 'react'; | ||
| import { throttle } from 'lodash'; | ||
| import { useResizeObserver } from '@elastic/eui'; | ||
| import { autoScaleWrapperStyle } from './with_auto_scale.styles'; | ||
|
|
||
| interface AutoScaleParams { | ||
| minScale?: number; | ||
| } | ||
| interface ClientDimensionable { | ||
| clientWidth: number; | ||
| clientHeight: number; | ||
| } | ||
|
|
||
| const MAX_SCALE = 1; | ||
| const MIN_SCALE = 0.3; | ||
|
|
||
| /** | ||
| * computeScale computes the ratio by which the child needs to shrink in order | ||
| * to fit into the parent. This function is only exported for testing purposes. | ||
| */ | ||
| export function computeScale( | ||
| parent: ClientDimensionable | null, | ||
| child: ClientDimensionable | null, | ||
| minScale: number = MIN_SCALE | ||
| ) { | ||
| if (!parent || !child) { | ||
| return 1; | ||
| } | ||
|
|
||
| const scaleX = parent.clientWidth / child.clientWidth; | ||
| const scaleY = parent.clientHeight / child.clientHeight; | ||
|
|
||
| return Math.max(Math.min(MAX_SCALE, Math.min(scaleX, scaleY)), minScale); | ||
| } | ||
|
|
||
| export function withAutoScale<T>( | ||
| WrappedComponent: ComponentType<T>, | ||
| autoScaleParams?: AutoScaleParams | ||
| ) { | ||
| return (props: T) => { | ||
| // An initial scale of 0 means we always redraw | ||
| // at least once, which is sub-optimal, but it | ||
| // prevents an annoying flicker. | ||
| const [scale, setScale] = useState(0); | ||
| const parentRef = useRef<HTMLDivElement>(null); | ||
| const childrenRef = useRef<HTMLDivElement>(null); | ||
| const parentDimensions = useResizeObserver(parentRef.current); | ||
|
|
||
| const scaleFn = useMemo( | ||
| () => | ||
| throttle(() => { | ||
| const newScale = computeScale( | ||
| { clientHeight: parentDimensions.height, clientWidth: parentDimensions.width }, | ||
| childrenRef.current, | ||
| autoScaleParams?.minScale | ||
| ); | ||
|
|
||
| // Prevent an infinite render loop | ||
| if (scale !== newScale) { | ||
| setScale(newScale); | ||
| } | ||
| }), | ||
| [parentDimensions, setScale, scale] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| scaleFn(); | ||
| }, [scaleFn]); | ||
|
|
||
| return ( | ||
| <div ref={parentRef} css={autoScaleWrapperStyle}> | ||
| <div | ||
| ref={childrenRef} | ||
| style={{ | ||
| transform: `scale(${scale || 0})`, | ||
| }} | ||
| > | ||
| <WrappedComponent {...props} /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
| } |
2 changes: 1 addition & 1 deletion
2
test/interpreter_functional/snapshots/baseline/combined_test3.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"labels":{"show":true},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}} | ||
| {"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"autoScale":null,"labels":{"show":true},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}} |
2 changes: 1 addition & 1 deletion
2
test/interpreter_functional/snapshots/baseline/final_output_test.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| {"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"labels":{"show":true},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}} | ||
| {"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"autoScale":null,"labels":{"show":true},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
Why do we need these changes? I don't notice any difference
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.
Because we use
VisualizationContainerhere with styles which includes styles for scrollbar, but styles ofVisualizationContainerloaded only when we open visualization first (because we load styles here src\plugins\visualizations\public\vis_async.ts). If we open metric directly in canvas you will see so that we don't have scroll at all because we don't load styles forVisualizationContainer. So to be sure that we will have correct scroll I added these styles here.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.
Got it, thanks 👍