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

fix: solve optimize command issue when sending data to New Relic #13

Merged
48 changes: 32 additions & 16 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export enum Outputs {
export default class Optimize extends Command {
static description = 'optimize asyncapi specification file';
isInteractive = false;
optimizations?: Optimizations[];
selectedOptimizations?: Optimizations[];
outputMethod?: Outputs;

static examples: Example[] = [
'asyncapi optimize ./asyncapi.yaml',
'asyncapi optimize ./asyncapi.yaml --no-tty',
'asyncapi optimize ./asyncapi.yaml --optimization=remove-components,reuse-components,move-to-components --no-tty',
'asyncapi optimize ./asyncapi.yaml --optimization=remove-components,reuse-components,move-to-components --output=terminal --no-tty',
'asyncapi optimize ./asyncapi.yaml --optimization=remove-components --optimization=reuse-components --optimization=move-to-components --no-tty',
'asyncapi optimize ./asyncapi.yaml --optimization=remove-components --output=terminal --no-tty',
];

static flags = {
Expand Down Expand Up @@ -81,28 +81,29 @@ export default class Optimize extends Command {
);
}
this.isInteractive = !flags['no-tty'];
this.optimizations = flags.optimization as Optimizations[];
this.selectedOptimizations = flags.optimization as Optimizations[];
this.outputMethod = flags.output as Outputs;

this.metricsMetadata.optimizations = this.optimizations;
this.metricsMetadata.optimized = false;

if (!(report.moveToComponents?.length || report.removeComponents?.length || report.reuseComponents?.length)) {
this.log(`No optimization has been applied since ${this.specFile.getFilePath() ?? this.specFile.getFileURL()} looks optimized!`);
return;
}

const isTTY = process.stdout.isTTY;
if (this.isInteractive && isTTY) {
await this.interactiveRun(report);
}

try {
peter-rr marked this conversation as resolved.
Show resolved Hide resolved
const optimizedDocument = optimizer.getOptimizedDocument({rules: {
moveToComponents: this.optimizations.includes(Optimizations.MOVE_TO_COMPONENTS),
removeComponents: this.optimizations.includes(Optimizations.REMOVE_COMPONENTS),
reuseComponents: this.optimizations.includes(Optimizations.REUSE_COMPONENTS)
moveToComponents: this.selectedOptimizations.includes(Optimizations.MOVE_TO_COMPONENTS),
removeComponents: this.selectedOptimizations.includes(Optimizations.REMOVE_COMPONENTS),
reuseComponents: this.selectedOptimizations.includes(Optimizations.REUSE_COMPONENTS)
}, output: Output.YAML});

this.collectMetricsData(report);

const specPath = this.specFile.getFilePath();
let newPath = '';

Expand All @@ -113,15 +114,18 @@ export default class Optimize extends Command {
newPath = 'optimized-asyncapi.yaml';
}

if (this.outputMethod === Outputs.TERMINAL) {
switch (this.outputMethod) {
case Outputs.TERMINAL:
this.log(optimizedDocument);
} else if (this.outputMethod === Outputs.NEW_FILE) {
break;
case Outputs.NEW_FILE:
await writeFile(newPath, optimizedDocument, { encoding: 'utf8' });
this.log(`Created file ${newPath}...`);
} else if (this.outputMethod === Outputs.OVERWRITE) {
break;
case Outputs.OVERWRITE:
await writeFile(specPath ?? 'asyncapi.yaml', optimizedDocument, { encoding: 'utf8' });

this.log(`Updated file ${specPath}...`);
break;
}
} catch (error) {
throw new ValidationError({
Expand All @@ -135,6 +139,7 @@ export default class Optimize extends Command {
if (!elements) {
return;
}

for (let i = 0; i < elements.length; i++) {
const element = elements[+i];
if (element.action==='move') {
Expand All @@ -148,6 +153,7 @@ export default class Optimize extends Command {

this.log('\n');
}

private async interactiveRun(report: Report) {
const canMove = report.moveToComponents?.length;
const canRemove = report.removeComponents?.length;
Expand Down Expand Up @@ -180,8 +186,8 @@ export default class Optimize extends Command {
choices
}]);

this.optimizations = optimizationRes.optimization;

this.selectedOptimizations = optimizationRes.optimization;
const outputRes = await inquirer.prompt([{
name: 'output',
message: 'where do you want to save the result:',
Expand All @@ -191,4 +197,14 @@ export default class Optimize extends Command {
}]);
this.outputMethod = outputRes.output;
}

private collectMetricsData(report: Report) {
for (const availableOptimization in report) {
const availableOptimizationKebabCase = availableOptimization.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase(); // optimization flags are kebab case
if (availableOptimization.length && this.selectedOptimizations?.includes(availableOptimizationKebabCase as Optimizations)) {
this.metricsMetadata[`optimization_${availableOptimization}`] = true;
this.metricsMetadata.optimized = true;
}
}
}
}
Loading