Skip to content

Commit

Permalink
Add metrics recording to some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-rr committed Nov 6, 2023
1 parent fd65361 commit 5b25de2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import bundle from '@asyncapi/bundler';
import { promises } from 'fs';
import path from 'path';
import { Specification, load } from '../models/SpecificationFile';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

const { writeFile } = promises;

Expand All @@ -26,6 +28,8 @@ export default class Bundle extends Command {
base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }),
};

parser = new Parser();

async run() {
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
Expand Down Expand Up @@ -73,6 +77,24 @@ export default class Bundle extends Command {
}
this.log(`Check out your shiny new bundled files at ${output}`);
}

const result = await load(output);

try {
// Metrics recording.
const {document} = await this.parser.parse(result.text());
if (document !== undefined) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['files'] = AsyncAPIFiles.length;
await this.recorder.recordActionExecution('bundle', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}

async loadFiles(filepaths: string[]): Promise<Specification[]> {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class Convert extends Command {
if (document !== undefined && convertedFileFormatted) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['from_version'] = metadata['_asyncapi_version'];
metadata['from_version'] = document.version();
metadata['to_version'] = flags['target-version'];
console.log(metadata);
await this.recorder.recordActionExecution('convert', metadata);
Expand Down
22 changes: 21 additions & 1 deletion src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { watchFlag } from '../../flags';
import { isLocalTemplate, Watcher } from '../../utils/generator';
import { ValidationError } from '../../errors/validation-error';
import { GeneratorError } from '../../errors/generator-error';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

import type { Example } from '@oclif/core/lib/interfaces';

Expand Down Expand Up @@ -107,6 +109,8 @@ export default class Template extends Command {
{ name: 'template', description: '- Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template', required: true }
];

parser = new Parser();

async run() {
const { args, flags } = await this.parse(Template); // NOSONAR

Expand Down Expand Up @@ -137,11 +141,27 @@ export default class Template extends Command {
this.error(`${template} template does not support AsyncAPI v3 documents, please checkout ${v3IssueLink}`);
}
}
await this.generate(asyncapi, template, output, options, genOption);
const result = await this.generate(asyncapi, template, output, options, genOption);
if (watchTemplate) {
const watcherHandler = this.watcherHandler(asyncapi, template, output, options, genOption);
await this.runWatchMode(asyncapi, template, output, watcherHandler);
}

try {
// Metrics recording.
const {document} = await this.parser.parse(asyncapiInput.text());
if (document !== undefined && result) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['template'] = template;
await this.recorder.recordActionExecution('generate_fromTemplate', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}

private parseFlags(disableHooks?: string[], params?: string[], mapBaseUrl?: string): ParsedFlags {
Expand Down
23 changes: 23 additions & 0 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as inquirer from 'inquirer';
import chalk from 'chalk';
import { promises } from 'fs';
import { Example } from '@oclif/core/lib/interfaces';
import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics';
import { Parser } from '@asyncapi/parser';

const { writeFile } = promises;

export enum Optimizations {
Expand Down Expand Up @@ -44,6 +47,8 @@ export default class Optimize extends Command {
{ name: 'spec-file', description: 'spec path, url, or context-name', required: false },
];

parser = new Parser();

async run() {
const { args, flags } = await this.parse(Optimize); //NOSONAR
const filePath = args['spec-file'];
Expand Down Expand Up @@ -123,7 +128,25 @@ export default class Optimize extends Command {
err: error
});
}

try {
// Metrics recording.
const {document} = await this.parser.parse(specFile.text());
const optimizedDoc = optimizer.getOptimizedDocument();
if (document !== undefined && optimizedDoc) {
const metadata = MetadataFromDocument(document);
metadata['success'] = true;
metadata['optimizations'] = this.optimizations;
await this.recorder.recordActionExecution('optimize', metadata);
await this.recorder.flush();
}
} catch (e: any) {
if (e instanceof Error) {
this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`);
}
}
}

private showOptimizations(elements: ReportElement[] | undefined) {
if (!elements) {
return;
Expand Down

0 comments on commit 5b25de2

Please sign in to comment.