From e3fc3c79476703c9f78c52ee1c585510df0bfebd Mon Sep 17 00:00:00 2001 From: Pedro Ramos Date: Fri, 16 Feb 2024 19:30:18 +0100 Subject: [PATCH] Add new command to disable analytics --- src/base.ts | 28 ++++++++++++++++++++++----- src/commands/config/analytics.ts | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/commands/config/analytics.ts diff --git a/src/base.ts b/src/base.ts index 0f4ae841924..9d90ed91094 100644 --- a/src/base.ts +++ b/src/base.ts @@ -2,6 +2,8 @@ import { Command } from '@oclif/core'; import { MetadataFromDocument, MetricMetadata, NewRelicSink, Recorder, Sink, StdOutSink } from '@smoya/asyncapi-adoption-metrics'; import { Parser } from '@asyncapi/parser'; import { Specification } from 'models/SpecificationFile'; +import { join, resolve } from 'path'; +import { existsSync, readFileSync, writeFile } from 'fs-extra'; class DiscardSink implements Sink { async send() { @@ -63,8 +65,8 @@ export default abstract class extends Command { async recordActionMetric(recordFunc: (recorder: Recorder) => Promise) { try { - await recordFunc(this.recorder); - await this.recorder.flush(); + await recordFunc(await this.recorder); + await (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}`); @@ -78,8 +80,18 @@ export default abstract class extends Command { await this.recordActionFinished(this.id as string, this.metricsMetadata, this.specFile?.text()); } - recorderFromEnv(prefix: string): Recorder { + async recorderFromEnv(prefix: string): Promise { let sink: Sink = new DiscardSink(); + const analyticsConfigFile = join(process.cwd(), '.asyncapi-analytics'); + + if (!existsSync(analyticsConfigFile)) { + await writeFile(analyticsConfigFile, JSON.stringify({ analyticsEnabled: 'true', infoMessageShown: 'false' }), { encoding: 'utf8' }); + } else { + const analyticsConfigFileContent = JSON.parse(readFileSync(resolve(analyticsConfigFile), 'utf-8')); + if (analyticsConfigFileContent.analyticsEnabled === 'false') { + process.env.ASYNCAPI_METRICS = 'false'; + } + } if (process.env.ASYNCAPI_METRICS !== 'false' && process.env.CI !== 'true') { switch (process.env.NODE_ENV) { @@ -92,8 +104,14 @@ export default abstract class extends Command { break; case 'production': // NODE_ENV set to `production` in bin/run_bin, which is specified in 'bin' package.json section - sink = new NewRelicSink(process.env.ASYNCAPI_METRICS_NEWRELIC_KEY || 'eu01xx73a8521047150dd9414f6aedd2FFFFNRAL'); - this.warn('AsyncAPI anonymously tracks command executions to improve the specification and tools, ensuring no sensitive data reaches our servers. It aids in comprehending how AsyncAPI tools are used and adopted, facilitating ongoing improvements to our specifications and tools.\n\nTo disable tracking, set the "ASYNCAPI_METRICS" env variable to "false" when executing the command. For instance:\n\nASYNCAPI_METRICS=false asyncapi validate spec_file.yaml'); + sink = new NewRelicSink(process.env.ASYNCAPI_METRICS_NEWRELIC_KEY || 'eu01xx73a8521047150dd9414f6aedd2FFFFNRAL'); + const analyticsConfigFileContent = JSON.parse(readFileSync(resolve(analyticsConfigFile), 'utf-8')); + + if (existsSync(analyticsConfigFile) && (analyticsConfigFileContent.infoMessageShown === 'false')) { + this.warn('AsyncAPI anonymously tracks command executions to improve the specification and tools, ensuring no sensitive data reaches our servers. It aids in comprehending how AsyncAPI tools are used and adopted, facilitating ongoing improvements to our specifications and tools.\n\nTo disable tracking, please run the following command :\n asyncapi config analytics --disable\n\nOnce disabled, if you want to enable tracking again then run:\n asyncapi config analytics'); + analyticsConfigFileContent.infoMessageShown = 'true'; + await writeFile(analyticsConfigFile, JSON.stringify(analyticsConfigFileContent), { encoding: 'utf8' }); + } break; } } diff --git a/src/commands/config/analytics.ts b/src/commands/config/analytics.ts new file mode 100644 index 00000000000..1d190401903 --- /dev/null +++ b/src/commands/config/analytics.ts @@ -0,0 +1,33 @@ +import { Flags } from '@oclif/core'; +import { readFileSync, writeFile } from 'fs-extra'; +import { join, resolve } from 'path'; +import Command from '../../base'; + +export default class Analytics extends Command { + static description = 'Enable or disable analytics for metrics collection'; + static flags = { + help: Flags.help({ char: 'h' }), + disable: Flags.boolean({ char: 'd', description: 'disable analytics', default: false }) + }; + + async run() { + try { + const { flags } = await this.parse(Analytics); + const isDisabled = flags.disable; + const analyticsConfigFile = join(process.cwd(), '.asyncapi-analytics'); + const analyticsConfigFileContent = JSON.parse(readFileSync(resolve(process.cwd(), '.asyncapi-analytics'), 'utf-8')); + + if (isDisabled) { + analyticsConfigFileContent.analyticsEnabled = 'false'; + await writeFile(analyticsConfigFile, JSON.stringify(analyticsConfigFileContent), {encoding: 'utf8'}); + this.log('Analytics disabled.'); + } else { + analyticsConfigFileContent.analyticsEnabled = 'true'; + await writeFile(analyticsConfigFile, JSON.stringify(analyticsConfigFileContent), {encoding: 'utf8'}); + this.log('Analytics enabled.'); + } + } catch (e: any) { + this.error(e as Error); + } + } +}