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

feat: add the flag for disabling optimizations #1447

Merged
merged 7 commits into from
Jul 31, 2024

Conversation

helios2003
Copy link
Contributor

Description
Now the disableOptimizationFor parameter can also be provided in the optimized document.

Related issue(s)
Fixes #1432

@@ -40,6 +40,7 @@ export default class Optimize extends Command {
optimization: Flags.string({char: 'p', default: Object.values(Optimizations), options: Object.values(Optimizations), multiple: true, description: 'select the type of optimizations that you want to apply.'}),
output: Flags.string({char: 'o', default: Outputs.TERMINAL, options: Object.values(Outputs), description: 'select where you want the output.'}),
'no-tty': Flags.boolean({ description: 'do not use an interactive terminal', default: false }),
disableOptimizationForSchema: Flags.boolean({ description: 'schema should be ignored from the optimization prcoess', default: false }),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we have a shorter and DX friendly flag ?
What about: --omit=schema in case we want to disable something else in the future we can reuse the same flag
--omit=components...
cc @KhudaDad414

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, even I was thinking that. I just used this since it was suggested here #1432 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

makes sense. In the original issue, I suggested --ignore but --omit also works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just for confirmation @Amzani, when you mention other things that can be omitted by changing the flag format, do you mean this, like reuseComponents, removeComponents etc can also be enabled or disabled by default?

{
  output: 'YAML',
  rules: {
    reuseComponents: true,
    removeComponents: true,
    moveAllToComponents: true,
    moveDuplicatesToComponents: false,
  },
  disableOptimizationFor: {
    schema: false,
  },
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

@KhudaDad414 is the best person to answer this one.

Copy link
Contributor

Choose a reason for hiding this comment

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

During all the time I've been working with Optimizer, I didn't see a need to disable optimization for anything else except schema, and that was only because schemas were disrupting the view of optimized AsyncAPI Document. So I think if at least one user requests anything else, it would be possible to think about changes to the logic, and for now it can be simply the introduction of one consistent across versions switch disableOptimizationForSchema and that's it.

@KhudaDad414 wdyt?

Copy link
Member

@KhudaDad414 KhudaDad414 Jun 3, 2024

Choose a reason for hiding this comment

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

@aeworxet I agree with @helios2003.

I don't think it makes a lot of sense to try to match the command names from optimizer to CLI. they are separate tools and they can evolve separately. purely from the DX point of view having a long command line options like disableOptimizationForSchema isn't ideal since user has to type it every time.
that's why I think having something like --ignore or --omit makes sense here.

optimizer is a library and it is used in other tools, so if we have a long descriptive option names, it doesn't matter since user will only deal with it once in their code.

Copy link
Contributor

Choose a reason for hiding this comment

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

they are separate tools and they can evolve separately

I don't have a strong opinion in this case either.
@helios2003, please refer to asyncapi/optimizer#252 in which disableOptimizationFor was introduced, to see a list of all places where it was used at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@helios2003 @aeworxet tbh I don't have a strong opinion here. one one side

disableOptimizationFor: {
  schema: false,
}

can be used to add more components to ignore in the future. things like:

disableOptimizationFor: {
  schema: false,
  message: true
}

the question is whether it's a valid case? will someone want this feature? I don't know.

and on the other hand changing the name to disableOptimizationForSchema would make a more seemless way of integration between CLI and optimizer.

I am bit confused. So do I need to add additional components to the flags and change it in both the repositories or just do it for schema?

Copy link
Member

Choose a reason for hiding this comment

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

@helios2003 since optimizer currently only supports schemas I think you can only support schemas here and call it a day.

@@ -40,6 +40,7 @@ export default class Optimize extends Command {
optimization: Flags.string({char: 'p', default: Object.values(Optimizations), options: Object.values(Optimizations), multiple: true, description: 'select the type of optimizations that you want to apply.'}),
output: Flags.string({char: 'o', default: Outputs.TERMINAL, options: Object.values(Outputs), description: 'select where you want the output.'}),
'no-tty': Flags.boolean({ description: 'do not use an interactive terminal', default: false }),
disableOptimizationForSchema: Flags.boolean({ description: 'schema should be ignored from the optimization prcoess', default: false }),
Copy link
Member

Choose a reason for hiding this comment

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

Just a typo here :)

Suggested change
disableOptimizationForSchema: Flags.boolean({ description: 'schema should be ignored from the optimization prcoess', default: false }),
disableOptimizationForSchema: Flags.boolean({ description: 'schema should be ignored from the optimization process', default: false }),

Comment on lines 85 to 87
if (flags['disableOptimizationForSchema'] === true) {
flags.disableOptimizationForSchema = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You basically wrote

if (flags['disableOptimizationForSchema'] === true) {
  flags['disableOptimizationForSchema'] = true;
}

cuz just to make sure.

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 tried set the flag so it can be used in the optimizedDocument.

Copy link
Contributor

Choose a reason for hiding this comment

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

The very presence of a command line switch in this CLI already means 'true' and its absence - 'false', so you already imported the value of this cmd switch into the JS execution context in lines R106-R108.
It's done in Bundler the same way:
https://github.com/asyncapi/cli/blob/master/src/commands/bundle.ts#L44

This makes code in lines R85-R87 redudant and it can be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes, my bad. I don't know how I missed it. Thank you. I will incorporate all the necessary changes in the future commit to this PR.

Copy link

sonarcloud bot commented Jun 5, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@helios2003
Copy link
Contributor Author

Hey folks, I apologize for the delay as I've been busy over the past week or two. I will address all the comments made by the maintainers on this PR in a few days. Thank you all for your patience and cooperation.

Copy link

sonarcloud bot commented Jun 20, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@helios2003
Copy link
Contributor Author

I have made the changes that were talked about in the discussion. The PR can be reviewed.

Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

Hey @helios2003,
I tested the result and it works great. there is one issue that I think should be solved before we can merge this one.

If I pass the ignore flag in the getOptimizedDocument stage, then the report won't reflect it. I mean a component will appear as it's being removed but it will not be removed.

I think this flag should be passed at the (getReport())[https://github.com/asyncapi/optimizer/blob/4fe33fe499d3b1add61f70a27d720f5c8d8e248c/src/Optimizer.ts#L62] stage. but the problem with this approach would be that the user won't see the report to decide if they want to ignore the schemas or not.

I think the best approach would be to accept the flag but also have the option to change it later on with this screen:
Screenshot 2024-07-01 at 11 41 56

this way, if someone is using --no-tty then they will not sure the Report and it doesn't matter if the report doesn't reflect the ignored schema.
And if someone is going the standard way, then they have an option to change their mind about ignoring schemas mid-way. (or ignore them after seeing the report)

@helios2003
Copy link
Contributor Author

I have tried making the changes as asked, but I am not sure if this is the correct way to implement it. PTAL @KhudaDad414

Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

Suggested some changes.

@@ -178,6 +190,11 @@ export default class Optimize extends Command {
this.showOptimizations(report.reuseComponents);
choices.push({name: 'reuse components', value: Optimizations.REUSE_COMPONENTS});
}
if (canIgnoreSchema) {
this.log("Do not ignore schema for the components");
Copy link
Member

Choose a reason for hiding this comment

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

strings must use single quote.

@@ -152,6 +163,7 @@ export default class Optimize extends Command {
const canMoveAll = report.moveAllToComponents?.length;
const canRemove = report.removeComponents?.length;
const canReuse = report.reuseComponents?.length;
const canIgnoreSchema = this.disableOptimizations?.includes(DisableOptimizations.SCHEMA);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this. we always show this option.

Copy link
Contributor Author

@helios2003 helios2003 Jul 5, 2024

Choose a reason for hiding this comment

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

So, we allow the user to ignore the schema halfway through even if they didn't put the flag initially and vice versa?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, that's the idea.

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 included these changes in the last commit.

@@ -178,6 +190,11 @@ export default class Optimize extends Command {
this.showOptimizations(report.reuseComponents);
choices.push({name: 'reuse components', value: Optimizations.REUSE_COMPONENTS});
}
if (canIgnoreSchema) {
this.log("Do not ignore schema for the components");
choices.push({name: 'Do not ignore schema', value: DisableOptimizations.SCHEMA});
Copy link
Member

Choose a reason for hiding this comment

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

I suggest we use the same option as flag. I mean the name should be Ignore schemas. Also it should be checked by default if --ignore=schema is passed.

@helios2003
Copy link
Contributor Author

Hi maintainers, can you please review the PR? It has been pending for quite a while.
cc: @Amzani, @KhudaDad414, @Souvikns, @Shurtu-gal

Copy link
Member

@KhudaDad414 KhudaDad414 left a comment

Choose a reason for hiding this comment

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

LGTM!

@helios2003
Copy link
Contributor Author

/rtm

Copy link
Collaborator

@Amzani Amzani left a comment

Choose a reason for hiding this comment

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

LGTM

@Shurtu-gal
Copy link
Collaborator

/rtm

1 similar comment
@Shurtu-gal
Copy link
Collaborator

/rtm

@Shurtu-gal
Copy link
Collaborator

/u

Copy link

sonarcloud bot commented Jul 31, 2024

@Shurtu-gal Shurtu-gal merged commit b825a34 into asyncapi:master Jul 31, 2024
10 checks passed
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[FEATURE] Support ignore feature of optimizer
7 participants