diff --git a/src/commands/bundle.ts b/src/commands/bundle.ts index d014fbbe2ff..98d81c464c1 100644 --- a/src/commands/bundle.ts +++ b/src/commands/bundle.ts @@ -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; @@ -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; @@ -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 { diff --git a/src/commands/convert.ts b/src/commands/convert.ts index 871bd820903..d6031844ed1 100644 --- a/src/commands/convert.ts +++ b/src/commands/convert.ts @@ -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); diff --git a/src/commands/generate/fromTemplate.ts b/src/commands/generate/fromTemplate.ts index e53ea665cb2..70a2bf2f0dc 100644 --- a/src/commands/generate/fromTemplate.ts +++ b/src/commands/generate/fromTemplate.ts @@ -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'; @@ -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 @@ -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 { diff --git a/src/commands/optimize.ts b/src/commands/optimize.ts index f21f14b764f..60283f29302 100644 --- a/src/commands/optimize.ts +++ b/src/commands/optimize.ts @@ -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 { @@ -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']; @@ -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;