Skip to content

Commit

Permalink
Add new command to disable analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-rr committed Feb 16, 2024
1 parent 85efb45 commit e3fc3c7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -63,8 +65,8 @@ export default abstract class extends Command {

async recordActionMetric(recordFunc: (recorder: Recorder) => Promise<void>) {
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}`);
Expand All @@ -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<Recorder> {
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) {
Expand All @@ -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;
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/commands/config/analytics.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit e3fc3c7

Please sign in to comment.