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

Correlations: Add transformation editor #66217

Merged
merged 27 commits into from Apr 18, 2023
Merged

Conversation

gelicia
Copy link
Contributor

@gelicia gelicia commented Apr 10, 2023

What is this feature?

This feature adds a UI that allows people to create transformations in their correlations.

Why do we need this feature?

Transformations were previously enabled via provisioning, but that does not include an intuitive interface for people to create their own transformations using Grafana's UI. This adds transformations to the existing correlations editor.

Who is this feature for?

For people who want to set up correlations in a ui instead of provisioning.

Which issue(s) does this PR fix?:

Fixes #62095

Special notes for your reviewer:

  • The FieldArray component is previously used in Alerting > Notification Policies > Edit to define matching labels. I believe this UI is good for instances where there will be a small enough number of rows that the repetition will not be distracting or take up too much space with redundant information, and I believe transformations is a good use case for it.
  • There is a lot of internal documentation meant to guide the user through a complex new feature. I left a note where we would create new transformation types to remind future developers to edit it. If we get to a place where we need to create lots of new transformations and the upkeep of the internal documentation becomes too much, we can absolutely dial it back, but for this initial launch, I think it is useful.

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@gelicia gelicia added add to changelog no-backport Skip backport of PR labels Apr 10, 2023
@gelicia gelicia added this to the 10.0.0 milestone Apr 10, 2023
@gelicia gelicia marked this pull request as ready for review April 10, 2023 17:48
@gelicia gelicia requested review from joshhunt and a team as code owners April 10, 2023 17:48
@gelicia gelicia requested review from JoaoSilvaGrafana, leventebalogh, sakjur, papagian and yangkb09 and removed request for a team April 10, 2023 17:48
@gelicia
Copy link
Contributor Author

gelicia commented Apr 11, 2023

Could be my color blindness but I don't see any difference between rows.

Here's a side by side comparison of a correlation with a slightly dark background vs a lighter background. The issue was the variables card background was getting lost on the rows with the slightly darker background. I also added the same comparison but for dark mode
Screenshot 2023-04-11 at 8 15 01 AM

Screenshot 2023-04-11 at 2 11 16 PM

@gelicia gelicia removed the levitate breaking change A label indicating a breaking change and assigned by Levitate. label Apr 11, 2023
@gelicia gelicia requested a review from ifrost April 11, 2023 19:10
Copy link
Contributor

@ifrost ifrost left a comment

Choose a reason for hiding this comment

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

Works really great! 🎉

I think there are two main missing bits to make it mergable:

(1) The validation for the expression field should be consistent with other fields:

Screen.Recording.2023-04-12.at.11.57.05.mov

(2) There should also be some test coverage added (at least a smoke test in CorrelationsPage.test.tsx)

Other comments are non-blockers.

});
const getStyles = (theme: GrafanaTheme2) => {
// show text color at 0.05% to show with alternating background colors of interactive table
const decomposedContrastColor = colorManipulator.decomposeColor(theme.colors.text.primary);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's a good idea to make it stand out better, but in long term we may need to rethink the design. Having all forms in the table rows may create more, similar issue. I'll cross-link this comment in #65517

packages/grafana-data/src/types/dataLink.ts Outdated Show resolved Hide resolved
});
const newValueDetails = getSupportedTransTypeDetails(value.value);

if (newValueDetails.showExpression) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The issue is that I would like to prevent data loss as much as possible for this.

Fair enough. It's quite complex logic and it'd be good to add test coverage for it.

@@ -8,6 +8,11 @@ export type GetCorrelationsResponse = Correlation[];

type CorrelationConfigType = 'query';

export enum TransformationType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use SupportedTransformationType from @grafana/data instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops - yep! Fixed

rules={{ required: { value: true, message: 'Please select a transformation type' } }}
/>
</Field>
<Field
Copy link
Contributor

Choose a reason for hiding this comment

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

New fields don't have any aria-labels, probably worth adding some.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Many were added to correspond to the tests but I double checked and made sure all buttons and fields have an aria label

<Select
{...field}
onChange={(value) => {
if (!readOnly) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks very confusing in read-only mode. Not a blocker for now but I think we should run it by UX. I'll also mention it in #65517. I think making the description accessible in read-only mode was suggested by @radiorental, but is it the right way of doing it?

@grafanabot grafanabot added the levitate breaking change A label indicating a breaking change and assigned by Levitate. label Apr 12, 2023
return (
<Select
{...field}
value={fieldVal.type}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ifrost this is to fix the bug I talked about.

Replication steps:

  1. Have a transformation saved already. Refresh to clean slate the state.
  2. Open the correlation and go to the transformation. Delete it
  3. Add a new transformation. The select will be pre-filled with the original transformation

To see what the deal is, if you add a console.log for field in the InputControl render and fieldVal in the fields.map above, you will see the InputControl field still persists this value, as it must grab it from some state that does not get affected by remove. If there's a more obvious solution here, please let me know :)

@gelicia gelicia requested a review from ifrost April 13, 2023 18:24
Copy link
Contributor

@ifrost ifrost left a comment

Choose a reason for hiding this comment

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

Validation and tests look good 👍 There's still a few comments I left last time that would be good to address.

@gelicia gelicia requested a review from Elfo404 April 14, 2023 14:09
@gelicia
Copy link
Contributor Author

gelicia commented Apr 14, 2023

Found a bug creating a whole new correlation where it gives the "Please select a transformation type" when there is one selected. Will fix and add a test for this.

Edit: I only saw this once and was never able to recreate it again. I added creating a transformation in the test for creating a correlation, it should catch this and fail if an error message pops up that prevents saving.

<Field
label={
<Stack gap={0.5}>
<Label htmlFor={`config.transformations.${index}.type`}>Type</Label>
Copy link
Member

Choose a reason for hiding this comment

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

we need to use the same id as inputId prop in the Select at line 81 otherwise that select is going to be unlabeled.

moreover, we should not use the index here as it causes this label to target all transformations being edited with the same index:

Screen.Recording.2023-04-15.at.09.06.57.mov

we could use htmlFor={`config.transformations.${fieldVal.id}.type`} here and inputId={`config.transformations.${fieldVal.id}.type`} in the select. and by doing that you should be able to leverage this label element to describe the input instead of using an explicit aria-label on the select

Suggested change
<Label htmlFor={`config.transformations.${index}.type`}>Type</Label>
<Label htmlFor={`config.transformations.${fieldVal.id}.type`}>Type</Label>

<Field
label={
<Stack gap={0.5}>
<Label>Field</Label>
Copy link
Member

Choose a reason for hiding this comment

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

we should add an htmlFor here:

Suggested change
<Label>Field</Label>
<Label htmlFor={`config.transformations.${fieldVal.id}.field`}>Field</Label>

readOnly={readOnly}
defaultValue={fieldVal.field}
label="field"
aria-label="field"
Copy link
Member

Choose a reason for hiding this comment

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

to pair with the above comment:

Suggested change
aria-label="field"
id={`config.transformations.${fieldVal.id}.field`}

}}
options={transformOptions}
width={25}
aria-label="Type"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
aria-label="Type"
inputId={`config.transformations.${fieldVal.id}.type`}

Comment on lines 308 to 311
return Object.keys(SupportedTransformationType).map((key) => {
const transType = getSupportedTransTypeDetails(
SupportedTransformationType[key as keyof typeof SupportedTransformationType]
);
Copy link
Member

Choose a reason for hiding this comment

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

i think you can do this instead so you don't have to do any ugly type assertion:

Suggested change
return Object.keys(SupportedTransformationType).map((key) => {
const transType = getSupportedTransTypeDetails(
SupportedTransformationType[key as keyof typeof SupportedTransformationType]
);
return Object.values(SupportedTransformationType).map((key) => {
const transType = getSupportedTransTypeDetails(key);

Copy link
Member

Choose a reason for hiding this comment

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

or probably better:

Suggested change
return Object.keys(SupportedTransformationType).map((key) => {
const transType = getSupportedTransTypeDetails(
SupportedTransformationType[key as keyof typeof SupportedTransformationType]
);
return Object.values(SupportedTransformationType).map((transformationType) => {
const transType = getSupportedTransTypeDetails(transformationType);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Elfo404 good call, this worked great!

@gelicia gelicia requested a review from Elfo404 April 17, 2023 15:53
Copy link
Member

@Elfo404 Elfo404 left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

@gelicia gelicia merged commit 9d69d31 into main Apr 18, 2023
15 of 16 checks passed
@gelicia gelicia deleted the kristina/transformations-editor branch April 18, 2023 12:17
@zerok zerok modified the milestones: 10.0.0, 10.0.0-preview May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/explore area/frontend levitate breaking change A label indicating a breaking change and assigned by Levitate. no-backport Skip backport of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Glue: Create transformations editor
5 participants